2017-01-21 00:11:18 -08:00
// 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.DotNet.Tools.Test.Utilities ;
2017-06-13 20:29:44 -07:00
using Microsoft.DotNet.Tools.Add.PackageReference ;
2017-01-21 00:11:18 -08:00
using System ;
using System.IO ;
using System.Linq ;
using Xunit ;
2017-01-31 21:31:02 -08:00
using Xunit.Abstractions ;
2017-01-21 00:11:18 -08:00
namespace Microsoft.DotNet.Cli.Package.Add.Tests
{
public class GivenDotnetPackageAdd : TestBase
{
2017-01-31 21:31:02 -08:00
private readonly ITestOutputHelper _output ;
public GivenDotnetPackageAdd ( ITestOutputHelper output )
{
_output = output ;
}
2017-01-21 00:11:18 -08:00
[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 ( ) ;
}
2017-01-31 21:31:02 -08:00
[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 ( ) ;
}
2017-01-21 00:11:18 -08:00
[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" ;
2018-05-24 15:41:03 -07:00
var framework = "netcoreapp2.2" ;
2017-01-21 00:11:18 -08:00
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 ( ) ;
}
2017-05-12 22:23:47 +02:00
[Fact]
public void WhenValidPackageIsPassedMSBuildDoesNotPrintVersionHeader ( )
{
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 ( ) . NotContain ( "Microsoft (R) Build Engine version" ) ;
cmd . StdErr . Should ( ) . BeEmpty ( ) ;
}
2017-01-21 00:11:18 -08:00
[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 ( ) ;
2017-06-13 20:29:44 -07:00
cmd . StdErr . Should ( ) . Contain ( LocalizableStrings . SpecifyExactlyOnePackageReference ) ;
2017-01-21 00:11:18 -08:00
}
[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 ( ) ;
2017-06-13 20:29:44 -07:00
cmd . StdErr . Should ( ) . Contain ( LocalizableStrings . SpecifyExactlyOnePackageReference ) ;
2017-01-21 00:11:18 -08:00
}
}
}