2016-02-09 16:35:43 -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 FluentAssertions ;
using Microsoft.DotNet.ProjectModel ;
using Microsoft.DotNet.Tools.Compiler ;
using Moq ;
using NuGet.Frameworks ;
using Xunit ;
namespace Microsoft.DotNet.Tools.Compiler.Tests
{
public class GivenACompilationDriverController
{
private string _projectJson ;
private Mock < ICompiler > _managedCompilerMock ;
private Mock < ICompiler > _nativeCompilerMock ;
private List < ProjectContext > _contexts ;
2016-04-19 19:14:59 -07:00
private BuildCommandApp _args ;
2016-02-09 16:35:43 -08:00
public GivenACompilationDriverController ( )
{
_projectJson =
2016-02-16 11:26:40 -08:00
Path . Combine ( AppContext . BaseDirectory , "TestAssets" , "TestProjects" , "TestAppWithLibrary" , "TestApp" , "project.json" ) ;
2016-02-09 16:35:43 -08:00
_managedCompilerMock = new Mock < ICompiler > ( ) ;
_managedCompilerMock . Setup ( c = > c
2016-04-19 19:14:59 -07:00
. Compile ( It . IsAny < ProjectContext > ( ) , It . IsAny < BuildCommandApp > ( ) ) )
2016-02-09 16:35:43 -08:00
. Returns ( true ) ;
_nativeCompilerMock = new Mock < ICompiler > ( ) ;
_nativeCompilerMock . Setup ( c = > c
2016-04-19 19:14:59 -07:00
. Compile ( It . IsAny < ProjectContext > ( ) , It . IsAny < BuildCommandApp > ( ) ) )
2016-02-09 16:35:43 -08:00
. Returns ( true ) ;
_contexts = new List < ProjectContext >
{
2016-04-12 17:29:07 -07:00
ProjectContext . Create ( _projectJson , NuGetFramework . Parse ( "netcoreapp1.0" ) )
2016-02-09 16:35:43 -08:00
} ;
2016-04-19 19:14:59 -07:00
_args = new BuildCommandApp ( "dotnet compile" , ".NET Compiler" , "Compiler for the .NET Platform" ) ;
2016-02-09 16:35:43 -08:00
}
[Fact]
public void It_compiles_all_project_contexts ( )
{
var compiledProjectContexts = new List < ProjectContext > ( ) ;
_managedCompilerMock . Setup ( c = > c
2016-04-19 19:14:59 -07:00
. Compile ( It . IsAny < ProjectContext > ( ) , It . IsAny < BuildCommandApp > ( ) ) )
. Callback < ProjectContext , BuildCommandApp > ( ( p , c ) = > compiledProjectContexts . Add ( p ) )
2016-02-09 16:35:43 -08:00
. Returns ( true ) ;
var compilerController = new CompilationDriver ( _managedCompilerMock . Object , _nativeCompilerMock . Object ) ;
compilerController . Compile ( _contexts , _args ) ;
compiledProjectContexts . Should ( ) . BeEquivalentTo ( _contexts ) ;
}
[Fact]
public void It_does_not_compile_native_when_the_native_parameter_is_not_passed ( )
{
var compilerController = new CompilationDriver ( _managedCompilerMock . Object , _nativeCompilerMock . Object ) ;
compilerController . Compile ( _contexts , _args ) ;
2016-04-19 19:14:59 -07:00
_nativeCompilerMock . Verify ( c = > c . Compile ( It . IsAny < ProjectContext > ( ) , It . IsAny < BuildCommandApp > ( ) ) , Times . Never ) ;
2016-02-09 16:35:43 -08:00
}
[Fact]
public void It_does_compile_native_when_the_native_parameter_is_passed ( )
{
var compilerController = new CompilationDriver ( _managedCompilerMock . Object , _nativeCompilerMock . Object ) ;
_args . IsNativeValue = true ;
compilerController . Compile ( _contexts , _args ) ;
2016-04-19 19:14:59 -07:00
_nativeCompilerMock . Verify ( c = > c . Compile ( It . IsAny < ProjectContext > ( ) , It . IsAny < BuildCommandApp > ( ) ) , Times . Once ) ;
2016-02-09 16:35:43 -08:00
}
}
}