From 7dc64194fe39e73277d38c4476f1796a4ce9a41c Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Tue, 11 Jun 2024 15:51:19 -0500 Subject: [PATCH 1/2] add missing option + tests --- .../dotnet-restore/RestoreCommandParser.cs | 2 + .../commands/dotnet-run/RunCommandParser.cs | 1 + .../BuildRelatedCommandParserTests.cs | 62 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 src/Tests/dotnet.Tests/ParserTests/BuildRelatedCommandParserTests.cs diff --git a/src/Cli/dotnet/commands/dotnet-restore/RestoreCommandParser.cs b/src/Cli/dotnet/commands/dotnet-restore/RestoreCommandParser.cs index bce6bc18d5c7..d3100d89f78a 100644 --- a/src/Cli/dotnet/commands/dotnet-restore/RestoreCommandParser.cs +++ b/src/Cli/dotnet/commands/dotnet-restore/RestoreCommandParser.cs @@ -170,6 +170,8 @@ private static IEnumerable ImplicitRestoreOptions(bool showHelp, bool yield return CommonOptions.PropertiesOption; + yield return CommonOptions.ArtifactsPathOption; + if (includeRuntimeOption) { CliOption> runtimeOption = new ForwardedOption>("--runtime") diff --git a/src/Cli/dotnet/commands/dotnet-run/RunCommandParser.cs b/src/Cli/dotnet/commands/dotnet-run/RunCommandParser.cs index d1ae11ed5aee..137599163bf9 100644 --- a/src/Cli/dotnet/commands/dotnet-run/RunCommandParser.cs +++ b/src/Cli/dotnet/commands/dotnet-run/RunCommandParser.cs @@ -83,6 +83,7 @@ private static CliCommand ConstructCommand() command.Options.Add(CommonOptions.ArchitectureOption); command.Options.Add(CommonOptions.OperatingSystemOption); command.Options.Add(CommonOptions.DisableBuildServersOption); + command.Options.Add(CommonOptions.ArtifactsPathOption); command.Arguments.Add(ApplicationArguments); diff --git a/src/Tests/dotnet.Tests/ParserTests/BuildRelatedCommandParserTests.cs b/src/Tests/dotnet.Tests/ParserTests/BuildRelatedCommandParserTests.cs new file mode 100644 index 000000000000..b3a4042afaac --- /dev/null +++ b/src/Tests/dotnet.Tests/ParserTests/BuildRelatedCommandParserTests.cs @@ -0,0 +1,62 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli; +using Microsoft.DotNet.Cli.CommandLineValidation; +using Microsoft.DotNet.Tools.Common; +using Parser = Microsoft.DotNet.Cli.Parser; + +namespace Microsoft.DotNet.Tests.ParserTests +{ + public class BuildRelatedCommandParserTests + { + + /// + /// These commands all implicitly use MSBuild under the covers and generally should expose + /// the same set of property- and behavior-impacting options. + /// + private static string[] BuildRelatedCommands = [ + "build", + "clean", + "pack", + "publish", + "restore", + "run", + "test" + ]; + + private static string[] OptionsToVerify = [ + "--artifacts-path" + ]; + + public static TheoryData BuildRelatedCommandsAndOptions() + { + var data = new TheoryData(); + foreach (var cmd in BuildRelatedCommands) + { + foreach (var opt in OptionsToVerify) + { + data.Add(cmd, opt); + } + } + return data; + } + + [MemberData(nameof(BuildRelatedCommandsAndOptions))] + [Theory] + public void Build(string command, string option) + { + var cliCommand = Parser.Instance.RootCommand.Children.OfType().FirstOrDefault(c => c.Name == command); + if (cliCommand is null) + { + throw new ArgumentException($"Command {command} not found in the dotnet CLI"); + } + var cliOption = cliCommand.Children.OfType().FirstOrDefault(o => o.Name == option || o.Aliases.Contains(option)); + if (cliOption is null) + { + throw new ArgumentException($"Option {option} not found in the {command} command"); + } + } + } +} From d8383e155fcc3e4c8c8b6393eaa60fe7872429e3 Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Tue, 11 Jun 2024 16:59:09 -0500 Subject: [PATCH 2/2] make the artifacts path option part of the explicit restore, not the implicit restore without this, the option is applied multiple times to implicit restores triggered by build, test, etc --- .../commands/dotnet-restore/RestoreCommandParser.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Cli/dotnet/commands/dotnet-restore/RestoreCommandParser.cs b/src/Cli/dotnet/commands/dotnet-restore/RestoreCommandParser.cs index d3100d89f78a..079851a9a157 100644 --- a/src/Cli/dotnet/commands/dotnet-restore/RestoreCommandParser.cs +++ b/src/Cli/dotnet/commands/dotnet-restore/RestoreCommandParser.cs @@ -25,11 +25,12 @@ internal static class RestoreCommandParser }.ForwardAsSingle(o => $"-property:RestoreSources={string.Join("%3B", o)}") .AllowSingleArgPerToken(); - private static IEnumerable FullRestoreOptions() => + private static IEnumerable FullRestoreOptions() => ImplicitRestoreOptions(true, true, true, true).Concat( - new CliOption[] { + [ CommonOptions.VerbosityOption, CommonOptions.InteractiveMsBuildForwardOption, + CommonOptions.ArtifactsPathOption, new ForwardedOption("--use-lock-file") { Description = LocalizableStrings.CmdUseLockFileOptionDescription, @@ -46,7 +47,8 @@ private static IEnumerable FullRestoreOptions() => new ForwardedOption("--force-evaluate") { Description = LocalizableStrings.CmdReevaluateOptionDescription - }.ForwardAs("-property:RestoreForceEvaluate=true") }); + }.ForwardAs("-property:RestoreForceEvaluate=true"), + ]); private static readonly CliCommand Command = ConstructCommand(); @@ -170,8 +172,6 @@ private static IEnumerable ImplicitRestoreOptions(bool showHelp, bool yield return CommonOptions.PropertiesOption; - yield return CommonOptions.ArtifactsPathOption; - if (includeRuntimeOption) { CliOption> runtimeOption = new ForwardedOption>("--runtime")