Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,29 @@ public IEnumerable<IStorageLocation> EnumerateLocations(
string searchPattern = EnumerationOptionsHelper.DefaultSearchPattern,
EnumerationOptions? enumerationOptions = null)
{
// Perform immediate validation and throw exceptions if necessary
ValidateExpression(searchPattern);
if (!_containers.TryGetValue(location, out IStorageContainer? parentContainer))
{
throw ExceptionFactory.DirectoryNotFound(location.FullPath);
}

// Return the actual enumeration implementation
return EnumerateLocationsImpl(location, type, requestParentAccess, searchPattern, enumerationOptions, parentContainer);
}

/// <summary>
/// Internal implementation of location enumeration that uses yield return.
/// This method contains the actual enumeration logic and is only called after validation passes.
/// </summary>
private IEnumerable<IStorageLocation> EnumerateLocationsImpl(
IStorageLocation location,
FileSystemTypes type,
bool requestParentAccess,
string searchPattern,
EnumerationOptions? enumerationOptions,
IStorageContainer parentContainer)
{
IDisposable parentAccess = new NoOpDisposable();
if (requestParentAccess)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ await That(sut.Statistics.DirectoryInfo["foo"])
public async Task Method_EnumerateDirectories_ShouldRegisterCall()
{
MockFileSystem sut = new();
sut.Initialize().WithSubdirectory("foo");

sut.DirectoryInfo.New("foo").EnumerateDirectories();

Expand All @@ -93,6 +94,7 @@ await That(sut.Statistics.DirectoryInfo["foo"])
public async Task Method_EnumerateDirectories_String_EnumerationOptions_ShouldRegisterCall()
{
MockFileSystem sut = new();
sut.Initialize().WithSubdirectory("foo");
string searchPattern = "foo";
EnumerationOptions enumerationOptions = new();

Expand All @@ -109,6 +111,7 @@ await That(sut.Statistics.DirectoryInfo["foo"])
public async Task Method_EnumerateDirectories_String_SearchOption_ShouldRegisterCall()
{
MockFileSystem sut = new();
sut.Initialize().WithSubdirectory("foo");
string searchPattern = "foo";
SearchOption searchOption = SearchOption.AllDirectories;

Expand All @@ -124,6 +127,7 @@ await That(sut.Statistics.DirectoryInfo["foo"])
public async Task Method_EnumerateDirectories_String_ShouldRegisterCall()
{
MockFileSystem sut = new();
sut.Initialize().WithSubdirectory("foo");
string searchPattern = "foo";

sut.DirectoryInfo.New("foo").EnumerateDirectories(searchPattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ await That(sut.Statistics.Directory).OnlyContainsMethodCall(nameof(IDirectory.De
public async Task Method_EnumerateDirectories_String_ShouldRegisterCall()
{
MockFileSystem sut = new();
sut.Initialize().WithSubdirectory("foo");
string path = "foo";

sut.Directory.EnumerateDirectories(path);
Expand All @@ -118,6 +119,7 @@ await That(sut.Statistics.Directory).OnlyContainsMethodCall(
public async Task Method_EnumerateDirectories_String_String_EnumerationOptions_ShouldRegisterCall()
{
MockFileSystem sut = new();
sut.Initialize().WithSubdirectory("foo");
string path = "foo";
string searchPattern = "foo";
EnumerationOptions enumerationOptions = new();
Expand All @@ -135,6 +137,7 @@ await That(sut.Statistics.Directory).OnlyContainsMethodCall(
public async Task Method_EnumerateDirectories_String_String_SearchOption_ShouldRegisterCall()
{
MockFileSystem sut = new();
sut.Initialize().WithSubdirectory("foo");
string path = "foo";
string searchPattern = "foo";
SearchOption searchOption = SearchOption.AllDirectories;
Expand All @@ -151,6 +154,7 @@ await That(sut.Statistics.Directory).OnlyContainsMethodCall(
public async Task Method_EnumerateDirectories_String_String_ShouldRegisterCall()
{
MockFileSystem sut = new();
sut.Initialize().WithSubdirectory("foo");
string path = "foo";
string searchPattern = "foo";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ await That(Act).Throws<DirectoryNotFoundException>()
await That(FileSystem.Directory.Exists(path)).IsFalse();
}

[Theory]
[AutoData]
public async Task
EnumerateDirectories_MissingDirectory_ShouldThrowDirectoryNotFoundExceptionImmediately(
string path)
{
string expectedPath = FileSystem.Path.Combine(BasePath, path);

void Act() =>
_ = FileSystem.Directory.EnumerateDirectories(path);

await That(Act).Throws<DirectoryNotFoundException>()
.WithMessageContaining($"'{expectedPath}'").And
.WithHResult(-2147024893);
await That(FileSystem.Directory.Exists(path)).IsFalse();
}

[Fact]
public async Task EnumerateDirectories_RelativePath_ShouldNotIncludeTrailingSlash()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ await That(Act).Throws<DirectoryNotFoundException>()
await That(FileSystem.Directory.Exists(path)).IsFalse();
}

[Theory]
[AutoData]
public async Task
EnumerateFileSystemEntries_MissingDirectory_ShouldThrowDirectoryNotFoundExceptionImmediately(
string path)
{
string expectedPath = FileSystem.Path.Combine(BasePath, path);

void Act() =>
_ = FileSystem.Directory.EnumerateFileSystemEntries(path);

await That(Act).Throws<DirectoryNotFoundException>()
.WithMessageContaining($"'{expectedPath}'").And
.WithHResult(-2147024893);
await That(FileSystem.Directory.Exists(path)).IsFalse();
}

[Theory]
[AutoData]
public async Task
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ await That(Act).Throws<DirectoryNotFoundException>()
await That(FileSystem.Directory.Exists(path)).IsFalse();
}

[Theory]
[AutoData]
public async Task
EnumerateFiles_MissingDirectory_ShouldThrowDirectoryNotFoundExceptionImmediately(
string path)
{
string expectedPath = FileSystem.Path.Combine(BasePath, path);

void Act() =>
_ = FileSystem.Directory.EnumerateFiles(path);

await That(Act).Throws<DirectoryNotFoundException>()
.WithMessageContaining($"'{expectedPath}'").And
.WithHResult(-2147024893);
await That(FileSystem.Directory.Exists(path)).IsFalse();
}

[Theory]
[AutoData]
public async Task
Expand Down
Loading