Skip to content

Commit

Permalink
feat: support remaining properties of EnumerationOptions (#590)
Browse files Browse the repository at this point in the history
* Add support for "IgnoreInaccessible"
  • Loading branch information
vbreuss authored May 5, 2024
1 parent 29ccbad commit d6b88dc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,17 @@ public IEnumerable<IStorageLocation> EnumerateLocations(
continue;
}

if (!_fileSystem.AccessControlStrategy
.IsAccessGranted(item.Key.FullPath, item.Value.Extensibility))
{
if (!enumerationOptions.IgnoreInaccessible)
{
throw ExceptionFactory.AccessToPathDenied(item.Key.FullPath);
}

continue;
}

if (type.HasFlag(item.Value.Type))
{
string name = _fileSystem.Execute.Path.GetFileName(item.Key.FullPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Testably.Abstractions.Testing.FileSystem;

namespace Testably.Abstractions.Tests.FileSystem.Directory;

Expand Down Expand Up @@ -208,6 +209,52 @@ public void
}
#endif

#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS
[SkippableTheory]
[InlineData(true)]
[InlineData(false)]
public void
EnumerateDirectories_WithEnumerationOptions_ShouldConsiderIgnoreInaccessible(
bool ignoreInaccessible)
{
Skip.IfNot(Test.RunsOnWindows);

string path = @"C:\Windows\System32";
EnumerationOptions enumerationOptions = new()
{
IgnoreInaccessible = ignoreInaccessible,
RecurseSubdirectories = true
};
if (FileSystem is MockFileSystem mockFileSystem)
{
FileSystem.Directory.CreateDirectory(
FileSystem.Path.Combine(path, "bar"));
FileSystem.Directory.CreateDirectory(
FileSystem.Path.Combine(path, "foo"));
mockFileSystem.WithAccessControlStrategy(
new DefaultAccessControlStrategy((p, _)
=> !p.EndsWith("foo", StringComparison.Ordinal)));
}

Exception? exception = Record.Exception(() =>
{
_ = FileSystem.Directory
.EnumerateDirectories(path, "*", enumerationOptions).ToList();
});

if (ignoreInaccessible)
{
exception.Should().BeNull();
}
else
{
exception.Should().BeException<UnauthorizedAccessException>(
messageContains: @"Access to the path 'C:\Windows\System32\",
hResult: -2147024891);
}
}
#endif

#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS
[SkippableTheory]
[InlineAutoData(MatchCasing.CaseInsensitive)]
Expand Down

0 comments on commit d6b88dc

Please sign in to comment.