Skip to content

Commit 48e2b32

Browse files
fix: Fixing UNC path GetFullPath when Current Directory is a UNC path (#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.
1 parent 8d1de19 commit 48e2b32

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/TestableIO.System.IO.Abstractions.TestingHelpers/MockPath.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public override string GetFullPath(string path)
8585
// unc paths need at least two segments, the others need one segment
8686
var isUnixRooted = mockFileDataAccessor.StringOperations.StartsWith(
8787
mockFileDataAccessor.Directory.GetCurrentDirectory(),
88-
string.Format(CultureInfo.InvariantCulture, "{0}", DirectorySeparatorChar));
88+
"/");
8989

9090
var minPathSegments = isUnc
9191
? 2
@@ -121,7 +121,7 @@ public override string GetFullPath(string path)
121121

122122
if (isUnixRooted && !isUnc)
123123
{
124-
fullPath = DirectorySeparatorChar + fullPath;
124+
fullPath = "/" + fullPath;
125125
}
126126
else if (isUnixRooted)
127127
{
@@ -156,4 +156,4 @@ public override string GetTempFileName()
156156
/// <inheritdoc />
157157
public override string GetTempPath() => defaultTempDirectory;
158158
}
159-
}
159+
}

tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,28 @@ public void MockDirectoryInfo_GetFiles_ShouldWorkWithUNCPath()
9696
Assert.AreEqual(fileName, files[0].FullName);
9797
}
9898

99+
[Test]
100+
[WindowsOnly(WindowsSpecifics.UNCPaths)]
101+
public void MockDirectoryInfo_GetFiles_ShouldWorkWithUNCPath_WhenCurrentDirectoryIsUnc()
102+
{
103+
var fileName = XFS.Path(@"\\unc\folder\file.txt");
104+
var directoryName = XFS.Path(@"\\unc\folder");
105+
// Arrange
106+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
107+
{
108+
{fileName, ""}
109+
});
110+
111+
fileSystem.Directory.SetCurrentDirectory(directoryName);
112+
113+
var directoryInfo = new MockDirectoryInfo(fileSystem, directoryName);
114+
115+
// Act
116+
var files = directoryInfo.GetFiles();
99117

118+
// Assert
119+
Assert.AreEqual(fileName, files[0].FullName);
120+
}
100121

101122
[Test]
102123
public void MockDirectoryInfo_FullName_ShouldReturnFullNameWithoutIncludingTrailingPathDelimiter()

0 commit comments

Comments
 (0)