2016-07-21 15:04:05 -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.
2016-10-14 10:23:50 -07:00
using System ;
2016-10-04 19:29:07 +02:00
using System.IO ;
2018-07-06 17:58:44 -07:00
using System.Linq ;
using System.Xml.Linq ;
2018-10-29 11:26:53 -07:00
using Microsoft.DotNet.TestFramework ;
2016-07-21 15:04:05 -05:00
using Microsoft.DotNet.Tools.Test.Utilities ;
using Xunit ;
2017-01-31 17:31:37 -08:00
[assembly: CollectionBehavior(DisableTestParallelization = true)]
2016-07-21 15:04:05 -05:00
namespace Microsoft.DotNet.Tests.EndToEnd
{
public class GivenDotNetUsesMSBuild : TestBase
{
2018-10-29 11:26:53 -07:00
private string _testPackagesDirectory ;
private string _testNuGetCache ;
public GivenDotNetUsesMSBuild ( )
2016-07-21 15:04:05 -05:00
{
2018-10-29 11:26:53 -07:00
_testPackagesDirectory = SetupTestPackages ( ) ;
2016-07-21 15:04:05 -05:00
2018-10-29 11:26:53 -07:00
_testNuGetCache = TestAssets . CreateTestDirectory ( testProjectName : string . Empty ,
callingMethod : "packages" ,
identifier : string . Empty )
. FullName ;
}
2016-07-21 15:04:05 -05:00
2018-10-29 11:26:53 -07:00
[Fact]
public void ItCanNewRestoreBuildRunCleanMSBuildProject ( )
{
var directory = TestAssets . CreateTestDirectory ( ) ;
string projectDirectory = directory . FullName ;
string newArgs = "console --debug:ephemeral-hive --no-restore" ;
new NewCommandShim ( )
. WithWorkingDirectory ( projectDirectory )
. Execute ( newArgs )
. Should ( ) . Pass ( ) ;
new RestoreCommand ( )
. WithWorkingDirectory ( projectDirectory )
. Execute ( "/p:SkipInvalidConfigurations=true" )
. Should ( ) . Pass ( ) ;
2016-07-21 15:04:05 -05:00
2018-10-29 11:26:53 -07:00
new BuildCommand ( )
. WithWorkingDirectory ( projectDirectory )
. Execute ( )
. Should ( ) . Pass ( ) ;
2016-07-21 15:04:05 -05:00
2018-10-29 11:26:53 -07:00
var runCommand = new RunCommand ( )
. WithWorkingDirectory ( projectDirectory ) ;
2018-10-19 08:44:39 -07:00
2018-10-29 11:26:53 -07:00
// Set DOTNET_ROOT as workaround for https://github.com/dotnet/cli/issues/10196
2018-11-12 11:31:10 -08:00
var dotnetRoot = Path . GetDirectoryName ( RepoDirectoriesProvider . DotnetUnderTest ) ;
if ( ! string . IsNullOrEmpty ( dotnetRoot ) )
{
runCommand = runCommand . WithEnvironmentVariable ( Environment . Is64BitProcess ? "DOTNET_ROOT" : "DOTNET_ROOT(x86)" ,
dotnetRoot ) ;
}
2018-10-19 08:44:39 -07:00
2018-10-29 11:26:53 -07:00
runCommand . ExecuteWithCapturedOutput ( )
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Hello World!" ) ;
2016-10-04 19:29:07 +02:00
2018-10-29 11:26:53 -07:00
var binDirectory = new DirectoryInfo ( projectDirectory ) . Sub ( "bin" ) ;
binDirectory . Should ( ) . HaveFilesMatching ( "*.dll" , SearchOption . AllDirectories ) ;
2016-10-04 19:29:07 +02:00
2018-10-29 11:26:53 -07:00
new CleanCommand ( )
. WithWorkingDirectory ( projectDirectory )
. Execute ( )
. Should ( ) . Pass ( ) ;
2016-10-04 19:29:07 +02:00
2018-10-29 11:26:53 -07:00
binDirectory . Should ( ) . NotHaveFilesMatching ( "*.dll" , SearchOption . AllDirectories ) ;
2016-07-21 15:04:05 -05:00
}
2016-10-03 21:40:24 -07:00
[Fact]
public void ItCanRunToolsInACSProj ( )
{
2016-10-27 18:46:43 -07:00
var testInstance = TestAssets . Get ( "MSBuildTestApp" )
. CreateInstance ( )
2018-10-29 11:26:53 -07:00
. WithSourceFiles ( ) ;
2016-10-27 18:46:43 -07:00
var testProjectDirectory = testInstance . Root ;
2016-10-03 21:40:24 -07:00
2018-10-29 11:26:53 -07:00
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. WithEnvironmentVariable ( "NUGET_PACKAGES" , _testNuGetCache )
. WithEnvironmentVariable ( "TEST_PACKAGES" , _testPackagesDirectory )
. Execute ( )
. Should ( )
. Pass ( ) ;
2016-10-03 21:40:24 -07:00
new DotnetCommand ( )
2016-10-27 18:46:43 -07:00
. WithWorkingDirectory ( testInstance . Root )
2018-10-29 11:26:53 -07:00
. WithEnvironmentVariable ( "NUGET_PACKAGES" , _testNuGetCache )
. WithEnvironmentVariable ( "TEST_PACKAGES" , _testPackagesDirectory )
2018-07-06 17:58:44 -07:00
. ExecuteWithCapturedOutput ( "-d portable" )
2016-10-03 21:40:24 -07:00
. Should ( )
2016-10-05 11:51:59 -07:00
. Pass ( )
. And
. HaveStdOutContaining ( "Hello Portable World!" ) ; ;
2016-10-03 21:40:24 -07:00
}
2016-10-10 17:13:46 -07:00
2018-10-29 11:26:53 -07:00
[Fact]
2016-11-23 10:19:00 -08:00
public void ItCanRunToolsThatPrefersTheCliRuntimeEvenWhenTheToolItselfDeclaresADifferentRuntime ( )
2016-11-22 13:56:13 -08:00
{
var testInstance = TestAssets . Get ( "MSBuildTestApp" )
. CreateInstance ( )
2018-10-29 11:26:53 -07:00
. WithSourceFiles ( ) ;
2016-11-22 13:56:13 -08:00
var testProjectDirectory = testInstance . Root ;
2018-10-29 11:26:53 -07:00
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. WithEnvironmentVariable ( "NUGET_PACKAGES" , _testNuGetCache )
. WithEnvironmentVariable ( "TEST_PACKAGES" , _testPackagesDirectory )
. Execute ( )
. Should ( )
. Pass ( ) ;
2016-11-22 13:56:13 -08:00
new DotnetCommand ( )
. WithWorkingDirectory ( testInstance . Root )
2018-10-29 11:26:53 -07:00
. WithEnvironmentVariable ( "NUGET_PACKAGES" , _testNuGetCache )
. WithEnvironmentVariable ( "TEST_PACKAGES" , _testPackagesDirectory )
2018-07-06 17:58:44 -07:00
. ExecuteWithCapturedOutput ( "-d prefercliruntime" )
2016-11-23 10:19:00 -08:00
. Should ( ) . Pass ( )
. And . HaveStdOutContaining ( "Hello I prefer the cli runtime World!" ) ; ;
2016-11-22 13:56:13 -08:00
}
2018-10-29 11:26:53 -07:00
[Fact]
2016-10-10 17:13:46 -07:00
public void ItCanRunAToolThatInvokesADependencyToolInACSProj ( )
{
var repoDirectoriesProvider = new RepoDirectoriesProvider ( ) ;
2016-10-14 12:45:04 -07:00
2016-10-27 18:46:43 -07:00
var testInstance = TestAssets . Get ( "TestAppWithProjDepTool" )
. CreateInstance ( )
2018-10-29 11:26:53 -07:00
. WithSourceFiles ( ) ;
2016-10-10 17:13:46 -07:00
2016-10-27 18:46:43 -07:00
var configuration = "Debug" ;
2016-10-10 17:13:46 -07:00
2016-10-27 18:46:43 -07:00
var testProjectDirectory = testInstance . Root ;
2016-10-10 17:13:46 -07:00
2018-10-29 11:26:53 -07:00
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. WithEnvironmentVariable ( "NUGET_PACKAGES" , _testNuGetCache )
. WithEnvironmentVariable ( "TEST_PACKAGES" , _testPackagesDirectory )
. Execute ( )
. Should ( )
. Pass ( ) ;
2016-10-27 18:46:43 -07:00
new BuildCommand ( )
2016-10-10 17:13:46 -07:00
. WithWorkingDirectory ( testProjectDirectory )
2018-10-29 11:26:53 -07:00
. WithEnvironmentVariable ( "NUGET_PACKAGES" , _testNuGetCache )
. WithEnvironmentVariable ( "TEST_PACKAGES" , _testPackagesDirectory )
2016-10-27 18:46:43 -07:00
. Execute ( $"-c {configuration} " )
2016-10-10 17:13:46 -07:00
. Should ( )
. Pass ( ) ;
2017-10-11 17:17:28 -07:00
new DotnetCommand ( )
2016-10-10 17:13:46 -07:00
. WithWorkingDirectory ( testProjectDirectory )
2018-10-29 11:26:53 -07:00
. WithEnvironmentVariable ( "NUGET_PACKAGES" , _testNuGetCache )
. WithEnvironmentVariable ( "TEST_PACKAGES" , _testPackagesDirectory )
2016-10-14 10:23:50 -07:00
. ExecuteWithCapturedOutput (
2018-07-09 10:25:13 -07:00
$"-d dependency-tool-invoker -c {configuration} -f netcoreapp3.0 portable" )
2016-10-27 18:46:43 -07:00
. Should ( ) . Pass ( )
2018-10-29 11:26:53 -07:00
. And . HaveStdOutContaining ( "Hello Portable World!" ) ;
}
[Fact]
public void BuildTestPackages ( )
{
}
private string SetupTestPackages ( )
{
var directory = TestAssets . CreateTestDirectory (
testProjectName : string . Empty ,
callingMethod : "TestPackages" ,
identifier : string . Empty ) ;
string testPackagesDirectory = Path . Combine ( directory . FullName , "testPackages" ) ;
if ( ! Directory . Exists ( testPackagesDirectory ) )
{
new DirectoryInfo ( testPackagesDirectory ) . Create ( ) ;
//Directory.CreateDirectory(testPackagesDirectory);
}
var testPackageNames = new [ ]
{
"dotnet-portable" ,
"dotnet-prefercliruntime" ,
"dotnet-dependency-tool-invoker"
} ;
foreach ( var testPackageName in testPackageNames )
{
var assetInfo = TestAssets . Get ( TestAssetKinds . TestPackages , testPackageName ) ;
var testProjectDirectory = new DirectoryInfo ( Path . Combine ( directory . FullName , testPackageName ) ) ;
if ( ! testProjectDirectory . Exists )
{
testProjectDirectory . Create ( ) ;
}
var testInstance = new TestAssetInstance ( assetInfo , testProjectDirectory )
. WithSourceFiles ( )
. WithRestoreFiles ( ) ;
new PackCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( )
. Pass ( ) ;
string nupkgFilePathInOutput = Directory . GetFiles ( Path . Combine ( testProjectDirectory . FullName , "bin" , "Debug" ) , "*.nupkg" )
. Single ( ) ;
string nupkgFile = Path . Combine ( testPackagesDirectory , Path . GetFileName ( nupkgFilePathInOutput ) ) ;
File . Copy ( nupkgFilePathInOutput , nupkgFile ) ;
}
return testPackagesDirectory ;
2016-10-10 17:13:46 -07:00
}
2016-07-21 15:04:05 -05:00
}
}