2016-02-10 00:35:43 +00: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 Microsoft.DotNet.ProjectModel;
|
|
|
|
|
using Moq;
|
|
|
|
|
using NuGet.Frameworks;
|
|
|
|
|
using Xunit;
|
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using System.Linq;
|
2016-02-11 01:23:53 +00:00
|
|
|
|
using Microsoft.Extensions.PlatformAbstractions;
|
2016-02-10 00:35:43 +00:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Compiler.Tests
|
|
|
|
|
{
|
|
|
|
|
public class GivenThatICareAboutScriptVariablesFromAManagedCompiler : IClassFixture<ScriptVariablesFixture>
|
|
|
|
|
{
|
|
|
|
|
private readonly ScriptVariablesFixture _fixture;
|
|
|
|
|
|
|
|
|
|
public GivenThatICareAboutScriptVariablesFromAManagedCompiler(ScriptVariablesFixture fixture)
|
|
|
|
|
{
|
|
|
|
|
_fixture = fixture;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-12 00:08:46 +00:00
|
|
|
|
[Fact]
|
|
|
|
|
public void It_passes_a_FullTargetFramework_variable_to_the_pre_compile_scripts()
|
|
|
|
|
{
|
|
|
|
|
_fixture.PreCompileScriptVariables.Should().ContainKey("compile:FullTargetFramework");
|
2016-04-13 00:29:07 +00:00
|
|
|
|
_fixture.PreCompileScriptVariables["compile:FullTargetFramework"].Should().Be(".NETCoreApp,Version=v1.0");
|
2016-02-12 00:08:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-10 00:35:43 +00:00
|
|
|
|
[Fact]
|
|
|
|
|
public void It_passes_a_TargetFramework_variable_to_the_pre_compile_scripts()
|
|
|
|
|
{
|
|
|
|
|
_fixture.PreCompileScriptVariables.Should().ContainKey("compile:TargetFramework");
|
2016-04-13 00:29:07 +00:00
|
|
|
|
_fixture.PreCompileScriptVariables["compile:TargetFramework"].Should().Be("netcoreapp1.0");
|
2016-02-10 00:35:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void It_passes_a_Configuration_variable_to_the_pre_compile_scripts()
|
|
|
|
|
{
|
|
|
|
|
_fixture.PreCompileScriptVariables.Should().ContainKey("compile:Configuration");
|
|
|
|
|
_fixture.PreCompileScriptVariables["compile:Configuration"].Should().Be(
|
|
|
|
|
ScriptVariablesFixture.ConfigValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void It_passes_a_OutputFile_variable_to_the_pre_compile_scripts()
|
|
|
|
|
{
|
|
|
|
|
_fixture.PreCompileScriptVariables.Should().ContainKey("compile:OutputFile");
|
|
|
|
|
_fixture.PreCompileScriptVariables["compile:OutputFile"].Should().Be(ScriptVariablesFixture.OutputFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void It_passes_a_OutputDir_variable_to_the_pre_compile_scripts()
|
|
|
|
|
{
|
|
|
|
|
_fixture.PreCompileScriptVariables.Should().ContainKey("compile:OutputDir");
|
|
|
|
|
_fixture.PreCompileScriptVariables["compile:OutputDir"].Should().Be(ScriptVariablesFixture.OutputPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void It_passes_a_ResponseFile_variable_to_the_pre_compile_scripts()
|
|
|
|
|
{
|
|
|
|
|
_fixture.PreCompileScriptVariables.Should().ContainKey("compile:ResponseFile");
|
|
|
|
|
_fixture.PreCompileScriptVariables["compile:ResponseFile"].Should().Be(ScriptVariablesFixture.ResponseFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void It_passes_a_RuntimeOutputDir_variable_to_the_pre_compile_scripts_if_rid_is_set_in_the_ProjectContext()
|
|
|
|
|
{
|
2016-02-11 01:23:53 +00:00
|
|
|
|
var rid = PlatformServices.Default.Runtime.GetLegacyRestoreRuntimeIdentifier();
|
|
|
|
|
var fixture = ScriptVariablesFixture.GetFixtureWithRids(rid);
|
2016-02-10 00:35:43 +00:00
|
|
|
|
fixture.PreCompileScriptVariables.Should().ContainKey("compile:RuntimeOutputDir");
|
2016-02-11 01:23:53 +00:00
|
|
|
|
fixture.PreCompileScriptVariables["compile:RuntimeOutputDir"].Should().Be(fixture.RuntimeOutputDir);
|
2016-02-10 00:35:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-12 00:08:46 +00:00
|
|
|
|
[Fact]
|
|
|
|
|
public void It_passes_a_FullTargetFramework_variable_to_the_post_compile_scripts()
|
|
|
|
|
{
|
|
|
|
|
_fixture.PostCompileScriptVariables.Should().ContainKey("compile:FullTargetFramework");
|
2016-04-13 00:29:07 +00:00
|
|
|
|
_fixture.PostCompileScriptVariables["compile:FullTargetFramework"].Should().Be(".NETCoreApp,Version=v1.0");
|
2016-02-12 00:08:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-10 00:35:43 +00:00
|
|
|
|
[Fact]
|
|
|
|
|
public void It_passes_a_TargetFramework_variable_to_the_post_compile_scripts()
|
|
|
|
|
{
|
|
|
|
|
_fixture.PostCompileScriptVariables.Should().ContainKey("compile:TargetFramework");
|
2016-04-13 00:29:07 +00:00
|
|
|
|
_fixture.PostCompileScriptVariables["compile:TargetFramework"].Should().Be("netcoreapp1.0");
|
2016-02-10 00:35:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void It_passes_a_Configuration_variable_to_the_post_compile_scripts()
|
|
|
|
|
{
|
|
|
|
|
_fixture.PostCompileScriptVariables.Should().ContainKey("compile:Configuration");
|
|
|
|
|
_fixture.PostCompileScriptVariables["compile:Configuration"].Should().Be(
|
|
|
|
|
ScriptVariablesFixture.ConfigValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void It_passes_a_OutputFile_variable_to_the_post_compile_scripts()
|
|
|
|
|
{
|
|
|
|
|
_fixture.PostCompileScriptVariables.Should().ContainKey("compile:OutputFile");
|
|
|
|
|
_fixture.PostCompileScriptVariables["compile:OutputFile"].Should().Be(ScriptVariablesFixture.OutputFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void It_passes_a_OutputDir_variable_to_the_post_compile_scripts()
|
|
|
|
|
{
|
|
|
|
|
_fixture.PostCompileScriptVariables.Should().ContainKey("compile:OutputDir");
|
|
|
|
|
_fixture.PostCompileScriptVariables["compile:OutputDir"].Should().Be(ScriptVariablesFixture.OutputPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void It_passes_a_ResponseFile_variable_to_the_post_compile_scripts()
|
|
|
|
|
{
|
|
|
|
|
_fixture.PostCompileScriptVariables.Should().ContainKey("compile:ResponseFile");
|
|
|
|
|
_fixture.PostCompileScriptVariables["compile:ResponseFile"].Should().Be(ScriptVariablesFixture.ResponseFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void It_passes_a_CompilerExitCode_variable_to_the_post_compile_scripts()
|
|
|
|
|
{
|
|
|
|
|
_fixture.PostCompileScriptVariables.Should().ContainKey("compile:CompilerExitCode");
|
|
|
|
|
_fixture.PostCompileScriptVariables["compile:CompilerExitCode"].Should().Be("0");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void It_passes_a_RuntimeOutputDir_variable_to_the_post_compile_scripts_if_rid_is_set_in_the_ProjectContext()
|
|
|
|
|
{
|
2016-02-11 01:23:53 +00:00
|
|
|
|
var rid = PlatformServices.Default.Runtime.GetLegacyRestoreRuntimeIdentifier();
|
|
|
|
|
var fixture = ScriptVariablesFixture.GetFixtureWithRids(rid);
|
2016-02-10 00:35:43 +00:00
|
|
|
|
fixture.PostCompileScriptVariables.Should().ContainKey("compile:RuntimeOutputDir");
|
2016-02-11 01:23:53 +00:00
|
|
|
|
fixture.PostCompileScriptVariables["compile:RuntimeOutputDir"].Should().Be(fixture.RuntimeOutputDir);
|
2016-02-10 00:35:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ScriptVariablesFixture
|
|
|
|
|
{
|
2016-04-13 00:29:07 +00:00
|
|
|
|
public readonly NuGetFramework TestAssetFramework = FrameworkConstants.CommonFrameworks.NetCoreApp10;
|
2016-02-10 00:35:43 +00:00
|
|
|
|
public const string ConfigValue = "Debug";
|
|
|
|
|
|
|
|
|
|
public static string TestAssetPath = Path.Combine(
|
|
|
|
|
AppContext.BaseDirectory,
|
|
|
|
|
"TestAssets",
|
|
|
|
|
"TestProjects",
|
2016-02-16 19:26:40 +00:00
|
|
|
|
"TestAppWithLibrary",
|
2016-02-10 00:35:43 +00:00
|
|
|
|
"TestApp");
|
|
|
|
|
|
|
|
|
|
public static string OutputPath = Path.Combine(
|
|
|
|
|
TestAssetPath,
|
|
|
|
|
"bin",
|
|
|
|
|
ConfigValue,
|
2016-04-13 00:29:07 +00:00
|
|
|
|
"netcoreapp1.0");
|
2016-02-10 00:35:43 +00:00
|
|
|
|
|
2016-02-11 01:23:53 +00:00
|
|
|
|
public string RuntimeOutputDir { get; private set; }
|
2016-02-10 00:35:43 +00:00
|
|
|
|
|
|
|
|
|
public static string OutputFile = Path.Combine(OutputPath, "TestApp.dll");
|
|
|
|
|
|
|
|
|
|
public static string ResponseFile = Path.Combine(
|
|
|
|
|
TestAssetPath,
|
|
|
|
|
"obj",
|
|
|
|
|
ConfigValue,
|
2016-04-13 00:29:07 +00:00
|
|
|
|
"netcoreapp1.0",
|
2016-02-10 00:35:43 +00:00
|
|
|
|
"dotnet-compile.rsp");
|
|
|
|
|
|
|
|
|
|
public Dictionary<string, string> PreCompileScriptVariables { get; private set; }
|
|
|
|
|
public Dictionary<string, string> PostCompileScriptVariables { get; private set; }
|
|
|
|
|
|
2016-02-11 01:23:53 +00:00
|
|
|
|
public ScriptVariablesFixture() : this(string.Empty)
|
2016-02-10 00:35:43 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-11 01:23:53 +00:00
|
|
|
|
private ScriptVariablesFixture(string rid)
|
2016-02-10 00:35:43 +00:00
|
|
|
|
{
|
|
|
|
|
var projectJson = Path.Combine(TestAssetPath, "project.json");
|
|
|
|
|
var command = new Mock<ICommand>();
|
|
|
|
|
command.Setup(c => c.Execute()).Returns(new CommandResult());
|
|
|
|
|
command.Setup(c => c.OnErrorLine(It.IsAny<Action<string>>())).Returns(() => command.Object);
|
|
|
|
|
command.Setup(c => c.OnOutputLine(It.IsAny<Action<string>>())).Returns(() => command.Object);
|
|
|
|
|
var commandFactory = new Mock<ICommandFactory>();
|
|
|
|
|
commandFactory.Setup(c => c
|
|
|
|
|
.Create(
|
|
|
|
|
It.IsAny<string>(),
|
|
|
|
|
It.IsAny<IEnumerable<string>>(),
|
2016-02-06 02:55:15 +00:00
|
|
|
|
It.IsAny<NuGetFramework>(),
|
|
|
|
|
It.IsAny<string>()))
|
2016-02-10 00:35:43 +00:00
|
|
|
|
.Returns(command.Object);
|
|
|
|
|
|
|
|
|
|
var _args = new CompilerCommandApp("dotnet compile", ".NET Compiler", "Compiler for the .NET Platform");
|
|
|
|
|
_args.ConfigValue = ConfigValue;
|
|
|
|
|
|
|
|
|
|
PreCompileScriptVariables = new Dictionary<string, string>();
|
|
|
|
|
PostCompileScriptVariables = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
var _scriptRunner = new Mock<IScriptRunner>();
|
|
|
|
|
_scriptRunner.Setup(
|
|
|
|
|
s =>
|
|
|
|
|
s.RunScripts(It.IsAny<ProjectContext>(), It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()))
|
|
|
|
|
.Callback<ProjectContext, string, Dictionary<string, string>>((p, n, v) =>
|
|
|
|
|
{
|
|
|
|
|
if (n.Equals(ScriptNames.PreCompile))
|
|
|
|
|
{
|
|
|
|
|
PreCompileScriptVariables = v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (n.Equals(ScriptNames.PostCompile))
|
|
|
|
|
{
|
|
|
|
|
PostCompileScriptVariables = v;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var managedCompiler = new ManagedCompiler(_scriptRunner.Object, commandFactory.Object);
|
|
|
|
|
|
2016-02-11 01:23:53 +00:00
|
|
|
|
var rids = new List<string>();
|
|
|
|
|
if (!string.IsNullOrEmpty(rid))
|
|
|
|
|
{
|
|
|
|
|
rids.Add(rid);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-13 00:29:07 +00:00
|
|
|
|
var context = ProjectContext.Create(projectJson, TestAssetFramework, rids);
|
2016-02-10 00:35:43 +00:00
|
|
|
|
managedCompiler.Compile(context, _args);
|
2016-02-11 01:23:53 +00:00
|
|
|
|
|
|
|
|
|
RuntimeOutputDir = Path.Combine(OutputPath, rid);
|
2016-02-10 00:35:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-11 01:23:53 +00:00
|
|
|
|
public static ScriptVariablesFixture GetFixtureWithRids(string rid)
|
2016-02-10 00:35:43 +00:00
|
|
|
|
{
|
2016-02-11 01:23:53 +00:00
|
|
|
|
return new ScriptVariablesFixture(rid);
|
2016-02-10 00:35:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|