Do not try running mono when it's not available on the runner

This commit is contained in:
Tamas Vajk
2025-04-11 12:42:12 +02:00
parent 159d31d494
commit 51388f2401
2 changed files with 43 additions and 12 deletions

View File

@@ -46,6 +46,11 @@ namespace Semmle.Autobuild.CSharp.Tests
public IList<string> RunProcessIn { get; } = new List<string>();
public IDictionary<string, int> RunProcess { get; } = new Dictionary<string, int>();
/// <summary>
/// (process-exit code) pairs for commands that are executed during the assembly of the autobuild script.
/// </summary>
public IDictionary<string, int> RunProcessExecuteDuring { get; } = new Dictionary<string, int>();
public IDictionary<string, string> RunProcessOut { get; } = new Dictionary<string, string>();
public IDictionary<string, string> RunProcessWorkingDirectory { get; } = new Dictionary<string, string>();
public HashSet<string> CreateDirectories { get; } = new HashSet<string>();
@@ -66,7 +71,7 @@ namespace Semmle.Autobuild.CSharp.Tests
if (wd != workingDirectory)
throw new ArgumentException($"Unexpected RunProcessWorkingDirectory, got {wd ?? "null"} expected {workingDirectory ?? "null"} in {pattern}");
if (!RunProcess.TryGetValue(pattern, out var ret))
if (!RunProcess.TryGetValue(pattern, out var ret) && !RunProcessExecuteDuring.TryGetValue(pattern, out ret))
throw new ArgumentException("Missing RunProcess " + pattern);
return ret;
@@ -81,7 +86,7 @@ namespace Semmle.Autobuild.CSharp.Tests
if (wd != workingDirectory)
throw new ArgumentException($"Unexpected RunProcessWorkingDirectory, got {wd ?? "null"} expected {workingDirectory ?? "null"} in {pattern}");
if (!RunProcess.TryGetValue(pattern, out var ret))
if (!RunProcess.TryGetValue(pattern, out var ret) && !RunProcessExecuteDuring.TryGetValue(pattern, out ret))
throw new ArgumentException("Missing RunProcess " + pattern);
return ret;
@@ -797,11 +802,32 @@ namespace Semmle.Autobuild.CSharp.Tests
}
[Fact]
public void TestDirsProjLinux()
public void TestDirsProjLinux_WithMono()
{
actions.RunProcessExecuteDuring[@"mono --version"] = 0;
actions.RunProcess[@"nuget restore C:\Project/dirs.proj -DisableParallelProcessing"] = 1;
actions.RunProcess[@"mono scratch/.nuget/nuget.exe restore C:\Project/dirs.proj -DisableParallelProcessing"] = 0;
actions.RunProcess[@"msbuild C:\Project/dirs.proj /t:rebuild"] = 0;
var autobuilder = TestDirsProjLinux();
TestAutobuilderScript(autobuilder, 0, 3);
}
[Fact]
public void TestDirsProjLinux_WithoutMono()
{
actions.RunProcessExecuteDuring[@"mono --version"] = 1;
actions.RunProcess[@"dotnet msbuild /t:restore C:\Project/dirs.proj"] = 0;
actions.RunProcess[@"dotnet msbuild C:\Project/dirs.proj /t:rebuild"] = 0;
var autobuilder = TestDirsProjLinux();
TestAutobuilderScript(autobuilder, 0, 2);
}
private CSharpAutobuilder TestDirsProjLinux()
{
actions.FileExists["csharp.log"] = true;
actions.FileExists[@"C:\Project/a/test.csproj"] = true;
actions.FileExists[@"C:\Project/dirs.proj"] = true;
@@ -830,8 +856,7 @@ namespace Semmle.Autobuild.CSharp.Tests
</Project>");
actions.LoadXml[@"C:\Project/dirs.proj"] = dirsproj;
var autobuilder = CreateAutoBuilder(false);
TestAutobuilderScript(autobuilder, 0, 3);
return CreateAutoBuilder(false);
}
[Fact]

View File

@@ -10,15 +10,13 @@ namespace Semmle.Autobuild.Shared
/// <summary>
/// Appends a call to msbuild.
/// </summary>
/// <param name="cmdBuilder"></param>
/// <param name="builder"></param>
/// <returns></returns>
public static CommandBuilder MsBuildCommand(this CommandBuilder cmdBuilder, IAutobuilder<AutobuildOptionsShared> builder)
public static CommandBuilder MsBuildCommand(this CommandBuilder cmdBuilder, IAutobuilder<AutobuildOptionsShared> builder, bool preferDotnet)
{
// mono doesn't ship with `msbuild` on Arm-based Macs, but we can fall back to
// msbuild that ships with `dotnet` which can be invoked with `dotnet msbuild`
// perhaps we should do this on all platforms?
return builder.Actions.IsRunningOnAppleSilicon()
return builder.Actions.IsRunningOnAppleSilicon() || preferDotnet
? cmdBuilder.RunCommand("dotnet").Argument("msbuild")
: cmdBuilder.RunCommand("msbuild");
}
@@ -75,13 +73,21 @@ namespace Semmle.Autobuild.Shared
QuoteArgument(projectOrSolution.FullPath).
Argument("-DisableParallelProcessing").
Script;
BuildScript GetMonoVersionScript() => new CommandBuilder(builder.Actions).
RunCommand("mono").
Argument("--version").
Script;
var preferDotnet = !builder.Actions.IsWindows() && GetMonoVersionScript().Run(builder.Actions, (_, _) => { }, (_, _, _) => { }) != 0;
var nugetRestore = GetNugetRestoreScript();
var msbuildRestoreCommand = new CommandBuilder(builder.Actions).
MsBuildCommand(builder).
MsBuildCommand(builder, preferDotnet).
Argument("/t:restore").
QuoteArgument(projectOrSolution.FullPath);
if (builder.Actions.IsRunningOnAppleSilicon())
if (builder.Actions.IsRunningOnAppleSilicon() || preferDotnet)
{
// On Apple Silicon, only try package restore with `dotnet msbuild /t:restore`
ret &= BuildScript.Try(msbuildRestoreCommand.Script);
@@ -119,7 +125,7 @@ namespace Semmle.Autobuild.Shared
command.RunCommand("set Platform=&& type NUL", quoteExe: false);
}
command.MsBuildCommand(builder);
command.MsBuildCommand(builder, preferDotnet);
command.QuoteArgument(projectOrSolution.FullPath);
var target = "rebuild";