2016-12-07 20:56:27 +00: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.Build.Construction ;
using Microsoft.DotNet.Tools.Test.Utilities ;
using Msbuild.Tests.Utilities ;
using System ;
using System.IO ;
using Xunit ;
namespace Microsoft.DotNet.Cli.List.P2P.Tests
{
public class GivenDotnetListP2Ps : TestBase
{
2016-12-16 18:23:26 +00:00
private const string HelpText = @ ".NET Core Project-to-Project dependency viewer
Usage : dotnet list < PROJECT_OR_SOLUTION > p2ps [ options ]
Arguments :
< PROJECT_OR_SOLUTION > The project or solution to operation on . If a file is not specified , the current directory is searched .
Options :
- h | - - help Show help information ";
2016-12-07 20:56:27 +00:00
const string FrameworkNet451Arg = "-f net451" ;
const string ConditionFrameworkNet451 = "== 'net451'" ;
const string FrameworkNetCoreApp10Arg = "-f netcoreapp1.0" ;
const string ConditionFrameworkNetCoreApp10 = "== 'netcoreapp1.0'" ;
[Theory]
[InlineData("--help")]
[InlineData("-h")]
public void WhenHelpOptionIsPassedItPrintsUsage ( string helpArg )
{
var cmd = new ListP2PsCommand ( ) . Execute ( helpArg ) ;
cmd . Should ( ) . Pass ( ) ;
2016-12-28 22:50:34 +00:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( HelpText ) ;
2016-12-16 18:23:26 +00:00
}
[Theory]
[InlineData("")]
[InlineData("unknownCommandName")]
public void WhenNoCommandIsPassedItPrintsError ( string commandName )
{
var cmd = new DotnetCommand ( )
. ExecuteWithCapturedOutput ( $"list {commandName}" ) ;
cmd . Should ( ) . Fail ( ) ;
cmd . StdErr . Should ( ) . Be ( "Required command was not provided." ) ;
2016-12-07 20:56:27 +00:00
}
2016-12-14 00:31:35 +00:00
[Fact]
public void WhenTooManyArgumentsArePassedItPrintsError ( )
{
var cmd = new AddP2PCommand ( )
. WithProject ( "one two three" )
. Execute ( "proj.csproj" ) ;
cmd . ExitCode . Should ( ) . NotBe ( 0 ) ;
2016-12-16 18:23:26 +00:00
cmd . StdErr . Should ( ) . Be ( "Unrecognized command or argument 'two'" ) ;
2016-12-14 00:31:35 +00:00
}
2016-12-07 20:56:27 +00:00
[Theory]
[InlineData("idontexist.csproj")]
[InlineData("ihave?inv@lid/char\\acters")]
public void WhenNonExistingProjectIsPassedItPrintsErrorAndUsage ( string projName )
{
var setup = Setup ( ) ;
var cmd = new ListP2PsCommand ( )
. WithWorkingDirectory ( setup . TestRoot )
. WithProject ( projName )
. Execute ( $"\" { setup . ValidRefCsprojPath } \ "" ) ;
cmd . ExitCode . Should ( ) . NotBe ( 0 ) ;
2016-12-16 18:23:26 +00:00
cmd . StdErr . Should ( ) . Be ( $"Could not find project or directory `{projName}`." ) ;
2016-12-28 22:50:34 +00:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( HelpText ) ;
2016-12-07 20:56:27 +00:00
}
[Fact]
public void WhenBrokenProjectIsPassedItPrintsErrorAndUsage ( )
{
string projName = "Broken/Broken.csproj" ;
var setup = Setup ( ) ;
var cmd = new ListP2PsCommand ( )
. WithWorkingDirectory ( setup . TestRoot )
. WithProject ( projName )
. Execute ( $"\" { setup . ValidRefCsprojPath } \ "" ) ;
cmd . ExitCode . Should ( ) . NotBe ( 0 ) ;
2016-12-16 18:23:26 +00:00
cmd . StdErr . Should ( ) . Be ( "Project `Broken/Broken.csproj` is invalid." ) ;
2016-12-28 22:50:34 +00:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( HelpText ) ;
2016-12-07 20:56:27 +00:00
}
[Fact]
public void WhenMoreThanOneProjectExistsInTheDirectoryItPrintsErrorAndUsage ( )
{
var setup = Setup ( ) ;
2016-12-16 18:23:26 +00:00
var workingDir = Path . Combine ( setup . TestRoot , "MoreThanOne" ) ;
2016-12-07 20:56:27 +00:00
var cmd = new ListP2PsCommand ( )
2016-12-16 18:23:26 +00:00
. WithWorkingDirectory ( workingDir )
2016-12-07 20:56:27 +00:00
. Execute ( $"\" { setup . ValidRefCsprojRelToOtherProjPath } \ "" ) ;
cmd . ExitCode . Should ( ) . NotBe ( 0 ) ;
2016-12-16 18:23:26 +00:00
cmd . StdErr . Should ( ) . Be ( $"Found more than one project in `{workingDir + Path.DirectorySeparatorChar}`. Please specify which one to use." ) ;
2016-12-28 22:50:34 +00:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( HelpText ) ;
2016-12-07 20:56:27 +00:00
}
[Fact]
public void WhenNoProjectsExistsInTheDirectoryItPrintsErrorAndUsage ( )
{
var setup = Setup ( ) ;
var cmd = new ListP2PsCommand ( )
. WithWorkingDirectory ( setup . TestRoot )
. Execute ( $"\" { setup . ValidRefCsprojPath } \ "" ) ;
cmd . ExitCode . Should ( ) . NotBe ( 0 ) ;
2016-12-16 18:23:26 +00:00
cmd . StdErr . Should ( ) . Be ( $"Could not find any project in `{setup.TestRoot + Path.DirectorySeparatorChar}`." ) ;
2016-12-28 22:50:34 +00:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( HelpText ) ;
2016-12-07 20:56:27 +00:00
}
[Fact]
public void WhenNoProjectReferencesArePresentInTheProjectItPrintsError ( )
{
var lib = NewLib ( ) ;
var cmd = new ListP2PsCommand ( )
. WithProject ( lib . CsProjPath )
. Execute ( ) ;
cmd . Should ( ) . Pass ( ) ;
2016-12-16 18:23:26 +00:00
cmd . StdOut . Should ( ) . Be ( $"There are no Project to Project references in project {lib.CsProjPath}. ;; Project to Project is the type of the item being requested (project, package, p2p) and {lib.CsProjPath} is the object operated on (a project file or a solution file). " ) ;
2016-12-07 20:56:27 +00:00
}
[Fact]
public void ItPrintsSingleReference ( )
{
2016-12-16 18:23:26 +00:00
const string OutputText = @ "Project reference(s)
- - - - - - - - - - - - - - - - - - - -
. . \ ItPrintsSingleReferenceref \ ItPrintsSingleReferenceref . csproj ";
2016-12-14 00:31:35 +00:00
var lib = NewLib ( "ItPrintsSingleReference" , "lib" ) ;
string ref1 = NewLib ( "ItPrintsSingleReference" , "ref" ) . CsProjPath ;
AddValidRef ( ref1 , lib ) ;
2016-12-07 20:56:27 +00:00
var cmd = new ListP2PsCommand ( )
. WithProject ( lib . CsProjPath )
. Execute ( ) ;
cmd . Should ( ) . Pass ( ) ;
2016-12-28 22:50:34 +00:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( OutputText ) ;
2016-12-07 20:56:27 +00:00
}
[Fact]
public void ItPrintsMultipleReferences ( )
{
2016-12-16 18:23:26 +00:00
const string OutputText = @ "Project reference(s)
- - - - - - - - - - - - - - - - - - - -
. . \ ItPrintsSingleReferenceref1 \ ItPrintsSingleReferenceref1 . csproj
. . \ ItPrintsSingleReferenceref2 \ ItPrintsSingleReferenceref2 . csproj
. . \ ItPrintsSingleReferenceref3 \ ItPrintsSingleReferenceref3 . csproj ";
2016-12-14 00:31:35 +00:00
var lib = NewLib ( "ItPrintsSingleReference" , "lib" ) ;
string ref1 = NewLib ( "ItPrintsSingleReference" , "ref1" ) . CsProjPath ;
string ref2 = NewLib ( "ItPrintsSingleReference" , "ref2" ) . CsProjPath ;
string ref3 = NewLib ( "ItPrintsSingleReference" , "ref3" ) . CsProjPath ;
2016-12-07 20:56:27 +00:00
2016-12-14 00:31:35 +00:00
AddValidRef ( ref1 , lib ) ;
AddValidRef ( ref2 , lib ) ;
AddValidRef ( ref3 , lib ) ;
2016-12-07 20:56:27 +00:00
var cmd = new ListP2PsCommand ( )
. WithProject ( lib . CsProjPath )
. Execute ( ) ;
cmd . Should ( ) . Pass ( ) ;
2016-12-28 22:50:34 +00:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( OutputText ) ;
2016-12-07 20:56:27 +00:00
}
private TestSetup Setup ( [ System . Runtime . CompilerServices . CallerMemberName ] string callingMethod = nameof ( Setup ) , string identifier = "" )
{
return new TestSetup (
TestAssets . Get ( TestSetup . TestGroup , TestSetup . ProjectName )
. CreateInstance ( callingMethod : callingMethod , identifier : identifier )
. WithSourceFiles ( )
. Root
. FullName ) ;
}
private ProjDir NewDir ( [ System . Runtime . CompilerServices . CallerMemberName ] string callingMethod = nameof ( NewDir ) , string identifier = "" )
{
return new ProjDir ( TestAssetsManager . CreateTestDirectory ( callingMethod : callingMethod , identifier : identifier ) . Path ) ;
}
private ProjDir NewLib ( [ System . Runtime . CompilerServices . CallerMemberName ] string callingMethod = nameof ( NewDir ) , string identifier = "" )
{
var dir = NewDir ( callingMethod : callingMethod , identifier : identifier ) ;
try
{
new NewCommand ( )
. WithWorkingDirectory ( dir . Path )
. ExecuteWithCapturedOutput ( "-t Lib" )
. Should ( ) . Pass ( ) ;
}
catch ( System . ComponentModel . Win32Exception e )
{
throw new Exception ( $"Intermittent error in `dotnet new` occurred when running it in dir `{dir.Path}`\nException:\n{e}" ) ;
}
return dir ;
}
2016-12-14 00:31:35 +00:00
private void AddValidRef ( string path , ProjDir proj )
2016-12-07 20:56:27 +00:00
{
new AddP2PCommand ( )
. WithProject ( proj . CsProjPath )
2016-12-14 00:31:35 +00:00
. Execute ( $"\" { path } \ "" )
2016-12-07 20:56:27 +00:00
. Should ( ) . Pass ( ) ;
}
}
}