[Static web assets] Use prebuilt packages in tests

* Avoids running dotnet restore and dotnet pack during tests which caused test hangs\n\nCommit migrated from 300eeb08d2
This commit is contained in:
Javier Calvarro Nelson 2020-05-04 21:19:08 +02:00 committed by GitHub
parent 1af2060d53
commit 7eca4ab2ec
10 changed files with 28 additions and 185 deletions

View File

@ -15,9 +15,6 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
{
public abstract class MSBuildIntegrationTestBase
{
internal static readonly string LocalNugetPackagesCacheTempPath =
Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()) + Path.DirectorySeparatorChar;
private static readonly AsyncLocal<ProjectDirectory> _project = new AsyncLocal<ProjectDirectory>();
private static readonly AsyncLocal<string> _projectTfm = new AsyncLocal<string>();
@ -49,10 +46,6 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
set { _project.Value = value; }
}
// Whether to use a local cache or not to prevent polluting the global cache
// with test packages.
public bool UseLocalPackageCache { get; set; }
protected string RazorIntermediateOutputPath => Path.Combine(IntermediateOutputPath, "Razor");
protected string RazorComponentIntermediateOutputPath => Path.Combine(IntermediateOutputPath, "RazorDeclaration");
@ -75,14 +68,6 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
{
var timeout = suppressTimeout ? (TimeSpan?)Timeout.InfiniteTimeSpan : null;
// Additional restore sources for packages used in testing
var additionalRestoreSources = string.Join(
',',
typeof(PackageTestProjectsFixture).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
.Where(a => a.Key == "Testing.AdditionalRestoreSources")
.Select(a => a.Value)
.ToArray());
var buildArgumentList = new List<string>
{
// Disable node-reuse. We don't want msbuild processes to stick around
@ -97,20 +82,8 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
$"/p:MicrosoftNETCoreApp50PackageVersion={BuildVariables.MicrosoftNETCoreApp50PackageVersion}",
$"/p:MicrosoftNetCompilersToolsetPackageVersion={BuildVariables.MicrosoftNetCompilersToolsetPackageVersion}",
// Additional restore sources for projects that require built packages
$"/p:RuntimeAdditionalRestoreSources={additionalRestoreSources}",
};
if (UseLocalPackageCache)
{
if (!Directory.Exists(LocalNugetPackagesCacheTempPath))
{
// The local cache folder needs to exist so that nuget
Directory.CreateDirectory(LocalNugetPackagesCacheTempPath);
}
}
if (!suppressBuildServer)
{
buildArgumentList.Add($@"/p:_RazorBuildServerPipeName=""{buildServerPipeName ?? BuildServer.PipeName}""");
@ -132,8 +105,7 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Project,
buildArguments,
timeout,
msBuildProcessKind,
UseLocalPackageCache ? LocalNugetPackagesCacheTempPath : null);
msBuildProcessKind);
}
internal void AddProjectFileContent(string content)

View File

