Skip to content

Commit

Permalink
feat: File and SubDirectory extensions support for IEnumerable params (
Browse files Browse the repository at this point in the history
…#43)

* Added more overloads to SubDirectory and File extensions
* Updated sdk to 7.0.400
* Update CI for SDKs to install
  • Loading branch information
gigi81 authored Aug 19, 2023
1 parent 7329b87 commit 522df03
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ jobs:
uses: actions/setup-dotnet@v3
with:
dotnet-version: |
3.1.x
5.0.x
6.0.x
7.0.x
- name: Run tests
run: dotnet test --collect:"XPlat Code Coverage" --logger "GitHubActions"
- name: Upload coverage
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.304",
"version": "7.0.400",
"rollForward": "latestMinor"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace System.IO.Abstractions
using System.Collections.Generic;
using System.Linq;

namespace System.IO.Abstractions
{
public static class IDirectoryInfoExtensions
{
Expand All @@ -13,6 +16,28 @@ public static IDirectoryInfo SubDirectory(this IDirectoryInfo info, string name)
return info.FileSystem.DirectoryInfo.New(info.FileSystem.Path.Combine(info.FullName, name));
}

/// <summary>
/// Get an <see cref="IDirectoryInfo"/> for the specified sub-directories <paramref name="names"/>
/// </summary>
/// <param name="info"></param>
/// <param name="names">Sub-directory names (ex. "test", "test2"). Empty or null names are automatically removed from this list</param>
/// <returns>An <see cref="IDirectoryInfo"/> for the specified sub-directory</returns>
public static IDirectoryInfo SubDirectory(this IDirectoryInfo info, params string[] names)
{
return info.SubDirectory((IEnumerable<string>)names);
}

/// <summary>
/// Get an <see cref="IDirectoryInfo"/> for the specified sub-directories <paramref name="names"/>
/// </summary>
/// <param name="info"></param>
/// <param name="names">Sub-directory names (ex. "test", "test2"). Empty or null names are automatically removed from this list</param>
/// <returns>An <see cref="IDirectoryInfo"/> for the specified sub-directory</returns>
public static IDirectoryInfo SubDirectory(this IDirectoryInfo info, IEnumerable<string> names)
{
return info.FileSystem.DirectoryInfo.New(info.FileSystem.Path.Combine(GetPaths(info, names)));
}

/// <summary>
/// Get an <see cref="IFileInfo"/> for the specified file <paramref name="name"/>
/// </summary>
Expand All @@ -24,6 +49,28 @@ public static IFileInfo File(this IDirectoryInfo info, string name)
return info.FileSystem.FileInfo.New(info.FileSystem.Path.Combine(info.FullName, name));
}

/// <summary>
/// Get an <see cref="IFileInfo"/> for the specified sub-directories file <paramref name="names"/>
/// </summary>
/// <param name="info"></param>
/// <param name="names">Sub-directories and file name (ex. "test", "test.txt"). Empty or null names are automatically removed from this list</param>
/// <returns>An <see cref="IFileInfo"/> for the specified file</returns>
public static IFileInfo File(this IDirectoryInfo info, params string[] names)
{
return info.File((IEnumerable<string>)names);
}

/// <summary>
/// Get an <see cref="IFileInfo"/> for the specified sub-directories file <paramref name="names"/>
/// </summary>
/// <param name="info"></param>
/// <param name="names">Sub-directories and file name (ex. "test", "test.txt"). Empty or null names are automatically removed from this list</param>
/// <returns>An <see cref="IFileInfo"/> for the specified file</returns>
public static IFileInfo File(this IDirectoryInfo info, IEnumerable<string> names)
{
return info.FileSystem.FileInfo.New(info.FileSystem.Path.Combine(GetPaths(info, names)));
}

/// <summary>
/// Throws an exception if the directory <paramref name="info"/> doesn't exists
/// </summary>
Expand All @@ -34,5 +81,12 @@ public static void ThrowIfNotFound(this IDirectoryInfo info)
if (!info.Exists)
throw new DirectoryNotFoundException(StringResources.Format("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION", info.FullName));
}

