Skip to content

Commit

Permalink
Fix GetRelativePath for same parts in different places (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roadrunner67 authored and matkoch committed Jan 10, 2022
1 parent 829fe75 commit 049db46
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 3 additions & 0 deletions source/Nuke.Common.Tests/PathConstructionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public void TestEquality(string path1, string path2, bool expected)
[InlineData("C:\\A\\B\\C", "C:\\A\\B", "..")]
[InlineData("C:\\A\\B\\", "C:\\A\\B\\C", "C")]
[InlineData("C:\\A\\B\\C", "C:\\A\\B\\D\\E", "..\\D\\E")]
[InlineData("C:\\A\\B\\C\\B", "C:\\A\\B\\D\\B", "..\\..\\D\\B")]
[InlineData("/bin/etc", "/bin/tmp", "../tmp")]
[InlineData("/bin/etc/bin", "/bin/tmp/bin", "../../tmp/bin")]
[InlineData("/same1/diff1/same2", "/diff1/diff2/same2", "../../../diff1/diff2/same2")]
public void TestGetRelativePath(string basePath, string destinationPath, string expected)
{
GetRelativePath(basePath, destinationPath).Should().Be(expected);
Expand Down
2 changes: 1 addition & 1 deletion source/Nuke.Common/IO/PathConstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static string GetRelativePath(string basePath, string destinationPath, bo
var destinationParts = destinationPath.Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries);

var sameParts = baseParts.Zip(destinationParts, (a, b) => new { Base = a, Destination = b })
.Count(x => x.Base.EqualsOrdinalIgnoreCase(x.Destination));
.TakeWhile(x => x.Base.EqualsOrdinalIgnoreCase(x.Destination)).Count();
return Enumerable.Repeat("..", baseParts.Length - sameParts).ToList()
.Concat(destinationParts.Skip(sameParts).ToList()).Join(separator);
}
Expand Down

0 comments on commit 049db46

Please sign in to comment.