-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Sdk Validation Fix #2511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sdk Validation Fix #2511
Changes from 4 commits
bb5980a
c2f40c1
fc8c22c
0e426ea
3217c28
edeb125
61780c9
5986aec
5f89f15
3df3b0b
da1dae4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
|
||
namespace BenchmarkDotNet.Validators | ||
{ | ||
public class DotNetSdkProvider : ISdkProvider | ||
{ | ||
public IEnumerable<string> GetInstalledSdks() | ||
{ | ||
var startInfo = new ProcessStartInfo("dotnet", "--list-sdks") | ||
{ | ||
RedirectStandardOutput = true, | ||
UseShellExecute = false, | ||
CreateNoWindow = true | ||
}; | ||
|
||
var process = Process.Start(startInfo); | ||
process.WaitForExit(); | ||
|
||
var output = process.StandardOutput.ReadToEnd(); | ||
var lines = output.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); | ||
|
||
return lines.Select(line => line.Split(' ')[0]); // The SDK version is the first part of each line. | ||
} | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace BenchmarkDotNet.Validators | ||
{ | ||
public interface ISdkProvider | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I think this interface should be moved to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes perfect sense. I appreciate the valuable insight. |
||
{ | ||
IEnumerable<string> GetInstalledSdks(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using BenchmarkDotNet.Jobs; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace BenchmarkDotNet.Validators | ||
{ | ||
public class SdkValidator : IValidator | ||
{ | ||
private readonly ISdkProvider sdkProvider; | ||
|
||
public static readonly SdkValidator Instance = new SdkValidator(new DotNetSdkProvider()); | ||
|
||
public SdkValidator(ISdkProvider sdkProvider) | ||
{ | ||
this.sdkProvider = sdkProvider; | ||
} | ||
|
||
public bool TreatsWarningsAsErrors => true; | ||
|
||
public IEnumerable<ValidationError> Validate(ValidationParameters validationParameters) | ||
{ | ||
foreach (var benchmark in validationParameters.Benchmarks) | ||
{ | ||
if (benchmark == null || benchmark.Job == null || benchmark.Job.Environment?.Runtime?.RuntimeMoniker == null) | ||
{ | ||
continue; | ||
} | ||
|
||
var runtimeMoniker = benchmark.Job.Environment.Runtime.RuntimeMoniker; | ||
if (!IsSdkInstalled(runtimeMoniker)) | ||
{ | ||
yield return new ValidationError( | ||
TreatsWarningsAsErrors, | ||
$"The required SDK for {runtimeMoniker} is not installed", | ||
benchmark); | ||
} | ||
} | ||
} | ||
|
||
private bool IsSdkInstalled(RuntimeMoniker runtimeMoniker) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be updated to check the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch. The path info is passed into |
||
{ | ||
string requiredSdkVersion = GetSdkVersionFromMoniker(runtimeMoniker); | ||
var installedSdks = sdkProvider.GetInstalledSdks(); | ||
|
||
return installedSdks.Any(sdk => sdk.StartsWith(requiredSdkVersion + ".") || sdk == requiredSdkVersion); | ||
} | ||
|
||
private string GetSdkVersionFromMoniker(RuntimeMoniker runtimeMoniker) | ||
{ | ||
switch (runtimeMoniker) | ||
{ | ||
case RuntimeMoniker.Net461: | ||
return "4.6.1"; | ||
|
||
case RuntimeMoniker.Net462: | ||
return "4.6.2"; | ||
|
||
case RuntimeMoniker.Net47: | ||
return "4.7"; | ||
|
||
case RuntimeMoniker.Net471: | ||
return "4.7.1"; | ||
|
||
case RuntimeMoniker.Net472: | ||
return "4.7.2"; | ||
|
||
case RuntimeMoniker.Net48: | ||
return "4.8"; | ||
|
||
case RuntimeMoniker.Net481: | ||
return "4.8.1"; | ||
|
||
case RuntimeMoniker.NetCoreApp20: | ||
return "2.0"; | ||
|
||
case RuntimeMoniker.NetCoreApp21: | ||
return "2.1"; | ||
|
||
case RuntimeMoniker.NetCoreApp22: | ||
return "2.2"; | ||
|
||
case RuntimeMoniker.NetCoreApp30: | ||
return "3.0"; | ||
|
||
case RuntimeMoniker.NetCoreApp31: | ||
return "3.1"; | ||
|
||
case RuntimeMoniker.Net50: | ||
return "5.0"; | ||
|
||
case RuntimeMoniker.Net60: | ||
return "6.0"; | ||
|
||
case RuntimeMoniker.Net70: | ||
return "7.0"; | ||
|
||
case RuntimeMoniker.Net80: | ||
return "8.0"; | ||
|
||
case RuntimeMoniker.Net90: | ||
return "9.0"; | ||
|
||
default: | ||
throw new NotImplementedException($"SDK version check not implemented for {runtimeMoniker}"); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using BenchmarkDotNet.Validators; | ||
using System.Collections.Generic; | ||
|
||
namespace BenchmarkDotNet.Tests.Validators | ||
{ | ||
public class FakeSdkProvider : ISdkProvider | ||
{ | ||
private readonly string[] installedSdks; | ||
|
||
public FakeSdkProvider(string[] installedSdks) | ||
{ | ||
this.installedSdks = installedSdks; | ||
} | ||
|
||
public IEnumerable<string> GetInstalledSdks() | ||
{ | ||
return installedSdks; | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.