Skip to content

Commit 1d02e4d

Browse files
committed
Fix FileSystem AddDirectory separator issue
1 parent 5ec8dad commit 1d02e4d

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

TestingHelpers/CrossPlatformExtensions.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,27 @@ public static class XFS
77
{
88
public static string Path(string path, Func<bool> isUnixF = null)
99
{
10-
if (isUnixF == null)
11-
{
12-
isUnixF = () =>
13-
{
14-
int p = (int)Environment.OSVersion.Platform;
15-
return (p == 4) || (p == 6) || (p == 128);
16-
};
17-
}
18-
19-
if (isUnixF())
10+
var isUnix = isUnixF ?? IsUnixPlatform;
11+
12+
if (isUnix())
2013
{
2114
path = Regex.Replace(path, @"^[a-zA-Z]:(?<path>.*)$", "${path}");
2215
path = path.Replace(@"\", "/");
2316
}
2417

2518
return path;
2619
}
20+
21+
public static string Separator(Func<bool> isUnixF = null)
22+
{
23+
var isUnix = isUnixF ?? IsUnixPlatform;
24+
return isUnix() ? "/" : @"\";
25+
}
26+
27+
public static bool IsUnixPlatform()
28+
{
29+
int p = (int)Environment.OSVersion.Platform;
30+
return (p == 4) || (p == 6) || (p == 128);
31+
}
2732
}
2833
}

TestingHelpers/MockFileSystem.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public void AddFile(string path, MockFileData mockFile)
9595
public void AddDirectory(string path)
9696
{
9797
var fixedPath = FixPath(path);
98+
var separator = XFS.Separator();
9899

99100
lock (files)
100101
{
@@ -103,7 +104,7 @@ public void AddDirectory(string path)
103104
throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, "Access to the path '{0}' is denied.", path));
104105

105106
var lastIndex = 0;
106-
while ((lastIndex = path.IndexOf('\\', lastIndex + 1)) > -1)
107+
while ((lastIndex = path.IndexOf(separator, lastIndex + 1)) > -1)
107108
{
108109
var segment = path.Substring(0, lastIndex + 1);
109110
if (!directory.Exists(segment))
@@ -112,7 +113,7 @@ public void AddDirectory(string path)
112113
}
113114
}
114115

115-
var s = path.EndsWith("\\", StringComparison.OrdinalIgnoreCase) ? path : path + "\\";
116+
var s = path.EndsWith(separator, StringComparison.OrdinalIgnoreCase) ? path : path + separator;
116117
files[s] = new MockDirectoryData();
117118
}
118119
}

0 commit comments

Comments
 (0)