Skip to content

Commit de4facf

Browse files
rkmfgreinacher
andauthored
feat: Add GetFileSystemEntries with SearchOption (#878)
* 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>
1 parent fca03be commit de4facf

File tree

11 files changed

+47
-21
lines changed

11 files changed

+47
-21
lines changed

src/System.IO.Abstractions.TestingHelpers/MockDirectory.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,15 @@ public override string[] GetFileSystemEntries(string path, string searchPattern)
380380
return dirs.Union(files).ToArray();
381381
}
382382

383+
/// <inheritdoc />
384+
public override string[] GetFileSystemEntries(string path, string searchPattern, SearchOption searchOption)
385+
{
386+
var dirs = GetDirectories(path, searchPattern, searchOption);
387+
var files = GetFiles(path, searchPattern, searchOption);
388+
389+
return dirs.Union(files).ToArray();
390+
}
391+
383392
/// <inheritdoc />
384393
public override DateTime GetLastAccessTime(string path)
385394
{
@@ -644,25 +653,19 @@ public override IEnumerable<string> EnumerateFiles(string path, string searchPat
644653
/// <inheritdoc />
645654
public override IEnumerable<string> EnumerateFileSystemEntries(string path)
646655
{
647-
var fileSystemEntries = new List<string>(GetFiles(path));
648-
fileSystemEntries.AddRange(GetDirectories(path));
649-
return fileSystemEntries;
656+
return GetFileSystemEntries(path);
650657
}
651658

652659
/// <inheritdoc />
653660
public override IEnumerable<string> EnumerateFileSystemEntries(string path, string searchPattern)
654661
{
655-
var fileSystemEntries = new List<string>(GetFiles(path, searchPattern));
656-
fileSystemEntries.AddRange(GetDirectories(path, searchPattern));
657-
return fileSystemEntries;
662+
return GetFileSystemEntries(path, searchPattern);
658663
}
659664

660665
/// <inheritdoc />
661666
public override IEnumerable<string> EnumerateFileSystemEntries(string path, string searchPattern, SearchOption searchOption)
662667
{
663-
var fileSystemEntries = new List<string>(GetFiles(path, searchPattern, searchOption));
664-
fileSystemEntries.AddRange(GetDirectories(path, searchPattern, searchOption));
665-
return fileSystemEntries;
668+
return GetFileSystemEntries(path, searchPattern, searchOption);
666669
}
667670

668671
#if FEATURE_ENUMERATION_OPTIONS

src/System.IO.Abstractions/DirectoryBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ internal DirectoryBase() { }
9595
/// <inheritdoc cref="IDirectory.GetFileSystemEntries(string,string)"/>
9696
public abstract string[] GetFileSystemEntries(string path, string searchPattern);
9797

98+
/// <inheritdoc cref="IDirectory.GetFileSystemEntries(string,string,SearchOption)"/>
99+
public abstract string[] GetFileSystemEntries(string path, string searchPattern, SearchOption searchOption);
100+
98101
/// <inheritdoc cref="IDirectory.GetLastAccessTime"/>
99102
public abstract DateTime GetLastAccessTime(string path);
100103

src/System.IO.Abstractions/DirectoryWrapper.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ public override string[] GetFileSystemEntries(string path, string searchPattern)
156156
return Directory.GetFileSystemEntries(path, searchPattern);
157157
}
158158

159+
/// <inheritdoc />
160+
public override string[] GetFileSystemEntries(string path, string searchPattern, SearchOption searchOption)
161+
{
162+
return Directory.GetFileSystemEntries(path, searchPattern, searchOption);
163+
}
164+
159165
/// <inheritdoc />
160166
public override DateTime GetLastAccessTime(string path)
161167
{

src/System.IO.Abstractions/IDirectory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public interface IDirectory
7777
string[] GetFileSystemEntries(string path);
7878
/// <inheritdoc cref="Directory.GetFileSystemEntries(string,string)"/>
7979
string[] GetFileSystemEntries(string path, string searchPattern);
80+
/// <inheritdoc cref="Directory.GetFileSystemEntries(string,string,SearchOption)"/>
81+
string[] GetFileSystemEntries(string path, string searchPattern, SearchOption searchOption);
8082
/// <inheritdoc cref="Directory.GetLastAccessTime"/>
8183
DateTime GetLastAccessTime(string path);
8284
/// <inheritdoc cref="Directory.GetLastAccessTimeUtc"/>

tests/System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Linq;
33
using System.Runtime.Versioning;
44
using System.Security.AccessControl;
@@ -956,6 +956,24 @@ public void MockDirectory_GetFileSystemEntries_Returns_Files_And_Directories()
956956
Assert.AreEqual(testPath, entries.Last());
957957
}
958958

959+
[Test]
960+
public void MockDirectory_GetFileSystemEntries_ShouldNotReturnSubDirectory_WithSearchOption()
961+
{
962+
string testPath = XFS.Path(@"c:\foo\bar.txt");
963+
string testDir = XFS.Path(@"c:\foo\bar");
964+
string testSubDir = XFS.Path(@"c:\foo\bar\baz");
965+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
966+
{
967+
{ testPath, new MockFileData("Demo text content") },
968+
{ testSubDir, new MockDirectoryData() },
969+
});
970+
971+
var entries = fileSystem.Directory.GetFileSystemEntries(XFS.Path(@"c:\foo"), "*", SearchOption.TopDirectoryOnly).OrderBy(k => k);
972+
Assert.AreEqual(2, entries.Count());
973+
Assert.AreEqual(testDir, entries.First());
974+
Assert.AreEqual(testPath, entries.Last());
975+
}
976+
959977
[Test]
960978
public void MockDirectory_GetFiles_ShouldThrowArgumentNullException_IfPathParamIsNull()
961979
{

tests/System.IO.Abstractions.Tests/__snapshots__/ApiParityTests.Directory_.NET 5.0.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"Void SetAccessControl(System.String, System.Security.AccessControl.DirectorySecurity)"
77
],
88
"MissingMembers": [
9-
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.EnumerationOptions)",
10-
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.SearchOption)"
9+
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.EnumerationOptions)"
1110
]
1211
}