@ -19,8 +19,7 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
ProjectDirectory project,
string arguments,
TimeSpan? timeout = null,
MSBuildProcessKind msBuildProcessKind = MSBuildProcessKind.Dotnet,
string localPackageCache = null)
MSBuildProcessKind msBuildProcessKind = MSBuildProcessKind.Dotnet)
{
var processStartInfo = new ProcessStartInfo()
{
@ -30,11 +29,6 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
RedirectStandardOutput = true,
};
if (localPackageCache != null)
{
processStartInfo.Environment.Add("NUGET_PACKAGES", localPackageCache);
}
if (msBuildProcessKind == MSBuildProcessKind.Desktop)
{
if (string.IsNullOrEmpty(BuildVariables.MSBuildPath))

View File

@ -16,28 +16,22 @@ using Xunit.Abstractions;
namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
{
public class StaticWebAssetsIntegrationTest : MSBuildIntegrationTestBase, IClassFixture<BuildServerTestFixture>, IClassFixture<PackageTestProjectsFixture>, IAsyncLifetime
public class StaticWebAssetsIntegrationTest : MSBuildIntegrationTestBase, IClassFixture<BuildServerTestFixture>
{
public StaticWebAssetsIntegrationTest(
BuildServerTestFixture buildServer,
PackageTestProjectsFixture packageTestProjects,
ITestOutputHelper output)
: base(buildServer)
{
UseLocalPackageCache = true;
PackageTestProjects = packageTestProjects;
Output = output;
}
public PackageTestProjectsFixture PackageTestProjects { get; private set; }
public ITestOutputHelper Output { get; private set; }
[Fact]
[InitializeTestProject("AppWithPackageAndP2PReference",language: "C#", additionalProjects: new[] { "ClassLibrary", "ClassLibrary2" })]
[InitializeTestProject("AppWithPackageAndP2PReference", language: "C#", additionalProjects: new[] { "ClassLibrary", "ClassLibrary2" })]
public async Task Build_GeneratesStaticWebAssetsManifest_Success_CreatesManifest()
{
await RestoreWithRetry();
var result = await DotnetMSBuild("Build");
var expectedManifest = GetExpectedManifest();
@ -65,7 +59,6 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
[InitializeTestProject("AppWithPackageAndP2PReference", additionalProjects: new[] { "ClassLibrary", "ClassLibrary2" })]
public async Task Publish_CopiesStaticWebAssetsToDestinationFolder()
{
await RestoreWithRetry();
var result = await DotnetMSBuild("Publish");
Assert.BuildPassed(result);
@ -115,7 +108,6 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
[InitializeTestProject("AppWithPackageAndP2PReference", additionalProjects: new[] { "ClassLibrary", "ClassLibrary2" })]
public async Task Publish_WithBuildReferencesDisabled_CopiesStaticWebAssetsToDestinationFolder()
{
await RestoreWithRetry();
var build = await DotnetMSBuild("Build");
Assert.BuildPassed(build);
@ -137,7 +129,6 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
[InitializeTestProject("AppWithPackageAndP2PReference", additionalProjects: new[] { "ClassLibrary", "ClassLibrary2" })]
public async Task Publish_NoBuild_CopiesStaticWebAssetsToDestinationFolder()
{
await RestoreWithRetry();
var build = await DotnetMSBuild("Build");
Assert.BuildPassed(build);
@ -159,7 +150,6 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
[InitializeTestProject("SimpleMvc")]
public async Task Build_DoesNotEmbedManifestWhen_NoStaticResourcesAvailable()
{
await RestoreWithRetry();
var result = await DotnetMSBuild("Build");
Assert.BuildPassed(result);
@ -173,10 +163,9 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
}
[Fact]
[InitializeTestProject("AppWithPackageAndP2PReference",language: "C#", additionalProjects: new[] { "ClassLibrary", "ClassLibrary2" })]
[InitializeTestProject("AppWithPackageAndP2PReference", language: "C#", additionalProjects: new[] { "ClassLibrary", "ClassLibrary2" })]
public async Task Clean_Success_RemovesManifestAndCache()
{
await RestoreWithRetry();
var result = await DotnetMSBuild("Build");
Assert.BuildPassed(result);
@ -195,11 +184,10 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
}
[Fact]
[InitializeTestProject("AppWithPackageAndP2PReference",language: "C#", additionalProjects: new[] { "ClassLibrary", "ClassLibrary2" })]
[InitializeTestProject("AppWithPackageAndP2PReference", language: "C#", additionalProjects: new[] { "ClassLibrary", "ClassLibrary2" })]
public async Task Rebuild_Success_RecreatesManifestAndCache()
{
// Arrange
await RestoreWithRetry();
var result = await DotnetMSBuild("Build");
var expectedManifest = GetExpectedManifest();
@ -243,10 +231,9 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
}
[Fact]
[InitializeTestProject("AppWithPackageAndP2PReference",language: "C#", additionalProjects: new[] { "ClassLibrary", "ClassLibrary2" })]
[InitializeTestProject("AppWithPackageAndP2PReference", language: "C#", additionalProjects: new[] { "ClassLibrary", "ClassLibrary2" })]
public async Task GenerateStaticWebAssetsManifest_IncrementalBuild_ReusesManifest()
{
await RestoreWithRetry();
var result = await DotnetMSBuild("GenerateStaticWebAssetsManifest");
Assert.BuildPassed(result);
@ -282,47 +269,6 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
}
}
public Task InitializeAsync()
{
try
{
return PackageTestProjects.PackAsync(Output);
}
catch (Exception ex)
{
// We observed pack would "complete" in the past but MSBuild would get stuck.
// The generated package looked right, but it caused flakyness on our build infrastructure.
// We will try and continue running (even if the process didn't complete) and see if we can
// complete the test successfully.
Output.WriteLine($"Pack failed or did not complete: '{ex}'.");
return Task.CompletedTask;
}
}
private async Task RestoreWithRetry()
{
for (var i = 0; i < 3; i++)
{
try
{
var result = await DotnetMSBuild("Restore");
if (result.ExitCode == 0)
{
break;
}
}
catch
{
// Keep retrying if it fails.
}
}
}
public Task DisposeAsync()
{
return Task.CompletedTask;
}
private string GetExpectedManifest()
{
// We need to do this for Mac as apparently the temp folder in mac is prepended by /private by the os, even though the current user
@ -330,7 +276,14 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
// validate the behavior at runtime.
var source = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? $"/private{Project.SolutionPath}" : Project.SolutionPath;
var restorePath = LocalNugetPackagesCacheTempPath;
var nugetPackages = Environment.GetEnvironmentVariable("NUGET_PACKAGES");
var restorePath = !string.IsNullOrEmpty(nugetPackages) ?
nugetPackages :
Path.Combine(
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Environment.GetEnvironmentVariable("USERPROFILE") : Environment.GetEnvironmentVariable("HOME"),
".nuget",
"packages");
var projects = new[]
{
Path.Combine(restorePath, "packagelibrarytransitivedependency", "1.0.0", "build", "..", "staticwebassets") + Path.DirectorySeparatorChar,
@ -347,71 +300,4 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
</StaticWebAssets>";
}
}
public class PackageTestProjectsFixture
{
private const int MaxPackRetries = 3;
private const int MaxPackTimeoutInMinutes = 5;
private bool _packed;
internal async Task PackAsync(ITestOutputHelper output)
{
if (_packed)
{
return;
}
var projectsToPack = GetProjectsToPack();
foreach (var project in projectsToPack)
{
output.WriteLine(project);
}
foreach (var project in projectsToPack)
{
var psi = new ProcessStartInfo
{
FileName = DotNetMuxer.MuxerPathOrDefault(),
#if DEBUG
Arguments = "msbuild /t:Restore;Pack /p:Configuration=Debug",
#else
Arguments = "msbuild /t:Restore;Pack /p:Configuration=Release",
#endif
WorkingDirectory = project,
RedirectStandardOutput = true,
RedirectStandardError = true
};
for (int i = 0; i < MaxPackRetries; i++)
{
try
{
var result = await MSBuildProcessManager.RunProcessCoreAsync(
psi,
TimeSpan.FromMinutes(MaxPackTimeoutInMinutes));
output.WriteLine(result.Output);
Assert.Equal(0, result.ExitCode);
break;
}
catch
{
await Task.Delay(1000);
}
}
}
_packed = true;
}
public static string[] GetProjectsToPack()
{
return typeof(PackageTestProjectsFixture).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
.Where(a => a.Key == "Testing.ProjectToPack")
.Select(a => a.Value)
.ToArray();
}
}
}

View File

@ -28,19 +28,9 @@
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
<_Parameter1>Testing.ProjectToPack</_Parameter1>
<_Parameter2>$(MSBuildThisFileDirectory)..\testapps\PackageLibraryDirectDependency</_Parameter2>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
<_Parameter1>Testing.ProjectToPack</_Parameter1>
<_Parameter2>$(MSBuildThisFileDirectory)..\testapps\PackageLibraryTransitiveDependency</_Parameter2>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
<_Parameter1>Testing.AdditionalRestoreSources</_Parameter1>
<_Parameter2>$(MSBuildThisFileDirectory)..\testapps\TestPackageRestoreSource</_Parameter2>
<_Parameter2>$(MSBuildThisFileDirectory)..\testapps\PregeneratedPackages</_Parameter2>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
@ -126,6 +116,9 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
<Target Name="RestoreTestProjects" BeforeTargets="Restore;Build" Condition="'$(DotNetBuildFromSource)' != 'true'">
<MSBuild Projects="..\testapps\RestoreTestProjects\RestoreTestProjects.csproj" Targets="Restore" Properties="MicrosoftNetCompilersToolsetPackageVersion=$(MicrosoftNetCompilersToolsetPackageVersion)" />
<MSBuild Projects="..\testapps\PackageLibraryDirectDependency\PackageLibraryDirectDependency.csproj" Targets="Restore" Properties="MicrosoftNetCompilersToolsetPackageVersion=$(MicrosoftNetCompilersToolsetPackageVersion)" />
<MSBuild Projects="..\testapps\PackageLibraryTransitiveDependency\PackageLibraryTransitiveDependency.csproj" Targets="Restore" Properties="MicrosoftNetCompilersToolsetPackageVersion=$(MicrosoftNetCompilersToolsetPackageVersion)" />
<!-- This target restores SimpleMvc11 and SimpleMvc11NetFx separately because otherwise the TFMs in SimpleMvc11.csproj and SimpleMvc11NetFx.csproj are not respected. -->
<PropertyGroup>
<TargetFramework11>netcoreapp1.1</TargetFramework11>

View File

@ -6,7 +6,7 @@
<PropertyGroup>
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<RuntimeAdditionalRestoreSources Condition="'$(RuntimeAdditionalRestoreSources)'==''">$(MSBuildThisFileDirectory)..\TestPackageRestoreSource\</RuntimeAdditionalRestoreSources>
<RuntimeAdditionalRestoreSources Condition="'$(RuntimeAdditionalRestoreSources)'==''">$(MSBuildThisFileDirectory)..\PregeneratedPackages\</RuntimeAdditionalRestoreSources>
<RestoreAdditionalProjectSources>$(RestoreAdditionalProjectSources);$(RuntimeAdditionalRestoreSources)</RestoreAdditionalProjectSources>
</PropertyGroup>

View File

@ -7,7 +7,7 @@
<PropertyGroup>
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<RuntimeAdditionalRestoreSources Condition="'$(RuntimeAdditionalRestoreSources)'=='' AND EXISTS('$(MSBuildThisFileDirectory)..\TestPackageRestoreSource\')">$(MSBuildThisFileDirectory)..\TestPackageRestoreSource\</RuntimeAdditionalRestoreSources>
<RuntimeAdditionalRestoreSources Condition="'$(RuntimeAdditionalRestoreSources)'=='' AND EXISTS('$(MSBuildThisFileDirectory)..\PregeneratedPackages\')">$(MSBuildThisFileDirectory)..\PregeneratedPackages\</RuntimeAdditionalRestoreSources>
<RestoreAdditionalProjectSources>$(RestoreAdditionalProjectSources);$(RuntimeAdditionalRestoreSources)</RestoreAdditionalProjectSources>
</PropertyGroup>

View File

@ -22,15 +22,7 @@
<ProjectReference Include="..\SimpleMvcFSharp\SimpleMvcFSharp.fsproj" />
<ProjectReference Include="..\SimpleMvc\SimpleMvc.csproj" />
<ProjectReference Include="..\SimplePages\SimplePages.csproj" />
<ProjectReference Include="..\PackageLibraryDirectDependency\PackageLibraryDirectDependency.csproj" />
<ProjectReference Include="..\PackageLibraryTransitiveDependency\PackageLibraryTransitiveDependency.csproj" />
<!--
We don't add AppWithPackageAndP2PReference, PackageLibraryDirectDependency or PackageLibraryTransitiveDependency here
as PackageLibraryDirectDependency and PackageLibraryTransitiveDependency are test packages that
need to be produced on the fly and AppWithPackageAndP2PReference depends on them so adding them here
will make restore fail.
-->
<ProjectReference Include="..\AppWithPackageAndP2PReference\AppWithPackageAndP2PReference.csproj" />
<ProjectReference Include="..\..\Microsoft.AspNetCore.Razor.Test.MvcShim.ClassLib\Microsoft.AspNetCore.Razor.Test.MvcShim.ClassLib.csproj"/>
<ProjectReference Include="..\..\Microsoft.AspNetCore.Razor.Test.ComponentShim\Microsoft.AspNetCore.Razor.Test.ComponentShim.csproj"/>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="pregenerated" value=".\PregeneratedPackages\" />
</packageSources>
</configuration>