diff --git a/src/dotnet/CommandLine/CommandLineApplication.cs b/src/dotnet/CommandLine/CommandLineApplication.cs index a7db54bce..81ad516b0 100644 --- a/src/dotnet/CommandLine/CommandLineApplication.cs +++ b/src/dotnet/CommandLine/CommandLineApplication.cs @@ -136,7 +136,7 @@ namespace Microsoft.DotNet.Cli.CommandLine public int Execute(params string[] args) { CommandLineApplication command = this; - IEnumerator arguments = null; + CommandArgumentEnumerator arguments = null; if (HandleResponseFiles) { @@ -181,9 +181,9 @@ namespace Microsoft.DotNet.Cli.CommandLine } else { - if (arguments == null) + if (arguments == null || arguments.CommandName != command.Name) { - arguments = new CommandArgumentEnumerator(command.Arguments.GetEnumerator()); + arguments = new CommandArgumentEnumerator(command.Arguments.GetEnumerator(), command.Name); } if (arguments.MoveNext()) @@ -629,11 +629,16 @@ namespace Microsoft.DotNet.Cli.CommandLine { private readonly IEnumerator _enumerator; - public CommandArgumentEnumerator(IEnumerator enumerator) + public CommandArgumentEnumerator( + IEnumerator enumerator, + string commandName) { + CommandName = commandName; _enumerator = enumerator; } + public string CommandName { get; } + public CommandArgument Current { get diff --git a/test/dotnet-add-package.Tests/GivenDotnetPackageAdd.cs b/test/dotnet-add-package.Tests/GivenDotnetPackageAdd.cs index def951aec..3cf2cd76c 100644 --- a/test/dotnet-add-package.Tests/GivenDotnetPackageAdd.cs +++ b/test/dotnet-add-package.Tests/GivenDotnetPackageAdd.cs @@ -2,18 +2,23 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using FluentAssertions; -using Microsoft.Build.Construction; -using Microsoft.DotNet.Cli.Sln.Internal; using Microsoft.DotNet.Tools.Test.Utilities; using System; using System.IO; using System.Linq; using Xunit; +using Xunit.Abstractions; namespace Microsoft.DotNet.Cli.Package.Add.Tests { public class GivenDotnetPackageAdd : TestBase { + private readonly ITestOutputHelper _output; + + public GivenDotnetPackageAdd(ITestOutputHelper output) + { + _output = output; + } [Fact] public void WhenValidPackageIsPassedBeforeVersionItGetsAdded() @@ -37,6 +42,35 @@ namespace Microsoft.DotNet.Cli.Package.Add.Tests cmd.StdErr.Should().BeEmpty(); } + [Fact] + public void + WhenValidProjectAndPackageArePassedItGetsAdded() + { + var testAsset = "TestAppSimple"; + var projectDirectory = TestAssets + .Get(testAsset) + .CreateInstance() + .WithSourceFiles() + .Root + .FullName; + + var csproj = $"{projectDirectory + Path.DirectorySeparatorChar + testAsset}.csproj"; + var packageName = "Newtonsoft.Json"; + var packageVersion = "9.0.1"; + var cmd = new DotnetCommand() + .WithWorkingDirectory(projectDirectory) + .ExecuteWithCapturedOutput($"add {csproj} package {packageName} --version {packageVersion}"); + + _output.WriteLine($"[STDOUT] {cmd.StdOut}\n[STDERR]{cmd.StdErr}\n"); + + cmd.Should().Pass(); + + cmd.StdOut.Should() + .Contain($"PackageReference for package \'{packageName}\' version \'{packageVersion}\' added to file '{csproj}'."); + + cmd.StdErr.Should().BeEmpty(); + } + [Fact] public void WhenValidPackageIsPassedAfterVersionItGetsAdded() {