-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Artifacts, CopyOnWrite: Avoid throwing when source and destination ar…
…e the same via a symlink
- Loading branch information
Showing
16 changed files
with
542 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// | ||
// Licensed under the MIT license. | ||
|
||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Shared; | ||
using Microsoft.Build.UnitTests.Common; | ||
using System.IO; | ||
using Xunit; | ||
|
||
namespace Microsoft.Build.CopyOnWrite.UnitTests; | ||
|
||
public class CopyExceptionHandlingTests : MSBuildSdkTestBase | ||
{ | ||
[Fact] | ||
public void GetRealPathOrNull() | ||
{ | ||
using var tempDir = new DisposableTempDirectory(); | ||
string regularFilePath = Path.Combine(tempDir.Path, "regular.txt"); | ||
File.WriteAllText(regularFilePath, "regular"); | ||
Assert.Equal(regularFilePath, CopyExceptionHandling.GetRealPathOrNull(regularFilePath)); | ||
|
||
string regularFilePathWithNonCanonicalSegments = | ||
Path.Combine(tempDir.Path, "..", Path.GetFileName(tempDir.Path), ".", "regular.txt"); | ||
Assert.Equal(regularFilePath, CopyExceptionHandling.GetRealPathOrNull(regularFilePathWithNonCanonicalSegments)); | ||
|
||
string symlinkPath = Path.Combine(tempDir.Path, "symlink_to_regular.txt"); | ||
string? errorMessage = null; | ||
NativeMethods.MakeSymbolicLink(symlinkPath, regularFilePath, ref errorMessage); | ||
if (errorMessage is null) | ||
{ | ||
Assert.Equal(regularFilePath, CopyExceptionHandling.GetRealPathOrNull(symlinkPath)); | ||
|
||
File.Delete(symlinkPath); | ||
NativeMethods.MakeSymbolicLink(symlinkPath, regularFilePathWithNonCanonicalSegments, ref errorMessage); | ||
Assert.Null(errorMessage); | ||
Assert.Equal(regularFilePath, CopyExceptionHandling.GetRealPathOrNull(symlinkPath)); | ||
} | ||
else | ||
{ | ||
Assert.True(IsWindows && !IsAdministratorOnWindows(), $"Allow Windows non-admins to skip symlink portion of test. Symlink creation failure: {errorMessage}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// | ||
// Licensed under the MIT license. | ||
|
||
using Microsoft.Build.Shared; | ||
using Microsoft.Build.UnitTests.Common; | ||
using System.IO; | ||
using Xunit; | ||
|
||
#nullable enable | ||
|
||
namespace Microsoft.Build.CopyOnWrite.UnitTests; | ||
|
||
public class CopyOnWriteTests : MSBuildSdkTestBase | ||
{ | ||
[Fact] | ||
public void PathsAreIdentical_NoSymlinks() | ||
{ | ||
string osRoot = IsWindows ? @"C:\" : "/"; | ||
string osCapitalizationDependentName = IsWindows ? "NAME" : "name"; | ||
Assert.True(CopyExceptionHandling.PathsAreIdentical(osRoot, osRoot)); | ||
Assert.True(CopyExceptionHandling.PathsAreIdentical(Path.Combine(osRoot, "name"), Path.Combine(osCapitalizationDependentName))); | ||
Assert.True(CopyExceptionHandling.PathsAreIdentical(osRoot, Path.Combine(osRoot, "subDir", ".."))); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/CopyOnWrite.UnitTests/Microsoft.Build.CopyOnWrite.UnitTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>net472;netcoreapp3.1;net6.0</TargetFrameworks> | ||
<IsPackable>false</IsPackable> | ||
<Nullable>Enable</Nullable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Win32.Registry" /> | ||
<PackageReference Include="MSBuild.ProjectCreation" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="xunit" /> | ||
<PackageReference Include="xunit.runner.visualstudio" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\CopyOnWrite\Microsoft.Build.CopyOnWrite.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.