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 ;
namespace Microsoft.DotNet.Cli.List.Proj.Tests
{
public class GivenDotnetListProj : TestBase
{
private const string HelpText = @ ".NET Projects in Solution viewer
Usage : dotnet list < PROJECT_OR_SOLUTION > projects [ 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 ";
[Theory]
[InlineData("--help")]
[InlineData("-h")]
public void WhenHelpOptionIsPassedItPrintsUsage ( string helpArg )
{
var cmd = new DotnetCommand ( )
. ExecuteWithCapturedOutput ( $"list projects {helpArg}" ) ;
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 ( )
. ExecuteWithCapturedOutput ( $"list {commandName}" ) ;
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 ( )
. ExecuteWithCapturedOutput ( "list one.sln two.sln three.sln projects" ) ;
cmd . Should ( ) . Fail ( ) ;
2016-12-16 18:23:26 +00:00
cmd . StdErr . Should ( ) . Be ( "Unrecognized command or argument 'two.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 ( )
. ExecuteWithCapturedOutput ( $"list {solutionName} projects" ) ;
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 )
. ExecuteWithCapturedOutput ( "list InvalidSolution.sln projects" ) ;
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 )
. ExecuteWithCapturedOutput ( "list projects" ) ;
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 )
. ExecuteWithCapturedOutput ( "list projects" ) ;
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 )
. ExecuteWithCapturedOutput ( "list projects" ) ;
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
. Get ( "SlnFileWithNoProjectReferences" )
. CreateInstance ( )
. WithSourceFiles ( )
. Root
. FullName ;
var cmd = new DotnetCommand ( )
. WithWorkingDirectory ( projectDirectory )
. ExecuteWithCapturedOutput ( "list projects" ) ;
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 )
. ExecuteWithCapturedOutput ( "list projects" ) ;
cmd . Should ( ) . Pass ( ) ;
2016-12-28 22:50:34 +00:00
cmd . StdOut . Should ( ) . BeVisuallyEquivalentTo ( OutputText ) ;
2016-12-16 16:41:47 +00:00
}
}
}