2016-01-14 11:52:54 -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.Collections.Generic ;
using System.IO ;
using System.Linq ;
using Microsoft.DotNet.Cli.Utils ;
using Microsoft.DotNet.Tools.Test.Utilities ;
using Xunit ;
namespace Microsoft.DotNet.Tools.Builder.Tests
{
public class IncrementalTestBase : TestBase
{
protected readonly TempDirectory _tempProjectRoot ;
private readonly string _testProjectsRoot ;
protected readonly string _mainProject ;
protected readonly string _expectedOutput ;
public IncrementalTestBase ( string testProjectsRoot , string mainProject , string expectedOutput )
{
_testProjectsRoot = testProjectsRoot ;
_mainProject = mainProject ;
_expectedOutput = expectedOutput ;
// create unique directories in the 'temp' folder
var root = Temp . CreateDirectory ( ) ;
// recursively copy projects to the temp dir and restore them
_tempProjectRoot = root . CopyDirectory ( testProjectsRoot ) ;
RunRestore ( _tempProjectRoot . Path ) ;
}
protected void TouchSourcesOfProject ( )
{
TouchSourcesOfProject ( _mainProject ) ;
}
protected void TouchSourcesOfProject ( string projectToTouch )
{
foreach ( var sourceFile in GetSourceFilesForProject ( projectToTouch ) )
{
TouchFile ( sourceFile ) ;
}
}
protected static void TouchFile ( string file )
{
File . SetLastWriteTimeUtc ( file , DateTime . UtcNow ) ;
}
protected CommandResult BuildProject ( bool forceIncrementalUnsafe = false , bool expectBuildFailure = false )
{
var outputDir = GetBinDirectory ( ) ;
2016-01-20 16:00:19 -08:00
var intermediateOutputDir = Path . Combine ( Directory . GetParent ( outputDir ) . FullName , "obj" , _mainProject ) ;
var mainProjectFile = GetProjectFile ( _mainProject ) ;
2016-01-14 11:52:54 -08:00
2016-01-20 16:00:19 -08:00
var buildCommand = new BuildCommand ( mainProjectFile , output : outputDir , tempOutput : intermediateOutputDir , forceIncrementalUnsafe : forceIncrementalUnsafe ) ;
2016-01-14 11:52:54 -08:00
var result = buildCommand . ExecuteWithCapturedOutput ( ) ;
if ( ! expectBuildFailure )
{
result . Should ( ) . Pass ( ) ;
TestOutputExecutable ( outputDir , buildCommand . GetOutputExecutableName ( ) , _expectedOutput ) ;
}
else
{
result . Should ( ) . Fail ( ) ;
}
return result ;
}
protected static void AssertProjectSkipped ( string skippedProject , CommandResult buildResult )
{
2016-01-26 06:39:13 -08:00
Assert . Contains ( $"Project {skippedProject} (DNXCore,Version=v5.0) was previously compiled. Skipping compilation." , buildResult . StdOut , StringComparison . OrdinalIgnoreCase ) ;
2016-01-14 11:52:54 -08:00
}
protected static void AssertProjectCompiled ( string rebuiltProject , CommandResult buildResult )
{
2016-01-26 06:39:13 -08:00
Assert . Contains ( $"Project {rebuiltProject} (DNXCore,Version=v5.0) will be compiled" , buildResult . StdOut , StringComparison . OrdinalIgnoreCase ) ;
2016-01-14 11:52:54 -08:00
}
protected string GetBinDirectory ( )
{
return Path . Combine ( _tempProjectRoot . Path , "bin" ) ;
}
protected virtual string GetProjectDirectory ( string projectName )
{
return Path . Combine ( _tempProjectRoot . Path ) ;
}
protected string GetProjectFile ( string projectName )
{
return Path . Combine ( GetProjectDirectory ( projectName ) , "project.json" ) ;
}
private string GetOutputFileForProject ( string projectName )
{
2016-01-21 15:01:21 -08:00
return Path . Combine ( GetCompilationOutputPath ( ) , projectName + ".dll" ) ;
2016-01-14 11:52:54 -08:00
}
private IEnumerable < string > GetSourceFilesForProject ( string projectName )
{
return Directory . EnumerateFiles ( GetProjectDirectory ( projectName ) ) .
Where ( f = > f . EndsWith ( ".cs" ) ) ;
}
2016-01-21 15:01:21 -08:00
protected string GetCompilationOutputPath ( )
{
var executablePath = Path . Combine ( GetBinDirectory ( ) , "Debug" , "dnxcore50" ) ;
return executablePath ;
}
2016-01-14 11:52:54 -08:00
private void RunRestore ( string args )
{
var restoreCommand = new RestoreCommand ( ) ;
restoreCommand . Execute ( $"--quiet {args}" ) . Should ( ) . Pass ( ) ;
}
}
}