2016-12-07 12:56:27 -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.Build.Construction ;
2017-06-20 17:57:32 -07:00
using Microsoft.DotNet.Tools ;
2016-12-07 12:56:27 -08:00
using Microsoft.DotNet.Tools.Test.Utilities ;
using Msbuild.Tests.Utilities ;
using System ;
using System.IO ;
using Xunit ;
2017-01-06 14:25:23 -08:00
namespace Microsoft.DotNet.Cli.List.Reference.Tests
2016-12-07 12:56:27 -08:00
{
2017-01-06 14:25:23 -08:00
public class GivenDotnetListReference : TestBase
2016-12-07 12:56:27 -08:00
{
2017-06-26 17:17:51 -07:00
private const string HelpText = @ "Usage: dotnet list <PROJECT> reference [options]
2016-12-16 10:23:26 -08:00
Arguments :
2017-03-22 15:26:58 -07: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 10:23:26 -08:00
Options :
2018-04-30 13:42:47 -07:00
- h , - - help Show command line help . ";
2016-12-16 10:23:26 -08:00
2017-06-26 17:17:51 -07:00
private const string ListCommandHelpText = @ "Usage: dotnet list [options] <PROJECT> [command]
2017-05-04 01:01:35 +02:00
Arguments :
< PROJECT > The project file to operate on . If a file is not specified , the command will search the current directory for one .
Options :
2018-04-30 13:42:47 -07:00
- h , - - help Show command line help .
2017-05-04 01:01:35 +02:00
Commands :
2018-04-30 13:42:47 -07:00
reference List all project - to - project references of the project . ";
2017-05-04 01:01:35 +02:00
2016-12-07 12:56:27 -08: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 14:25:23 -08:00
var cmd = new ListReferenceCommand ( ) . Execute ( helpArg ) ;
2016-12-07 12:56:27 -08:00
cmd . Should ( ) . Pass ( ) ;
2017-06-20 17:57:32 -07:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentToIfNotLocalized ( HelpText ) ;
2016-12-16 10:23:26 -08:00
}
[Theory]
[InlineData("")]
[InlineData("unknownCommandName")]
public void WhenNoCommandIsPassedItPrintsError ( string commandName )
{
var cmd = new DotnetCommand ( )
. ExecuteWithCapturedOutput ( $"list {commandName}" ) ;
cmd . Should ( ) . Fail ( ) ;
2017-06-20 17:57:32 -07:00
cmd . StdErr . Should ( ) . Be ( CommonLocalizableStrings . RequiredCommandNotPassed ) ;
cmd . StdOut . Should ( ) . BeVisuallyEquivalentToIfNotLocalized ( ListCommandHelpText ) ;
2016-12-07 12:56:27 -08:00
}
2016-12-13 14:31:35 -10:00
[Fact]
public void WhenTooManyArgumentsArePassedItPrintsError ( )
{
2017-03-15 19:57:03 -07:00
var cmd = new ListReferenceCommand ( )
2016-12-13 14:31:35 -10:00
. WithProject ( "one two three" )
. Execute ( "proj.csproj" ) ;
cmd . ExitCode . Should ( ) . NotBe ( 0 ) ;
2017-06-27 13:26:14 -07:00
cmd . StdErr . Should ( ) . BeVisuallyEquivalentTo ( $@"{string.Format(CommandLine.LocalizableStrings.UnrecognizedCommandOrArgument, " two ")}
{ string . Format ( CommandLine . LocalizableStrings . UnrecognizedCommandOrArgument , "three" ) } ");
2016-12-13 14:31:35 -10:00
}
2016-12-07 12:56:27 -08:00
[Theory]
[InlineData("idontexist.csproj")]
[InlineData("ihave?inv@lid/char\\acters")]
public void WhenNonExistingProjectIsPassedItPrintsErrorAndUsage ( string projName )
{
var setup = Setup ( ) ;
2017-01-06 14:25:23 -08:00
var cmd = new ListReferenceCommand ( )
2016-12-07 12:56:27 -08:00
. WithWorkingDirectory ( setup . TestRoot )
. WithProject ( projName )
. Execute ( $"\" { setup . ValidRefCsprojPath } \ "" ) ;
cmd . ExitCode . Should ( ) . NotBe ( 0 ) ;
2017-06-20 17:57:32 -07:00
cmd . StdErr . Should ( ) . Be ( string . Format ( CommonLocalizableStrings . CouldNotFindProjectOrDirectory , projName ) ) ;
cmd . StdOut . Should ( ) . BeVisuallyEquivalentToIfNotLocalized ( HelpText ) ;
2016-12-07 12:56:27 -08:00
}
[Fact]
public void WhenBrokenProjectIsPassedItPrintsErrorAndUsage ( )
{
string projName = "Broken/Broken.csproj" ;
var setup = Setup ( ) ;
2017-01-06 14:25:23 -08:00
var cmd = new ListReferenceCommand ( )
2016-12-07 12:56:27 -08:00
. WithWorkingDirectory ( setup . TestRoot )
. WithProject ( projName )
. Execute ( $"\" { setup . ValidRefCsprojPath } \ "" ) ;
cmd . ExitCode . Should ( ) . NotBe ( 0 ) ;
2017-06-20 17:57:32 -07:00
cmd . StdErr . Should ( ) . Be ( string . Format ( CommonLocalizableStrings . ProjectIsInvalid , "Broken/Broken.csproj" ) ) ;
cmd . StdOut . Should ( ) . BeVisuallyEquivalentToIfNotLocalized ( HelpText ) ;
2016-12-07 12:56:27 -08:00
}
[Fact]
public void WhenMoreThanOneProjectExistsInTheDirectoryItPrintsErrorAndUsage ( )
{
var setup = Setup ( ) ;
2016-12-16 10:23:26 -08:00
var workingDir = Path . Combine ( setup . TestRoot , "MoreThanOne" ) ;
2017-01-06 14:25:23 -08:00
var cmd = new ListReferenceCommand ( )
2016-12-16 10:23:26 -08:00
. WithWorkingDirectory ( workingDir )
2016-12-07 12:56:27 -08:00
. Execute ( $"\" { setup . ValidRefCsprojRelToOtherProjPath } \ "" ) ;
cmd . ExitCode . Should ( ) . NotBe ( 0 ) ;
2017-06-20 17:57:32 -07:00
cmd . StdErr . Should ( ) . Be ( string . Format ( CommonLocalizableStrings . MoreThanOneProjectInDirectory , workingDir + Path . DirectorySeparatorChar ) ) ;
cmd . StdOut . Should ( ) . BeVisuallyEquivalentToIfNotLocalized ( HelpText ) ;
2016-12-07 12:56:27 -08:00
}
[Fact]
public void WhenNoProjectsExistsInTheDirectoryItPrintsErrorAndUsage ( )
{
var setup = Setup ( ) ;
2017-01-06 14:25:23 -08:00
var cmd = new ListReferenceCommand ( )
2016-12-07 12:56:27 -08:00
. WithWorkingDirectory ( setup . TestRoot )
. Execute ( $"\" { setup . ValidRefCsprojPath } \ "" ) ;
cmd . ExitCode . Should ( ) . NotBe ( 0 ) ;
2017-06-20 17:57:32 -07:00
cmd . StdErr . Should ( ) . Be ( string . Format ( CommonLocalizableStrings . CouldNotFindAnyProjectInDirectory , setup . TestRoot + Path . DirectorySeparatorChar ) ) ;
cmd . StdOut . Should ( ) . BeVisuallyEquivalentToIfNotLocalized ( HelpText ) ;
2016-12-07 12:56:27 -08:00
}
[Fact]
public void WhenNoProjectReferencesArePresentInTheProjectItPrintsError ( )
{
var lib = NewLib ( ) ;
2017-01-06 14:25:23 -08:00
var cmd = new ListReferenceCommand ( )
2016-12-07 12:56:27 -08:00
. WithProject ( lib . CsProjPath )
. Execute ( ) ;
cmd . Should ( ) . Pass ( ) ;
2017-06-20 17:57:32 -07:00
cmd . StdOut . Should ( ) . Be ( string . Format ( CommonLocalizableStrings . NoReferencesFound , CommonLocalizableStrings . P2P , lib . CsProjPath ) ) ;
2016-12-07 12:56:27 -08:00
}
[Fact]
public void ItPrintsSingleReference ( )
{
2017-06-20 17:57:32 -07:00
string OutputText = CommonLocalizableStrings . ProjectReferenceOneOrMore ;
OutputText + = $ @ "
{ new string ( '-' , OutputText . Length ) }
2017-02-15 15:35:03 -08:00
. . \ ref \ ref . csproj ";
2016-12-16 10:23:26 -08:00
2017-02-15 15:35:03 -08:00
var lib = NewLib ( "lib" ) ;
string ref1 = NewLib ( "ref" ) . CsProjPath ;
2016-12-13 14:31:35 -10:00
AddValidRef ( ref1 , lib ) ;
2016-12-07 12:56:27 -08:00
2017-01-06 14:25:23 -08:00
var cmd = new ListReferenceCommand ( )
2016-12-07 12:56:27 -08:00
. WithProject ( lib . CsProjPath )
. Execute ( ) ;
cmd . Should ( ) . Pass ( ) ;
2016-12-28 14:50:34 -08:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( OutputText ) ;
2016-12-07 12:56:27 -08:00
}
[Fact]
public void ItPrintsMultipleReferences ( )
{
2017-06-20 17:57:32 -07:00
string OutputText = CommonLocalizableStrings . ProjectReferenceOneOrMore ;
OutputText + = $ @ "
{ new string ( '-' , OutputText . Length ) }
2017-02-15 15:35:03 -08:00
. . \ ref1 \ ref1 . csproj
. . \ ref2 \ ref2 . csproj
. . \ ref3 \ ref3 . csproj ";
2016-12-16 10:23:26 -08:00
2017-02-15 15:35:03 -08:00
var lib = NewLib ( "lib" ) ;
string ref1 = NewLib ( "ref1" ) . CsProjPath ;
string ref2 = NewLib ( "ref2" ) . CsProjPath ;
string ref3 = NewLib ( "ref3" ) . CsProjPath ;
2016-12-07 12:56:27 -08:00
2016-12-13 14:31:35 -10:00
AddValidRef ( ref1 , lib ) ;
AddValidRef ( ref2 , lib ) ;
AddValidRef ( ref3 , lib ) ;
2016-12-07 12:56:27 -08:00
2017-01-06 14:25:23 -08:00
var cmd = new ListReferenceCommand ( )
2016-12-07 12:56:27 -08:00
. WithProject ( lib . CsProjPath )
. Execute ( ) ;
cmd . Should ( ) . Pass ( ) ;
2016-12-28 14:50:34 -08:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( OutputText ) ;
2016-12-07 12:56:27 -08: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 15:35:03 -08:00
private ProjDir NewDir ( string testProjectName = "temp" , [ System . Runtime . CompilerServices . CallerMemberName ] string callingMethod = nameof ( NewDir ) , string identifier = "" )
2016-12-07 12:56:27 -08:00
{
2017-02-15 15:35:03 -08:00
return new ProjDir ( TestAssets . CreateTestDirectory ( testProjectName : testProjectName , callingMethod : callingMethod , identifier : identifier ) . FullName ) ;
2016-12-07 12:56:27 -08:00
}
2017-02-15 15:35:03 -08:00
private ProjDir NewLib ( string testProjectName = "temp" , [ System . Runtime . CompilerServices . CallerMemberName ] string callingMethod = nameof ( NewDir ) , string identifier = "" )
2016-12-07 12:56:27 -08:00
{
2017-02-15 15:35:03 -08:00
var dir = NewDir ( testProjectName : testProjectName , callingMethod : callingMethod , identifier : identifier ) ;
2016-12-07 12:56:27 -08:00
try
{
2017-07-10 15:57:30 -07:00
string newArgs = $"classlib -o \" { dir . Path } \ " --debug:ephemeral-hive --no-restore" ;
2017-01-31 17:31:37 -08:00
new NewCommandShim ( )
2016-12-07 12:56:27 -08:00
. WithWorkingDirectory ( dir . Path )
2017-01-31 17:31:37 -08:00
. ExecuteWithCapturedOutput ( newArgs )
2016-12-07 12:56:27 -08: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-13 14:31:35 -10:00
private void AddValidRef ( string path , ProjDir proj )
2016-12-07 12:56:27 -08:00
{
2017-01-06 14:08:50 -08:00
new AddReferenceCommand ( )
2016-12-07 12:56:27 -08:00
. WithProject ( proj . CsProjPath )
2016-12-13 14:31:35 -10:00
. Execute ( $"\" { path } \ "" )
2016-12-07 12:56:27 -08:00
. Should ( ) . Pass ( ) ;
}
}
}