2016-10-27 18:46:43 -07: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 System.IO ;
2017-05-25 20:53:03 -07:00
using FluentAssertions ;
2017-06-13 00:04:25 -07:00
using Microsoft.DotNet.TestFramework ;
2016-10-27 18:46:43 -07:00
using Microsoft.DotNet.Tools.Test.Utilities ;
using Xunit ;
2017-07-18 08:34:08 -07:00
using LocalizableStrings = Microsoft . DotNet . Tools . Run . LocalizableStrings ;
2016-10-27 18:46:43 -07:00
namespace Microsoft.DotNet.Cli.Run.Tests
{
public class GivenDotnetRunBuildsCsproj : TestBase
{
[Fact]
public void ItCanRunAMSBuildProject ( )
{
var testAppName = "MSBuildTestApp" ;
2017-02-15 15:35:03 -08:00
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
2016-10-27 18:46:43 -07:00
2017-02-15 15:35:03 -08:00
var testProjectDirectory = testInstance . Root . FullName ;
2016-10-27 18:46:43 -07:00
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( "/p:SkipInvalidConfigurations=true" )
. Should ( ) . Pass ( ) ;
new BuildCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( ) . Pass ( ) ;
new RunCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. ExecuteWithCapturedOutput ( )
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Hello World!" ) ;
}
2017-06-02 23:32:53 -07:00
[Fact]
public void ItImplicitlyRestoresAProjectWhenRunning ( )
{
var testAppName = "MSBuildTestApp" ;
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
new RunCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. ExecuteWithCapturedOutput ( )
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Hello World!" ) ;
}
2017-06-13 00:04:25 -07:00
[Fact]
public void ItCanRunAMultiTFMProjectWithImplicitRestore ( )
{
var testInstance = TestAssets . Get (
TestAssetKinds . DesktopTestProjects ,
"NETFrameworkReferenceNETStandard20" )
. CreateInstance ( )
. WithSourceFiles ( ) ;
string projectDirectory = Path . Combine ( testInstance . Root . FullName , "MultiTFMTestApp" ) ;
new RunCommand ( )
. WithWorkingDirectory ( projectDirectory )
2017-08-18 15:36:01 -07:00
. ExecuteWithCapturedOutput ( "--framework netcoreapp2.1" )
2017-06-13 00:04:25 -07:00
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "This string came from the test library!" ) ;
}
2017-11-27 21:09:26 -08:00
[Fact]
public void ItDoesNotImplicitlyBuildAProjectWhenRunningWithTheNoBuildOption ( )
{
var testAppName = "MSBuildTestApp" ;
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
var result = new RunCommand ( )
. WithWorkingDirectory ( testInstance . Root . FullName )
. ExecuteWithCapturedOutput ( "--no-build -v:m" ) ;
result . Should ( ) . Fail ( ) ;
if ( ! DotnetUnderTest . IsLocalized ( ) )
{
result . Should ( ) . NotHaveStdOutContaining ( "Restore" ) ;
}
}
2017-06-02 23:32:53 -07:00
[Fact]
public void ItDoesNotImplicitlyRestoreAProjectWhenRunningWithTheNoRestoreOption ( )
{
var testAppName = "MSBuildTestApp" ;
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
new RunCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. ExecuteWithCapturedOutput ( "--no-restore" )
. Should ( ) . Fail ( )
2017-06-23 19:48:07 -07:00
. And . HaveStdOutContaining ( "project.assets.json" ) ;
2017-06-02 23:32:53 -07:00
}
2016-10-27 18:46:43 -07:00
[Fact]
public void ItBuildsTheProjectBeforeRunning ( )
{
var testAppName = "MSBuildTestApp" ;
2017-02-15 15:35:03 -08:00
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
2016-10-27 18:46:43 -07:00
2017-02-15 15:35:03 -08:00
var testProjectDirectory = testInstance . Root . FullName ;
2016-10-27 18:46:43 -07:00
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( "/p:SkipInvalidConfigurations=true" )
. Should ( ) . Pass ( ) ;
new RunCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. ExecuteWithCapturedOutput ( )
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Hello World!" ) ;
}
[Fact]
public void ItCanRunAMSBuildProjectWhenSpecifyingAFramework ( )
{
var testAppName = "MSBuildTestApp" ;
2017-02-15 15:35:03 -08:00
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
2016-10-27 18:46:43 -07:00
2017-02-15 15:35:03 -08:00
var testProjectDirectory = testInstance . Root . FullName ;
2016-10-27 18:46:43 -07:00
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( "/p:SkipInvalidConfigurations=true" )
. Should ( ) . Pass ( ) ;
new RunCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
2017-08-18 15:36:01 -07:00
. ExecuteWithCapturedOutput ( "--framework netcoreapp2.1" )
2016-10-27 18:46:43 -07:00
. Should ( ) . Pass ( )
2017-03-02 20:35:20 -08:00
. And . HaveStdOutContaining ( "Hello World!" ) ;
2016-10-27 18:46:43 -07:00
}
2017-03-02 20:35:20 -08:00
[Fact]
public void ItRunsPortableAppsFromADifferentPathAfterBuilding ( )
2016-10-27 18:46:43 -07:00
{
var testInstance = TestAssets . Get ( "MSBuildTestApp" )
. CreateInstance ( )
. WithSourceFiles ( )
. WithRestoreFiles ( ) ;
2017-03-02 20:35:20 -08:00
new BuildCommand ( )
. WithWorkingDirectory ( testInstance . Root )
. Execute ( )
. Should ( ) . Pass ( ) ;
new RunCommand ( )
. WithWorkingDirectory ( testInstance . Root )
. ExecuteWithCapturedOutput ( $"--no-build" )
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Hello World!" ) ;
}
[Fact]
public void ItRunsPortableAppsFromADifferentPathWithoutBuilding ( )
{
var testAppName = "MSBuildTestApp" ;
2016-10-27 18:46:43 -07:00
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( )
. WithRestoreFiles ( ) ;
2017-03-02 20:35:20 -08:00
var projectFile = testInstance . Root . GetFile ( testAppName + ".csproj" ) ;
2016-10-27 18:46:43 -07:00
2017-03-02 20:35:20 -08:00
new RunCommand ( )
. WithWorkingDirectory ( testInstance . Root . Parent )
. ExecuteWithCapturedOutput ( $"--project {projectFile.FullName}" )
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Hello World!" ) ;
2016-12-18 00:45:25 -08:00
}
2017-03-24 06:44:53 +01:00
[Fact]
public void ItRunsPortableAppsFromADifferentPathSpecifyingOnlyTheDirectoryWithoutBuilding ( )
{
var testAppName = "MSBuildTestApp" ;
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( )
. WithRestoreFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
new RunCommand ( )
. WithWorkingDirectory ( testInstance . Root . Parent )
. ExecuteWithCapturedOutput ( $"--project {testProjectDirectory}" )
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Hello World!" ) ;
}
2017-08-31 15:31:44 -07:00
[Fact]
2016-12-18 00:45:25 -08:00
public void ItRunsAppWhenRestoringToSpecificPackageDirectory ( )
{
2017-02-15 15:35:03 -08:00
var rootPath = TestAssets . CreateTestDirectory ( ) . FullName ;
2016-12-18 00:45:25 -08:00
string dir = "pkgs" ;
string args = $"--packages {dir}" ;
2017-07-10 15:57:30 -07:00
string newArgs = $"console -o \" { rootPath } \ " --no-restore" ;
2017-01-31 17:31:37 -08:00
new NewCommandShim ( )
2016-12-18 00:45:25 -08:00
. WithWorkingDirectory ( rootPath )
2017-01-31 17:31:37 -08:00
. Execute ( newArgs )
2016-12-18 00:45:25 -08:00
. Should ( )
. Pass ( ) ;
new RestoreCommand ( )
. WithWorkingDirectory ( rootPath )
. Execute ( args )
. Should ( )
. Pass ( ) ;
new RunCommand ( )
. WithWorkingDirectory ( rootPath )
2017-06-02 23:32:53 -07:00
. ExecuteWithCapturedOutput ( "--no-restore" )
2016-12-18 00:45:25 -08:00
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Hello World" ) ;
}
2017-03-21 13:46:08 -05:00
[Fact]
public void ItReportsAGoodErrorWhenProjectHasMultipleFrameworks ( )
{
var testAppName = "MSBuildAppWithMultipleFrameworks" ;
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( )
. WithRestoreFiles ( ) ;
// use --no-build so this test can run on all platforms.
// the test app targets net451, which can't be built on non-Windows
new RunCommand ( )
. WithWorkingDirectory ( testInstance . Root )
. ExecuteWithCapturedOutput ( "--no-build" )
. Should ( ) . Fail ( )
. And . HaveStdErrContaining ( "--framework" ) ;
}
2017-04-12 16:03:45 -07:00
[Fact]
public void ItCanPassArgumentsToSubjectAppByDoubleDash ( )
{
const string testAppName = "MSBuildTestApp" ;
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( )
. WithRestoreFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
new RunCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. ExecuteWithCapturedOutput ( "-- foo bar baz" )
. Should ( )
. Pass ( )
. And . HaveStdOutContaining ( "echo args:foo;bar;baz" ) ;
}
2017-05-25 20:53:03 -07:00
[Fact]
public void ItGivesAnErrorWhenAttemptingToUseALaunchProfileThatDoesNotExistWhenThereIsNoLaunchSettingsFile ( )
{
var testAppName = "MSBuildTestApp" ;
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( "/p:SkipInvalidConfigurations=true" )
. Should ( ) . Pass ( ) ;
new BuildCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( ) . Pass ( ) ;
new RunCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. ExecuteWithCapturedOutput ( "--launch-profile test" )
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Hello World!" )
2017-07-18 08:34:08 -07:00
. And . HaveStdErrContaining ( LocalizableStrings . RunCommandExceptionCouldNotLocateALaunchSettingsFile ) ;
2017-05-25 20:53:03 -07:00
}
[Fact]
public void ItUsesLaunchProfileOfTheSpecifiedName ( )
{
2017-05-31 11:48:01 -07:00
var testAppName = "AppWithLaunchSettings" ;
2017-05-25 20:53:03 -07:00
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( "/p:SkipInvalidConfigurations=true" )
. Should ( ) . Pass ( ) ;
new BuildCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( ) . Pass ( ) ;
var cmd = new RunCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. ExecuteWithCapturedOutput ( "--launch-profile Second" ) ;
cmd . Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Second" ) ;
cmd . StdErr . Should ( ) . BeEmpty ( ) ;
}
[Fact]
public void ItDefaultsToTheFirstUsableLaunchProfile ( )
{
2017-05-31 11:48:01 -07:00
var testAppName = "AppWithLaunchSettings" ;
2017-05-25 20:53:03 -07:00
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( "/p:SkipInvalidConfigurations=true" )
. Should ( ) . Pass ( ) ;
new BuildCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( ) . Pass ( ) ;
var cmd = new RunCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. ExecuteWithCapturedOutput ( ) ;
cmd . Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "First" ) ;
cmd . StdErr . Should ( ) . BeEmpty ( ) ;
}
[Fact]
public void ItGivesAnErrorWhenTheLaunchProfileNotFound ( )
{
2017-05-31 11:48:01 -07:00
var testAppName = "AppWithLaunchSettings" ;
2017-05-25 20:53:03 -07:00
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( "/p:SkipInvalidConfigurations=true" )
. Should ( ) . Pass ( ) ;
new BuildCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( ) . Pass ( ) ;
new RunCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. ExecuteWithCapturedOutput ( "--launch-profile Third" )
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "(NO MESSAGE)" )
2017-07-18 08:34:08 -07:00
. And . HaveStdErrContaining ( string . Format ( LocalizableStrings . RunCommandExceptionCouldNotApplyLaunchSettings , "Third" , "" ) . Trim ( ) ) ;
2017-05-25 20:53:03 -07:00
}
[Fact]
public void ItGivesAnErrorWhenTheLaunchProfileCanNotBeHandled ( )
{
2017-05-31 11:48:01 -07:00
var testAppName = "AppWithLaunchSettings" ;
2017-05-25 20:53:03 -07:00
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( "/p:SkipInvalidConfigurations=true" )
. Should ( ) . Pass ( ) ;
new BuildCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( ) . Pass ( ) ;
new RunCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. ExecuteWithCapturedOutput ( "--launch-profile \"IIS Express\"" )
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "(NO MESSAGE)" )
2017-07-18 08:34:08 -07:00
. And . HaveStdErrContaining ( string . Format ( LocalizableStrings . RunCommandExceptionCouldNotApplyLaunchSettings , "IIS Express" , "" ) . Trim ( ) ) ;
2017-05-25 20:53:03 -07:00
}
[Fact]
public void ItSkipsLaunchProfilesWhenTheSwitchIsSupplied ( )
{
2017-05-31 11:48:01 -07:00
var testAppName = "AppWithLaunchSettings" ;
2017-05-25 20:53:03 -07:00
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( "/p:SkipInvalidConfigurations=true" )
. Should ( ) . Pass ( ) ;
new BuildCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( ) . Pass ( ) ;
var cmd = new RunCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. ExecuteWithCapturedOutput ( "--no-launch-profile" ) ;
cmd . Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "(NO MESSAGE)" ) ;
cmd . StdErr . Should ( ) . BeEmpty ( ) ;
}
[Fact]
public void ItSkipsLaunchProfilesWhenTheSwitchIsSuppliedWithoutErrorWhenThereAreNoLaunchSettings ( )
{
var testAppName = "MSBuildTestApp" ;
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( "/p:SkipInvalidConfigurations=true" )
. Should ( ) . Pass ( ) ;
new BuildCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( ) . Pass ( ) ;
var cmd = new RunCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. ExecuteWithCapturedOutput ( "--no-launch-profile" ) ;
cmd . Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Hello World!" ) ;
cmd . StdErr . Should ( ) . BeEmpty ( ) ;
}
[Fact]
public void ItSkipsLaunchProfilesWhenThereIsNoUsableDefault ( )
{
2017-05-31 14:07:34 -07:00
var testAppName = "AppWithLaunchSettingsNoDefault" ;
2017-05-25 20:53:03 -07:00
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( "/p:SkipInvalidConfigurations=true" )
. Should ( ) . Pass ( ) ;
new BuildCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( ) . Pass ( ) ;
var cmd = new RunCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. ExecuteWithCapturedOutput ( ) ;
cmd . Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "(NO MESSAGE)" )
2017-07-18 08:34:08 -07:00
. And . HaveStdErrContaining ( string . Format ( LocalizableStrings . RunCommandExceptionCouldNotApplyLaunchSettings , LocalizableStrings . DefaultLaunchProfileDisplayName , "" ) . Trim ( ) ) ;
2017-05-25 20:53:03 -07:00
}
[Fact]
2017-05-29 12:54:19 -07:00
public void ItPrintsAnErrorWhenLaunchSettingsAreCorrupted ( )
2017-05-25 20:53:03 -07:00
{
2017-05-31 11:48:01 -07:00
var testAppName = "AppWithCorruptedLaunchSettings" ;
2017-05-25 20:53:03 -07:00
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( "/p:SkipInvalidConfigurations=true" )
. Should ( ) . Pass ( ) ;
new BuildCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( ) . Pass ( ) ;
var cmd = new RunCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. ExecuteWithCapturedOutput ( ) ;
cmd . Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "(NO MESSAGE)" )
2017-07-18 08:34:08 -07:00
. And . HaveStdErrContaining ( string . Format ( LocalizableStrings . RunCommandExceptionCouldNotApplyLaunchSettings , LocalizableStrings . DefaultLaunchProfileDisplayName , "" ) . Trim ( ) ) ;
2017-05-25 20:53:03 -07:00
}
2017-11-27 23:03:33 -08:00
[Fact]
public void ItRunsWithTheSpecifiedVerbosity ( )
{
var testAppName = "MSBuildTestApp" ;
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
var result = new RunCommand ( )
. WithWorkingDirectory ( testInstance . Root . FullName )
. ExecuteWithCapturedOutput ( "-v:n" ) ;
result . Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Hello World!" ) ;
if ( ! DotnetUnderTest . IsLocalized ( ) )
{
result . Should ( ) . HaveStdOutContaining ( "Restore" )
. And . HaveStdOutContaining ( "CoreCompile" ) ;
}
}
2016-10-27 18:46:43 -07:00
}
2017-03-02 20:35:20 -08:00
}