Skip to content

Commit eb8ce5b

Browse files
committed
SemanticVersion fixed
1 parent 5678b2c commit eb8ce5b

File tree

5 files changed

+221
-17
lines changed

5 files changed

+221
-17
lines changed

Core.Common.Standard.Tests/PathHelperTests.cs

Lines changed: 138 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,145 @@ public class PathHelperTests
99
[TestMethod]
1010
public void TestCombineWithRelativePathAndDuplicateDirectoryName()
1111
{
12-
PathHelper pathHelper = new PathHelper();
12+
PathHelper pathHelper = new();
1313
string result = pathHelper.Combine(@"C:\One\Two\Three\Two", "../Four");
1414
Assert.AreEqual(@"C:\One\Two\Three\Four", result);
1515
}
16-
16+
17+
[TestMethod]
18+
public void TestRelativeToWithTwoRelativePaths()
19+
{
20+
PathHelper pathHelper = new();
21+
string result = pathHelper.RelativeTo("./", "./one");
22+
Assert.AreEqual(@"..", result);
23+
}
24+
25+
[TestMethod]
26+
public void TestRelativeToWithTwoRelativePaths1()
27+
{
28+
PathHelper pathHelper = new();
29+
string result = pathHelper.RelativeTo("./", "./one/two");
30+
Assert.AreEqual(@"..\..", result);
31+
}
32+
33+
[TestMethod]
34+
public void TestRelativeToWithTwoRelativePaths2()
35+
{
36+
PathHelper pathHelper = new();
37+
string result = pathHelper.RelativeTo("./one", "./");
38+
Assert.AreEqual(@"one", result);
39+
}
40+
41+
[TestMethod]
42+
public void TestRelativeToWithTwoRelativePaths3()
43+
{
44+
PathHelper pathHelper = new();
45+
string result = pathHelper.RelativeTo("./one/two", "./");
46+
Assert.AreEqual(@"one\two", result);
47+
}
48+
49+
[TestMethod]
50+
public void TestRelativeToWithTwoRelativePaths4()
51+
{
52+
PathHelper pathHelper = new();
53+
string result = pathHelper.RelativeTo("./one", "./two");
54+
Assert.AreEqual(@"..\one", result);
55+
}
56+
57+
[TestMethod]
58+
public void TestRelativeToWithTwoRelativePaths5()
59+
{
60+
PathHelper pathHelper = new();
61+
string result = pathHelper.RelativeTo("./relative/one", "./relative/two");
62+
Assert.AreEqual(@"..\one", result);
63+
}
64+
65+
[TestMethod]
66+
public void TestRelativeToWithTwoRelativePaths6()
67+
{
68+
PathHelper pathHelper = new();
69+
string result = pathHelper.RelativeTo("./relativeA/one", "./relativeB/two");
70+
Assert.AreEqual(@"..\..\relativeA\one", result);
71+
}
72+
73+
[TestMethod]
74+
public void TestRelativeToWithRelativeAndAbsolutePath()
75+
{
76+
PathHelper pathHelper = new(@"C:\some\Absolute\path");
77+
string result = pathHelper.RelativeTo("./relative", @"C:\some\Absolute\path");
78+
Assert.AreEqual(@"relative", result);
79+
}
80+
81+
[TestMethod]
82+
public void TestRelativeToWithAbsoluteAndRelativePath()
83+
{
84+
PathHelper pathHelper = new(@"C:\some\Absolute\path");
85+
string result = pathHelper.RelativeTo(@"C:\some\Absolute\path", "./relative");
86+
Assert.AreEqual(@"..", result);
87+
}
88+
89+
[TestMethod]
90+
public void TestRelativeToWithAbsoluteAndRelativePath2()
91+
{
92+
PathHelper pathHelper = new(@"C:\some\Absolute\path");
93+
string result = pathHelper.RelativeTo(@"C:\some\other\path", "./relative");
94+
Assert.AreEqual(@"..\..\..\other\path", result);
95+
}
96+
97+
[TestMethod]
98+
public void TestRelativeToWithRelativeParentAndAbsolutePath()
99+
{
100+
PathHelper pathHelper = new(@"C:\some\Absolute\path");
101+
string result = pathHelper.RelativeTo("../relative", @"C:\some\Absolute\path");
102+
Assert.AreEqual(@"..\relative", result);
103+
}
104+
105+
[TestMethod]
106+
public void TestRelativeToWithRelativeParentAndAbsolutePath2()
107+
{
108+
PathHelper pathHelper = new(@"C:\some\Absolute\path");
109+
string result = pathHelper.RelativeTo("../relative", @"C:\some\other\path");
110+
Assert.AreEqual(@"..\..\Absolute\relative", result);
111+
}
112+
113+
[TestMethod]
114+
public void TestRelativeToWithAbsoluteAndRelativeParentPath()
115+
{
116+
PathHelper pathHelper = new(@"C:\some\Absolute\path");
117+
string result = pathHelper.RelativeTo(@"C:\some\Absolute\path", "../relative");
118+
Assert.AreEqual(@"..\path", result);
119+
}
120+
121+
[TestMethod]
122+
public void TestRelativeToWithAbsoluteAndRelativeParentPath2()
123+
{
124+
PathHelper pathHelper = new(@"C:\some\Absolute\path");
125+
string result = pathHelper.RelativeTo(@"C:\some\other\path", "../relative");
126+
Assert.AreEqual(@"..\..\other\path", result);
127+
}
128+
129+
[TestMethod]
130+
public void TestRelativeToWithTwoAbsolutePaths()
131+
{
132+
PathHelper pathHelper = new();
133+
string result = pathHelper.RelativeTo(@"C:\some\Absolute\path\one", @"C:\some\Absolute\path\two");
134+
Assert.AreEqual(@"..\one", result);
135+
}
136+
137+
[TestMethod]
138+
public void TestRelativeToWithTwoAbsolutePaths2()
139+
{
140+
PathHelper pathHelper = new();
141+
string result = pathHelper.RelativeTo(@"C:\some\Absolute\path\one", @"C:\some\Absolute\path\two\three");
142+
Assert.AreEqual(@"..\..\one", result);
143+
}
144+
145+
[TestMethod]
146+
public void TestRelativeToWithTwoAbsolutePaths3()
147+
{
148+
PathHelper pathHelper = new();
149+
string result = pathHelper.RelativeTo(@"C:\some\Absolute\path\one\three", @"C:\some\Absolute\path\two");
150+
Assert.AreEqual(@"..\one\three", result);
151+
}
17152
}
18-
}
153+
}

