Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initialize MockFileSystem with drive from current directory #274

Merged
merged 7 commits into from
Mar 5, 2023
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
# Generated files
/Tests/*/Generated

# NCrunch
*.ncrunchproject
*.ncrunchsolution
/.NCrunch_*/StoredText/

############################################################
################## VISUAL STUDIO TEMPLATE ##################
############################################################
Expand Down
2 changes: 1 addition & 1 deletion Source/Testably.Abstractions.Testing/MockFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public MockFileSystem WithSafeFileHandleStrategy(

private void AddDriveFromCurrentDirectory()
{
string? root = Path.GetPathRoot(Directory.GetCurrentDirectory());
string? root = Path.GetPathRoot(System.IO.Directory.GetCurrentDirectory());
if (root != null &&
root[0] != _storage.MainDrive.Name[0])
{
Expand Down
13 changes: 13 additions & 0 deletions Source/Testably.Abstractions.Testing/MockFileSystemExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using Testably.Abstractions.Testing.Helpers;
using Testably.Abstractions.Testing.Storage;

namespace Testably.Abstractions.Testing;
Expand All @@ -8,6 +10,17 @@ namespace Testably.Abstractions.Testing;
/// </summary>
public static class MockFileSystemExtensions
{
/// <summary>
/// Returns the default drive in the <paramref name="mockFileSystem" />.
/// </summary>
public static IDriveInfo GetDefaultDrive(this MockFileSystem mockFileSystem)
{
string driveName = "".PrefixRoot();
return mockFileSystem.DriveInfo
.GetDrives()
.First(d => d.Name.StartsWith(driveName));
}

/// <summary>
/// Changes the parameters of the default drive (e.g. 'C:\' on Windows or '/' on Linux)
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using System.Linq;
using System.Text;
using Testably.Abstractions.Testing.FileSystem;

Expand All @@ -23,7 +22,7 @@ public DriveInfoMockTests()
public void AvailableFreeSpace_CannotGetNegative(long size)
{
FileSystem.WithDrive(d => d.SetTotalSize(size));
IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single();
IDriveInfo drive = FileSystem.GetDefaultDrive();

FileSystem.WithDrive(d => d.ChangeUsedBytes(-1));

Expand All @@ -44,7 +43,7 @@ public void AvailableFreeSpace_NotEnoughSpace_ShouldThrowIOException(
FileSystem.File.WriteAllBytes(path, bytes);
});

IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single();
IDriveInfo drive = FileSystem.GetDefaultDrive();
exception.Should().BeOfType<IOException>()
.Which.Message.Should().Contain($"'{drive.Name}'");
drive.AvailableFreeSpace.Should().Be(fileSize - 1);
Expand All @@ -60,7 +59,7 @@ public void AvailableFreeSpace_ShouldBeChangedWhenAppendingToAFile(
int fileSize2 = encoding.GetBytes(fileContent2).Length;
FileSystem.WithDrive(d
=> d.SetTotalSize(fileSize1 + fileSize2 + expectedRemainingBytes));
IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single();
IDriveInfo drive = FileSystem.GetDefaultDrive();

FileSystem.File.WriteAllText(path, fileContent1, encoding);
drive.AvailableFreeSpace.Should().Be(expectedRemainingBytes + fileSize2);
Expand All @@ -77,7 +76,7 @@ public void AvailableFreeSpace_ShouldBeChangedWhenWorkingWithStreams(
int reduceLength, string path, string previousContent)
{
FileSystem.File.WriteAllText(path, previousContent);
IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single();
IDriveInfo drive = FileSystem.GetDefaultDrive();
long previousFreeSpace = drive.AvailableFreeSpace;

FileSystemStream stream = FileSystem.File.OpenWrite(path);
Expand All @@ -101,7 +100,7 @@ public void AvailableFreeSpace_ShouldBeReducedByWritingToFile(

FileSystem.File.WriteAllBytes(path, bytes);

IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single();
IDriveInfo drive = FileSystem.GetDefaultDrive();

drive.AvailableFreeSpace.Should().Be(0);
}
Expand All @@ -118,7 +117,7 @@ public void AvailableFreeSpace_ShouldBeReleasedWhenDeletingAFile(
FileSystem.File.WriteAllBytes(path, bytes);
FileSystem.File.Delete(path);

IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single();
IDriveInfo drive = FileSystem.GetDefaultDrive();

drive.AvailableFreeSpace.Should().Be(fileSize);
}
Expand All @@ -129,7 +128,7 @@ public void AvailableFreeSpace_ShouldBeSetTotalSize(long size)
{
FileSystem.WithDrive(d => d.SetTotalSize(size));

IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single();
IDriveInfo drive = FileSystem.GetDefaultDrive();

drive.AvailableFreeSpace.Should().Be(size);
}
Expand Down Expand Up @@ -228,7 +227,7 @@ public void SetDriveFormat_Default_ShouldBeNTFS()
{
FileSystem.WithDrive(d => d.SetDriveFormat());

IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single();
IDriveInfo drive = FileSystem.GetDefaultDrive();
drive.DriveFormat.Should().Be("NTFS");
}

Expand All @@ -238,7 +237,7 @@ public void SetDriveFormat_ShouldChangeDriveFormat(string driveFormat)
{
FileSystem.WithDrive(d => d.SetDriveFormat(driveFormat));

IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single();
IDriveInfo drive = FileSystem.GetDefaultDrive();
drive.DriveFormat.Should().Be(driveFormat);
}

Expand All @@ -247,7 +246,7 @@ public void SetDriveType_Default_ShouldBeFixed()
{
FileSystem.WithDrive(d => d.SetDriveType());

IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single();
IDriveInfo drive = FileSystem.GetDefaultDrive();
drive.DriveType.Should().Be(DriveType.Fixed);
}

Expand All @@ -257,7 +256,7 @@ public void SetDriveType_ShouldChangeDriveType(DriveType driveType)
{
FileSystem.WithDrive(d => d.SetDriveType(driveType));

IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single();
IDriveInfo drive = FileSystem.GetDefaultDrive();
drive.DriveType.Should().Be(driveType);
}

Expand All @@ -268,7 +267,7 @@ public void SetIsReady_ShouldChangeIsReady(bool isReady)
{
FileSystem.WithDrive(d => d.SetIsReady(isReady));

IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single();
IDriveInfo drive = FileSystem.GetDefaultDrive();
drive.IsReady.Should().Be(isReady);
}

Expand All @@ -277,7 +276,7 @@ public void SetTotalSize_Default_ShouldBe1Gigabyte()
{
FileSystem.WithDrive(d => d.SetTotalSize());

IDriveInfo drive = FileSystem.DriveInfo.GetDrives().Single();
IDriveInfo drive = FileSystem.GetDefaultDrive();

drive.AvailableFreeSpace.Should().Be(1024 * 1024 * 1024);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,23 @@ public void InitializeIn_MissingDrive_ShouldCreateDrive(string directoryName)
{
Skip.IfNot(Test.RunsOnWindows);

directoryName = Path.Combine("D:\\", directoryName);
MockFileSystem sut = new();
IDriveInfo[] drives = sut.DriveInfo.GetDrives();
for (char c = 'D'; c <= 'Z'; c++)
{
if (drives.Any(d => d.Name.StartsWith($"{c}")))
{
continue;
}

directoryName = Path.Combine($"{c}:\\", directoryName);
break;
}

sut.InitializeIn(directoryName);

sut.Directory.Exists(directoryName).Should().BeTrue();
sut.DriveInfo.GetDrives().Length.Should().Be(drives.Length + 1);
}

[Theory]
Expand Down
29 changes: 23 additions & 6 deletions Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ public void FileSystemMock_FileInfo_Encrypt(string path, string contents)
}

[SkippableFact]
public void FileSystemMock_ShouldBeInitializedWithASingleDefaultDrive()
public void FileSystemMock_ShouldBeInitializedWithADefaultDrive()
{
string expectedDriveName = "".PrefixRoot();
MockFileSystem sut = new();

IDriveInfo[] drives = sut.DriveInfo.GetDrives();
IDriveInfo drive = drives.Single();
IDriveInfo drive = sut.GetDefaultDrive();

drives.Should().NotBeEmpty();
drive.Name.Should().Be(expectedDriveName);
drive.AvailableFreeSpace.Should().BeGreaterThan(0);
drive.DriveFormat.Should()
Expand All @@ -91,6 +92,20 @@ public void FileSystemMock_ShouldBeInitializedWithASingleDefaultDrive()
drive.VolumeLabel.Should().NotBeNullOrEmpty();
}

[SkippableFact]
public void FileSystemMock_ShouldInitializeDriveFromCurrentDirectory()
{
string? driveName = Path.GetPathRoot(Directory.GetCurrentDirectory());

Skip.If(!Test.RunsOnWindows || driveName?.StartsWith("C") != false);

MockFileSystem sut = new();

IDriveInfo[] drives = sut.DriveInfo.GetDrives();
drives.Length.Should().Be(2);
drives.Should().Contain(d => d.Name == driveName);
}

[SkippableTheory]
[AutoData]
public void WithAccessControl_Denied_CreateDirectoryShouldThrowIOException(
Expand Down Expand Up @@ -153,7 +168,7 @@ public void WithDrive_ExistingName_ShouldUpdateDrive()

IDriveInfo[] drives = sut.DriveInfo.GetDrives();

drives.Length.Should().Be(1);
drives.Length.Should().BeGreaterOrEqualTo(1);
drives.Should().ContainSingle(d => d.Name == driveName);
}

Expand Down Expand Up @@ -196,7 +211,7 @@ public void WithDrive_WithCallback_ShouldUpdateDrive(long totalSize)
MockFileSystem sut = new();
sut.WithDrive(d => d.SetTotalSize(totalSize));

IDriveInfo drive = sut.DriveInfo.GetDrives().Single();
IDriveInfo drive = sut.GetDefaultDrive();

drive.TotalSize.Should().Be(totalSize);
drive.TotalFreeSpace.Should().Be(totalSize);
Expand Down Expand Up @@ -225,11 +240,13 @@ public void WithUncDrive_ShouldNotBeIncludedInGetDrives(
MockFileSystem sut = new();
string uncPrefix = new(sut.Path.DirectorySeparatorChar, 2);
string uncDrive = $"{uncPrefix}{server}";
int expectedLogicalDrives = sut.Directory.GetLogicalDrives().Length;
int expectedDrives = sut.DriveInfo.GetDrives().Length;

sut.WithUncDrive(uncDrive);

sut.Directory.GetLogicalDrives().Length.Should().Be(1);
sut.DriveInfo.GetDrives().Length.Should().Be(1);
sut.Directory.GetLogicalDrives().Length.Should().Be(expectedLogicalDrives);
sut.DriveInfo.GetDrives().Length.Should().Be(expectedDrives);
}

[SkippableTheory]
Expand Down