Skip to content

Commit 522df03

Browse files
authored
feat: File and SubDirectory extensions support for IEnumerable params (#43)
* Added more overloads to SubDirectory and File extensions * Updated sdk to 7.0.400 * Update CI for SDKs to install
1 parent 7329b87 commit 522df03

File tree

6 files changed

+146
-6
lines changed

6 files changed

+146
-6
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ jobs:
2323
uses: actions/setup-dotnet@v3
2424
with:
2525
dotnet-version: |
26-
3.1.x
2726
5.0.x
2827
6.0.x
28+
7.0.x
2929
- name: Run tests
3030
run: dotnet test --collect:"XPlat Code Coverage" --logger "GitHubActions"
3131
- name: Upload coverage

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
3-
"version": "7.0.304",
3+
"version": "7.0.400",
44
"rollForward": "latestMinor"
55
}
66
}

src/System.IO.Abstractions.Extensions/IDirectoryInfoExtensions.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace System.IO.Abstractions
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace System.IO.Abstractions
25
{
36
public static class IDirectoryInfoExtensions
47
{
@@ -13,6 +16,28 @@ public static IDirectoryInfo SubDirectory(this IDirectoryInfo info, string name)
1316
return info.FileSystem.DirectoryInfo.New(info.FileSystem.Path.Combine(info.FullName, name));
1417
}
1518

19+
/// <summary>
20+
/// Get an <see cref="IDirectoryInfo"/> for the specified sub-directories <paramref name="names"/>
21+
/// </summary>
22+
/// <param name="info"></param>
23+
/// <param name="names">Sub-directory names (ex. "test", "test2"). Empty or null names are automatically removed from this list</param>
24+
/// <returns>An <see cref="IDirectoryInfo"/> for the specified sub-directory</returns>
25+
public static IDirectoryInfo SubDirectory(this IDirectoryInfo info, params string[] names)
26+
{
27+
return info.SubDirectory((IEnumerable<string>)names);
28+
}
29+
30+
/// <summary>
31+
/// Get an <see cref="IDirectoryInfo"/> for the specified sub-directories <paramref name="names"/>
32+
/// </summary>
33+
/// <param name="info"></param>
34+
/// <param name="names">Sub-directory names (ex. "test", "test2"). Empty or null names are automatically removed from this list</param>
35+
/// <returns>An <see cref="IDirectoryInfo"/> for the specified sub-directory</returns>
36+
public static IDirectoryInfo SubDirectory(this IDirectoryInfo info, IEnumerable<string> names)
37+
{
38+
return info.FileSystem.DirectoryInfo.New(info.FileSystem.Path.Combine(GetPaths(info, names)));
39+
}
40+
1641
/// <summary>
1742
/// Get an <see cref="IFileInfo"/> for the specified file <paramref name="name"/>
1843
/// </summary>
@@ -24,6 +49,28 @@ public static IFileInfo File(this IDirectoryInfo info, string name)
2449
return info.FileSystem.FileInfo.New(info.FileSystem.Path.Combine(info.FullName, name));
2550
}
2651

52+
/// <summary>
53+
/// Get an <see cref="IFileInfo"/> for the specified sub-directories file <paramref name="names"/>
54+
/// </summary>
55+
/// <param name="info"></param>
56+
/// <param name="names">Sub-directories and file name (ex. "test", "test.txt"). Empty or null names are automatically removed from this list</param>
57+
/// <returns>An <see cref="IFileInfo"/> for the specified file</returns>
58+
public static IFileInfo File(this IDirectoryInfo info, params string[] names)
59+
{
60+
return info.File((IEnumerable<string>)names);
61+
}
62+
63+
/// <summary>
64+
/// Get an <see cref="IFileInfo"/> for the specified sub-directories file <paramref name="names"/>
65+
/// </summary>
66+
/// <param name="info"></param>
67+
/// <param name="names">Sub-directories and file name (ex. "test", "test.txt"). Empty or null names are automatically removed from this list</param>
68+
/// <returns>An <see cref="IFileInfo"/> for the specified file</returns>
69+
public static IFileInfo File(this IDirectoryInfo info, IEnumerable<string> names)
70+
{
71+
return info.FileSystem.FileInfo.New(info.FileSystem.Path.Combine(GetPaths(info, names)));
72+
}
73+
2774
/// <summary>
2875
/// Throws an exception if the directory <paramref name="info"/> doesn't exists
2976
/// </summary>
@@ -34,5 +81,12 @@ public static void ThrowIfNotFound(this IDirectoryInfo info)
3481
if (!info.Exists)
3582
throw new DirectoryNotFoundException(StringResources.Format("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION", info.FullName));
3683
}
84+
85+
private static string[] GetPaths(IDirectoryInfo info, IEnumerable<string> names)
86+
{
87+
return new[] { info.FullName }
88+
.Concat(names.Where(n => !String.IsNullOrEmpty(n)))
89+
.ToArray();
90+
}
3791
}
3892
}

