Skip to content

Commit e579d29

Browse files
DeveloperGuofgreinacher
authored andcommitted
Fix MockDirectoryInfo GetFiles for UNC paths caused by bug in StringExtensions.NormalizeSlashes (#421) (#422)
Fixes #421
1 parent 4f397f8 commit e579d29

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ public void MockDirectoryInfo_Exists(string path, bool expected)
5555
Assert.That(result, Is.EqualTo(expected));
5656
}
5757

58+
[Test]
59+
[WindowsOnly(WindowsSpecifics.UNCPaths)]
60+
public void MockDirectoryInfo_GetFiles_ShouldWorkWithUNCPath()
61+
{
62+
var fileName = XFS.Path(@"\\unc\folder\file.txt");
63+
var directoryName = XFS.Path(@"\\unc\folder");
64+
// Arrange
65+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
66+
{
67+
{fileName, ""}
68+
});
69+
70+
var directoryInfo = new MockDirectoryInfo(fileSystem, directoryName);
71+
72+
// Act
73+
var files = directoryInfo.GetFiles();
74+
75+
// Assert
76+
Assert.AreEqual(fileName, files[0].FullName);
77+
}
78+
79+
80+
5881
[Test]
5982
public void MockDirectoryInfo_FullName_ShouldReturnFullNameWithoutIncludingTrailingPathDelimiter()
6083
{

System.IO.Abstractions.TestingHelpers.Tests/StringExtensionsTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,13 @@ public void TrimSlashes_SlashRoot_PreserveSlashRoot()
112112
{
113113
Assert.AreEqual("/", "/".TrimSlashes());
114114
}
115+
116+
[TestCase(@"\\unc\folder\file.txt", @"\\unc\folder\file.txt")]
117+
[TestCase(@"//unc/folder/file.txt", @"\\unc\folder\file.txt")]
118+
[WindowsOnly(WindowsSpecifics.UNCPaths)]
119+
public void NormalizeSlashes_KeepsUNCPathPrefix(string path, string expectedValue)
120+
{
121+
Assert.AreEqual(expectedValue, path.NormalizeSlashes());
122+
}
115123
}
116124
}

System.IO.Abstractions.TestingHelpers/StringExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static string NormalizeSlashes(this string path)
9999

100100
// UNC Paths start with double slash but no reason
101101
// to have more than 2 slashes at the start of a path
102-
if (XFS.IsWindowsPlatform() && prefixSeps.Length > 2)
102+
if (XFS.IsWindowsPlatform() && prefixSeps.Length >= 2)
103103
{
104104
prefixSeps = prefixSeps.Substring(0, 2);
105105
}

0 commit comments

Comments
 (0)