diff --git a/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs b/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs index 8016a6f2..bb668030 100644 --- a/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs +++ b/Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs @@ -261,6 +261,17 @@ public IEnumerable 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); diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateDirectoriesTests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateDirectoriesTests.cs index 194f3e1d..d34cf6e2 100644 --- a/Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateDirectoriesTests.cs +++ b/Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateDirectoriesTests.cs @@ -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; @@ -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( + messageContains: @"Access to the path 'C:\Windows\System32\", + hResult: -2147024891); + } + } +#endif + #if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS [SkippableTheory] [InlineAutoData(MatchCasing.CaseInsensitive)]