Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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, Execute.Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)
? Storage.MainDrive
: Storage.GetOrAddDrive(drive);
driveCallback?.Invoke(driveInfoMock);
Expand Down
57 changes: 40 additions & 17 deletions Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public InMemoryStorage(MockFileSystem fileSystem)
CurrentDirectory = string.Empty.PrefixRoot(_fileSystem);
DriveInfoMock mainDrive = DriveInfoMock.New(CurrentDirectory, _fileSystem);
_drives.TryAdd(mainDrive.GetName(), mainDrive);
IStorageLocation rootLocation =
InMemoryLocation.New(_fileSystem, mainDrive, mainDrive.GetName());
_containers.TryAdd(rootLocation, InMemoryContainer.NewDirectory(rootLocation, _fileSystem));

MainDrive = mainDrive;
}

Expand Down Expand Up @@ -189,7 +193,7 @@ public IEnumerable<IStorageLocation> EnumerateLocations(
EnumerationOptions? enumerationOptions = null)
{
ValidateExpression(searchPattern);
if (!_containers.TryGetValue(location, out var parentContainer))
if (!_containers.TryGetValue(location, out IStorageContainer? parentContainer))
{
throw ExceptionFactory.DirectoryNotFound(location.FullPath);
}
Expand Down Expand Up @@ -227,11 +231,11 @@ public IEnumerable<IStorageLocation> EnumerateLocations(
}

if (enumerationOptions.ReturnSpecialDirectories &&
type == FileSystemTypes.Directory)
type == FileSystemTypes.Directory)
{
IStorageDrive? drive = _fileSystem.Storage.GetDrive(fullPath);
if (drive == null &&
!fullPath.IsUncPath(_fileSystem))
!fullPath.IsUncPath(_fileSystem))
{
drive = _fileSystem.Storage.MainDrive;
}
Expand All @@ -255,11 +259,12 @@ public IEnumerable<IStorageLocation> EnumerateLocations(

foreach (KeyValuePair<IStorageLocation, IStorageContainer> item in _containers
.Where(x => x.Key.FullPath.StartsWith(fullPath,
_fileSystem.Execute.StringComparisonMode) &&
!x.Key.Equals(location)))
_fileSystem.Execute.StringComparisonMode) &&
!x.Key.Equals(location)))
{
if (type.HasFlag(item.Value.Type) &&
IncludeItemInEnumeration(item, fullPathWithoutTrailingSlash, enumerationOptions))
IncludeItemInEnumeration(item, fullPathWithoutTrailingSlash,
enumerationOptions))
{
string? itemPath = item.Key.FullPath;
if (itemPath.EndsWith(_fileSystem.Path.DirectorySeparatorChar))
Expand All @@ -269,14 +274,14 @@ public IEnumerable<IStorageLocation> EnumerateLocations(

string name = _fileSystem.Execute.Path.GetFileName(itemPath);
if (EnumerationOptionsHelper.MatchesPattern(
_fileSystem.Execute,
enumerationOptions,
name,
searchPattern) ||
(_fileSystem.Execute.IsNetFramework &&
SearchPatternMatchesFileExtensionOnNetFramework(
searchPattern,
_fileSystem.Execute.Path.GetExtension(name))))
_fileSystem.Execute,
enumerationOptions,
name,
searchPattern) ||
(_fileSystem.Execute.IsNetFramework &&
SearchPatternMatchesFileExtensionOnNetFramework(
searchPattern,
_fileSystem.Execute.Path.GetExtension(name))))
{
yield return item.Key;
}
Expand Down Expand Up @@ -346,8 +351,19 @@ public IEnumerable<IStorageDrive> GetDrives()
drive = _fileSystem.Storage.MainDrive;
}

return InMemoryLocation.New(_fileSystem, drive, path.GetFullPathOrWhiteSpace(_fileSystem),
path);
string fullPath;
if (path.IsUncPath(_fileSystem) &&
_fileSystem.Execute is { IsNetFramework: true } or {IsWindows: false } &&
path.LastIndexOf(_fileSystem.Path.DirectorySeparatorChar) <= 2)
{
fullPath = path;
}
else
{
fullPath = path.GetFullPathOrWhiteSpace(_fileSystem);
}

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

/// <inheritdoc cref="IStorage.GetOrAddDrive(string)" />
Expand All @@ -360,7 +376,14 @@ public IEnumerable<IStorageDrive> GetDrives()
}

DriveInfoMock drive = DriveInfoMock.New(driveName, _fileSystem);
return _drives.GetOrAdd(drive.GetName(), _ => drive);
return _drives.GetOrAdd(drive.GetName(), _ =>
{
IStorageLocation rootLocation =
InMemoryLocation.New(_fileSystem, drive, drive.GetName());
_containers.TryAdd(rootLocation,
InMemoryContainer.NewDirectory(rootLocation, _fileSystem));
return drive;
});
}

/// <inheritdoc cref="IStorage.GetOrCreateContainer" />
Expand Down
40 changes: 36 additions & 4 deletions Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Testably.Abstractions.Helpers;
using Testably.Abstractions.Testing.FileSystem;
Expand Down Expand Up @@ -115,13 +116,14 @@ 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]
[AutoData]
public async Task WithAccessControl_Denied_CreateDirectoryShouldThrowUnauthorizedAccessException(
string path)
public async Task
WithAccessControl_Denied_CreateDirectoryShouldThrowUnauthorizedAccessException(
string path)
{
Skip.If(!Test.RunsOnWindows);

Expand Down Expand Up @@ -241,6 +243,20 @@ await That(drives).HasSingle().Matching(d
=> string.Equals(d.Name, expectedDriveName, StringComparison.Ordinal));
}

[Fact]
public async Task WithDrive_ShouldInitializeDrivesWithRootDirectory()
{
Skip.IfNot(Test.RunsOnWindows, "Linux does not support different drives.");

MockFileSystem sut = new MockFileSystem().WithDrive("V");
List<IFileSystemInfo> fileSystemInfos = sut.DriveInfo.GetDrives()
.SelectMany(drive => drive.RootDirectory
.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
.ToList();

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

[Theory]
[AutoData]
public async Task WithDrive_WithCallback_ShouldUpdateDrive(long totalSize)
Expand Down Expand Up @@ -305,6 +321,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-Path";
sut.WithUncDrive("UNC-Path");
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