|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using FluentAssertions; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace Docfx.Common.Tests; |
| 8 | + |
| 9 | +public class PathUtilityTest |
| 10 | +{ |
| 11 | + [Theory] |
| 12 | + [MemberData(nameof(TestData.AdditionalTests), MemberType = typeof(TestData))] |
| 13 | + public void TestMakeRelativePath(string basePath, string targetPath, string expected) |
| 14 | + { |
| 15 | + // Act |
| 16 | + var result = PathUtility.MakeRelativePath(basePath, targetPath); |
| 17 | + |
| 18 | + // Assert |
| 19 | + result.Should().Be(expected); |
| 20 | + } |
| 21 | + |
| 22 | + [Theory] |
| 23 | + [MemberData(nameof(TestData.EscapedPaths), MemberType = typeof(TestData))] |
| 24 | + public void TestMakeRelativePathWithEncodedPath(string inputPath) |
| 25 | + { |
| 26 | + // Arrange |
| 27 | + string basePath = "./"; |
| 28 | + var expected = inputPath; |
| 29 | + |
| 30 | + // Act |
| 31 | + var result = PathUtility.MakeRelativePath(basePath, inputPath); |
| 32 | + |
| 33 | + // Assert |
| 34 | + result.Should().Be(expected); |
| 35 | + } |
| 36 | + |
| 37 | + private static class TestData |
| 38 | + { |
| 39 | + public static TheoryData<string, string, string> AdditionalTests = new() |
| 40 | + { |
| 41 | + { @"/a/b/d", @"/a/b/file.md", @"../file.md"}, // root relative path |
| 42 | + { @"~/a/b/d", @"~/a/b/file.md", @"../file.md"}, // user home directory relative path |
| 43 | + { @"./", @"\\UNCPath\file.md", @"//UNCPath/file.md"}, // UNC path |
| 44 | + { @"./", @"file:///C:/temp/test.md", @"file:/C:/temp/test.md"}, // `file:` Uri path |
| 45 | + { @"file:///C:/temp", @"file:///C:/temp/test.md", @"test.md"}, // `file:` Uri relative path |
| 46 | + { @"/temp/dir", @"/temp/dir/subdir/", @"subdir/"}, // If target path endsWith directory separator char. resolved path should contain directory separator. |
| 47 | + }; |
| 48 | + |
| 49 | + public static TheoryData<string> EscapedPaths = new() |
| 50 | + { |
| 51 | + "EscapedHypen(%2D).md", // Contains escaped hypen char |
| 52 | + "EscapedSpace(%20)_with_NonAsciiChar(α).md", // Contains escaped space char and non-unicode char |
| 53 | + }; |
| 54 | + } |
| 55 | +} |
0 commit comments