private static string[] GetPaths(IDirectoryInfo info, IEnumerable<string> names)
{
return new[] { info.FullName }
.Concat(names.Where(n => !String.IsNullOrEmpty(n)))
.ToArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<PackageId>TestableIO.System.IO.Abstractions.Extensions</PackageId>
<TargetFrameworks>net6.0;netstandard2.1;netstandard2.0;net461</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.1;netstandard2.0;net462</TargetFrameworks>
<Description>Convenience functionalities on top of System.IO.Abstractions</Description>
<RootNamespace>System.IO.Abstractions</RootNamespace>
<PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/../../ReleaseNotes.md"))</PackageReleaseNotes>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using System.Collections.Generic;

namespace System.IO.Abstractions.Extensions.Tests
{
Expand Down Expand Up @@ -30,6 +31,59 @@ public void SubDirectory_Extension_Test()
Assert.IsFalse(fs.Directory.Exists(expectedPath));
}

[TestCase("test1", "test2")]
[TestCase("test1", "", "test2")]
[TestCase("test1", null, "test2")]
public void SubDirectoryWithParams_Extension_Test(params string[] subFolders)
{
//arrange
var fs = new FileSystem();
var current = fs.DirectoryInfo.New(fs.Directory.GetCurrentDirectory());
var expectedPath = fs.Path.Combine(current.FullName, "test1", "test2");

//make sure directory doesn't exists
Assert.IsFalse(fs.Directory.Exists(expectedPath));

//create directory
var created = current.SubDirectory(subFolders);
created.Create();

//assert it exists
Assert.IsTrue(fs.Directory.Exists(expectedPath));
Assert.AreEqual(expectedPath, created.FullName);

//delete directory
created.Delete();
Assert.IsFalse(fs.Directory.Exists(expectedPath));
}

[TestCase("test1", "test2")]
[TestCase("test1", "", "test2")]
[TestCase("test1", null, "test2")]
public void SubDirectoryWithIEnumerable_Extension_Test(params string[] names)
{
//arrange
var fs = new FileSystem();
var current = fs.DirectoryInfo.New(fs.Directory.GetCurrentDirectory());
var expectedPath = fs.Path.Combine(current.FullName, "test1", "test2");

//make sure directory doesn't exists
Assert.IsFalse(fs.Directory.Exists(expectedPath));

//create directory
var list = new List<string>(names);
var created = current.SubDirectory(list);
created.Create();

//assert it exists
Assert.IsTrue(fs.Directory.Exists(expectedPath));
Assert.AreEqual(expectedPath, created.FullName);

//delete directory
created.Delete();
Assert.IsFalse(fs.Directory.Exists(expectedPath));
}

[Test]
public void File_Extension_Test()
{
Expand Down Expand Up @@ -58,6 +112,38 @@ public void File_Extension_Test()
Assert.IsFalse(fs.File.Exists(expectedPath));
}

[TestCase("test1", "test2", "test.txt")]
[TestCase("test1", "", "test2", "test.txt")]
[TestCase("test1", null, "test2", "test.txt")]

public void FileWithParams_Extension_Test(params string[] names)
{
//arrange
var fs = new FileSystem();
var current = fs.DirectoryInfo.New(fs.Directory.GetCurrentDirectory());
var expectedPath = fs.Path.Combine(current.FullName, "test1", "test2", "test.txt");

//make sure file doesn't exists
Assert.IsFalse(fs.File.Exists(expectedPath));

//act, create file
var created = current.File(names);
created.Directory.Create();
using (var stream = created.Create())
{
stream.Dispose();
}

//assert it exists
Assert.IsTrue(fs.File.Exists(expectedPath));
Assert.AreEqual(expectedPath, created.FullName);

//delete file
created.Delete();
created.Directory.Delete();
Assert.IsFalse(fs.File.Exists(expectedPath));
}

[Test]
public void ThrowIfNotFound_IfDirectoryDoesNotExists_ThrowsException()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net5.0;net461</TargetFrameworks>
<TargetFrameworks Condition="!$([MSBuild]::IsOsUnixLike())">$(TargetFrameworks);net461</TargetFrameworks>
<TargetFrameworks>net7.0;net6.0;net5.0</TargetFrameworks>
<TargetFrameworks Condition="!$([MSBuild]::IsOsUnixLike())">$(TargetFrameworks);net462</TargetFrameworks>
<IsPackable>false</IsPackable>
<IsTestable>true</IsTestable>
</PropertyGroup>
Expand Down

0 comments on commit 522df03

Please sign in to comment.