Add support for running tests using desktop msbuild

This commit is contained in:
Pranav K 2018-01-03 12:20:03 -08:00
parent 98e74b9a69
commit 1849056093
8 changed files with 140 additions and 14 deletions

View File

@ -7,4 +7,27 @@
<AdditionalProperties>Configuration=$(Configuration)NoVSIX</AdditionalProperties>
</Solutions>
</ItemGroup>
<PropertyGroup>
<PrepareDependsOn>$(PrepareDependsOn);GenerateMSBuildLocationFile</PrepareDependsOn>
<RazorDesignTestProject>$(RepositoryRoot)test\Microsoft.AspNetCore.Razor.Design.Test\</RazorDesignTestProject>
<MSBuildLocationFileTemplate>$(RazorDesignTestProject)BuildVariables.cs.template</MSBuildLocationFileTemplate>
<MSBuildLocationFileOutput>$(RazorDesignTestProject)obj\BuildVariables.generated.cs</MSBuildLocationFileOutput>
</PropertyGroup>
<Target Name="GenerateMSBuildLocationFile"
DependsOnTargets="GetToolsets"
Inputs="$(MSBuildLocationFileTemplate);$(VisualStudioMSBuildx86Path)"
Outputs="$(MSBuildLocationFileOutput)">
<PropertyGroup>
<TemplateProperties>MSBuildLocation=$(VisualStudioMSBuildx86Path)</TemplateProperties>
</PropertyGroup>
<GenerateFileFromTemplate
TemplateFile="$(MSBuildLocationFileTemplate)"
Properties="$(TemplateProperties)"
OutputPath="$(MSBuildLocationFileOutput)" />
</Target>
</Project>

View File

@ -0,0 +1,21 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
{
internal static partial class BuildVariables
{
private static string _msBuildPath = "";
static partial void InitializeVariables();
public static string MSBuildPath
{
get
{
InitializeVariables();
return _msBuildPath;
}
}
}
}

View File

@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
{
internal static partial class BuildVariables
{
static partial void InitializeVariables()
{
_msBuildPath = @"${MSBuildLocation}";
}
}
}

View File

@ -3,6 +3,7 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.DotNet.PlatformAbstractions;
using Xunit;
@ -12,9 +13,19 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
{
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task Build_SimpleMvc_CanBuildSuccessfully()
public Task Build_SimpleMvc_UsingDotnetMSBuild_CanBuildSuccessfully()
=> Build_SimpleMvc_CanBuildSuccessfully(MSBuildProcessKind.Dotnet);
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux)]
[OSSkipCondition(OperatingSystems.MacOSX)]
[InitializeTestProject("SimpleMvc")]
public Task Build_SimpleMvc_UsingDesktopMSBuild_CanBuildSuccessfully()
=> Build_SimpleMvc_CanBuildSuccessfully(MSBuildProcessKind.Desktop);
private async Task Build_SimpleMvc_CanBuildSuccessfully(MSBuildProcessKind msBuildProcessKind)
{
var result = await DotnetMSBuild("Build", "/p:RazorCompileOnBuild=true");
var result = await DotnetMSBuild("Build", "/p:RazorCompileOnBuild=true", msBuildProcessKind: msBuildProcessKind);
Assert.BuildPassed(result);
Assert.FileExists(result, OutputPath, "SimpleMvc.dll");

View File

@ -42,12 +42,21 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
protected string TargetFramework { get; set; } = "netcoreapp2.0";
internal Task<MSBuildResult> DotnetMSBuild(string target, string args = null, bool suppressRestore = false, bool suppressTimeout = false)
internal Task<MSBuildResult> DotnetMSBuild(
string target,
string args = null,
bool suppressRestore = false,
bool suppressTimeout = false,
MSBuildProcessKind msBuildProcessKind = MSBuildProcessKind.Dotnet)
{
var timeout = suppressTimeout ? (TimeSpan?)Timeout.InfiniteTimeSpan : null;
var restoreArgument = suppressRestore ? "" : "/restore";
return MSBuildProcessManager.RunProcessAsync(Project, $"{restoreArgument} /t:{target} /p:Configuration={Configuration} {args}", timeout);
return MSBuildProcessManager.RunProcessAsync(
Project,
$"{restoreArgument} /t:{target} /p:Configuration={Configuration} {args}",
timeout,
msBuildProcessKind);
}
internal void ReplaceContent(string content, params string[] paths)

View File

@ -0,0 +1,20 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
{
internal enum MSBuildProcessKind
{
/// <summary>dotnet msbuild</summary>
Dotnet,
/// <summary>msbuild.exe</summary>
Desktop,
}
}

View File

@ -11,21 +11,41 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
{
internal static class MSBuildProcessManager
{
public static Task<MSBuildResult> RunProcessAsync(ProjectDirectory project, string arguments, TimeSpan? timeout = null)
public static Task<MSBuildResult> RunProcessAsync(
ProjectDirectory project,
string arguments,
TimeSpan? timeout = null,
MSBuildProcessKind msBuildProcessKind = MSBuildProcessKind.Dotnet)
{
timeout = timeout ?? TimeSpan.FromSeconds(30);
var processStartInfo = new ProcessStartInfo()
{
WorkingDirectory = project.DirectoryPath,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
};
if (msBuildProcessKind == MSBuildProcessKind.Desktop)
{
if (string.IsNullOrEmpty(BuildVariables.MSBuildPath))
{
throw new ArgumentException("Unable to locate MSBuild.exe to run desktop tests.");
}
processStartInfo.FileName = BuildVariables.MSBuildPath;
processStartInfo.Arguments = arguments;
}
else
{
processStartInfo.FileName = "dotnet";
processStartInfo.Arguments = $"msbuild {arguments}";
}
var process = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = "dotnet",
Arguments = "msbuild " + arguments,
WorkingDirectory = project.DirectoryPath,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
},
StartInfo = processStartInfo,
EnableRaisingEvents = true,
};

View File

@ -12,10 +12,13 @@
<DefineConstants Condition="'$(PreserveWorkingDirectory)'=='true'">$(DefineConstants);PRESERVE_WORKING_DIRECTORY</DefineConstants>
<!-- Copy references locally so that we can use them in the test. -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<BuildVariablesGeneratedFile>$(MSBuildProjectDirectory)\obj\BuildVariables.generated.cs</BuildVariablesGeneratedFile>
<CompileDependsOn>EnsureBuildVariablesGeneratedFile;$(CompileDependsOn)</CompileDependsOn>
</PropertyGroup>
<ItemGroup>
<None Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
<Compile Include="$(BuildVariablesGeneratedFile)" Condition="Exists('$(BuildVariablesGeneratedFile)')" />
</ItemGroup>
<ItemGroup>
@ -40,5 +43,11 @@
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>
<Target Name="EnsureBuildVariablesGeneratedFile">
<Error
Text="BuildVariables.generated.cs was not found. Run .\build /t:Prepare from the root of the repository to generate it."
Condition="!Exists('$(BuildVariablesGeneratedFile)')" />
</Target>
</Project>