PathUtilityTests.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. using Microsoft.DotNet.Tools.Common;
  4. namespace Microsoft.DotNet.Cli.Utils
  5. {
  6. public class PathUtilityTests
  7. {
  8. /// <summary>
  9. /// Tests that PathUtility.GetRelativePath treats drive references as case insensitive on Windows.
  10. /// </summary>
  11. [WindowsOnlyFact]
  12. public void GetRelativePathWithCaseInsensitiveDrives()
  13. {
  14. Assert.Equal(@"bar\", PathUtility.GetRelativePath(@"C:\foo\", @"C:\foo\bar\"));
  15. Assert.Equal(@"Bar\Baz\", PathUtility.GetRelativePath(@"c:\foo\", @"C:\Foo\Bar\Baz\"));
  16. Assert.Equal(@"baz\Qux\", PathUtility.GetRelativePath(@"C:\fOO\bar\", @"c:\foo\BAR\baz\Qux\"));
  17. Assert.Equal(@"d:\foo\", PathUtility.GetRelativePath(@"C:\foo\", @"d:\foo\"));
  18. }
  19. [WindowsOnlyFact]
  20. public void GetRelativePathForFilePath()
  21. {
  22. Assert.Equal(
  23. $@"mytool\1.0.1\mytool\1.0.1\tools\{ToolsetInfo.CurrentTargetFramework}\any\mytool.dll",
  24. PathUtility.GetRelativePath(
  25. @"C:\Users\myuser\.dotnet\tools\mytool.exe",
  26. $@"C:\Users\myuser\.dotnet\tools\mytool\1.0.1\mytool\1.0.1\tools\{ToolsetInfo.CurrentTargetFramework}\any\mytool.dll"));
  27. }
  28. [WindowsOnlyFact]
  29. public void GetRelativePathRequireTrailingSlashForDirectoryPath()
  30. {
  31. Assert.NotEqual(
  32. $@"mytool\1.0.1\mytool\1.0.1\tools\{ToolsetInfo.CurrentTargetFramework}\any\mytool.dll",
  33. PathUtility.GetRelativePath(
  34. @"C:\Users\myuser\.dotnet\tools",
  35. $@"C:\Users\myuser\.dotnet\tools\mytool\1.0.1\mytool\1.0.1\tools\{ToolsetInfo.CurrentTargetFramework}\any\mytool.dll"));
  36. Assert.Equal(
  37. $@"mytool\1.0.1\mytool\1.0.1\tools\{ToolsetInfo.CurrentTargetFramework}\any\mytool.dll",
  38. PathUtility.GetRelativePath(
  39. @"C:\Users\myuser\.dotnet\tools\",
  40. $@"C:\Users\myuser\.dotnet\tools\mytool\1.0.1\mytool\1.0.1\tools\{ToolsetInfo.CurrentTargetFramework}\any\mytool.dll"));
  41. }
  42. /// <summary>
  43. /// Tests that PathUtility.RemoveExtraPathSeparators works correctly with drive references on Windows.
  44. /// </summary>
  45. [WindowsOnlyFact]
  46. public void RemoveExtraPathSeparatorsWithDrives()
  47. {
  48. Assert.Equal(@"c:\foo\bar\baz\", PathUtility.RemoveExtraPathSeparators(@"c:\\\foo\\\\bar\baz\\"));
  49. Assert.Equal(@"D:\QUX\", PathUtility.RemoveExtraPathSeparators(@"D:\\\\\QUX\"));
  50. }
  51. }
  52. }