Skip to content

Commit

Permalink
Added RequiresSymbolicLinksTheoryAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred-brands committed Mar 13, 2023
1 parent 12efd74 commit 353b02c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Tasks.UnitTests/Microsoft.Build.Tasks.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Link>Shared\LongPathSupportDisabledFactAttribute.cs</Link>
</Compile>
<Compile Include="..\UnitTests.Shared\RequiresSymbolicLinksFactAttribute.cs" />
<Compile Include="..\UnitTests.Shared\RequiresSymbolicLinksTheoryAttribute.cs" />

<EmbeddedResource Include="SampleResx" />
<EmbeddedResource Include="AssemblyDependency\CacheFileSamples\Microsoft.VisualStudio.LanguageServices.Implementation.csprojAssemblyReference.cache" />
Expand Down
55 changes: 55 additions & 0 deletions src/UnitTests.Shared/RequiresSymbolicLinksTheoryAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;

using Microsoft.Build.Shared;
using Xunit;

namespace Microsoft.Build.UnitTests
{
/// <summary>
/// A custom <see cref="FactAttribute"/> that skips the test if the OS doesn't support creating symlinks.
/// </summary>
public sealed class RequiresSymbolicLinksTheoryAttribute : TheoryAttribute
{
private static readonly bool s_runningInAzurePipeline =
bool.TryParse(Environment.GetEnvironmentVariable("TF_BUILD"), out bool value) && value;

public RequiresSymbolicLinksTheoryAttribute()
{
if (s_runningInAzurePipeline || !NativeMethodsShared.IsWindows)
{
return;
}

// In Windows, a process can create symlinks only if it has sufficient permissions.
// We simply try to create one and if it fails we skip the test.
string sourceFile = FileUtilities.GetTemporaryFile();
string destinationFile = FileUtilities.GetTemporaryFile();
try
{
File.Create(sourceFile).Dispose();

string? errorMessage = null;
if (!NativeMethodsShared.MakeSymbolicLink(destinationFile, sourceFile, ref errorMessage))
{
Skip = "Requires permission to create symbolic links. Need to be run elevated or under development mode " +
"(https://learn.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development).";
}
}
finally
{
if (File.Exists(sourceFile))
{
File.Delete(sourceFile);
}
if (File.Exists(destinationFile))
{
File.Delete(destinationFile);
}
}
}
}
}

0 comments on commit 353b02c

Please sign in to comment.