|
| 1 | +using Testably.Abstractions.Testing.FileSystem; |
| 2 | + |
| 3 | +namespace Testably.Abstractions.Testing.Tests.FileSystem; |
| 4 | + |
| 5 | +public class DirectoryMockTests |
| 6 | +{ |
| 7 | + [Fact] |
| 8 | + public async Task |
| 9 | + EnumerateDirectories_UnauthorizedParentAccess_ShouldThrowUnauthorizedAccessExceptionImmediately() |
| 10 | + { |
| 11 | + Skip.IfNot(Test.RunsOnWindows); |
| 12 | + |
| 13 | + string path = "foo"; |
| 14 | + MockFileSystem fileSystem = new(); |
| 15 | + IDirectoryInfo sut = fileSystem.Directory.CreateDirectory(path); |
| 16 | + fileSystem.WithAccessControlStrategy( |
| 17 | + new DefaultAccessControlStrategy((p, _) |
| 18 | + => !p.EndsWith(path, StringComparison.Ordinal))); |
| 19 | + |
| 20 | + void Act() => |
| 21 | + _ = fileSystem.Directory.EnumerateDirectories(path); |
| 22 | + |
| 23 | + await That(Act).Throws<UnauthorizedAccessException>() |
| 24 | + .WithMessageContaining($"'{sut.FullName}'").And |
| 25 | + .WithHResult(-2147024891); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public async Task |
| 30 | + EnumerateFiles_UnauthorizedParentAccess_ShouldThrowUnauthorizedAccessExceptionImmediately() |
| 31 | + { |
| 32 | + Skip.IfNot(Test.RunsOnWindows); |
| 33 | + |
| 34 | + string path = "foo"; |
| 35 | + MockFileSystem fileSystem = new(); |
| 36 | + IDirectoryInfo sut = fileSystem.Directory.CreateDirectory(path); |
| 37 | + fileSystem.WithAccessControlStrategy( |
| 38 | + new DefaultAccessControlStrategy((p, _) |
| 39 | + => !p.EndsWith(path, StringComparison.Ordinal))); |
| 40 | + |
| 41 | + void Act() => |
| 42 | + _ = fileSystem.Directory.EnumerateFiles(path); |
| 43 | + |
| 44 | + await That(Act).Throws<UnauthorizedAccessException>() |
| 45 | + .WithMessageContaining($"'{sut.FullName}'").And |
| 46 | + .WithHResult(-2147024891); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public async Task |
| 51 | + EnumerateFileSystemEntries_UnauthorizedParentAccess_ShouldThrowUnauthorizedAccessExceptionImmediately() |
| 52 | + { |
| 53 | + Skip.IfNot(Test.RunsOnWindows); |
| 54 | + |
| 55 | + string path = "foo"; |
| 56 | + MockFileSystem fileSystem = new(); |
| 57 | + IDirectoryInfo sut = fileSystem.Directory.CreateDirectory(path); |
| 58 | + fileSystem.WithAccessControlStrategy( |
| 59 | + new DefaultAccessControlStrategy((p, _) |
| 60 | + => !p.EndsWith(path, StringComparison.Ordinal))); |
| 61 | + |
| 62 | + void Act() => |
| 63 | + _ = fileSystem.Directory.EnumerateFileSystemEntries(path); |
| 64 | + |
| 65 | + await That(Act).Throws<UnauthorizedAccessException>() |
| 66 | + .WithMessageContaining($"'{sut.FullName}'").And |
| 67 | + .WithHResult(-2147024891); |
| 68 | + } |
| 69 | +} |
0 commit comments