Fixing argument parsing in add package command (#5421)
* Fixing argument parsing in add package command * Adding check to throw if extra args were passed * Removing string and adding test cases for dotnet add package command * Add new test to test.sln, and fix naming, and clean csproj
This commit is contained in:
parent
8ac7312fa3
commit
04a7fca9fc
7 changed files with 179 additions and 11 deletions
|
@ -9,7 +9,7 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
|
|||
|
||||
public const string AppDescription = "Command to add package reference";
|
||||
|
||||
public const string AppHelpText = "Package references to add";
|
||||
public const string CmdPackageDescription = "Package references to add";
|
||||
|
||||
public const string SpecifyExactlyOnePackageReference = "Please specify one package reference to add.";
|
||||
|
||||
|
@ -25,6 +25,8 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
|
|||
|
||||
public const string CmdDGFileException = "Unable to Create Dependency graph file for project '{0}'. Cannot add package reference.";
|
||||
|
||||
public const string CmdPackage = "PACKAGE_NAME";
|
||||
|
||||
public const string CmdVersion = "VERSION";
|
||||
|
||||
public const string CmdFramework = "FRAMEWORK";
|
||||
|
|
|
@ -1,12 +1,6 @@
|
|||
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.Build.Evaluation;
|
||||
using Microsoft.DotNet.Cli;
|
||||
using Microsoft.DotNet.Cli.CommandLine;
|
||||
|
@ -15,6 +9,12 @@ using Microsoft.DotNet.Tools.Common;
|
|||
using Microsoft.DotNet.Tools.MSBuild;
|
||||
using Microsoft.DotNet.Tools.NuGet;
|
||||
using NuGet.Frameworks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.Add.PackageReference
|
||||
{
|
||||
|
@ -25,6 +25,7 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
|
|||
private CommandOption _noRestoreOption;
|
||||
private CommandOption _sourceOption;
|
||||
private CommandOption _packageDirectoryOption;
|
||||
private CommandArgument _packageNameArgument;
|
||||
|
||||
public static DotNetSubCommandBase Create()
|
||||
{
|
||||
|
@ -33,12 +34,16 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
|
|||
Name = "package",
|
||||
FullName = LocalizableStrings.AppFullName,
|
||||
Description = LocalizableStrings.AppDescription,
|
||||
HandleRemainingArguments = true,
|
||||
ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText,
|
||||
HandleRemainingArguments = false
|
||||
};
|
||||
|
||||
command.HelpOption("-h|--help");
|
||||
|
||||
command._packageNameArgument = command.Argument(
|
||||
$"<{LocalizableStrings.CmdPackage}>",
|
||||
LocalizableStrings.CmdPackageDescription,
|
||||
multipleValues: false);
|
||||
|
||||
command._versionOption = command.Option(
|
||||
$"-v|--version <{LocalizableStrings.CmdVersion}>",
|
||||
LocalizableStrings.CmdVersionDescription,
|
||||
|
@ -69,7 +74,7 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
|
|||
|
||||
public override int Run(string fileOrDirectory)
|
||||
{
|
||||
if (RemainingArguments.Count != 1)
|
||||
if (_packageNameArgument.Values.Count != 1 || string.IsNullOrWhiteSpace(_packageNameArgument.Value) || RemainingArguments.Count > 0)
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.SpecifyExactlyOnePackageReference);
|
||||
}
|
||||
|
@ -94,7 +99,7 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
|
|||
GetProjectDependencyGraph(projectFilePath, tempDgFilePath);
|
||||
}
|
||||
|
||||
var result = NuGetCommand.Run(TransformArgs(RemainingArguments.First(), tempDgFilePath, projectFilePath));
|
||||
var result = NuGetCommand.Run(TransformArgs(_packageNameArgument.Value, tempDgFilePath, projectFilePath));
|
||||
DisposeTemporaryFile(tempDgFilePath);
|
||||
|
||||
return result;
|
||||
|
|
|
@ -76,6 +76,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "dotnet-sln-remove.Tests", "
|
|||
EndProject
|
||||
Project("{13B669BE-BB05-4DDF-9536-439F39A36129}") = "dotnet-sln-remove.Tests", "dotnet-sln-remove.Tests\dotnet-sln-remove.Tests.csproj", "{92BA9F90-E25B-4A1C-9598-2295D3DFC12F}"
|
||||
EndProject
|
||||
Project("{13B669BE-BB05-4DDF-9536-439F39A36129}") = "dotnet-add-package.Tests", "dotnet-add-package.Tests\dotnet-add-package.Tests.csproj", "{3501AB72-3E05-45EE-9000-9515F5A139AC}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -246,6 +248,18 @@ Global
|
|||
{92BA9F90-E25B-4A1C-9598-2295D3DFC12F}.Release|x64.Build.0 = Release|x64
|
||||
{92BA9F90-E25B-4A1C-9598-2295D3DFC12F}.Release|x86.ActiveCfg = Release|x86
|
||||
{92BA9F90-E25B-4A1C-9598-2295D3DFC12F}.Release|x86.Build.0 = Release|x86
|
||||
{3501AB72-3E05-45EE-9000-9515F5A139AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{3501AB72-3E05-45EE-9000-9515F5A139AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{3501AB72-3E05-45EE-9000-9515F5A139AC}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{3501AB72-3E05-45EE-9000-9515F5A139AC}.Debug|x64.Build.0 = Debug|x64
|
||||
{3501AB72-3E05-45EE-9000-9515F5A139AC}.Debug|x86.ActiveCfg = Debug|x86
|
||||
{3501AB72-3E05-45EE-9000-9515F5A139AC}.Debug|x86.Build.0 = Debug|x86
|
||||
{3501AB72-3E05-45EE-9000-9515F5A139AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{3501AB72-3E05-45EE-9000-9515F5A139AC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{3501AB72-3E05-45EE-9000-9515F5A139AC}.Release|x64.ActiveCfg = Release|x64
|
||||
{3501AB72-3E05-45EE-9000-9515F5A139AC}.Release|x64.Build.0 = Release|x64
|
||||
{3501AB72-3E05-45EE-9000-9515F5A139AC}.Release|x86.ActiveCfg = Release|x86
|
||||
{3501AB72-3E05-45EE-9000-9515F5A139AC}.Release|x86.Build.0 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
119
test/dotnet-add-package.Tests/GivenDotnetPackageAdd.cs
Normal file
119
test/dotnet-add-package.Tests/GivenDotnetPackageAdd.cs
Normal file
|
@ -0,0 +1,119 @@
|
|||
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
||||
// 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;
|
||||
|
||||
namespace Microsoft.DotNet.Cli.Package.Add.Tests
|
||||
{
|
||||
public class GivenDotnetPackageAdd : TestBase
|
||||
{
|
||||
|
||||
[Fact]
|
||||
public void WhenValidPackageIsPassedBeforeVersionItGetsAdded()
|
||||
{
|
||||
var testAsset = "TestAppSimple";
|
||||
var projectDirectory = TestAssets
|
||||
.Get(testAsset)
|
||||
.CreateInstance()
|
||||
.WithSourceFiles()
|
||||
.Root
|
||||
.FullName;
|
||||
|
||||
var packageName = "Newtonsoft.Json";
|
||||
var packageVersion = "9.0.1";
|
||||
var cmd = new DotnetCommand()
|
||||
.WithWorkingDirectory(projectDirectory)
|
||||
.ExecuteWithCapturedOutput($"add package {packageName} --version {packageVersion}");
|
||||
cmd.Should().Pass();
|
||||
cmd.StdOut.Should().Contain($"PackageReference for package '{packageName}' version '{packageVersion}' " +
|
||||
$"added to file '{projectDirectory + Path.DirectorySeparatorChar + testAsset}.csproj'.");
|
||||
cmd.StdErr.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WhenValidPackageIsPassedAfterVersionItGetsAdded()
|
||||
{
|
||||
var testAsset = "TestAppSimple";
|
||||
var projectDirectory = TestAssets
|
||||
.Get(testAsset)
|
||||
.CreateInstance()
|
||||
.WithSourceFiles()
|
||||
.Root
|
||||
.FullName;
|
||||
|
||||
var packageName = "Newtonsoft.Json";
|
||||
var packageVersion = "9.0.1";
|
||||
var cmd = new DotnetCommand()
|
||||
.WithWorkingDirectory(projectDirectory)
|
||||
.ExecuteWithCapturedOutput($"add package --version {packageVersion} {packageName}");
|
||||
cmd.Should().Pass();
|
||||
cmd.StdOut.Should().Contain($"PackageReference for package '{packageName}' version '{packageVersion}' " +
|
||||
$"added to file '{projectDirectory + Path.DirectorySeparatorChar + testAsset}.csproj'.");
|
||||
cmd.StdErr.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WhenValidPackageIsPassedWithFrameworkItGetsAdded()
|
||||
{
|
||||
var testAsset = "TestAppSimple";
|
||||
var projectDirectory = TestAssets
|
||||
.Get(testAsset)
|
||||
.CreateInstance()
|
||||
.WithSourceFiles()
|
||||
.Root
|
||||
.FullName;
|
||||
|
||||
var packageName = "Newtonsoft.Json";
|
||||
var packageVersion = "9.0.1";
|
||||
var framework = "netcoreapp1.0";
|
||||
var cmd = new DotnetCommand()
|
||||
.WithWorkingDirectory(projectDirectory)
|
||||
.ExecuteWithCapturedOutput($"add package {packageName} --version {packageVersion} --framework {framework}");
|
||||
cmd.Should().Pass();
|
||||
cmd.StdOut.Should().Contain($"PackageReference for package '{packageName}' version '{packageVersion}' " +
|
||||
$"added to file '{projectDirectory + Path.DirectorySeparatorChar + testAsset}.csproj'.");
|
||||
cmd.StdErr.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WhenMultiplePackagesArePassedCommandFails()
|
||||
{
|
||||
var projectDirectory = TestAssets
|
||||
.Get("TestAppSimple")
|
||||
.CreateInstance()
|
||||
.WithSourceFiles()
|
||||
.Root
|
||||
.FullName;
|
||||
|
||||
var cmd = new DotnetCommand()
|
||||
.WithWorkingDirectory(projectDirectory)
|
||||
.ExecuteWithCapturedOutput($"add package package1 package2 package3");
|
||||
cmd.Should().Fail();
|
||||
cmd.StdErr.Should().Contain("Please specify one package reference to add.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WhenNoPackageisPassedCommandFails()
|
||||
{
|
||||
var projectDirectory = TestAssets
|
||||
.Get("TestAppSimple")
|
||||
.CreateInstance()
|
||||
.WithSourceFiles()
|
||||
.Root
|
||||
.FullName;
|
||||
|
||||
var cmd = new DotnetCommand()
|
||||
.WithWorkingDirectory(projectDirectory)
|
||||
.ExecuteWithCapturedOutput($"add package");
|
||||
cmd.Should().Fail();
|
||||
cmd.StdErr.Should().Contain("Please specify one package reference to add.");
|
||||
}
|
||||
}
|
||||
}
|
1
test/dotnet-add-package.Tests/MSBuild.exe
Normal file
1
test/dotnet-add-package.Tests/MSBuild.exe
Normal file
|
@ -0,0 +1 @@
|
|||
https://github.com/Microsoft/msbuild/issues/927
|
1
test/dotnet-add-package.Tests/MSBuild.exe.config
Normal file
1
test/dotnet-add-package.Tests/MSBuild.exe.config
Normal file
|
@ -0,0 +1 @@
|
|||
https://github.com/Microsoft/msbuild/issues/927
|
|
@ -0,0 +1,26 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp1.0</TargetFramework>
|
||||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
|
||||
<AssemblyName>dotnet-add-package.Tests</AssemblyName>
|
||||
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">$(PackageTargetFallback);dotnet5.4;portable-net451+win8</PackageTargetFallback>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Microsoft.DotNet.Tools.Tests.Utilities\Microsoft.DotNet.Tools.Tests.Utilities.csproj" />
|
||||
<ProjectReference Include="..\..\src\Microsoft.DotNet.Cli.Sln.Internal\Microsoft.DotNet.Cli.Sln.Internal.csproj" />
|
||||
<ProjectReference Include="..\..\src\Microsoft.DotNet.Cli.Utils\Microsoft.DotNet.Cli.Utils.csproj" />
|
||||
<ProjectReference Include="..\..\src\Microsoft.DotNet.InternalAbstractions\Microsoft.DotNet.InternalAbstractions.csproj" />
|
||||
<ProjectReference Include="..\..\src\Microsoft.DotNet.TestFramework\Microsoft.DotNet.TestFramework.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NetCore.App" Version="1.0.3" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(CLI_TestPlatform_Version)" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0-beta4-build1194" />
|
||||
<PackageReference Include="xunit" Version="2.2.0-beta4-build3444" />
|
||||
<PackageReference Include="Microsoft.Build" Version="$(CLI_MSBuild_Version)" />
|
||||
</ItemGroup>
|
||||
</Project>
|
Loading…
Reference in a new issue