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 ;
2017-01-06 22:25:23 +00:00
namespace Microsoft.DotNet.Cli.List.Reference.Tests
2016-12-07 20:56:27 +00:00
{
2017-01-06 22:25:23 +00:00
public class GivenDotnetListReference : TestBase
2016-12-07 20:56:27 +00:00
{
2016-12-16 18:23:26 +00:00
private const string HelpText = @ ".NET Core Project-to-Project dependency viewer
2017-01-06 22:25:23 +00:00
Usage : dotnet list < PROJECT > reference [ options ]
2016-12-16 18:23:26 +00:00
Arguments :
2017-01-06 20:58:23 +00:00
< PROJECT > The project file to operate on . If a file is not specified , the command will search the current directory for one .
2016-12-16 18:23:26 +00:00
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 )
{
2017-01-06 22:25:23 +00:00
var cmd = new ListReferenceCommand ( ) . Execute ( helpArg ) ;
2016-12-07 20:56:27 +00:00
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 ( )
{
2017-01-06 22:08:50 +00:00
var cmd = new AddReferenceCommand ( )
2016-12-14 00:31:35 +00:00
. 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 ( ) ;
2017-01-06 22:25:23 +00:00
var cmd = new ListReferenceCommand ( )
2016-12-07 20:56:27 +00:00
. 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 ( ) ;
2017-01-06 22:25:23 +00:00
var cmd = new ListReferenceCommand ( )
2016-12-07 20:56:27 +00:00
. 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" ) ;
2017-01-06 22:25:23 +00:00
var cmd = new ListReferenceCommand ( )
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 ( ) ;
2017-01-06 22:25:23 +00:00
var cmd = new ListReferenceCommand ( )
2016-12-07 20:56:27 +00:00
. 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 ( ) ;
2017-01-06 22:25:23 +00:00
var cmd = new ListReferenceCommand ( )
2016-12-07 20:56:27 +00:00
. 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)
- - - - - - - - - - - - - - - - - - - -
2017-02-15 23:35:03 +00:00
. . \ ref \ ref . csproj ";
2016-12-16 18:23:26 +00:00
2017-02-15 23:35:03 +00:00
var lib = NewLib ( "lib" ) ;
string ref1 = NewLib ( "ref" ) . CsProjPath ;
2016-12-14 00:31:35 +00:00
AddValidRef ( ref1 , lib ) ;
2016-12-07 20:56:27 +00:00
2017-01-06 22:25:23 +00:00
var cmd = new ListReferenceCommand ( )
2016-12-07 20:56:27 +00:00
. 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)
- - - - - - - - - - - - - - - - - - - -
2017-02-15 23:35:03 +00:00
. . \ ref1 \ ref1 . csproj
. . \ ref2 \ ref2 . csproj
. . \ ref3 \ ref3 . csproj ";
2016-12-16 18:23:26 +00:00
2017-02-15 23:35:03 +00:00
var lib = NewLib ( "lib" ) ;
string ref1 = NewLib ( "ref1" ) . CsProjPath ;
string ref2 = NewLib ( "ref2" ) . CsProjPath ;
string ref3 = NewLib ( "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
2017-01-06 22:25:23 +00:00
var cmd = new ListReferenceCommand ( )
2016-12-07 20:56:27 +00:00
. 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 ) ;
}
2017-02-15 23:35:03 +00:00
private ProjDir NewDir ( string testProjectName = "temp" , [ System . Runtime . CompilerServices . CallerMemberName ] string callingMethod = nameof ( NewDir ) , string identifier = "" )
2016-12-07 20:56:27 +00:00
{
2017-02-15 23:35:03 +00:00
return new ProjDir ( TestAssets . CreateTestDirectory ( testProjectName : testProjectName , callingMethod : callingMethod , identifier : identifier ) . FullName ) ;
2016-12-07 20:56:27 +00:00
}
2017-02-15 23:35:03 +00:00
private ProjDir NewLib ( string testProjectName = "temp" , [ System . Runtime . CompilerServices . CallerMemberName ] string callingMethod = nameof ( NewDir ) , string identifier = "" )
2016-12-07 20:56:27 +00:00
{
2017-02-15 23:35:03 +00:00
var dir = NewDir ( testProjectName : testProjectName , callingMethod : callingMethod , identifier : identifier ) ;
2016-12-07 20:56:27 +00:00
try
{
2017-02-01 01:31:37 +00:00
string newArgs = $"classlib -o \" { dir . Path } \ " --debug:ephemeral-hive" ;
new NewCommandShim ( )
2016-12-07 20:56:27 +00:00
. WithWorkingDirectory ( dir . Path )
2017-02-01 01:31:37 +00:00
. ExecuteWithCapturedOutput ( newArgs )
2016-12-07 20:56:27 +00:00
. 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
{
2017-01-06 22:08:50 +00:00
new AddReferenceCommand ( )
2016-12-07 20:56:27 +00:00
. WithProject ( proj . CsProjPath )
2016-12-14 00:31:35 +00:00
. Execute ( $"\" { path } \ "" )
2016-12-07 20:56:27 +00:00
. Should ( ) . Pass ( ) ;
}
}
}