Skip to content

Commit 4137777

Browse files
committed
Fix MockPath to deal with Nix paths
1 parent d64f35f commit 4137777

2 files changed

Lines changed: 102 additions & 42 deletions

File tree

TestHelpers.Tests/MockPathTests.cs

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using NUnit.Framework;
2+
using System.Collections.Generic;
23

34
namespace System.IO.Abstractions.TestingHelpers.Tests
45
{
56
public class MockPathTests
67
{
7-
const string TestPath = "C:\\test\\test.bmp";
8+
static readonly string TestPath = XFS
9+
.ForWin("C:\\test\\test.bmp")
10+
.ForUnix("/test/test.bmp");
811

912
private MockPath SetupMockPath()
1013
{
@@ -21,7 +24,10 @@ public void ChangeExtension_ExtensionNoPeriod_PeriodAdded()
2124
var result = mockPath.ChangeExtension(TestPath, "doc");
2225

2326
//Assert
24-
Assert.AreEqual("C:\\test\\test.doc", result);
27+
Assert.AreEqual(XFS
28+
.ForWin("C:\\test\\test.doc")
29+
.ForUnix("/test/test.doc")
30+
, result);
2531
}
2632

2733
[Test]
@@ -31,10 +37,16 @@ public void Combine_SentTwoPaths_Combines()
3137
var mockPath = new MockPath(new MockFileSystem());
3238

3339
//Act
34-
var result = mockPath.Combine("C:\\test", "test.bmp");
40+
var result = mockPath.Combine(XFS
41+
.ForWin("C:\\test")
42+
.ForUnix("/test")
43+
, "test.bmp");
3544

3645
//Assert
37-
Assert.AreEqual("C:\\test\\test.bmp", result);
46+
Assert.AreEqual(XFS
47+
.ForWin("C:\\test\\test.bmp")
48+
.ForUnix("/test/test.bmp")
49+
, result);
3850
}
3951

4052
[Test]
@@ -47,7 +59,10 @@ public void GetDirectoryName_SentPath_ReturnsDirectory()
4759
var result = mockPath.GetDirectoryName(TestPath);
4860

4961
//Assert
50-
Assert.AreEqual("C:\\test", result);
62+
Assert.AreEqual(XFS
63+
.ForWin("C:\\test")
64+
.ForUnix("/test")
65+
, result);
5166
}
5267

5368
[Test]
@@ -102,14 +117,21 @@ public void GetFullPath_SendInPath_ReturnsFullPath()
102117
Assert.AreEqual(TestPath, result);
103118
}
104119

