Skip to content

Commit

Permalink
fix: initialize filesystem in drive of current directory (#232)
Browse files Browse the repository at this point in the history
Fixes
- #231

---------

Co-authored-by: Valentin Breuß <v.breuss@tig.at>
  • Loading branch information
vbreuss and vbtig authored Feb 6, 2023
1 parent 242e51f commit 215bda6
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ private static string ValidateDriveLetter(string driveName,

if (fileSystem.Path.IsPathRooted(driveName))
{
return fileSystem.Path.GetPathRoot(driveName)!;
return Execute.OnWindows(() =>
{
var rootedPath = fileSystem.Path.GetPathRoot(driveName)!;
return $"{rootedPath.TrimEnd('\\')}\\";
},
() => fileSystem.Path.GetPathRoot(driveName)!);
}

throw ExceptionFactory.InvalidDriveName();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using Testably.Abstractions.Testing.FileSystemInitializer;

namespace Testably.Abstractions.Testing;
Expand All @@ -24,6 +25,13 @@ public static IFileSystemInitializer<TFileSystem> InitializeIn<TFileSystem>(
string basePath)
where TFileSystem : IFileSystem
{
if (Path.IsPathRooted(basePath) &&
fileSystem is MockFileSystem mockFileSystem)
{
string? drive = Path.GetPathRoot(basePath);
mockFileSystem.WithDrive(drive);
}

fileSystem.Directory.CreateDirectory(basePath);
fileSystem.Directory.SetCurrentDirectory(basePath);
return new Initializer<TFileSystem>(fileSystem, ".");
Expand Down
11 changes: 11 additions & 0 deletions Source/Testably.Abstractions.Testing/MockFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public MockFileSystem()
FileSystemWatcher = new FileSystemWatcherFactoryMock(this);
SafeFileHandleStrategy = new NullSafeFileHandleStrategy();
AccessControlStrategy = new NullAccessControlStrategy();
AddDriveFromCurrentDirectory();
}

#region IFileSystem Members
Expand Down Expand Up @@ -150,4 +151,14 @@ public MockFileSystem WithSafeFileHandleStrategy(
SafeFileHandleStrategy = safeFileHandleStrategy;
return this;
}

private void AddDriveFromCurrentDirectory()
{
string? root = Path.GetPathRoot(Directory.GetCurrentDirectory());
if (root != null &&
root[0] != _storage.MainDrive.Name[0])
{
Storage.GetOrAddDrive(root);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Testably.Abstractions.Testing;
public static class MockFileSystemExtensions
{
/// <summary>
/// Changes the parameters of the default drive ('C:\' on Windows, '/' on Linux)
/// Changes the parameters of the default drive (e.g. 'C:\' on Windows or '/' on Linux)
/// </summary>
public static MockFileSystem WithDrive(
this MockFileSystem mockFileSystem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Linq;
using Testably.Abstractions.Testing.FileSystemInitializer;
using Testably.Abstractions.Testing.Tests.TestHelpers;

namespace Testably.Abstractions.Testing.Tests;

Expand Down Expand Up @@ -148,6 +149,19 @@ public void Initialize_WithSubdirectory_ShouldExist(string directoryName)
result.Directory.Exists.Should().BeTrue();
}

[SkippableTheory]
[AutoData]
public void InitializeIn_MissingDrive_ShouldCreateDrive(string directoryName)
{
Skip.IfNot(Test.RunsOnWindows);

directoryName = Path.Combine("D:\\", directoryName);
MockFileSystem sut = new();
sut.InitializeIn(directoryName);

sut.Directory.Exists(directoryName).Should().BeTrue();
}

[Theory]
[AutoData]
public void InitializeIn_ShouldSetCurrentDirectory(string path)
Expand Down
17 changes: 17 additions & 0 deletions Tests/Testably.Abstractions.Testing.Tests/MockFileSystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,23 @@ public void WithDrive_NewName_ShouldCreateNewDrives(string driveName)
drives.Should().ContainSingle(d => d.Name == driveName);
}

[SkippableTheory]
[InlineData("D")]
[InlineData("D:")]
public void WithDrive_ShouldHavePathSeparatorSuffix(string driveName)
{
Skip.IfNot(Test.RunsOnWindows, "Linux does not support different drives.");

string expectedDriveName = $"D:{Path.DirectorySeparatorChar}";
MockFileSystem sut = new();
sut.WithDrive(driveName);

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

drives.Length.Should().BeLessOrEqualTo(2);
drives.Should().ContainSingle(d => d.Name == expectedDriveName);
}

[SkippableTheory]
[AutoData]
public void WithDrive_WithCallback_ShouldUpdateDrive(long totalSize)
Expand Down
10 changes: 10 additions & 0 deletions Tests/Testably.Abstractions.Tests/FileSystem/Path/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public void GetInvalidFileNameChars_ShouldReturnDefaultValue()
result.Should().BeEquivalentTo(System.IO.Path.GetInvalidFileNameChars());
}

[SkippableTheory]
[InlineData("D:")]
public void GetPathRoot_WithoutTrailingSeparatorChar_ShouldReturnDefaultValue(string path)
{
var expectedResult = System.IO.Path.GetPathRoot(path);
string? result = FileSystem.Path.GetPathRoot(path);

result.Should().Be(expectedResult);
}

[SkippableTheory]
[AutoData]
public void GetPathRoot_ShouldReturnDefaultValue(string path)
Expand Down

0 comments on commit 215bda6

Please sign in to comment.