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
39 changes: 24 additions & 15 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 @@ -360,7 +365,11 @@ public IEnumerable<IStorageDrive> GetDrives()
}

DriveInfoMock drive = DriveInfoMock.New(driveName, _fileSystem);
return _drives.GetOrAdd(drive.GetName(), _ => drive);
return _drives.GetOrAdd(drive.GetName(), _ =>
{
GetOrCreateContainer(GetLocation(drive.GetName()), InMemoryContainer.NewDirectory);
return drive;
});
}

/// <inheritdoc cref="IStorage.GetOrCreateContainer" />
Expand Down
20 changes: 17 additions & 3 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 @@ -120,8 +121,9 @@ public async Task ToString_ShouldContainStorageInformation()

[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,18 @@ await That(drives).HasSingle().Matching(d
=> string.Equals(d.Name, expectedDriveName, StringComparison.Ordinal));
}

[Fact]
public async Task WithDrive_ShouldInitializeDrivesWithRootDirectory()
{
MockFileSystem sut = new MockFileSystem().WithDrive("d");
List<IFileInfo> files = sut.DriveInfo.GetDrives()
.SelectMany(drive => drive.RootDirectory
.EnumerateFiles("*", SearchOption.AllDirectories))
.ToList();

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

[Theory]
[AutoData]
public async Task WithDrive_WithCallback_ShouldUpdateDrive(long totalSize)
Expand Down
Loading