Skip to content

Commit 152a5d2

Browse files
authored
Better search versions error experience (#45144)
2 parents 991efb3 + 7c92aa3 commit 152a5d2

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

src/Cli/dotnet/commands/dotnet-workload/search/WorkloadSearchVersionsCommandParser.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.CommandLine;
5+
using Microsoft.DotNet.Workloads.Workload;
56
using Microsoft.DotNet.Workloads.Workload.Search;
67
using LocalizableStrings = Microsoft.DotNet.Workloads.Workload.Search.LocalizableStrings;
78

@@ -56,6 +57,15 @@ private static CliCommand ConstructCommand()
5657
}
5758
});
5859

60+
command.Validators.Add(result =>
61+
{
62+
var versionArgument = result.GetValue(WorkloadVersionArgument);
63+
if (versionArgument is not null && !WorkloadSetVersion.IsWorkloadSetPackageVersion(versionArgument))
64+
{
65+
result.AddError(string.Format(CommandLineValidation.LocalizableStrings.UnrecognizedCommandOrArgument, versionArgument));
66+
}
67+
});
68+
5969
command.SetAction(parseResult => new WorkloadSearchVersionsCommand(parseResult).Execute());
6070

6171
return command;

src/Common/WorkloadSetVersion.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,46 @@ namespace Microsoft.DotNet.Workloads.Workload
88
{
99
static class WorkloadSetVersion
1010
{
11-
public static string ToWorkloadSetPackageVersion(string workloadSetVersion, out SdkFeatureBand sdkFeatureBand)
11+
private static string[] SeparateCoreComponents(string workloadSetVersion, out string[] sections)
12+
{
13+
sections = workloadSetVersion.Split(['-', '+'], 2);
14+
return sections[0].Split('.');
15+
}
16+
17+
public static bool IsWorkloadSetPackageVersion(string workloadSetVersion)
1218
{
13-
string[] sections = workloadSetVersion.Split(new char[] { '-', '+' }, 2);
14-
string versionCore = sections[0];
15-
string? preReleaseOrBuild = sections.Length > 1 ? sections[1] : null;
19+
int coreComponentsLength = SeparateCoreComponents(workloadSetVersion, out _).Length;
20+
return coreComponentsLength >= 3 && coreComponentsLength <= 4;
21+
}
1622

17-
string[] coreComponents = versionCore.Split('.');
23+
public static string ToWorkloadSetPackageVersion(string workloadSetVersion, out SdkFeatureBand sdkFeatureBand)
24+
{
25+
string[] coreComponents = SeparateCoreComponents(workloadSetVersion, out string[] sections);
1826
string major = coreComponents[0];
1927
string minor = coreComponents[1];
2028
string patch = coreComponents[2];
21-
2229
string packageVersion = $"{major}.{patch}.";
2330
if (coreComponents.Length == 3)
2431
{
2532
// No workload set patch version
2633
packageVersion += "0";
27-
2834
// Use preview specifier (if any) from workload set version as part of SDK feature band
2935
sdkFeatureBand = new SdkFeatureBand(workloadSetVersion);
3036
}
3137
else
3238
{
3339
// Workload set version has workload patch version (ie 4 components)
3440
packageVersion += coreComponents[3];
35-
3641
// Don't include any preview specifiers in SDK feature band
3742
sdkFeatureBand = new SdkFeatureBand($"{major}.{minor}.{patch}");
3843
}
3944

40-
if (preReleaseOrBuild != null)
45+
if (sections.Length > 1)
4146
{
4247
// Figure out if we split on a '-' or '+'
4348
char separator = workloadSetVersion[sections[0].Length];
44-
packageVersion += separator + preReleaseOrBuild;
49+
packageVersion += separator + sections[1];
4550
}
46-
4751
return packageVersion;
4852
}
4953

test/dotnet-workload-search.Tests/GivenDotnetWorkloadSearch.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ public GivenDotnetWorkloadSearch(ITestOutputHelper log) : base(log)
2828
_reporter = new BufferedReporter();
2929
}
3030

31+
[Theory]
32+
[InlineData("--invalidArgument")]
33+
[InlineData("notAVersion")]
34+
[InlineData("1.2")] // too short
35+
[InlineData("1.2.3.4.5")] // too long
36+
[InlineData("1.2-3.4")] // numbers after [-, +] don't count
37+
public void GivenInvalidArgumentToWorkloadSearchVersionItFailsCleanly(string argument)
38+
{
39+
_reporter.Clear();
40+
var parseResult = Parser.Instance.Parse($"dotnet workload search version {argument}");
41+
var workloadResolver = new MockWorkloadResolver(Enumerable.Empty<WorkloadResolver.WorkloadInfo>());
42+
var workloadResolverFactory = new MockWorkloadResolverFactory(dotnetPath: null, "9.0.100", workloadResolver);
43+
var command = () => new WorkloadSearchVersionsCommand(parseResult, _reporter, workloadResolverFactory);
44+
command.Should().Throw<CommandParsingException>();
45+
}
46+
3147
[Fact]
3248
public void GivenNoWorkloadsAreInstalledSearchIsEmpty()
3349
{

0 commit comments

Comments
 (0)