diff --git a/src/Cli/dotnet/Commands/Tool/Install/ToolInstallCommandParser.cs b/src/Cli/dotnet/Commands/Tool/Install/ToolInstallCommandParser.cs index 8ce76001466e..ca9f1e17f80d 100644 --- a/src/Cli/dotnet/Commands/Tool/Install/ToolInstallCommandParser.cs +++ b/src/Cli/dotnet/Commands/Tool/Install/ToolInstallCommandParser.cs @@ -49,7 +49,8 @@ internal static class ToolInstallCommandParser public static readonly Option CreateManifestIfNeededOption = new("--create-manifest-if-needed") { Description = CliCommandStrings.CreateManifestIfNeededOptionDescription, - Arity = ArgumentArity.Zero + Arity = ArgumentArity.ZeroOrOne, + DefaultValueFactory = _ => true, }; public static readonly Option AllowPackageDowngradeOption = new("--allow-downgrade") diff --git a/test/dotnet.Tests/CommandTests/Tool/Install/ToolInstallLocalCommandTests.cs b/test/dotnet.Tests/CommandTests/Tool/Install/ToolInstallLocalCommandTests.cs index e1871f2ca072..b1d51d58f0ec 100644 --- a/test/dotnet.Tests/CommandTests/Tool/Install/ToolInstallLocalCommandTests.cs +++ b/test/dotnet.Tests/CommandTests/Tool/Install/ToolInstallLocalCommandTests.cs @@ -128,22 +128,41 @@ public void WhenRunWithPackageIdItShouldSaveToCacheAndAddToManifestFile() } [Fact] - public void GivenNoManifestFileItShouldThrow() + public void GivenCreateManifestIfNeededWithoutArgumentTheDefaultIsTrueForLegacyBehavior() { _fileSystem.File.Delete(_manifestFilePath); - var toolInstallLocalCommand = GetDefaultTestToolInstallLocalCommand(); + ParseResult parseResult = + Parser.Instance.Parse( + $"dotnet tool install {_packageIdA.ToString()} --create-manifest-if-needed"); - Action a = () => toolInstallLocalCommand.Execute(); - a.Should().Throw() - .And.Message.Should() - .Contain(CliStrings.CannotFindAManifestFile); + var toolInstallLocalCommand = new ToolInstallLocalCommand( + parseResult, + _packageIdA, + _toolPackageDownloaderMock, + _toolManifestFinder, + _toolManifestEditor, + _localToolsResolverCache, + _reporter); + + toolInstallLocalCommand.Execute().Should().Be(0); } [Fact] public void GivenNoManifestFileItShouldThrowAndContainNoManifestGuide() { _fileSystem.File.Delete(_manifestFilePath); - var toolInstallLocalCommand = GetDefaultTestToolInstallLocalCommand(); + ParseResult parseResult = + Parser.Instance.Parse( + $"dotnet tool install {_packageIdA.ToString()} --create-manifest-if-needed false"); + + var toolInstallLocalCommand = new ToolInstallLocalCommand( + parseResult, + _packageIdA, + _toolPackageDownloaderMock, + _toolManifestFinder, + _toolManifestEditor, + _localToolsResolverCache, + _reporter); Action a = () => toolInstallLocalCommand.Execute(); a.Should().Throw() @@ -408,6 +427,28 @@ public void GivenNoManifestFileAndCreateManifestIfNeededFlagItShouldCreateManife _fileSystem.File.Exists(Path.Combine(_temporaryDirectory, ".config", "dotnet-tools.json")).Should().BeTrue(); } + [Fact] + public void GivenNoManifestFileItUsesCreateManifestIfNeededByDefault() + { + _fileSystem.File.Delete(_manifestFilePath); + + ParseResult parseResult = + Parser.Instance.Parse( + $"dotnet tool install {_packageIdA.ToString()}"); + + var installLocalCommand = new ToolInstallLocalCommand( + parseResult, + _packageIdA, + _toolPackageDownloaderMock, + _toolManifestFinder, + _toolManifestEditor, + _localToolsResolverCache, + _reporter); + + installLocalCommand.Execute().Should().Be(0); + _fileSystem.File.Exists(Path.Combine(_temporaryDirectory, ".config", "dotnet-tools.json")).Should().BeTrue(); + } + [Fact] public void GivenNoManifestFileAndCreateManifestIfNeededFlagItShouldCreateManifestInSln() { diff --git a/test/dotnet.Tests/CompletionTests/snapshots/bash/DotnetCliSnapshotTests.VerifyCompletions.verified.sh b/test/dotnet.Tests/CompletionTests/snapshots/bash/DotnetCliSnapshotTests.VerifyCompletions.verified.sh index 6e76bbce5854..6a7eeb7f841d 100644 --- a/test/dotnet.Tests/CompletionTests/snapshots/bash/DotnetCliSnapshotTests.VerifyCompletions.verified.sh +++ b/test/dotnet.Tests/CompletionTests/snapshots/bash/DotnetCliSnapshotTests.VerifyCompletions.verified.sh @@ -1647,6 +1647,10 @@ _testhost_tool_install() { COMPREPLY=( $(compgen -W "d detailed diag diagnostic m minimal n normal q quiet" -- "$cur") ) return ;; + --create-manifest-if-needed) + COMPREPLY=( $(compgen -W "False True" -- "$cur") ) + return + ;; esac COMPREPLY=( $(compgen -W "$opts" -- "$cur") ) diff --git a/test/dotnet.Tests/CompletionTests/snapshots/nushell/DotnetCliSnapshotTests.VerifyCompletions.verified.nu b/test/dotnet.Tests/CompletionTests/snapshots/nushell/DotnetCliSnapshotTests.VerifyCompletions.verified.nu index bd3568c9fe1e..4024e6b4d046 100644 --- a/test/dotnet.Tests/CompletionTests/snapshots/nushell/DotnetCliSnapshotTests.VerifyCompletions.verified.nu +++ b/test/dotnet.Tests/CompletionTests/snapshots/nushell/DotnetCliSnapshotTests.VerifyCompletions.verified.nu @@ -22,4 +22,4 @@ let-env config = { completer: $external_completer # add it here } } -} +} \ No newline at end of file diff --git a/test/dotnet.Tests/CompletionTests/snapshots/zsh/DotnetCliSnapshotTests.VerifyCompletions.verified.zsh b/test/dotnet.Tests/CompletionTests/snapshots/zsh/DotnetCliSnapshotTests.VerifyCompletions.verified.zsh index 109551b4e1a4..d9b0a60082e8 100644 --- a/test/dotnet.Tests/CompletionTests/snapshots/zsh/DotnetCliSnapshotTests.VerifyCompletions.verified.zsh +++ b/test/dotnet.Tests/CompletionTests/snapshots/zsh/DotnetCliSnapshotTests.VerifyCompletions.verified.zsh @@ -1044,7 +1044,7 @@ _testhost() { '-v=[Set the MSBuild verbosity level. Allowed values are q\[uiet\], m\[inimal\], n\[ormal\], d\[etailed\], and diag\[nostic\].]:LEVEL:((d\:"d" detailed\:"detailed" diag\:"diag" diagnostic\:"diagnostic" m\:"m" minimal\:"minimal" n\:"n" normal\:"normal" q\:"q" quiet\:"quiet" ))' \ '--arch=[The target architecture.]: : ' \ '-a=[The target architecture.]: : ' \ - '--create-manifest-if-needed[Create a tool manifest if one isn'\''t found during tool installation. For information on how manifests are located, see https\://aka.ms/dotnet/tools/create-manifest-if-needed]' \ + '--create-manifest-if-needed=[Create a tool manifest if one isn'\''t found during tool installation. For information on how manifests are located, see https\://aka.ms/dotnet/tools/create-manifest-if-needed]: :((False\:"False" True\:"True" ))' \ '--allow-downgrade[Allow package downgrade when installing a .NET tool package.]' \ '--allow-roll-forward[Allow a .NET tool to roll forward to newer versions of the .NET runtime if the runtime it targets isn'\''t installed.]' \ '--help[Show command line help.]' \