Skip to content

Commit 8c56d85

Browse files
committed
fix: initialize drives with root directory
1 parent 78b0421 commit 8c56d85

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public InMemoryStorage(MockFileSystem fileSystem)
3636
CurrentDirectory = string.Empty.PrefixRoot(_fileSystem);
3737
DriveInfoMock mainDrive = DriveInfoMock.New(CurrentDirectory, _fileSystem);
3838
_drives.TryAdd(mainDrive.GetName(), mainDrive);
39+
IStorageLocation rootLocation =
40+
InMemoryLocation.New(_fileSystem, mainDrive, mainDrive.GetName());
41+
_containers.TryAdd(rootLocation, InMemoryContainer.NewDirectory(rootLocation, _fileSystem));
42+
3943
MainDrive = mainDrive;
4044
}
4145

@@ -189,7 +193,7 @@ public IEnumerable<IStorageLocation> EnumerateLocations(
189193
EnumerationOptions? enumerationOptions = null)
190194
{
191195
ValidateExpression(searchPattern);
192-
if (!_containers.TryGetValue(location, out var parentContainer))
196+
if (!_containers.TryGetValue(location, out IStorageContainer? parentContainer))
193197
{
194198
throw ExceptionFactory.DirectoryNotFound(location.FullPath);
195199
}
@@ -227,11 +231,11 @@ public IEnumerable<IStorageLocation> EnumerateLocations(
227231
}
228232

229233
if (enumerationOptions.ReturnSpecialDirectories &&
230-
type == FileSystemTypes.Directory)
234+
type == FileSystemTypes.Directory)
231235
{
232236
IStorageDrive? drive = _fileSystem.Storage.GetDrive(fullPath);
233237
if (drive == null &&
234-
!fullPath.IsUncPath(_fileSystem))
238+
!fullPath.IsUncPath(_fileSystem))
235239
{
236240
drive = _fileSystem.Storage.MainDrive;
237241
}
@@ -255,11 +259,12 @@ public IEnumerable<IStorageLocation> EnumerateLocations(
255259

256260
foreach (KeyValuePair<IStorageLocation, IStorageContainer> item in _containers
257261
.Where(x => x.Key.FullPath.StartsWith(fullPath,
258-
_fileSystem.Execute.StringComparisonMode) &&
259-
!x.Key.Equals(location)))
262+
_fileSystem.Execute.StringComparisonMode) &&
263+
!x.Key.Equals(location)))
260264
{
261265
if (type.HasFlag(item.Value.Type) &&
262-
IncludeItemInEnumeration(item, fullPathWithoutTrailingSlash, enumerationOptions))
266+
IncludeItemInEnumeration(item, fullPathWithoutTrailingSlash,
267+
enumerationOptions))
263268
{
264269
string? itemPath = item.Key.FullPath;
265270
if (itemPath.EndsWith(_fileSystem.Path.DirectorySeparatorChar))
@@ -269,14 +274,14 @@ public IEnumerable<IStorageLocation> EnumerateLocations(
269274

270275
string name = _fileSystem.Execute.Path.GetFileName(itemPath);
271276
if (EnumerationOptionsHelper.MatchesPattern(
272-
_fileSystem.Execute,
273-
enumerationOptions,
274-
name,
275-
searchPattern) ||
276-
(_fileSystem.Execute.IsNetFramework &&
277-
SearchPatternMatchesFileExtensionOnNetFramework(
278-
searchPattern,
279-
_fileSystem.Execute.Path.GetExtension(name))))
277+
_fileSystem.Execute,
278+
enumerationOptions,
279+
name,
280+
searchPattern) ||
281+
(_fileSystem.Execute.IsNetFramework &&
282+
SearchPatternMatchesFileExtensionOnNetFramework(
283+
searchPattern,
284+
_fileSystem.Execute.Path.GetExtension(name))))
280285
{
281286
yield return item.Key;
282287
}
@@ -360,7 +365,11 @@ public IEnumerable<IStorageDrive> GetDrives()
360365
}
361366

362367
DriveInfoMock drive = DriveInfoMock.New(driveName, _fileSystem);
363-
return _drives.GetOrAdd(drive.GetName(), _ => drive);
368+
return _drives.GetOrAdd(drive.GetName(), _ =>
369+
{
370+
GetOrCreateContainer(GetLocation(drive.GetName()), InMemoryContainer.NewDirectory);
371+
return drive;
372+
});
364373
}
365374

366375
/// <inheritdoc cref="IStorage.GetOrCreateContainer" />

Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System.Collections.Generic;
2+
using System.IO;
23
using System.Linq;
34
using Testably.Abstractions.Helpers;
45
using Testably.Abstractions.Testing.FileSystem;
@@ -120,8 +121,9 @@ public async Task ToString_ShouldContainStorageInformation()
120121

121122
[Theory]
122123
[AutoData]
123-
public async Task WithAccessControl_Denied_CreateDirectoryShouldThrowUnauthorizedAccessException(
124-
string path)
124+
public async Task
125+
WithAccessControl_Denied_CreateDirectoryShouldThrowUnauthorizedAccessException(
126+
string path)
125127
{
126128
Skip.If(!Test.RunsOnWindows);
127129

@@ -241,6 +243,18 @@ await That(drives).HasSingle().Matching(d
241243
=> string.Equals(d.Name, expectedDriveName, StringComparison.Ordinal));
242244
}
243245

246+
[Fact]
247+
public async Task WithDrive_ShouldInitializeDrivesWithRootDirectory()
248+
{
249+
MockFileSystem sut = new MockFileSystem().WithDrive("d");
250+
List<IFileInfo> files = sut.DriveInfo.GetDrives()
251+
.SelectMany(drive => drive.RootDirectory
252+
.EnumerateFiles("*", SearchOption.AllDirectories))
253+
.ToList();
254+
255+
await That(files.Count).IsEqualTo(0);
256+
}
257+
244258
[Theory]
245259
[AutoData]
246260
public async Task WithDrive_WithCallback_ShouldUpdateDrive(long totalSize)

0 commit comments

Comments
 (0)