Skip to content

Commit dbe1807

Browse files
committed
Also fix it for UNC drives
1 parent c408bc5 commit dbe1807

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

Source/Testably.Abstractions.Testing/MockFileSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public MockFileSystem WithDrive(string? drive,
223223
Action<IStorageDrive>? driveCallback = null)
224224
{
225225
IStorageDrive driveInfoMock =
226-
drive == null
226+
drive == null || string.Equals(drive, "\\", StringComparison.Ordinal)
227227
? Storage.MainDrive
228228
: Storage.GetOrAddDrive(drive);
229229
driveCallback?.Invoke(driveInfoMock);

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,15 @@ public IEnumerable<IStorageDrive> GetDrives()
351351
drive = _fileSystem.Storage.MainDrive;
352352
}
353353

354-
return InMemoryLocation.New(_fileSystem, drive, path.GetFullPathOrWhiteSpace(_fileSystem),
355-
path);
354+
string fullPath = path;
355+
if (!fullPath.IsUncPath(_fileSystem) ||
356+
!_fileSystem.Execute.IsNetFramework ||
357+
fullPath.LastIndexOf(_fileSystem.Path.DirectorySeparatorChar) > 2)
358+
{
359+
fullPath = path.GetFullPathOrWhiteSpace(_fileSystem);
360+
}
361+
362+
return InMemoryLocation.New(_fileSystem, drive, fullPath, path);
356363
}
357364

358365
/// <inheritdoc cref="IStorage.GetOrAddDrive(string)" />
@@ -367,7 +374,10 @@ public IEnumerable<IStorageDrive> GetDrives()
367374
DriveInfoMock drive = DriveInfoMock.New(driveName, _fileSystem);
368375
return _drives.GetOrAdd(drive.GetName(), _ =>
369376
{
370-
GetOrCreateContainer(GetLocation(drive.GetName()), InMemoryContainer.NewDirectory);
377+
IStorageLocation rootLocation =
378+
InMemoryLocation.New(_fileSystem, drive, drive.GetName());
379+
_containers.TryAdd(rootLocation,
380+
InMemoryContainer.NewDirectory(rootLocation, _fileSystem));
371381
return drive;
372382
});
373383
}

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public async Task ToString_ShouldContainStorageInformation()
116116

117117
string result = sut.ToString();
118118

119-
await That(result).Contains("directories: 0, files: 1");
119+
await That(result).Contains("directories: 1, files: 1");
120120
}
121121

122122
[Theory]
@@ -246,13 +246,13 @@ await That(drives).HasSingle().Matching(d
246246
[Fact]
247247
public async Task WithDrive_ShouldInitializeDrivesWithRootDirectory()
248248
{
249-
MockFileSystem sut = new MockFileSystem().WithDrive("d");
250-
List<IFileInfo> files = sut.DriveInfo.GetDrives()
249+
MockFileSystem sut = new MockFileSystem().WithDrive("V");
250+
List<IFileSystemInfo> fileSystemInfos = sut.DriveInfo.GetDrives()
251251
.SelectMany(drive => drive.RootDirectory
252-
.EnumerateFiles("*", SearchOption.AllDirectories))
252+
.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
253253
.ToList();
254254

255-
await That(files.Count).IsEqualTo(0);
255+
await That(fileSystemInfos).HasCount(0);
256256
}
257257

258258
[Theory]
@@ -319,6 +319,22 @@ public async Task WithUncDrive_ShouldCreateUncDrive(
319319
await That(result).IsEqualTo(contents);
320320
}
321321

322+
[Fact]
323+
public async Task WithUncDrive_ShouldInitializeDrivesWithRootDirectory()
324+
{
325+
MockFileSystem sut = new();
326+
string uncPrefix = new(sut.Path.DirectorySeparatorChar, 2);
327+
string uncDrive = $"{uncPrefix}unc-server";
328+
sut.WithUncDrive(uncDrive);
329+
IDriveInfo drive = sut.DriveInfo.New(uncDrive);
330+
331+
List<IFileSystemInfo> fileSystemInfos = drive.RootDirectory
332+
.EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
333+
.ToList();
334+
335+
await That(fileSystemInfos).HasCount(0);
336+
}
337+
322338
[Theory]
323339
[AutoData]
324340
public async Task WithUncDrive_ShouldNotBeIncludedInGetDrives(

Tests/Testably.Abstractions.Testing.Tests/Storage/InMemoryContainerTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ public async Task ToString_File_ShouldIncludePathAndFileSize(byte[] bytes)
303303
MockFileSystem fileSystem = new();
304304
string expectedPath = fileSystem.Path.GetFullPath("foo.txt");
305305
fileSystem.File.WriteAllBytes(expectedPath, bytes);
306-
IStorageContainer sut = fileSystem.StorageContainers.Single();
306+
IStorageContainer sut = fileSystem.StorageContainers
307+
.Single(x => x.Type == FileSystemTypes.File);
307308

308309
string? result = sut.ToString();
309310

0 commit comments

Comments
 (0)