Skip to content

Better search versions error experience #45144

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

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.CommandLine;
using Microsoft.DotNet.Workloads.Workload;
using Microsoft.DotNet.Workloads.Workload.Search;
using LocalizableStrings = Microsoft.DotNet.Workloads.Workload.Search.LocalizableStrings;

Expand Down Expand Up @@ -53,6 +54,18 @@ private static CliCommand ConstructCommand()
}
});

command.Validators.Add(result =>
{
var versionArgument = result.GetValue(WorkloadVersionArgument);
if (versionArgument is not null)
{
if (!WorkloadSetVersion.IsWorkloadSetPackageVersion(versionArgument))
{
result.AddError(string.Format(CommandLineValidation.LocalizableStrings.UnrecognizedCommandOrArgument, versionArgument));
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Combine nested boolean conditions

Suggested change
if (versionArgument is not null)
{
if (!WorkloadSetVersion.IsWorkloadSetPackageVersion(versionArgument))
{
result.AddError(string.Format(CommandLineValidation.LocalizableStrings.UnrecognizedCommandOrArgument, versionArgument));
}
}
if (versionArgument is not null &&
!WorkloadSetVersion.IsWorkloadSetPackageVersion(versionArgument))
{
result.AddError(string.Format(CommandLineValidation.LocalizableStrings.UnrecognizedCommandOrArgument, versionArgument));
}

});

command.SetAction(parseResult => new WorkloadSearchVersionsCommand(parseResult).Execute());

return command;
Expand Down
13 changes: 12 additions & 1 deletion src/Common/WorkloadSetVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ namespace Microsoft.DotNet.Workloads.Workload
{
static class WorkloadSetVersion
{
public static bool IsWorkloadSetPackageVersion(string workloadSetVersion)
{

string[] sections = workloadSetVersion.Split(['-', '+'], 2);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first few lines here are duplicated from below, but they can't really be de-duped, as they are just defining variables. The variables would need to be defined either way.

string versionCore = sections[0];
string? preReleaseOrBuild = sections.Length > 1 ? sections[1] : null;

string[] coreComponents = versionCore.Split('.');
return coreComponents.Length >= 3 && coreComponents.Length <= 4;
}

public static string ToWorkloadSetPackageVersion(string workloadSetVersion, out SdkFeatureBand sdkFeatureBand)
{
string[] sections = workloadSetVersion.Split(new char[] { '-', '+' }, 2);
string[] sections = workloadSetVersion.Split(['-', '+'], 2);
string versionCore = sections[0];
string? preReleaseOrBuild = sections.Length > 1 ? sections[1] : null;

Copy link
Member

@MiYanni MiYanni Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deduplicated the code. Also, noticed preReleaseOrBuild is unused in IsWorkloadSetPackageVersion. I cannot do a suggestion block since I cannot highlight the bottom of the ToWorkloadSetPackageVersion method in GitHub to include in this comment.

        private static string[] SeparateCoreComponents(string workloadSetVersion, out string[] sections)
        {
            sections = workloadSetVersion.Split(['-', '+'], 2);
            return sections[0].Split('.');
        }

        public static bool IsWorkloadSetPackageVersion(string workloadSetVersion)
        {
            string[] coreComponents = SeparateCoreComponents(workloadSetVersion, out _);
            return coreComponents.Length >= 3 && coreComponents.Length <= 4;
        }

        public static string ToWorkloadSetPackageVersion(string workloadSetVersion, out SdkFeatureBand sdkFeatureBand)
        {
            string[] coreComponents = SeparateCoreComponents(workloadSetVersion, out string[] sections);
            string major = coreComponents[0];
            string minor = coreComponents[1];
            string patch = coreComponents[2];
            string packageVersion = $"{major}.{patch}.";
            if (coreComponents.Length == 3)
            {
                //  No workload set patch version
                packageVersion += "0";
                //  Use preview specifier (if any) from workload set version as part of SDK feature band
                sdkFeatureBand = new SdkFeatureBand(workloadSetVersion);
            }
            else
            {
                //  Workload set version has workload patch version (ie 4 components)
                packageVersion += coreComponents[3];
                //  Don't include any preview specifiers in SDK feature band
                sdkFeatureBand = new SdkFeatureBand($"{major}.{minor}.{patch}");
            }

            if (sections.Length > 1)
            {
                //  Figure out if we split on a '-' or '+'
                char separator = workloadSetVersion[sections[0].Length];
                packageVersion += separator + sections[1];
            }
            return packageVersion;
        }

Expand Down
16 changes: 16 additions & 0 deletions test/dotnet-workload-search.Tests/GivenDotnetWorkloadSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ public GivenDotnetWorkloadSearch(ITestOutputHelper log) : base(log)
_reporter = new BufferedReporter();
}

[Theory]
[InlineData("--invalidArgument")]
[InlineData("notAVersion")]
[InlineData("1.2")] // too short
[InlineData("1.2.3.4.5")] // too long
[InlineData("1.2-3.4")] // numbers after [-, +] don't count
public void GivenInvalidArgumentToWorkloadSearchVersionItFailsCleanly(string argument)
{
_reporter.Clear();
var parseResult = Parser.Instance.Parse($"dotnet workload search version {argument}");
var workloadResolver = new MockWorkloadResolver(Enumerable.Empty<WorkloadResolver.WorkloadInfo>());
var workloadResolverFactory = new MockWorkloadResolverFactory(dotnetPath: null, "9.0.100", workloadResolver);
var command = () => new WorkloadSearchVersionsCommand(parseResult, _reporter, workloadResolverFactory);
command.Should().Throw<CommandParsingException>();
}

[Fact]
public void GivenNoWorkloadsAreInstalledSearchIsEmpty()
{
Expand Down
Loading