Skip to content

Commit

Permalink
fix: Fixing UNC path GetFullPath when Current Directory is a UNC path (
Browse files Browse the repository at this point in the history
…#887)

Currently, if you switch current directory in the `MockFileSystem` to a UNC path, and try to enumerate files, it blows up.

Attempting to fix it, although it feels like a hack.
  • Loading branch information
michal-ciechan authored Dec 19, 2022
1 parent 8d1de19 commit 48e2b32
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public override string GetFullPath(string path)
// unc paths need at least two segments, the others need one segment
var isUnixRooted = mockFileDataAccessor.StringOperations.StartsWith(
mockFileDataAccessor.Directory.GetCurrentDirectory(),
string.Format(CultureInfo.InvariantCulture, "{0}", DirectorySeparatorChar));
"/");

var minPathSegments = isUnc
? 2
Expand Down Expand Up @@ -121,7 +121,7 @@ public override string GetFullPath(string path)

if (isUnixRooted && !isUnc)
{
fullPath = DirectorySeparatorChar + fullPath;
fullPath = "/" + fullPath;
}
else if (isUnixRooted)
{
Expand Down Expand Up @@ -156,4 +156,4 @@ public override string GetTempFileName()
/// <inheritdoc />
public override string GetTempPath() => defaultTempDirectory;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,28 @@ public void MockDirectoryInfo_GetFiles_ShouldWorkWithUNCPath()
Assert.AreEqual(fileName, files[0].FullName);
}

[Test]
[WindowsOnly(WindowsSpecifics.UNCPaths)]
public void MockDirectoryInfo_GetFiles_ShouldWorkWithUNCPath_WhenCurrentDirectoryIsUnc()
{
var fileName = XFS.Path(@"\\unc\folder\file.txt");
var directoryName = XFS.Path(@"\\unc\folder");
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{fileName, ""}
});

fileSystem.Directory.SetCurrentDirectory(directoryName);

var directoryInfo = new MockDirectoryInfo(fileSystem, directoryName);

// Act
var files = directoryInfo.GetFiles();

// Assert
Assert.AreEqual(fileName, files[0].FullName);
}

[Test]
public void MockDirectoryInfo_FullName_ShouldReturnFullNameWithoutIncludingTrailingPathDelimiter()
Expand Down

0 comments on commit 48e2b32

Please sign in to comment.