tests/System.IO.Abstractions.Tests/__snapshots__/ApiParityTests.Directory_.NET 6.0.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
],
88
"MissingMembers": [
99
"System.IO.Abstractions.IFileSystemInfo ResolveLinkTarget(System.String, Boolean)",
10-
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.EnumerationOptions)",
11-
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.SearchOption)"
10+
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.EnumerationOptions)"
1211
]
1312
}

tests/System.IO.Abstractions.Tests/__snapshots__/ApiParityTests.Directory_.NET Core 2.1.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"System.Collections.Generic.IEnumerable`1[System.String] EnumerateFiles(System.String, System.String, System.IO.EnumerationOptions)",
1212
"System.String[] GetDirectories(System.String, System.String, System.IO.EnumerationOptions)",
1313
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.EnumerationOptions)",
14-
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.SearchOption)",
1514
"System.String[] GetFiles(System.String, System.String, System.IO.EnumerationOptions)"
1615
]
1716
}

tests/System.IO.Abstractions.Tests/__snapshots__/ApiParityTests.Directory_.NET Core 3.1.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"Void SetAccessControl(System.String, System.Security.AccessControl.DirectorySecurity)"
77
],
88
"MissingMembers": [
9-
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.EnumerationOptions)",
10-
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.SearchOption)"
9+
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.EnumerationOptions)"
1110
]
1211
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
22
"ExtraMembers": [],
3-
"MissingMembers": [
4-
"System.String[] GetFileSystemEntries(System.String, System.String, System.IO.SearchOption)"
5-
]
3+
"MissingMembers": []
64
}

0 commit comments

Comments
 (0)