Core.Common.Standard/DataAccess/PathHelper.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public PathHelper(string root = null)
2929

3030
public bool IsAbsolute(string path)
3131
{
32-
return absolutePathRegex.IsMatch(path);
32+
return !string.IsNullOrEmpty(path) && absolutePathRegex.IsMatch(path);
3333
}
3434

3535
public string ToAbsolute(params string[] pathChunks)
@@ -71,6 +71,10 @@ public string RemoveRelativeChar(string path)
7171

7272
public string Combine(params string[] pathChunks)
7373
{
74+
if (pathChunks.Skip(1).Any(this.IsAbsolute))
75+
{
76+
throw new InvalidOperationException("Can not combine two absolut paths. Only the first path segment in Combine(...) method can be absolute.");
77+
}
7478
List<string> safePathChunks = pathChunks.Where(x => !string.IsNullOrEmpty(x)).ToList();
7579
if (safePathChunks.Count == 0)
7680
{
@@ -122,8 +126,8 @@ public string Parent(string path)
122126

123127
public string RelativeTo(string path, string to)
124128
{
125-
string[] pathChunks = this.Format(path)?.Split(Path.DirectorySeparatorChar) ?? new string[0];
126-
string[] toChunks = this.Format(to)?.Split(Path.DirectorySeparatorChar) ?? new string[0];
129+
string[] pathChunks = this.ToAbsolute(path).Split(Path.DirectorySeparatorChar);
130+
string[] toChunks = this.ToAbsolute(to).Split(Path.DirectorySeparatorChar);
127131
int sameChunks = 0;
128132
for (int index = 0; index < pathChunks.Length; index++)
129133
{
@@ -142,4 +146,4 @@ public string RelativeTo(string path, string to)
142146
return newPath;
143147
}
144148
}
145-
}
149+
}

0 commit comments

Comments
 (0)