2016-12-16 06:41:47 -10: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.Cli.Sln.Internal ;
2017-06-13 21:13:47 -07:00
using Microsoft.DotNet.Tools ;
2016-12-16 06:41:47 -10:00
using Microsoft.DotNet.Tools.Test.Utilities ;
using System ;
using System.IO ;
using System.Linq ;
using Xunit ;
2017-01-06 10:58:23 -10:00
namespace Microsoft.DotNet.Cli.Sln.List.Tests
2016-12-16 06:41:47 -10:00
{
2017-01-06 10:58:23 -10:00
public class GivenDotnetSlnList : TestBase
2016-12-16 06:41:47 -10:00
{
2017-06-26 17:17:51 -07:00
private const string HelpText = @ "Usage: dotnet sln <SLN_FILE> list [options]
2016-12-16 06:41:47 -10:00
Arguments :
2017-03-22 15:26:58 -07:00
< SLN_FILE > Solution file to operate on . If not specified , the command will search the current directory for one .
2016-12-16 06:41:47 -10:00
Options :
2017-05-09 21:05:09 -07:00
- h , - - help Show help information .
2017-03-14 11:23:19 -07:00
";
2016-12-16 06:41:47 -10:00
2017-06-26 17:17:51 -07:00
private const string SlnCommandHelpText = @ "Usage: dotnet sln [options] <SLN_FILE> [command]
2017-05-04 01:01:35 +02:00
Arguments :
< SLN_FILE > Solution file to operate on . If not specified , the command will search the current directory for one .
Options :
2017-05-09 21:05:09 -07:00
- h , - - help Show help information .
2017-05-04 01:01:35 +02:00
Commands :
add < args > . NET Add project ( s ) to a solution file Command
list . NET List project ( s ) in a solution file Command
remove < args > . NET Remove project ( s ) from a solution file Command
";
2016-12-16 06:41:47 -10:00
[Theory]
[InlineData("--help")]
[InlineData("-h")]
public void WhenHelpOptionIsPassedItPrintsUsage ( string helpArg )
{
var cmd = new DotnetCommand ( )
2017-01-06 10:58:23 -10:00
. ExecuteWithCapturedOutput ( $"sln list {helpArg}" ) ;
2016-12-16 06:41:47 -10:00
cmd . Should ( ) . Pass ( ) ;
2017-06-13 21:13:47 -07:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentToIfNotLocalized ( HelpText ) ;
2016-12-16 06:41:47 -10:00
}
[Theory]
[InlineData("")]
[InlineData("unknownCommandName")]
public void WhenNoCommandIsPassedItPrintsError ( string commandName )
{
var cmd = new DotnetCommand ( )
2017-01-06 10:58:23 -10:00
. ExecuteWithCapturedOutput ( $"sln {commandName}" ) ;
2016-12-16 06:41:47 -10:00
cmd . Should ( ) . Fail ( ) ;
2017-06-13 21:13:47 -07:00
cmd . StdErr . Should ( ) . Be ( CommonLocalizableStrings . RequiredCommandNotPassed ) ;
cmd . StdOut . Should ( ) . BeVisuallyEquivalentToIfNotLocalized ( SlnCommandHelpText ) ;
2016-12-16 06:41:47 -10:00
}
[Fact]
public void WhenTooManyArgumentsArePassedItPrintsError ( )
{
var cmd = new DotnetCommand ( )
2017-01-06 10:58:23 -10:00
. ExecuteWithCapturedOutput ( "sln one.sln two.sln three.sln list" ) ;
2016-12-16 06:41:47 -10:00
cmd . Should ( ) . Fail ( ) ;
2017-06-27 13:26:14 -07:00
cmd . StdErr . Should ( ) . BeVisuallyEquivalentTo ( $@"{string.Format(CommandLine.LocalizableStrings.UnrecognizedCommandOrArgument, " two . sln ")}
{ string . Format ( CommandLine . LocalizableStrings . UnrecognizedCommandOrArgument , "three.sln" ) } ");
2016-12-16 06:41:47 -10:00
}
[Theory]
[InlineData("idontexist.sln")]
[InlineData("ihave?invalidcharacters.sln")]
[InlineData("ihaveinv@lidcharacters.sln")]
[InlineData("ihaveinvalid/characters")]
[InlineData("ihaveinvalidchar\\acters")]
public void WhenNonExistingSolutionIsPassedItPrintsErrorAndUsage ( string solutionName )
{
var cmd = new DotnetCommand ( )
2017-01-06 10:58:23 -10:00
. ExecuteWithCapturedOutput ( $"sln {solutionName} list" ) ;
2016-12-16 06:41:47 -10:00
cmd . Should ( ) . Fail ( ) ;
2017-06-13 21:13:47 -07:00
cmd . StdErr . Should ( ) . Be ( string . Format ( CommonLocalizableStrings . CouldNotFindSolutionOrDirectory , solutionName ) ) ;
cmd . StdOut . Should ( ) . BeVisuallyEquivalentToIfNotLocalized ( HelpText ) ;
2016-12-16 06:41:47 -10:00
}
[Fact]
public void WhenInvalidSolutionIsPassedItPrintsErrorAndUsage ( )
{
var projectDirectory = TestAssets
. Get ( "InvalidSolution" )
. CreateInstance ( )
. WithSourceFiles ( )
. Root
. FullName ;
var cmd = new DotnetCommand ( )
. WithWorkingDirectory ( projectDirectory )
2017-01-06 10:58:23 -10:00
. ExecuteWithCapturedOutput ( "sln InvalidSolution.sln list" ) ;
2016-12-16 06:41:47 -10:00
cmd . Should ( ) . Fail ( ) ;
2017-06-13 21:13:47 -07:00
cmd . StdErr . Should ( ) . Be ( string . Format ( CommonLocalizableStrings . InvalidSolutionFormatString , "InvalidSolution.sln" , LocalizableStrings . FileHeaderMissingError ) ) ;
cmd . StdOut . Should ( ) . BeVisuallyEquivalentToIfNotLocalized ( HelpText ) ;
2016-12-16 06:41:47 -10:00
}
[Fact]
public void WhenInvalidSolutionIsFoundItPrintsErrorAndUsage ( )
{
var projectDirectory = TestAssets
. Get ( "InvalidSolution" )
. CreateInstance ( )
. WithSourceFiles ( )
. Root
. FullName ;
var solutionFullPath = Path . Combine ( projectDirectory , "InvalidSolution.sln" ) ;
var cmd = new DotnetCommand ( )
. WithWorkingDirectory ( projectDirectory )
2017-01-06 10:58:23 -10:00
. ExecuteWithCapturedOutput ( "sln list" ) ;
2016-12-16 06:41:47 -10:00
cmd . Should ( ) . Fail ( ) ;
2017-06-13 21:13:47 -07:00
cmd . StdErr . Should ( ) . Be ( string . Format ( CommonLocalizableStrings . InvalidSolutionFormatString , solutionFullPath , LocalizableStrings . FileHeaderMissingError ) ) ;
cmd . StdOut . Should ( ) . BeVisuallyEquivalentToIfNotLocalized ( HelpText ) ;
2016-12-16 06:41:47 -10:00
}
[Fact]
public void WhenNoSolutionExistsInTheDirectoryItPrintsErrorAndUsage ( )
{
var projectDirectory = TestAssets
. Get ( "TestAppWithSlnAndCsprojFiles" )
. CreateInstance ( )
. WithSourceFiles ( )
. Root
. FullName ;
var solutionDir = Path . Combine ( projectDirectory , "App" ) ;
var cmd = new DotnetCommand ( )
. WithWorkingDirectory ( solutionDir )
2017-01-06 10:58:23 -10:00
. ExecuteWithCapturedOutput ( "sln list" ) ;
2016-12-16 06:41:47 -10:00
cmd . Should ( ) . Fail ( ) ;
2017-06-13 21:13:47 -07:00
cmd . StdErr . Should ( ) . Be ( string . Format ( CommonLocalizableStrings . SolutionDoesNotExist , solutionDir + Path . DirectorySeparatorChar ) ) ;
cmd . StdOut . Should ( ) . BeVisuallyEquivalentToIfNotLocalized ( HelpText ) ;
2016-12-16 06:41:47 -10:00
}
[Fact]
public void WhenMoreThanOneSolutionExistsInTheDirectoryItPrintsErrorAndUsage ( )
{
var projectDirectory = TestAssets
. Get ( "TestAppWithMultipleSlnFiles" )
. CreateInstance ( )
. WithSourceFiles ( )
. Root
. FullName ;
var cmd = new DotnetCommand ( )
. WithWorkingDirectory ( projectDirectory )
2017-01-06 10:58:23 -10:00
. ExecuteWithCapturedOutput ( "sln list" ) ;
2016-12-16 06:41:47 -10:00
cmd . Should ( ) . Fail ( ) ;
2017-06-13 21:13:47 -07:00
cmd . StdErr . Should ( ) . Be ( string . Format ( CommonLocalizableStrings . MoreThanOneSolutionInDirectory , projectDirectory + Path . DirectorySeparatorChar ) ) ;
cmd . StdOut . Should ( ) . BeVisuallyEquivalentToIfNotLocalized ( HelpText ) ;
2016-12-16 06:41:47 -10:00
}
[Fact]
public void WhenNoProjectReferencesArePresentInTheSolutionItPrintsANoProjectMessage ( )
{
var projectDirectory = TestAssets
2017-01-03 07:18:45 -10:00
. Get ( "TestAppWithEmptySln" )
2016-12-16 06:41:47 -10:00
. CreateInstance ( )
. WithSourceFiles ( )
. Root
. FullName ;
var cmd = new DotnetCommand ( )
. WithWorkingDirectory ( projectDirectory )
2017-01-06 10:58:23 -10:00
. ExecuteWithCapturedOutput ( "sln list" ) ;
2016-12-16 06:41:47 -10:00
cmd . Should ( ) . Pass ( ) ;
2017-06-13 21:13:47 -07:00
cmd . StdOut . Should ( ) . Be ( CommonLocalizableStrings . NoProjectsFound ) ;
2016-12-16 06:41:47 -10:00
}
[Fact]
public void WhenProjectReferencesArePresentInTheSolutionItListsThem ( )
{
2017-06-13 21:13:47 -07:00
string OutputText = CommonLocalizableStrings . ProjectReferenceOneOrMore ;
OutputText + = $ @ "
{ new string ( '-' , OutputText . Length ) }
2016-12-20 13:04:01 -10:00
{ Path . Combine ( "App" , "App.csproj" ) }
{ Path . Combine ( "Lib" , "Lib.csproj" ) } ";
2016-12-16 10:23:26 -08:00
2016-12-16 06:41:47 -10:00
var projectDirectory = TestAssets
. Get ( "TestAppWithSlnAndExistingCsprojReferences" )
. CreateInstance ( )
. WithSourceFiles ( )
. Root
. FullName ;
var cmd = new DotnetCommand ( )
. WithWorkingDirectory ( projectDirectory )
2017-01-06 10:58:23 -10:00
. ExecuteWithCapturedOutput ( "sln list" ) ;
2016-12-16 06:41:47 -10:00
cmd . Should ( ) . Pass ( ) ;
2016-12-28 14:50:34 -08:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( OutputText ) ;
2016-12-16 06:41:47 -10:00
}
}
}