105-
[TestCase(@"c:\a", @"b", @"c:\a\b")]
106-
[TestCase(@"c:\a\b", @"c", @"c:\a\b\c")]
107-
[TestCase(@"c:\a\b", @"c\", @"c:\a\b\c\")]
108-
[TestCase(@"c:\a\b", @".\c\", @"c:\a\b\c\")]
109-
[TestCase(@"c:\a\b", @"..\c", @"c:\a\c")]
110-
[TestCase(@"c:\a\b\c", @"..\c\..\", @"c:\a\b\")]
111-
[TestCase(@"c:\a\b\c", @"..\..\..\..\..\d", @"c:\d")]
112-
[TestCase(@"c:\a\b\c\", @"..\..\..\..\..\d\", @"c:\d\")]
120+
public static IEnumerable<string[]> GetFullPath_RelativePaths_Cases
121+
{
122+
get
123+
{
124+
yield return new [] { XFS.ForWin(@"c:\a").ForUnix("/a"), "b", XFS.ForWin(@"c:\a\b").ForUnix("/a/b") };
125+
yield return new [] { XFS.ForWin(@"c:\a\b").ForUnix("/a/b"), "c", XFS.ForWin(@"c:\a\b\c").ForUnix("/a/b/c") };
126+
yield return new [] { XFS.ForWin(@"c:\a\b").ForUnix("/a/b"), XFS.ForWin(@"c\").ForUnix("c/"), XFS.ForWin(@"c:\a\b\c\").ForUnix("/a/b/c/") };
127+
yield return new [] { XFS.ForWin(@"c:\a\b").ForUnix("/a/b"), XFS.ForWin(@"..\c").ForUnix("../c"), XFS.ForWin(@"c:\a\c").ForUnix("/a/c") };
128+
yield return new [] { XFS.ForWin(@"c:\a\b\c").ForUnix("/a/b/c"), XFS.ForWin(@"..\c\..\").ForUnix("../c/../"), XFS.ForWin(@"c:\a\b\").ForUnix("/a/b/") };
129+
yield return new [] { XFS.ForWin(@"c:\a\b\c").ForUnix("/a/b/c"), XFS.ForWin(@"..\..\..\..\..\d").ForUnix("../../../../../d"), XFS.ForWin(@"c:\d").ForUnix("/d") };
130+
yield return new [] { XFS.ForWin(@"c:\a\b\c").ForUnix("/a/b/c"), XFS.ForWin(@"..\..\..\..\..\d\").ForUnix("../../../../../d/"), XFS.ForWin(@"c:\d\").ForUnix("/d/") };
131+
}
132+
}
133+
134+
[TestCaseSource("GetFullPath_RelativePaths_Cases")]
113135
public void GetFullPath_RelativePaths_ShouldReturnTheAbsolutePathWithCurrentDirectory(string currentDir, string relativePath, string expectedResult)
114136
{
115137
//Arrange
@@ -124,11 +146,19 @@ public void GetFullPath_RelativePaths_ShouldReturnTheAbsolutePathWithCurrentDire
124146
Assert.AreEqual(expectedResult, actualResult);
125147
}
126148

127-
[TestCase(@"c:\a\b\..\c", @"c:\a\c")]
128-
[TestCase(@"c:\a\b\.\.\..\.\c", @"c:\a\c")]
129-
[TestCase(@"c:\a\b\.\c", @"c:\a\b\c")]
130-
[TestCase(@"c:\a\b\.\.\.\.\c", @"c:\a\b\c")]
131-
[TestCase(@"c:\a\..\..\c", @"c:\c")]
149+
public static IEnumerable<string[]> GetFullPath_RootedPathWithRelativeSegments_Cases
150+
{
151+
get
152+
{
153+
yield return new [] { XFS.ForWin(@"c:\a\b\..\c").ForUnix("/a/b/../c"), XFS.ForWin(@"c:\a\c").ForUnix("/a/c") };
154+
yield return new [] { XFS.ForWin(@"c:\a\b\.\.\..\.\c").ForUnix("/a/b/././.././c"), XFS.ForWin(@"c:\a\c").ForUnix("/a/c") };
155+
yield return new [] { XFS.ForWin(@"c:\a\b\.\c").ForUnix("/a/b/./c"), XFS.ForWin(@"c:\a\b\c").ForUnix("/a/b/c") };
156+
yield return new [] { XFS.ForWin(@"c:\a\b\.\.\.\.\c").ForUnix("/a/b/././././c"), XFS.ForWin(@"c:\a\b\c").ForUnix("/a/b/c") };
157+
yield return new [] { XFS.ForWin(@"c:\a\..\..\c").ForUnix("/a/../../c"), XFS.ForWin(@"c:\c").ForUnix("/c") };
158+
}
159+
}
160+
161+
[TestCaseSource("GetFullPath_RootedPathWithRelativeSegments_Cases")]
132162
public void GetFullPath_RootedPathWithRelativeSegments_ShouldReturnAnRootedAbsolutePath(string rootedPath, string expectedResult)
133163
{
134164
//Arrange
@@ -142,15 +172,22 @@ public void GetFullPath_RootedPathWithRelativeSegments_ShouldReturnAnRootedAbsol
142172
Assert.AreEqual(expectedResult, actualResult);
143173
}
144174

145-
[TestCase(@"c:\a", @"/b", @"c:\b")]
146-
[TestCase(@"c:\a", @"/b\", @"c:\b\")]
147-
[TestCase(@"c:\a", @"\b", @"c:\b")]
148-
[TestCase(@"c:\a", @"\b\..\c", @"c:\c")]
149-
[TestCase(@"z:\a", @"\b\..\c", @"z:\c")]
150-
[TestCase(@"z:\a", @"\b\..\c", @"z:\c")]
151-
[TestCase(@"z:\a", @"\\computer\share\c", @"\\computer\share\c")]
152-
[TestCase(@"z:\a", @"\\computer\share\c\..\d", @"\\computer\share\d")]
153-
[TestCase(@"z:\a", @"\\computer\share\c\..\..\d", @"\\computer\share\d")]
175+
public static IEnumerable<string[]> GetFullPath_AbsolutePaths_Cases
176+
{
177+
get
178+
{
179+
yield return new [] { XFS.ForWin(@"c:\a").ForUnix("/a"), XFS.ForWin(@"/b").ForUnix(@"/b"), XFS.ForWin(@"c:\b").ForUnix("/b") };
180+
yield return new [] { XFS.ForWin(@"c:\a").ForUnix("/a"), XFS.ForWin(@"/b\").ForUnix(@"/b/"), XFS.ForWin(@"c:\b\").ForUnix("/b/") };
181+
yield return new [] { XFS.ForWin(@"c:\a").ForUnix("/a"), XFS.ForWin(@"\b").ForUnix(@"/b"), XFS.ForWin(@"c:\b").ForUnix("/b") };
182+
yield return new [] { XFS.ForWin(@"c:\a").ForUnix("/a"), XFS.ForWin(@"\b\..c").ForUnix(@"/b/../c"), XFS.ForWin(@"c:\c").ForUnix("/c") };
183+
yield return new [] { XFS.ForWin(@"z:\a").ForUnix("/a"), XFS.ForWin(@"\b\..c").ForUnix(@"/b/../c"), XFS.ForWin(@"z:\c").ForUnix("/c") };
184+
yield return new [] { XFS.ForWin(@"z:\a").ForUnix("/a"), XFS.ForWin(@"\\computer\share\c").ForUnix(@"//computer/share/c"), XFS.ForWin(@"\\computer\share\c").ForUnix("//computer/share/c") };
185+
yield return new [] { XFS.ForWin(@"z:\a").ForUnix("/a"), XFS.ForWin(@"\\computer\share\c\..\d").ForUnix(@"//computer/share/c/../d"), XFS.ForWin(@"\\computer\share\d").ForUnix("//computer/share/d") };
186+
yield return new [] { XFS.ForWin(@"z:\a").ForUnix("/a"), XFS.ForWin(@"\\computer\share\c\..\..\d").ForUnix(@"//computer/share/c/../../d"), XFS.ForWin(@"\\computer\share\d").ForUnix("//computer/share/d") };
187+
}
188+
}
189+
190+
[TestCaseSource("GetFullPath_AbsolutePaths_Cases")]
154191
public void GetFullPath_AbsolutePaths_ShouldReturnThePathWithTheRoot_Or_Unc(string currentDir, string absolutePath, string expectedResult)
155192
{
156193
//Arrange
@@ -173,7 +210,7 @@ public void GetFullPath_InvalidUNCPaths_ShouldThrowArgumentException()
173210
var mockPath = new MockPath(mockFileSystem);
174211

175212
//Act
176-
TestDelegate action = () => mockPath.GetFullPath(@"\\shareZ");
213+
TestDelegate action = () => mockPath.GetFullPath(XFS.ForWin(@"\\shareZ").ForUnix(@"//shareZ"));
177214

178215
//Assert
179216
Assert.Throws<ArgumentException>(action);
@@ -243,7 +280,10 @@ public void GetPathRoot_SendInPath_ReturnsRoot()
243280
var result = mockPath.GetPathRoot(TestPath);
244281

245282
//Assert
246-
Assert.AreEqual("C:\\", result);
283+
Assert.AreEqual(XFS
284+
.ForWin("C:\\")
285+
.ForUnix("/")
286+
, result);
247287
}
248288

249289
[Test]

TestingHelpers/MockPath.cs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,41 +32,49 @@ public override string GetFullPath(string path)
3232

3333
path = path.Replace(AltDirectorySeparatorChar, DirectorySeparatorChar);
3434

35+
bool isUnc =
36+
path.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase) ||
37+
path.StartsWith(@"//", StringComparison.OrdinalIgnoreCase);
38+
3539
string root = GetPathRoot(path);
3640

3741
bool hasTrailingSlash = path[path.Length - 1] == DirectorySeparatorChar;
3842
string[] pathSegments;
39-
bool isUnc = false;
4043

4144
if (root.Length == 0)
4245
{
4346
// relative path on the current drive or volume
4447
path = mockFileDataAccessor.Directory.GetCurrentDirectory() + DirectorySeparatorChar + path;
4548
pathSegments = GetSegments(path);
4649
}
47-
else if (@"\".Equals(root, StringComparison.OrdinalIgnoreCase) || @"/".Equals(root, StringComparison.OrdinalIgnoreCase))
48-
{
49-
// absolute path on the current drive or volume
50-
pathSegments = GetSegments(GetPathRoot(mockFileDataAccessor.Directory.GetCurrentDirectory()), path);
51-
}
52-
else if (root.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase))
50+
else if (isUnc)
5351
{
5452
// unc path
5553
pathSegments = GetSegments(path);
5654
if (pathSegments.Length < 2)
5755
{
5856
throw new ArgumentException(@"The UNC path should be of the form \\server\share.", "path");
5957
}
60-
61-
isUnc = true;
6258
}
63-
else
59+
else if (@"\".Equals(root, StringComparison.OrdinalIgnoreCase) || @"/".Equals(root, StringComparison.OrdinalIgnoreCase))
60+
{
61+
// absolute path on the current drive or volume
62+
pathSegments = GetSegments(GetPathRoot(mockFileDataAccessor.Directory.GetCurrentDirectory()), path);
63+
}
64+
else
6465
{
6566
pathSegments = GetSegments(path);
6667
}
6768

6869
// unc paths need at least two segments, the others need one segment
69-
var minPathSegments = isUnc ? 2 : 1;
70+
bool isUnixRooted =
71+
mockFileDataAccessor.Directory.GetCurrentDirectory()
72+
.StartsWith(DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture));
73+
74+
var minPathSegments = isUnc
75+
? 2
76+
: isUnixRooted ? 0 : 1;
77+
7078
var stack = new Stack<string>();
7179
foreach (var segment in pathSegments)
7280
{
@@ -88,14 +96,26 @@ public override string GetFullPath(string path)
8896
}
8997
}
9098

91-
var fullPath = isUnc ? @"\\" : string.Empty;
92-
fullPath += string.Join(DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture), stack.Reverse().ToArray());
99+
var fullPath = string.Join(DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture), stack.Reverse().ToArray());
93100

94101
if (hasTrailingSlash)
95102
{
96103
fullPath += DirectorySeparatorChar;
97104
}
98105

106+
if (isUnixRooted && !isUnc)
107+
{
108+
fullPath = DirectorySeparatorChar + fullPath;
109+
}
110+
else if (isUnixRooted && isUnc)
111+
{
112+
fullPath = @"//" + fullPath;
113+
}
114+
else if (!isUnixRooted && isUnc)
115+
{
116+
fullPath = @"\\" + fullPath;
117+
}
118+
99119
return fullPath;
100120
}
101121

0 commit comments

Comments
 (0)