-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CoW, Artifacts: Handle symlinked same-file path (#492)
Avoid throwing when source and destination are the same via a symlink.
- Loading branch information
Showing
16 changed files
with
531 additions
and
56 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
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,61 @@ | ||
// 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 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", ".."))); | ||
} | ||
|
||
[Fact] | ||
public void PathsAreIdentical_Symlinks() | ||
{ | ||
if (!UserCanCreateSymlinks()) | ||
{ | ||
return; | ||
} | ||
|
||
using var tempDir = new DisposableTempDirectory(); | ||
string regularFilePath = Path.Combine(tempDir.Path, "regular.txt"); | ||
File.WriteAllText(regularFilePath, "regular"); | ||
Assert.True(CopyExceptionHandling.PathsAreIdentical(regularFilePath, regularFilePath)); | ||
|
||
string regularFilePathWithNonCanonicalSegments = | ||
Path.Combine(tempDir.Path, "..", Path.GetFileName(tempDir.Path), ".", "regular.txt"); | ||
Assert.True(CopyExceptionHandling.PathsAreIdentical(regularFilePath, regularFilePathWithNonCanonicalSegments)); | ||
|
||
string symlinkPath = Path.Combine(tempDir.Path, "symlink_to_regular.txt"); | ||
string? errorMessage = null; | ||
bool linkCreated = NativeMethods.MakeSymbolicLink(symlinkPath, regularFilePath, ref errorMessage); | ||
Assert.True(linkCreated); | ||
Assert.Null(errorMessage); | ||
Assert.True(CopyExceptionHandling.PathsAreIdentical(regularFilePath, symlinkPath)); | ||
|
||
File.Delete(symlinkPath); | ||
errorMessage = null; | ||
linkCreated = NativeMethods.MakeSymbolicLink(symlinkPath, regularFilePathWithNonCanonicalSegments, ref errorMessage); | ||
Assert.True(linkCreated); | ||
Assert.Null(errorMessage); | ||
Assert.True(CopyExceptionHandling.PathsAreIdentical(regularFilePath, symlinkPath)); | ||
} | ||
|
||
private bool UserCanCreateSymlinks() | ||
{ | ||
return !IsWindows || IsAdministratorOnWindows(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
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,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>net472;netcoreapp3.1;net6.0</TargetFrameworks> | ||
<IsPackable>false</IsPackable> | ||
<Nullable>Enable</Nullable> | ||
|
||
<!-- Suppress "Error : System.CodeDom 7.0.0 doesn't support netcoreapp3.1 and has not been tested with it." --> | ||
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="CopyOnWrite" /> | ||
<PackageReference Include="Microsoft.Build.Tasks.Core" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="Microsoft.Win32.Registry" /> | ||
<PackageReference Include="MSBuild.ProjectCreation" /> | ||
<PackageReference Include="Newtonsoft.Json" /> | ||
<PackageReference Include="System.CodeDom" /> | ||
<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
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.