Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Also fix it for UNC drives
  • Loading branch information
vbreuss committed Sep 6, 2025
commit dbe180718f73edf0bfceceda4179240087474c8c
2 changes: 1 addition & 1 deletion Source/Testably.Abstractions.Testing/MockFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public MockFileSystem WithDrive(string? drive,
Action<IStorageDrive>? driveCallback = null)
{
IStorageDrive driveInfoMock =
drive == null
drive == null || string.Equals(drive, "\\", StringComparison.Ordinal)
? Storage.MainDrive
: Storage.GetOrAddDrive(drive);
driveCallback?.Invoke(driveInfoMock);
Expand Down
16 changes: 13 additions & 3 deletions Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,15 @@ public IEnumerable<IStorageDrive> GetDrives()
drive = _fileSystem.Storage.MainDrive;
}

return InMemoryLocation.New(_fileSystem, drive, path.GetFullPathOrWhiteSpace(_fileSystem),
path);
string fullPath = path;
if (!fullPath.IsUncPath(_fileSystem) ||
!_fileSystem.Execute.IsNetFramework ||
fullPath.LastIndexOf(_fileSystem.Path.DirectorySeparatorChar) > 2)
{
fullPath = path.GetFullPathOrWhiteSpace(_fileSystem);
}

return InMemoryLocation.New(_fileSystem, drive, fullPath, path);
}

/// <inheritdoc cref="IStorage.GetOrAddDrive(string)" />
Expand All @@ -367,7 +374,10 @@ public IEnumerable<IStorageDrive> GetDrives()
DriveInfoMock drive = DriveInfoMock.New(driveName, _fileSystem);
return _drives.GetOrAdd(drive.GetName(), _ =>
{
GetOrCreateContainer(GetLocation(drive.GetName()), InMemoryContainer.NewDirectory);
IStorageLocation rootLocation =
InMemoryLocation.New(_fileSystem, drive, drive.GetName());
_containers.TryAdd(rootLocation,
InMemoryContainer.NewDirectory(rootLocation, _fileSystem));
return drive;
});
}
Expand Down
26 changes: 21 additions & 5 deletions Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public async Task ToString_ShouldContainStorageInformation()

string result = sut.ToString();

await That(result).Contains("directories: 0, files: 1");
await That(result).Contains("directories: 1, files: 1");
}

[Theory]
Expand Down Expand Up @@ -246,13 +246,13 @@ await That(drives).HasSingle().Matching(d
[Fact]
public async Task WithDrive_ShouldInitializeDrivesWithRootDirectory()
{
MockFileSystem sut = new MockFileSystem().WithDrive("d");
List<IFileInfo> files = sut.DriveInfo.GetDrives()
MockFileSystem sut = new MockFileSystem().WithDrive("V");
List<IFileSystemInfo> fileSystemInfos = sut.DriveInfo.GetDrives()
.SelectMany(drive => drive.RootDirectory
.EnumerateFiles("*", SearchOption.AllDirectories))
.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
.ToList();

await That(files.Count).IsEqualTo(0);
await That(fileSystemInfos).HasCount(0);
}

[Theory]
Expand Down Expand Up @@ -319,6 +319,22 @@ public async Task WithUncDrive_ShouldCreateUncDrive(
await That(result).IsEqualTo(contents);
}

[Fact]
public async Task WithUncDrive_ShouldInitializeDrivesWithRootDirectory()
{
MockFileSystem sut = new();
string uncPrefix = new(sut.Path.DirectorySeparatorChar, 2);
string uncDrive = $"{uncPrefix}unc-server";
sut.WithUncDrive(uncDrive);
IDriveInfo drive = sut.DriveInfo.New(uncDrive);

List<IFileSystemInfo> fileSystemInfos = drive.RootDirectory
.EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
.ToList();

await That(fileSystemInfos).HasCount(0);
}

[Theory]
[AutoData]
public async Task WithUncDrive_ShouldNotBeIncludedInGetDrives(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ public async Task ToString_File_ShouldIncludePathAndFileSize(byte[] bytes)
MockFileSystem fileSystem = new();
string expectedPath = fileSystem.Path.GetFullPath("foo.txt");
fileSystem.File.WriteAllBytes(expectedPath, bytes);
IStorageContainer sut = fileSystem.StorageContainers.Single();
IStorageContainer sut = fileSystem.StorageContainers
.Single(x => x.Type == FileSystemTypes.File);

string? result = sut.ToString();

Expand Down
Loading