Skip to content

Commit e6644d0

Browse files
authored
chore: Suppress exception thrown internally on mono is not installed environment (#2966)
* chore: suppress exception thrown on mono is not installed environment * chore: reflect the reviewed content * chore: reflect reviewed content. and remove redirect stdout
1 parent b978f0e commit e6644d0

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

src/BenchmarkDotNet/Environments/HostEnvironmentInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected HostEnvironmentInfo()
7575
ChronometerFrequency = Chronometer.Frequency;
7676
HardwareTimerKind = Chronometer.HardwareTimerKind;
7777
DotNetSdkVersion = new Lazy<string>(DotNetCliCommandExecutor.GetDotNetSdkVersion);
78-
IsMonoInstalled = new Lazy<bool>(() => ProcessHelper.RunAndReadOutput("mono", "--version").IsNotBlank());
78+
IsMonoInstalled = new Lazy<bool>(() => ProcessHelper.TestCommandExists("mono"));
7979
AntivirusProducts = new Lazy<ICollection<Antivirus>>(RuntimeInformation.GetAntivirusProducts);
8080
VirtualMachineHypervisor = new Lazy<VirtualMachineHypervisor>(RuntimeInformation.GetVirtualMachineHypervisor);
8181
Os = new Lazy<OsInfo>(OsDetector.GetOs);

src/BenchmarkDotNet/Helpers/ProcessHelper.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Collections.Immutable;
44
using System.Diagnostics;
5+
using BenchmarkDotNet.Detectors;
56
using BenchmarkDotNet.Loggers;
67

78
namespace BenchmarkDotNet.Helpers
@@ -79,5 +80,44 @@ internal static (int exitCode, ImmutableArray<string> output) RunAndReadOutputLi
7980
return (process.ExitCode, output);
8081
}
8182
}
83+
84+
internal static bool TestCommandExists(string commandName, string arguments = "--version")
85+
{
86+
// Check command existence by using where/which command.
87+
try
88+
{
89+
using var process = Process.Start(new ProcessStartInfo
90+
{
91+
FileName = OsDetector.IsWindows() ? "where" : "which",
92+
Arguments = commandName,
93+
UseShellExecute = false,
94+
CreateNoWindow = true
95+
})!;
96+
process.WaitForExit();
97+
return process.ExitCode == 0;
98+
}
99+
catch
100+
{
101+
// On some environment. which command is not installed. (e.g. Alpine Linux)
102+
}
103+
104+
// Check command existence by executing actual command with --version argument.
105+
try
106+
{
107+
using var process = Process.Start(new ProcessStartInfo
108+
{
109+
FileName = commandName,
110+
Arguments = arguments,
111+
UseShellExecute = false,
112+
CreateNoWindow = true
113+
})!;
114+
process.WaitForExit();
115+
return process.ExitCode == 0;
116+
}
117+
catch
118+
{
119+
return false;
120+
}
121+
}
82122
}
83123
}

0 commit comments

Comments
 (0)