Skip to content

Commit

Permalink
feat: Add GetFileSystemEntries with SearchOption (#878)
Browse files Browse the repository at this point in the history
* Add GetFileSystemEntries with SearchOption

* Bump version

* Add test for GetFileSystemEntries with SearchOption

* call GetFileSystemEntries from EnumerateFileSystemEntries to reduce duplication

Co-authored-by: Florian Greinacher <florian@greinacher.de>
  • Loading branch information
rkm and fgreinacher authored Sep 10, 2022
1 parent fca03be commit de4facf
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 21 deletions.
21 changes: 12 additions & 9 deletions src/System.IO.Abstractions.TestingHelpers/MockDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,15 @@ public override string[] GetFileSystemEntries(string path, string searchPattern)
return dirs.Union(files).ToArray();
}

/// <inheritdoc />
public override string[] GetFileSystemEntries(string path, string searchPattern, SearchOption searchOption)
{
var dirs = GetDirectories(path, searchPattern, searchOption);
var files = GetFiles(path, searchPattern, searchOption);

return dirs.Union(files).ToArray();
}

/// <inheritdoc />
public override DateTime GetLastAccessTime(string path)
{
Expand Down Expand Up @@ -644,25 +653,19 @@ public override IEnumerable<string> EnumerateFiles(string path, string searchPat
/// <inheritdoc />
public override IEnumerable<string> EnumerateFileSystemEntries(string path)
{
var fileSystemEntries = new List<string>(GetFiles(path));
fileSystemEntries.AddRange(GetDirectories(path));
return fileSystemEntries;
return GetFileSystemEntries(path);
}

/// <inheritdoc />
public override IEnumerable<string> EnumerateFileSystemEntries(string path, string searchPattern)
{
var fileSystemEntries = new List<string>(GetFiles(path, searchPattern));
fileSystemEntries.AddRange(GetDirectories(path, searchPattern));
return fileSystemEntries;
return GetFileSystemEntries(path, searchPattern);
}

/// <inheritdoc />
public override IEnumerable<string> EnumerateFileSystemEntries(string path, string searchPattern, SearchOption searchOption)
{
var fileSystemEntries = new List<string>(GetFiles(path, searchPattern, searchOption));
fileSystemEntries.AddRange(GetDirectories(path, searchPattern, searchOption));
return fileSystemEntries;
return GetFileSystemEntries(path, searchPattern, searchOption);
}

#if FEATURE_ENUMERATION_OPTIONS
Expand Down
3 changes: 3 additions & 0 deletions src/System.IO.Abstractions/DirectoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ internal DirectoryBase() { }
/// <inheritdoc cref="IDirectory.GetFileSystemEntries(string,string)"/>
public abstract string[] GetFileSystemEntries(string path, string searchPattern);

/// <inheritdoc cref="IDirectory.GetFileSystemEntries(string,string,SearchOption)"/>
public abstract string[] GetFileSystemEntries(string path, string searchPattern, SearchOption searchOption);

/// <inheritdoc cref="IDirectory.GetLastAccessTime"/>
public abstract DateTime GetLastAccessTime(string path);

Expand Down
6 changes: 6 additions & 0 deletions src/System.IO.Abstractions/DirectoryWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ public override string[] GetFileSystemEntries(string path, string searchPattern)
return Directory.GetFileSystemEntries(path, searchPattern);
}

/// <inheritdoc />
public override string[] GetFileSystemEntries(string path, string searchPattern, SearchOption searchOption)
{
return Directory.GetFileSystemEntries(path, searchPattern, searchOption);
}

/// <inheritdoc />
public override DateTime GetLastAccessTime(string path)
{
Expand Down
2 changes: 2 additions & 0 deletions src/System.IO.Abstractions/IDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public interface IDirectory
string[] GetFileSystemEntries(string path);
/// <inheritdoc cref="Directory.GetFileSystemEntries(string,string)"/>
string[] GetFileSystemEntries(string path, string searchPattern);
/// <inheritdoc cref="Directory.GetFileSystemEntries(string,string,SearchOption)"/>
string[] GetFileSystemEntries(string path, string searchPattern, SearchOption searchOption);
/// <inheritdoc cref="Directory.GetLastAccessTime"/>
DateTime GetLastAccessTime(string path);
/// <inheritdoc cref="Directory.GetLastAccessTimeUtc"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Versioning;
using System.Security.AccessControl;
Expand Down Expand Up @@ -956,6 +956,24 @@ public void MockDirectory_GetFileSystemEntries_Returns_Files_And_Directories()
Assert.AreEqual(testPath, entries.Last());
}

[Test]
public void MockDirectory_GetFileSystemEntries_ShouldNotReturnSubDirectory_WithSearchOption()
{
string testPath = XFS.Path(@"c:\foo\bar.txt");
string testDir = XFS.Path(@"c:\foo\bar");
string testSubDir = XFS.Path(@"c:\foo\bar\baz");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ testPath, new MockFileData("Demo text content") },
{ testSubDir, new MockDirectoryData() },
});

var entries = fileSystem.Directory.GetFileSystemEntries(XFS.Path(@"c:\foo"), "*", SearchOption.TopDirectoryOnly).OrderBy(k => k);
Assert.AreEqual(2, entries.Count());
Assert.AreEqual(testDir, entries.First());
Assert.AreEqual(testPath, entries.Last());
}

[Test]
public void MockDirectory_GetFiles_ShouldThrowArgumentNullException_IfPathParamIsNull()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"Void SetAccessControl(System.String, System.Security.AccessControl.DirectorySecurity)"
],
"MissingMembers": [
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.EnumerationOptions)",
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.SearchOption)"
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.EnumerationOptions)"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
],
"MissingMembers": [
"System.IO.Abstractions.IFileSystemInfo ResolveLinkTarget(System.String, Boolean)",
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.EnumerationOptions)",
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.SearchOption)"
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.EnumerationOptions)"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"System.Collections.Generic.IEnumerable`1[System.String] EnumerateFiles(System.String, System.String, System.IO.EnumerationOptions)",
"System.String[] GetDirectories(System.String, System.String, System.IO.EnumerationOptions)",
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.EnumerationOptions)",
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.SearchOption)",
"System.String[] GetFiles(System.String, System.String, System.IO.EnumerationOptions)"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"Void SetAccessControl(System.String, System.Security.AccessControl.DirectorySecurity)"
],
"MissingMembers": [
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.EnumerationOptions)",
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.SearchOption)"
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.EnumerationOptions)"
]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"ExtraMembers": [],
"MissingMembers": [
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.SearchOption)"
]
"MissingMembers": []
}
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "17.1",
"version": "17.2",
"assemblyVersion": {
"precision": "major"
},
Expand Down

0 comments on commit de4facf

Please sign in to comment.