2016-02-03 10:57:25 -08: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 ;
using System.Linq ;
using FluentAssertions ;
using FluentAssertions.Common ;
using Microsoft.DotNet.ProjectModel ;
using Microsoft.DotNet.Tools.Test.Utilities ;
using Microsoft.Extensions.PlatformAbstractions ;
using NuGet.Frameworks ;
using Xunit ;
namespace Microsoft.DotNet.Tools.Builder.Tests
{
public class BuildOutputTests : TestBase
{
2016-02-11 14:17:20 -08:00
private string _testProjectsRoot ;
private string _runtime ;
private DirectoryInfo _rootDirInfo ;
private DirectoryInfo _testAppDirDirInfo ;
private DirectoryInfo _testLibDirInfo ;
2016-02-03 10:57:25 -08:00
private readonly string [ ] _runtimeFiles =
{
"TestApp" + FileNameSuffixes . DotNet . DynamicLib ,
"TestApp" + FileNameSuffixes . DotNet . ProgramDatabase ,
"TestApp" + FileNameSuffixes . CurrentPlatform . Exe ,
"TestApp" + FileNameSuffixes . Deps ,
"TestLibrary" + FileNameSuffixes . DotNet . DynamicLib ,
"TestLibrary" + FileNameSuffixes . DotNet . ProgramDatabase
} ;
private readonly string [ ] _appCompileFiles =
{
"TestApp" + FileNameSuffixes . DotNet . DynamicLib ,
"TestApp" + FileNameSuffixes . DotNet . ProgramDatabase
} ;
private readonly string [ ] _libCompileFiles =
{
"TestLibrary" + FileNameSuffixes . DotNet . DynamicLib ,
"TestLibrary" + FileNameSuffixes . DotNet . ProgramDatabase ,
} ;
2016-02-11 14:17:20 -08:00
private void GetProjectInfo ( string testRoot )
2016-02-03 10:57:25 -08:00
{
2016-02-11 14:17:20 -08:00
_testProjectsRoot = testRoot ;
_rootDirInfo = new DirectoryInfo ( _testProjectsRoot ) ;
_testAppDirDirInfo = new DirectoryInfo ( Path . Combine ( _testProjectsRoot , "TestApp" ) ) ;
_testLibDirInfo = new DirectoryInfo ( Path . Combine ( _testProjectsRoot , "TestLibrary" ) ) ;
2016-02-03 10:57:25 -08:00
var contexts = ProjectContext . CreateContextForEachFramework (
2016-02-11 14:17:20 -08:00
_testAppDirDirInfo . FullName ,
2016-02-03 10:57:25 -08:00
null ,
PlatformServices . Default . Runtime . GetAllCandidateRuntimeIdentifiers ( ) ) ;
2016-02-11 14:17:20 -08:00
_runtime = contexts . FirstOrDefault ( c = > ! string . IsNullOrEmpty ( c . RuntimeIdentifier ) ) ? . RuntimeIdentifier ;
2016-02-03 10:57:25 -08:00
}
private string FormatPath ( string input , string framework , string runtime )
{
return input . Replace ( "{fw}" , framework ) . Replace ( "{rid}" , runtime ) ;
}
[Theory]
// global.json exists
2016-02-11 15:55:37 -08:00
[InlineData("1", true, null, null, "TestLibrary/bin/Debug/{fw}", "TestApp/bin/Debug/{fw}", "TestApp/bin/Debug/{fw}/{rid}")]
[InlineData("2", true, "out", null, "TestLibrary/bin/Debug/{fw}", "TestApp/bin/Debug/{fw}", "out")]
[InlineData("3", true, null, "build", "build/TestLibrary/bin/Debug/{fw}", "build/TestApp/bin/Debug/{fw}", "build/TestApp/bin/Debug/{fw}/{rid}")]
[InlineData("4", true, "out", "build", "build/TestLibrary/bin/Debug/{fw}", "build/TestApp/bin/Debug/{fw}", "out")]
2016-02-03 10:57:25 -08:00
//no global.json
2016-02-11 14:17:20 -08:00
//[InlineData(false, null, null, "TestLibrary/bin/debug/{fw}", "TestApp/bin/debug/{fw}", "TestApp/bin/debug/{fw}/{rid}")]
//[InlineData(false, "out", null, "TestLibrary/bin/debug/{fw}", "TestApp/bin/debug/{fw}", "out")]
2016-02-03 10:57:25 -08:00
//[InlineData(false, null, "build", "build/TestLibrary/bin/debug/{fw}", "build/TestApp/bin/debug/{fw}", "build/TestApp/bin/debug/{fw}/{rid}")]
//[InlineData(false, "out", "build", "build/TestLibrary/bin/debug/{fw}", "build/TestApp/bin/debug/{fw}", "out")]
2016-02-11 15:55:37 -08:00
public void DefaultPaths ( string testIdentifer , bool global , string outputValue , string baseValue , string expectedLibCompile , string expectedAppCompile , string expectedAppRuntime )
2016-02-03 10:57:25 -08:00
{
2016-02-11 15:55:37 -08:00
var testInstance = TestAssetsManager . CreateTestInstance ( "TestAppWithLibrary" , identifier : testIdentifer )
2016-02-11 14:17:20 -08:00
. WithLockFiles ( ) ;
GetProjectInfo ( testInstance . TestRoot ) ;
2016-02-03 10:57:25 -08:00
2016-02-11 14:17:20 -08:00
new BuildCommand ( GetProjectPath ( _testAppDirDirInfo ) ,
output : outputValue ! = null ? Path . Combine ( _testProjectsRoot , outputValue ) : string . Empty ,
buidBasePath : baseValue ! = null ? Path . Combine ( _testProjectsRoot , baseValue ) : string . Empty ,
2016-02-03 10:57:25 -08:00
framework : DefaultFramework )
. ExecuteWithCapturedOutput ( ) . Should ( ) . Pass ( ) ;
2016-02-11 14:17:20 -08:00
var libdebug = _rootDirInfo . Sub ( FormatPath ( expectedLibCompile , DefaultFramework , _runtime ) ) ;
var appdebug = _rootDirInfo . Sub ( FormatPath ( expectedAppCompile , DefaultFramework , _runtime ) ) ;
var appruntime = _rootDirInfo . Sub ( FormatPath ( expectedAppRuntime , DefaultFramework , _runtime ) ) ;
2016-02-03 10:57:25 -08:00
libdebug . Should ( ) . Exist ( ) . And . HaveFiles ( _libCompileFiles ) ;
appdebug . Should ( ) . Exist ( ) . And . HaveFiles ( _appCompileFiles ) ;
appruntime . Should ( ) . Exist ( ) . And . HaveFiles ( _runtimeFiles ) ;
}
[Fact]
public void ResourceTest ( )
{
2016-02-11 15:55:37 -08:00
var testInstance = TestAssetsManager . CreateTestInstance ( "TestAppWithLibrary" )
. WithLockFiles ( ) ;
GetProjectInfo ( testInstance . TestRoot ) ;
2016-02-03 10:57:25 -08:00
var names = new [ ]
{
"uk-UA" ,
"en" ,
"en-US"
} ;
2016-02-11 14:17:20 -08:00
foreach ( var folder in new [ ] { _testAppDirDirInfo , _testLibDirInfo } )
2016-02-03 10:57:25 -08:00
{
foreach ( var name in names )
{
2016-02-11 14:17:20 -08:00
var resourceFile = Path . Combine ( folder . FullName , $"Resource.{name}.resx" ) ;
File . WriteAllText ( resourceFile , "<root></root>" ) ;
2016-02-03 10:57:25 -08:00
}
}
2016-02-11 14:17:20 -08:00
new BuildCommand ( GetProjectPath ( _testAppDirDirInfo ) , framework : DefaultFramework )
2016-02-03 10:57:25 -08:00
. ExecuteWithCapturedOutput ( ) . Should ( ) . Pass ( ) ;
2016-02-11 14:17:20 -08:00
var libdebug = _testLibDirInfo . Sub ( "bin/Debug" ) . Sub ( DefaultFramework ) ;
var appdebug = _testAppDirDirInfo . Sub ( "bin/Debug" ) . Sub ( DefaultFramework ) ;
var appruntime = appdebug . Sub ( _runtime ) ;
2016-02-03 10:57:25 -08:00
foreach ( var name in names )
{
libdebug . Sub ( name ) . Should ( ) . Exist ( ) . And . HaveFile ( "TestLibrary.resources.dll" ) ;
appdebug . Sub ( name ) . Should ( ) . Exist ( ) . And . HaveFile ( "TestApp.resources.dll" ) ;
2016-02-11 14:17:20 -08:00
appruntime . Sub ( name ) . Should ( ) . Exist ( ) . And . HaveFiles ( new [ ] { "TestLibrary.resources.dll" , "TestApp.resources.dll" } ) ;
2016-02-03 10:57:25 -08:00
}
}
private void CopyProjectToTempDir ( string projectDir , TempDirectory tempDir )
{
// copy all the files to temp dir
foreach ( var file in Directory . EnumerateFiles ( projectDir ) )
{
tempDir . CopyFile ( file ) ;
}
}
2016-02-11 14:17:20 -08:00
private string GetProjectPath ( DirectoryInfo projectDir )
2016-02-03 10:57:25 -08:00
{
2016-02-11 14:17:20 -08:00
return Path . Combine ( projectDir . FullName , "project.json" ) ;
2016-02-03 10:57:25 -08:00
}
}
}