src/System.IO.Abstractions.Extensions/System.IO.Abstractions.Extensions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<PackageId>TestableIO.System.IO.Abstractions.Extensions</PackageId>
5-
<TargetFrameworks>net6.0;netstandard2.1;netstandard2.0;net461</TargetFrameworks>
5+
<TargetFrameworks>net6.0;netstandard2.1;netstandard2.0;net462</TargetFrameworks>
66
<Description>Convenience functionalities on top of System.IO.Abstractions</Description>
77
<RootNamespace>System.IO.Abstractions</RootNamespace>
88
<PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/../../ReleaseNotes.md"))</PackageReleaseNotes>

tests/System.IO.Abstractions.Extensions.Tests/DirectoryInfoExtensionsTests.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using NUnit.Framework;
2+
using System.Collections.Generic;
23

34
namespace System.IO.Abstractions.Extensions.Tests
45
{
@@ -30,6 +31,59 @@ public void SubDirectory_Extension_Test()
3031
Assert.IsFalse(fs.Directory.Exists(expectedPath));
3132
}
3233

34+
[TestCase("test1", "test2")]
35+
[TestCase("test1", "", "test2")]
36+
[TestCase("test1", null, "test2")]
37+
public void SubDirectoryWithParams_Extension_Test(params string[] subFolders)
38+
{
39+
//arrange
40+
var fs = new FileSystem();
41+
var current = fs.DirectoryInfo.New(fs.Directory.GetCurrentDirectory());
42+
var expectedPath = fs.Path.Combine(current.FullName, "test1", "test2");
43+
44+
//make sure directory doesn't exists
45+
Assert.IsFalse(fs.Directory.Exists(expectedPath));
46+
47+
//create directory
48+
var created = current.SubDirectory(subFolders);
49+
created.Create();
50+
51+
//assert it exists
52+
Assert.IsTrue(fs.Directory.Exists(expectedPath));
53+
Assert.AreEqual(expectedPath, created.FullName);
54+
55+
//delete directory
56+
created.Delete();
57+
Assert.IsFalse(fs.Directory.Exists(expectedPath));
58+
}
59+
60+
[TestCase("test1", "test2")]
61+
[TestCase("test1", "", "test2")]
62+
[TestCase("test1", null, "test2")]
63+
public void SubDirectoryWithIEnumerable_Extension_Test(params string[] names)
64+
{
65+
//arrange
66+
var fs = new FileSystem();
67+
var current = fs.DirectoryInfo.New(fs.Directory.GetCurrentDirectory());
68+
var expectedPath = fs.Path.Combine(current.FullName, "test1", "test2");
69+
70+
//make sure directory doesn't exists
71+
Assert.IsFalse(fs.Directory.Exists(expectedPath));
72+
73+
//create directory
74+
var list = new List<string>(names);
75+
var created = current.SubDirectory(list);
76+
created.Create();
77+
78+
//assert it exists
79+
Assert.IsTrue(fs.Directory.Exists(expectedPath));
80+
Assert.AreEqual(expectedPath, created.FullName);
81+
82+
//delete directory
83+
created.Delete();
84+
Assert.IsFalse(fs.Directory.Exists(expectedPath));
85+
}
86+
3387
[Test]
3488
public void File_Extension_Test()
3589
{
@@ -58,6 +112,38 @@ public void File_Extension_Test()
58112
Assert.IsFalse(fs.File.Exists(expectedPath));
59113
}
60114

115+
[TestCase("test1", "test2", "test.txt")]
116+
[TestCase("test1", "", "test2", "test.txt")]
117+
[TestCase("test1", null, "test2", "test.txt")]
118+
119+
public void FileWithParams_Extension_Test(params string[] names)
120+
{
121+
//arrange
122+
var fs = new FileSystem();
123+
var current = fs.DirectoryInfo.New(fs.Directory.GetCurrentDirectory());
124+
var expectedPath = fs.Path.Combine(current.FullName, "test1", "test2", "test.txt");
125+
126+
//make sure file doesn't exists
127+
Assert.IsFalse(fs.File.Exists(expectedPath));
128+
129+
//act, create file
130+
var created = current.File(names);
131+
created.Directory.Create();
132+
using (var stream = created.Create())
133+
{
134+
stream.Dispose();
135+
}
136+
137+
//assert it exists
138+
Assert.IsTrue(fs.File.Exists(expectedPath));
139+
Assert.AreEqual(expectedPath, created.FullName);
140+
141+
//delete file
142+
created.Delete();
143+
created.Directory.Delete();
144+
Assert.IsFalse(fs.File.Exists(expectedPath));
145+
}
146+
61147
[Test]
62148
public void ThrowIfNotFound_IfDirectoryDoesNotExists_ThrowsException()
63149
{

tests/System.IO.Abstractions.Extensions.Tests/System.IO.Abstractions.Extensions.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net6.0;net5.0;net461</TargetFrameworks>
5-
<TargetFrameworks Condition="!$([MSBuild]::IsOsUnixLike())">$(TargetFrameworks);net461</TargetFrameworks>
4+
<TargetFrameworks>net7.0;net6.0;net5.0</TargetFrameworks>
5+
<TargetFrameworks Condition="!$([MSBuild]::IsOsUnixLike())">$(TargetFrameworks);net462</TargetFrameworks>
66
<IsPackable>false</IsPackable>
77
<IsTestable>true</IsTestable>
88
</PropertyGroup>

0 commit comments

Comments
 (0)