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 ;
namespace Microsoft.DotNet.Tools.Builder.Tests
{
public class IncrementalTestBase : TestBase
{
2016-02-11 14:17:20 -08:00
protected virtual string MainProject
{
get ; set ;
}
protected virtual string ExpectedOutput
{
get ; set ;
}
2016-01-14 11:52:54 -08:00
2016-02-11 14:17:20 -08:00
protected virtual string TestProjectRoot
{
get ; set ;
}
protected IncrementalTestBase ( )
{
}
2016-01-14 11:52:54 -08:00
public IncrementalTestBase ( string testProjectsRoot , string mainProject , string expectedOutput )
{
2016-01-26 14:53:56 -08:00
MainProject = mainProject ;
ExpectedOutput = expectedOutput ;
2016-02-11 14:17:20 -08:00
TestProjectRoot = testProjectsRoot ;
2016-01-14 11:52:54 -08:00
}
protected void TouchSourcesOfProject ( )
{
2016-01-26 14:53:56 -08:00
TouchSourcesOfProject ( MainProject ) ;
2016-01-14 11:52:54 -08:00
}
protected void TouchSourcesOfProject ( string projectToTouch )
{
foreach ( var sourceFile in GetSourceFilesForProject ( projectToTouch ) )
{
TouchFile ( sourceFile ) ;
}
}
protected static void TouchFile ( string file )
{
File . SetLastWriteTimeUtc ( file , DateTime . UtcNow ) ;
}
2016-02-05 15:29:34 -08:00
protected CommandResult BuildProject ( bool noDependencies = false , bool noIncremental = false , bool expectBuildFailure = false )
2016-01-14 11:52:54 -08:00
{
2016-01-26 14:53:56 -08:00
var mainProjectFile = GetProjectFile ( MainProject ) ;
2016-02-16 11:26:40 -08:00
return BuildProject ( mainProjectFile , noDependencies , noIncremental , expectBuildFailure ) ;
2016-02-11 14:17:20 -08:00
}
2016-01-14 11:52:54 -08:00
2016-02-12 17:26:58 -08:00
protected CommandResult BuildProject ( string projectFile , bool noDependencies = false , bool noIncremental = false , bool expectBuildFailure = false )
2016-02-11 14:17:20 -08:00
{
2016-03-01 17:35:32 -06:00
var buildCommand = new BuildCommand ( projectFile , output : GetOutputDir ( ) , framework : "netstandardapp1.5" , noIncremental : noIncremental , noDependencies : noDependencies ) ;
2016-01-14 11:52:54 -08:00
var result = buildCommand . ExecuteWithCapturedOutput ( ) ;
if ( ! expectBuildFailure )
{
result . Should ( ) . Pass ( ) ;
2016-02-11 14:17:20 -08:00
TestOutputExecutable ( GetOutputExePath ( ) , buildCommand . GetOutputExecutableName ( ) , ExpectedOutput ) ;
2016-01-14 11:52:54 -08:00
}
else
{
result . Should ( ) . Fail ( ) ;
}
return result ;
}
2016-02-11 14:17:20 -08:00
protected virtual string GetOutputExePath ( )
{
return GetBinRoot ( ) ;
}
protected virtual string GetOutputDir ( )
{
return GetBinRoot ( ) ;
}
2016-01-26 14:53:56 -08:00
protected string GetBinRoot ( )
2016-01-14 11:52:54 -08:00
{
2016-02-11 14:17:20 -08:00
return Path . Combine ( TestProjectRoot , "bin" ) ;
2016-01-14 11:52:54 -08:00
}
protected virtual string GetProjectDirectory ( string projectName )
{
2016-02-11 14:17:20 -08:00
return Path . Combine ( TestProjectRoot ) ;
2016-01-14 11:52:54 -08:00
}
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 ( )
{
2016-03-01 17:35:32 -06:00
var executablePath = Path . Combine ( GetBinRoot ( ) , "Debug" , "netstandardapp1.5" ) ;
2016-01-21 15:01:21 -08:00
return executablePath ;
}
2016-03-03 22:57:43 -08:00
protected string GetIntermediaryOutputPath ( )
{
var executablePath = Path . Combine ( TestProjectRoot , "obj" , "Debug" , "netstandardapp1.5" ) ;
return executablePath ;
}
2016-01-14 11:52:54 -08:00
}
2016-02-11 14:17:20 -08:00
}