2016-12-16 16:41:47 +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.DotNet.Cli.Sln.Internal ;
using Microsoft.DotNet.Tools.Test.Utilities ;
using System ;
using System.IO ;
using System.Linq ;
using Xunit ;
2017-01-06 20:58:23 +00:00
namespace Microsoft.DotNet.Cli.Sln.List.Tests
2016-12-16 16:41:47 +00:00
{
2017-01-06 20:58:23 +00:00
public class GivenDotnetSlnList : TestBase
2016-12-16 16:41:47 +00:00
{
2017-01-06 20:58:23 +00:00
private const string HelpText = @ ".NET List project(s) in a solution file Command
2016-12-16 16:41:47 +00:00
2017-01-06 20:58:23 +00:00
Usage : dotnet sln < SLN_FILE > list [ options ]
2016-12-16 16:41:47 +00:00
Arguments :
2017-03-14 18:23:19 +00:00
< SLN_FILE > Solution file to operate on . If not specified , the command will search the current directory for one .
2016-12-16 16:41:47 +00:00
Options :
2017-03-14 18:23:19 +00:00
- h , - - help Show help information
";
2016-12-16 16:41:47 +00:00
[Theory]
[InlineData("--help")]
[InlineData("-h")]
public void WhenHelpOptionIsPassedItPrintsUsage ( string helpArg )
{
var cmd = new DotnetCommand ( )
2017-01-06 20:58:23 +00:00
. ExecuteWithCapturedOutput ( $"sln list {helpArg}" ) ;
2016-12-16 16:41:47 +00:00
cmd . Should ( ) . Pass ( ) ;
2016-12-28 22:50:34 +00:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( HelpText ) ;
2016-12-16 16:41:47 +00:00
}
[Theory]
[InlineData("")]
[InlineData("unknownCommandName")]
public void WhenNoCommandIsPassedItPrintsError ( string commandName )
{
var cmd = new DotnetCommand ( )
2017-01-06 20:58:23 +00:00
. ExecuteWithCapturedOutput ( $"sln {commandName}" ) ;
2016-12-16 16:41:47 +00:00
cmd . Should ( ) . Fail ( ) ;
2016-12-16 18:23:26 +00:00
cmd . StdErr . Should ( ) . Be ( "Required command was not provided." ) ;
2016-12-16 16:41:47 +00:00
}
[Fact]
public void WhenTooManyArgumentsArePassedItPrintsError ( )
{
var cmd = new DotnetCommand ( )
2017-01-06 20:58:23 +00:00
. ExecuteWithCapturedOutput ( "sln one.sln two.sln three.sln list" ) ;
2016-12-16 16:41:47 +00:00
cmd . Should ( ) . Fail ( ) ;
2017-03-16 20:22:08 +00:00
cmd . StdErr . Should ( ) . BeVisuallyEquivalentTo ( "Unrecognized command or argument 'two.sln'\r\nUnrecognized command or argument 'three.sln'" ) ;
2016-12-16 16:41:47 +00: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 20:58:23 +00:00
. ExecuteWithCapturedOutput ( $"sln {solutionName} list" ) ;
2016-12-16 16:41:47 +00:00
cmd . Should ( ) . Fail ( ) ;
2016-12-16 18:23:26 +00:00
cmd . StdErr . Should ( ) . Be ( $"Could not find solution or directory `{solutionName}`." ) ;
2016-12-28 22:50:34 +00:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( HelpText ) ;
2016-12-16 16:41:47 +00:00
}
[Fact]
public void WhenInvalidSolutionIsPassedItPrintsErrorAndUsage ( )
{
var projectDirectory = TestAssets
. Get ( "InvalidSolution" )
. CreateInstance ( )
. WithSourceFiles ( )
. Root
. FullName ;
var cmd = new DotnetCommand ( )
. WithWorkingDirectory ( projectDirectory )
2017-01-06 20:58:23 +00:00
. ExecuteWithCapturedOutput ( "sln InvalidSolution.sln list" ) ;
2016-12-16 16:41:47 +00:00
cmd . Should ( ) . Fail ( ) ;
2016-12-29 19:21:55 +00:00
cmd . StdErr . Should ( ) . Be ( "Invalid solution `InvalidSolution.sln`. Invalid format in line 1: File header is missing" ) ;
2016-12-28 22:50:34 +00:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( HelpText ) ;
2016-12-16 16:41:47 +00: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 20:58:23 +00:00
. ExecuteWithCapturedOutput ( "sln list" ) ;
2016-12-16 16:41:47 +00:00
cmd . Should ( ) . Fail ( ) ;
2016-12-29 19:21:55 +00:00
cmd . StdErr . Should ( ) . Be ( $"Invalid solution `{solutionFullPath}`. Invalid format in line 1: File header is missing" ) ;
2016-12-28 22:50:34 +00:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( HelpText ) ;
2016-12-16 16:41:47 +00: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 20:58:23 +00:00
. ExecuteWithCapturedOutput ( "sln list" ) ;
2016-12-16 16:41:47 +00:00
cmd . Should ( ) . Fail ( ) ;
2016-12-16 18:23:26 +00:00
cmd . StdErr . Should ( ) . Be ( $"Specified solution file {solutionDir + Path.DirectorySeparatorChar} does not exist, or there is no solution file in the directory." ) ;
2016-12-28 22:50:34 +00:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( HelpText ) ;
2016-12-16 16:41:47 +00:00
}
[Fact]
public void WhenMoreThanOneSolutionExistsInTheDirectoryItPrintsErrorAndUsage ( )
{
var projectDirectory = TestAssets
. Get ( "TestAppWithMultipleSlnFiles" )
. CreateInstance ( )
. WithSourceFiles ( )
. Root
. FullName ;
var cmd = new DotnetCommand ( )
. WithWorkingDirectory ( projectDirectory )
2017-01-06 20:58:23 +00:00
. ExecuteWithCapturedOutput ( "sln list" ) ;
2016-12-16 16:41:47 +00:00
cmd . Should ( ) . Fail ( ) ;
2016-12-16 18:23:26 +00:00
cmd . StdErr . Should ( ) . Be ( $"Found more than one solution file in {projectDirectory + Path.DirectorySeparatorChar}. Please specify which one to use." ) ;
2016-12-28 22:50:34 +00:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( HelpText ) ;
2016-12-16 16:41:47 +00:00
}
[Fact]
public void WhenNoProjectReferencesArePresentInTheSolutionItPrintsANoProjectMessage ( )
{
var projectDirectory = TestAssets
2017-01-03 17:18:45 +00:00
. Get ( "TestAppWithEmptySln" )
2016-12-16 16:41:47 +00:00
. CreateInstance ( )
. WithSourceFiles ( )
. Root
. FullName ;
var cmd = new DotnetCommand ( )
. WithWorkingDirectory ( projectDirectory )
2017-01-06 20:58:23 +00:00
. ExecuteWithCapturedOutput ( "sln list" ) ;
2016-12-16 16:41:47 +00:00
cmd . Should ( ) . Pass ( ) ;
2016-12-16 18:23:26 +00:00
cmd . StdOut . Should ( ) . Be ( "No projects found in the solution." ) ;
2016-12-16 16:41:47 +00:00
}
[Fact]
public void WhenProjectReferencesArePresentInTheSolutionItListsThem ( )
{
2016-12-20 23:04:01 +00:00
string OutputText = $ @ "Project reference(s)
2016-12-16 18:23:26 +00:00
- - - - - - - - - - - - - - - - - - - -
2016-12-20 23:04:01 +00:00
{ Path . Combine ( "App" , "App.csproj" ) }
{ Path . Combine ( "Lib" , "Lib.csproj" ) } ";
2016-12-16 18:23:26 +00:00
2016-12-16 16:41:47 +00:00
var projectDirectory = TestAssets
. Get ( "TestAppWithSlnAndExistingCsprojReferences" )
. CreateInstance ( )
. WithSourceFiles ( )
. Root
. FullName ;
var cmd = new DotnetCommand ( )
. WithWorkingDirectory ( projectDirectory )
2017-01-06 20:58:23 +00:00
. ExecuteWithCapturedOutput ( "sln list" ) ;
2016-12-16 16:41:47 +00:00
cmd . Should ( ) . Pass ( ) ;
2016-12-28 22:50:34 +00:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( OutputText ) ;
2016-12-16 16:41:47 +00:00
}
}
}