2016-09-23 11:11:44 -05: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 ;
using System.IO ;
2017-01-26 12:17:21 -06:00
using System.Diagnostics ;
using System.Runtime.InteropServices ;
2016-09-23 11:11:44 -05:00
using FluentAssertions ;
2016-09-30 17:47:23 -05:00
using Microsoft.DotNet.Cli.Utils ;
using Microsoft.DotNet.PlatformAbstractions ;
2017-06-13 00:04:25 -07:00
using Microsoft.DotNet.TestFramework ;
2016-09-23 11:11:44 -05:00
using Microsoft.DotNet.Tools.Test.Utilities ;
using Xunit ;
2016-10-27 18:46:43 -07:00
namespace Microsoft.DotNet.Cli.Publish.Tests
2016-09-23 11:11:44 -05:00
{
2016-10-27 18:46:43 -07:00
public class GivenDotnetPublishPublishesProjects : TestBase
2016-09-23 11:11:44 -05:00
{
[Fact]
public void ItPublishesARunnablePortableApp ( )
{
var testAppName = "MSBuildTestApp" ;
2017-02-15 15:35:03 -08:00
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
2016-09-23 11:11:44 -05:00
2017-02-15 15:35:03 -08:00
var testProjectDirectory = testInstance . Root . FullName ;
2016-09-23 11:11:44 -05:00
2016-10-27 18:46:43 -07:00
new RestoreCommand ( )
2016-09-27 14:41:06 -07:00
. WithWorkingDirectory ( testProjectDirectory )
2017-04-03 18:09:50 -05:00
. Execute ( )
2016-10-26 22:23:40 +00:00
. Should ( ) . Pass ( ) ;
2016-09-27 14:41:06 -07:00
2016-10-27 18:46:43 -07:00
new PublishCommand ( )
2016-09-23 11:11:44 -05:00
. WithWorkingDirectory ( testProjectDirectory )
2018-05-24 15:41:03 -07:00
. Execute ( "--framework netcoreapp2.2" )
2016-10-26 22:23:40 +00:00
. Should ( ) . Pass ( ) ;
2016-09-23 11:11:44 -05:00
var configuration = Environment . GetEnvironmentVariable ( "CONFIGURATION" ) ? ? "Debug" ;
2018-05-24 15:41:03 -07:00
var outputDll = Path . Combine ( testProjectDirectory , "bin" , configuration , "netcoreapp2.2" , "publish" , $"{testAppName}.dll" ) ;
2016-09-23 11:11:44 -05:00
2017-03-16 01:31:16 -07:00
new DotnetCommand ( )
2016-09-23 11:11:44 -05:00
. ExecuteWithCapturedOutput ( outputDll )
2016-10-26 22:23:40 +00:00
. Should ( ) . Pass ( )
2016-10-27 18:46:43 -07:00
. And . HaveStdOutContaining ( "Hello World" ) ;
2016-09-23 11:11:44 -05:00
}
2016-09-30 17:47:23 -05:00
2017-06-02 23:32:53 -07:00
[Fact]
public void ItImplicitlyRestoresAProjectWhenPublishing ( )
{
var testAppName = "MSBuildTestApp" ;
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
new PublishCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
2018-05-24 15:41:03 -07:00
. Execute ( "--framework netcoreapp2.2" )
2017-06-02 23:32:53 -07:00
. Should ( ) . Pass ( ) ;
}
2017-06-13 00:04:25 -07:00
[Fact]
public void ItCanPublishAMultiTFMProjectWithImplicitRestore ( )
{
var testInstance = TestAssets . Get (
TestAssetKinds . DesktopTestProjects ,
"NETFrameworkReferenceNETStandard20" )
. CreateInstance ( )
. WithSourceFiles ( ) ;
string projectDirectory = Path . Combine ( testInstance . Root . FullName , "MultiTFMTestApp" ) ;
new PublishCommand ( )
. WithWorkingDirectory ( projectDirectory )
2018-05-24 15:41:03 -07:00
. Execute ( "--framework netcoreapp2.2" )
2017-06-13 00:04:25 -07:00
. Should ( ) . Pass ( ) ;
}
2017-06-02 23:32:53 -07:00
[Fact]
public void ItDoesNotImplicitlyRestoreAProjectWhenPublishingWithTheNoRestoreOption ( )
{
var testAppName = "MSBuildTestApp" ;
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
new PublishCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
2018-05-24 15:41:03 -07:00
. ExecuteWithCapturedOutput ( "--framework netcoreapp2.2 --no-restore" )
2017-06-02 23:32:53 -07:00
. Should ( ) . Fail ( )
2017-06-23 19:48:07 -07:00
. And . HaveStdOutContaining ( "project.assets.json" ) ;
2017-06-02 23:32:53 -07:00
}
2016-09-30 17:47:23 -05:00
[Fact]
public void ItPublishesARunnableSelfContainedApp ( )
{
2016-10-03 09:37:15 -05:00
var testAppName = "MSBuildTestApp" ;
2016-09-30 17:47:23 -05:00
2016-10-27 18:46:43 -07:00
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( )
. WithSourceFiles ( )
. WithRestoreFiles ( ) ;
2016-09-30 17:47:23 -05:00
2016-10-27 18:46:43 -07:00
var testProjectDirectory = testInstance . Root ;
var rid = DotnetLegacyRuntimeIdentifiers . InferLegacyRestoreRuntimeIdentifier ( ) ;
2016-10-26 22:23:40 +00:00
2016-10-27 18:46:43 -07:00
new PublishCommand ( )
2018-05-24 15:41:03 -07:00
. WithFramework ( "netcoreapp2.2" )
2016-09-30 17:47:23 -05:00
. WithRuntime ( rid )
. WithWorkingDirectory ( testProjectDirectory )
2017-04-03 18:09:50 -05:00
. Execute ( )
2016-10-26 22:23:40 +00:00
. Should ( ) . Pass ( ) ;
2016-09-30 17:47:23 -05:00
var configuration = Environment . GetEnvironmentVariable ( "CONFIGURATION" ) ? ? "Debug" ;
2016-10-27 18:46:43 -07:00
var outputProgram = testProjectDirectory
2018-05-24 15:41:03 -07:00
. GetDirectory ( "bin" , configuration , "netcoreapp2.2" , rid , "publish" , $"{testAppName}{Constants.ExeSuffix}" )
2016-10-27 18:46:43 -07:00
. FullName ;
2016-09-30 17:47:23 -05:00
2017-04-04 11:24:51 -05:00
EnsureProgramIsRunnable ( outputProgram ) ;
new TestCommand ( outputProgram )
. ExecuteWithCapturedOutput ( )
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Hello World" ) ;
}
[Fact]
public void ItPublishesARidSpecificAppSettingSelfContainedToTrue ( )
{
var testAppName = "MSBuildTestApp" ;
var outputDirectory = PublishAppWithSelfContained ( testAppName , true ) ;
var outputProgram = Path . Combine ( outputDirectory . FullName , $"{testAppName}{Constants.ExeSuffix}" ) ;
EnsureProgramIsRunnable ( outputProgram ) ;
2017-01-26 12:17:21 -06:00
2016-09-30 17:47:23 -05:00
new TestCommand ( outputProgram )
. ExecuteWithCapturedOutput ( )
2016-10-26 22:23:40 +00:00
. Should ( ) . Pass ( )
2016-10-27 18:46:43 -07:00
. And . HaveStdOutContaining ( "Hello World" ) ;
2016-09-30 17:47:23 -05:00
}
2016-12-18 00:45:25 -08:00
2017-04-04 11:24:51 -05:00
[Fact]
public void ItPublishesARidSpecificAppSettingSelfContainedToFalse ( )
2017-04-03 18:09:50 -05:00
{
var testAppName = "MSBuildTestApp" ;
2017-04-04 11:24:51 -05:00
var outputDirectory = PublishAppWithSelfContained ( testAppName , false ) ;
outputDirectory . Should ( ) . OnlyHaveFiles ( new [ ] {
$"{testAppName}.dll" ,
$"{testAppName}.pdb" ,
$"{testAppName}.deps.json" ,
$"{testAppName}.runtimeconfig.json" ,
} ) ;
2017-04-03 18:09:50 -05:00
2017-04-04 11:24:51 -05:00
new DotnetCommand ( )
. ExecuteWithCapturedOutput ( Path . Combine ( outputDirectory . FullName , $"{testAppName}.dll" ) )
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Hello World" ) ;
}
private DirectoryInfo PublishAppWithSelfContained ( string testAppName , bool selfContained )
{
2017-04-03 18:09:50 -05:00
var testInstance = TestAssets . Get ( testAppName )
. CreateInstance ( $"PublishesSelfContained{selfContained}" )
. WithSourceFiles ( )
. WithRestoreFiles ( ) ;
var testProjectDirectory = testInstance . Root ;
var rid = DotnetLegacyRuntimeIdentifiers . InferLegacyRestoreRuntimeIdentifier ( ) ;
new PublishCommand ( )
. WithRuntime ( rid )
. WithSelfContained ( selfContained )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( ) . Pass ( ) ;
var configuration = Environment . GetEnvironmentVariable ( "CONFIGURATION" ) ? ? "Debug" ;
2017-04-04 11:24:51 -05:00
return testProjectDirectory
2018-05-24 15:41:03 -07:00
. GetDirectory ( "bin" , configuration , "netcoreapp2.2" , rid , "publish" ) ;
2017-04-04 11:24:51 -05:00
}
2017-04-03 18:09:50 -05:00
2017-04-04 11:24:51 -05:00
private static void EnsureProgramIsRunnable ( string path )
{
if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
2017-04-03 18:09:50 -05:00
{
2017-04-04 11:24:51 -05:00
//Workaround for https://github.com/dotnet/corefx/issues/15516
Process . Start ( "chmod" , $"u+x {path}" ) . WaitForExit ( ) ;
2017-04-03 18:09:50 -05:00
}
}
2017-08-31 15:31:44 -07:00
[Fact]
2016-12-18 00:45:25 -08:00
public void ItPublishesAppWhenRestoringToSpecificPackageDirectory ( )
{
2017-02-15 15:35:03 -08:00
var rootPath = TestAssets . CreateTestDirectory ( ) . FullName ;
2016-12-18 00:45:25 -08:00
var rootDir = new DirectoryInfo ( rootPath ) ;
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 PublishCommand ( )
. WithWorkingDirectory ( rootPath )
2017-06-02 23:32:53 -07:00
. ExecuteWithCapturedOutput ( "--no-restore" )
2016-12-18 00:45:25 -08:00
. Should ( ) . Pass ( ) ;
var configuration = Environment . GetEnvironmentVariable ( "CONFIGURATION" ) ? ? "Debug" ;
var outputProgram = rootDir
2017-08-18 15:36:01 -07:00
. GetDirectory ( "bin" , configuration , "netcoreapp2.1" , "publish" , $"{rootDir.Name}.dll" )
2016-12-18 00:45:25 -08:00
. FullName ;
new TestCommand ( outputProgram )
. ExecuteWithCapturedOutput ( )
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Hello World" ) ;
}
2018-04-04 15:02:36 -07:00
[Fact]
public void ItFailsToPublishWithNoBuildIfNotPreviouslyBuilt ( )
{
var rootPath = TestAssets . CreateTestDirectory ( ) . FullName ;
string newArgs = $"console -o \" { rootPath } \ "" ;
2018-04-05 17:58:38 -07:00
new NewCommandShim ( ) // note implicit restore here
2018-04-04 15:02:36 -07:00
. WithWorkingDirectory ( rootPath )
. Execute ( newArgs )
2018-04-05 17:58:38 -07:00
. Should ( )
. Pass ( ) ;
2018-04-04 15:02:36 -07:00
new PublishCommand ( )
. WithWorkingDirectory ( rootPath )
. ExecuteWithCapturedOutput ( "--no-build" )
2018-04-05 17:58:38 -07:00
. Should ( )
. Fail ( )
. And . HaveStdOutContaining ( "MSB3030" ) ; // "Could not copy ___ because it was not found."
2018-04-04 15:02:36 -07:00
}
2018-04-05 17:58:38 -07:00
[Theory]
[InlineData(false)]
[InlineData(true)]
public void ItPublishesSuccessfullyWithNoBuildIfPreviouslyBuilt ( bool selfContained )
2018-04-04 15:02:36 -07:00
{
2018-04-05 17:58:38 -07:00
var rootPath = TestAssets . CreateTestDirectory ( identifier : selfContained ? "_sc" : "" ) . FullName ;
2018-04-04 15:02:36 -07:00
var rootDir = new DirectoryInfo ( rootPath ) ;
string newArgs = $"console -o \" { rootPath } \ " --no-restore" ;
new NewCommandShim ( )
. WithWorkingDirectory ( rootPath )
. Execute ( newArgs )
. Should ( )
. Pass ( ) ;
2018-04-05 17:58:38 -07:00
var rid = selfContained ? DotnetLegacyRuntimeIdentifiers . InferLegacyRestoreRuntimeIdentifier ( ) : "" ;
var ridArg = selfContained ? $"-r {rid}" : "" ;
2018-04-04 15:02:36 -07:00
new BuildCommand ( )
. WithWorkingDirectory ( rootPath )
2018-04-05 17:58:38 -07:00
. ExecuteWithCapturedOutput ( ridArg )
2018-04-04 15:02:36 -07:00
. Should ( )
. Pass ( ) ;
new PublishCommand ( )
. WithWorkingDirectory ( rootPath )
2018-04-05 17:58:38 -07:00
. ExecuteWithCapturedOutput ( $"{ridArg} --no-build" )
2018-04-04 15:02:36 -07:00
. Should ( )
. Pass ( ) ;
var configuration = Environment . GetEnvironmentVariable ( "CONFIGURATION" ) ? ? "Debug" ;
var outputProgram = rootDir
2018-04-05 17:58:38 -07:00
. GetDirectory ( "bin" , configuration , "netcoreapp2.1" , rid , "publish" , $"{rootDir.Name}.dll" )
2018-04-04 15:02:36 -07:00
. FullName ;
new TestCommand ( outputProgram )
. ExecuteWithCapturedOutput ( )
. Should ( )
. Pass ( )
. And . HaveStdOutContaining ( "Hello World" ) ;
}
2018-04-05 17:58:38 -07:00
[Fact]
public void ItFailsToPublishWithNoBuildIfPreviouslyBuiltWithoutRid ( )
{
var rootPath = TestAssets . CreateTestDirectory ( ) . FullName ;
var rootDir = new DirectoryInfo ( rootPath ) ;
string newArgs = $"console -o \" { rootPath } \ " --no-restore" ;
new NewCommandShim ( )
. WithWorkingDirectory ( rootPath )
. Execute ( newArgs )
. Should ( )
. Pass ( ) ;
new BuildCommand ( )
. WithWorkingDirectory ( rootPath )
. ExecuteWithCapturedOutput ( )
. Should ( )
. Pass ( ) ;
new PublishCommand ( )
. WithWorkingDirectory ( rootPath )
. ExecuteWithCapturedOutput ( "-r win-x64 --no-build" )
. Should ( )
. Fail ( ) ;
}
2016-09-23 11:11:44 -05:00
}
}