dotnet-installer/test/Microsoft.DotNet.Cli.Utils.Tests/PathUtilityTests.cs
Peter Huene 03f0c51292 Fix relative path handling on Windows.
On Windows, `PathUtility.GetRelativePath` was not properly handling
paths that differed by case in the drive reference (e.g. "C:\" vs.
"c:\").  The fix was to add the missing case-insensitive comparison
argument.

Replaced uses of `PathUtility.GetRelativePath` with
`Path.GetRelativePath` where possible (requires 2.0.0+).

Additionally, `PathUtility.RemoveExtraPathSeparators` was not handling
paths with drive references on Windows.  If the path contained a drive
reference, the separator between the drive reference and the first part
of the path was removed.  This is due to `Path.Combine` not handling
this case, so an explicit concatenation of the separator was added.

This commit resolves issue #7699.
2017-10-24 11:23:52 -07:00

34 lines
1.5 KiB
C#

// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.DotNet.Tools.Common;
using Microsoft.DotNet.Tools.Test.Utilities;
using Xunit;
namespace Microsoft.DotNet.Cli.Utils
{
public class PathUtilityTests : TestBase
{
/// <summary>
/// Tests that PathUtility.GetRelativePath treats drive references as case insensitive on Windows.
/// </summary>
[WindowsOnlyFact]
public void GetRelativePathWithCaseInsensitiveDrives()
{
Assert.Equal(@"bar\", PathUtility.GetRelativePath(@"C:\foo\", @"C:\foo\bar\"));
Assert.Equal(@"Bar\Baz\", PathUtility.GetRelativePath(@"c:\foo\", @"C:\Foo\Bar\Baz\"));
Assert.Equal(@"baz\Qux\", PathUtility.GetRelativePath(@"C:\fOO\bar\", @"c:\foo\BAR\baz\Qux\"));
Assert.Equal(@"d:\foo\", PathUtility.GetRelativePath(@"C:\foo\", @"d:\foo\"));
}
/// <summary>
/// Tests that PathUtility.RemoveExtraPathSeparators works correctly with drive references on Windows.
/// </summary>
[WindowsOnlyFact]
public void RemoveExtraPathSeparatorsWithDrives()
{
Assert.Equal(@"c:\foo\bar\baz\", PathUtility.RemoveExtraPathSeparators(@"c:\\\foo\\\\bar\baz\\"));
Assert.Equal(@"D:\QUX\", PathUtility.RemoveExtraPathSeparators(@"D:\\\\\QUX\"));
}
}
}