Skip to content

Commit 90357af

Browse files
Copilotvbreuss
andcommitted
Fix DirectoryMock.EnumerateDirectories immediate exception throwing
Split InMemoryStorage.EnumerateLocations into validation and implementation methods. Validation and exceptions now occur immediately when method is called, while enumeration logic happens during iteration as expected. Co-authored-by: vbreuss <3438234+vbreuss@users.noreply.github.com>
1 parent 6c40f5a commit 90357af

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,29 @@ public IEnumerable<IStorageLocation> EnumerateLocations(
193193
string searchPattern = EnumerationOptionsHelper.DefaultSearchPattern,
194194
EnumerationOptions? enumerationOptions = null)
195195
{
196+
// Perform immediate validation and throw exceptions if necessary
196197
ValidateExpression(searchPattern);
197198
if (!_containers.TryGetValue(location, out IStorageContainer? parentContainer))
198199
{
199200
throw ExceptionFactory.DirectoryNotFound(location.FullPath);
200201
}
201202

203+
// Return the actual enumeration implementation
204+
return EnumerateLocationsImpl(location, type, requestParentAccess, searchPattern, enumerationOptions, parentContainer);
205+
}
206+
207+
/// <summary>
208+
/// Internal implementation of location enumeration that uses yield return.
209+
/// This method contains the actual enumeration logic and is only called after validation passes.
210+
/// </summary>
211+
private IEnumerable<IStorageLocation> EnumerateLocationsImpl(
212+
IStorageLocation location,
213+
FileSystemTypes type,
214+
bool requestParentAccess,
215+
string searchPattern,
216+
EnumerationOptions? enumerationOptions,
217+
IStorageContainer parentContainer)
218+
{
202219
IDisposable parentAccess = new NoOpDisposable();
203220
if (requestParentAccess)
204221
{

Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateDirectoriesTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ await That(Act).Throws<DirectoryNotFoundException>()
4141
await That(FileSystem.Directory.Exists(path)).IsFalse();
4242
}
4343

44+
[Theory]
45+
[AutoData]
46+
public async Task
47+
EnumerateDirectories_MissingDirectory_ShouldThrowDirectoryNotFoundExceptionImmediately(
48+
string path)
49+
{
50+
string expectedPath = FileSystem.Path.Combine(BasePath, path);
51+
52+
void Act() =>
53+
_ = FileSystem.Directory.EnumerateDirectories(path);
54+
55+
await That(Act).Throws<DirectoryNotFoundException>()
56+
.WithMessageContaining($"'{expectedPath}'").And
57+
.WithHResult(-2147024893);
58+
await That(FileSystem.Directory.Exists(path)).IsFalse();
59+
}
60+
4461
[Fact]
4562
public async Task EnumerateDirectories_RelativePath_ShouldNotIncludeTrailingSlash()
4663
{

Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateFileSystemInfosTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ await That(Act).Throws<DirectoryNotFoundException>()
2828
await That(FileSystem.Directory.Exists(path)).IsFalse();
2929
}
3030

31+
[Theory]
32+
[AutoData]
33+
public async Task
34+
EnumerateFileSystemEntries_MissingDirectory_ShouldThrowDirectoryNotFoundExceptionImmediately(
35+
string path)
36+
{
37+
string expectedPath = FileSystem.Path.Combine(BasePath, path);
38+
39+
void Act() =>
40+
_ = FileSystem.Directory.EnumerateFileSystemEntries(path);
41+
42+
await That(Act).Throws<DirectoryNotFoundException>()
43+
.WithMessageContaining($"'{expectedPath}'").And
44+
.WithHResult(-2147024893);
45+
await That(FileSystem.Directory.Exists(path)).IsFalse();
46+
}
47+
3148
[Theory]
3249
[AutoData]
3350
public async Task

Tests/Testably.Abstractions.Tests/FileSystem/Directory/EnumerateFilesTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ await That(Act).Throws<DirectoryNotFoundException>()
2525
await That(FileSystem.Directory.Exists(path)).IsFalse();
2626
}
2727

28+
[Theory]
29+
[AutoData]
30+
public async Task
31+
EnumerateFiles_MissingDirectory_ShouldThrowDirectoryNotFoundExceptionImmediately(
32+
string path)
33+
{
34+
string expectedPath = FileSystem.Path.Combine(BasePath, path);
35+
36+
void Act() =>
37+
_ = FileSystem.Directory.EnumerateFiles(path);
38+
39+
await That(Act).Throws<DirectoryNotFoundException>()
40+
.WithMessageContaining($"'{expectedPath}'").And
41+
.WithHResult(-2147024893);
42+
await That(FileSystem.Directory.Exists(path)).IsFalse();
43+
}
44+
2845
[Theory]
2946
[AutoData]
3047
public async Task

0 commit comments

Comments
 (0)