From 3369bb6e904e01efea3f9d3089d6c7cb38e720a8 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Fri, 19 Aug 2016 14:39:21 -0700 Subject: [PATCH] Fix test runs on xplat --- .../ApplicationWithConfigureMvcTest.cs | 26 ++++------------ .../ApplicationWithTagHelpersTest.cs | 10 ++---- .../Infrastructure/ApplicationTestFixture.cs | 13 +++++--- .../Infrastructure/HttpClientExtensions.cs | 31 +++++++++++++++++++ .../Infrastructure/RuntimeFlavors.cs | 24 ++++++++++++++ .../SimpleAppTest.cs | 15 +++++++-- .../project.json | 2 +- .../RazorRewriter.cs | 3 +- 8 files changed, 86 insertions(+), 38 deletions(-) create mode 100644 test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/Infrastructure/HttpClientExtensions.cs create mode 100644 test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/Infrastructure/RuntimeFlavors.cs diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/ApplicationWithConfigureMvcTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/ApplicationWithConfigureMvcTest.cs index f19c4d15fd..63c8225cc4 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/ApplicationWithConfigureMvcTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/ApplicationWithConfigureMvcTest.cs @@ -21,29 +21,16 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests public ApplicationTestFixture Fixture { get; } - public static IEnumerable ApplicationWithTagHelpersData + public static IEnumerable SupportedFlavorsTheoryData { get { - var runtimeFlavors = new[] - { - RuntimeFlavor.Clr, - RuntimeFlavor.CoreClr, - }; - - var urls = new[] - { - "Index", - "ViewWithPreprocessor", - }; - - return Enumerable.Zip(urls, runtimeFlavors, (a, b) => new object[] { a, b }); + return RuntimeFlavors.SupportedFlavors.Select(f => new object[] { f }); } } [Theory] - [InlineData(RuntimeFlavor.Clr)] - [InlineData(RuntimeFlavor.CoreClr)] + [MemberData(nameof(SupportedFlavorsTheoryData))] public async Task Precompilation_RunsConfiguredCompilationCallbacks(RuntimeFlavor flavor) { // Arrange @@ -56,7 +43,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests }; // Act - var response = await httpClient.GetStringAsync(""); + var response = await httpClient.GetStringWithRetryAsync("", Fixture.Logger); // Assert TestEmbeddedResource.AssertContent("ApplicationWithConfigureMvc.Home.Index.txt", response); @@ -64,8 +51,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests } [Theory] - [InlineData(RuntimeFlavor.Clr)] - [InlineData(RuntimeFlavor.CoreClr)] + [MemberData(nameof(SupportedFlavorsTheoryData))] public async Task Precompilation_UsesConfiguredParseOptions(RuntimeFlavor flavor) { // Arrange @@ -78,7 +64,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests }; // Act - var response = await httpClient.GetStringAsync("Home/ViewWithPreprocessor"); + var response = await httpClient.GetStringWithRetryAsync("Home/ViewWithPreprocessor", Fixture.Logger); // Assert TestEmbeddedResource.AssertContent( diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/ApplicationWithTagHelpersTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/ApplicationWithTagHelpersTest.cs index b2fddca721..dcb6219a8e 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/ApplicationWithTagHelpersTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/ApplicationWithTagHelpersTest.cs @@ -25,12 +25,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation { get { - var runtimeFlavors = new[] - { - RuntimeFlavor.Clr, - RuntimeFlavor.CoreClr, - }; - var urls = new[] { "ClassLibraryTagHelper", @@ -38,7 +32,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation "NuGetPackageTagHelper", }; - return Enumerable.Zip(urls, runtimeFlavors, (a, b) => new object[] { a, b }); + return Enumerable.Zip(urls, RuntimeFlavors.SupportedFlavors, (a, b) => new object[] { a, b }); } } @@ -56,7 +50,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation }; // Act - var response = await httpClient.GetStringAsync($"Home/{url}"); + var response = await httpClient.GetStringWithRetryAsync($"Home/{url}", Fixture.Logger); // Assert TestEmbeddedResource.AssertContent($"ApplicationWithTagHelpers.Home.{url}.txt", response); diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/Infrastructure/ApplicationTestFixture.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/Infrastructure/ApplicationTestFixture.cs index 3e18e43254..91e28815a2 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/Infrastructure/ApplicationTestFixture.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/Infrastructure/ApplicationTestFixture.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; +using System.Threading.Tasks; using Microsoft.AspNetCore.Server.IntegrationTesting; using Microsoft.DotNet.Cli.Utils; using Microsoft.Extensions.Logging; @@ -30,8 +31,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation public string TempRestoreDirectory { get; } = CreateTempRestoreDirectory(); + public ILogger Logger { get; private set; } + public IApplicationDeployer CreateDeployment(RuntimeFlavor flavor) { + Logger = new LoggerFactory() + .AddConsole() + .CreateLogger($"{ApplicationName}:{flavor}"); + if (!_isRestored) { Restore(); @@ -61,11 +68,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation }, }; - var logger = new LoggerFactory() - .AddConsole() - .CreateLogger($"{ApplicationName}:{flavor}"); - - return ApplicationDeployerFactory.Create(deploymentParameters, logger); + return ApplicationDeployerFactory.Create(deploymentParameters, Logger); } protected virtual void Restore() diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/Infrastructure/HttpClientExtensions.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/Infrastructure/HttpClientExtensions.cs new file mode 100644 index 0000000000..553608c120 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/Infrastructure/HttpClientExtensions.cs @@ -0,0 +1,31 @@ +// 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.Net.Http; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Server.IntegrationTesting; +using Microsoft.Extensions.Logging; +using Xunit; + +namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation +{ + public static class HttpClientExtensions + { + public static async Task GetStringWithRetryAsync( + this HttpClient httpClient, + string url, + ILogger logger) + { + var response = await RetryHelper.RetryRequest(() => httpClient.GetAsync(url), logger, retryCount: 5); + var content = await response.Content.ReadAsStringAsync(); + + Assert.True(response.IsSuccessStatusCode, + $"Failed to GET content from {url}. Status code {response.StatusCode}." + + Environment.NewLine + + content); + + return content; + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/Infrastructure/RuntimeFlavors.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/Infrastructure/RuntimeFlavors.cs new file mode 100644 index 0000000000..e4781e38db --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/Infrastructure/RuntimeFlavors.cs @@ -0,0 +1,24 @@ +// 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.Collections.Generic; +using System.Runtime.InteropServices; +using Microsoft.AspNetCore.Server.IntegrationTesting; + +namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation +{ + public static class RuntimeFlavors + { + public static IEnumerable SupportedFlavors + { + get + { + yield return RuntimeFlavor.CoreClr; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + yield return RuntimeFlavor.Clr; + } + } + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/SimpleAppTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/SimpleAppTest.cs index aaa2944ac4..d0a89b9bf7 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/SimpleAppTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/SimpleAppTest.cs @@ -2,6 +2,8 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Generic; +using System.Linq; using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Server.IntegrationTesting; @@ -18,9 +20,16 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation public ApplicationTestFixture Fixture { get; } + public static IEnumerable SupportedFlavorsTheoryData + { + get + { + return RuntimeFlavors.SupportedFlavors.Select(f => new object[] { f }); + } + } + [Theory] - [InlineData(RuntimeFlavor.Clr)] - [InlineData(RuntimeFlavor.CoreClr)] + [MemberData(nameof(SupportedFlavorsTheoryData))] public async Task Precompilation_WorksForSimpleApps(RuntimeFlavor flavor) { // Arrange @@ -33,7 +42,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Precompilation }; // Act - var response = await httpClient.GetStringAsync(""); + var response = await httpClient.GetStringWithRetryAsync("", Fixture.Logger); // Assert TestEmbeddedResource.AssertContent("SimpleAppTest.Home.Index.txt", response); diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/project.json b/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/project.json index 3e08ef5540..754ecbfe63 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/project.json +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Precompilation.Tests/project.json @@ -1,7 +1,7 @@ { "buildOptions": { "define": [ - "GENERATE_BASELINES" + "__remove_this_to__GENERATE_BASELINES" ], "embed": "Resources/*" }, diff --git a/testapps/ApplicationWithConfigureMvc/RazorRewriter.cs b/testapps/ApplicationWithConfigureMvc/RazorRewriter.cs index 71ab2e314e..5c956c8e70 100644 --- a/testapps/ApplicationWithConfigureMvc/RazorRewriter.cs +++ b/testapps/ApplicationWithConfigureMvc/RazorRewriter.cs @@ -10,7 +10,8 @@ namespace ApplicationWithConfigureStartup { if (node.Token.IsKind(SyntaxKind.StringLiteralToken)) { - return node.WithToken(SyntaxFactory.Literal(node.Token.ValueText.Replace("\r\n", "\r\n
"))); + return node.WithToken(SyntaxFactory.Literal( + node.Token.ValueText.Replace(Environment.NewLine, Environment.NewLine + "
"))); } return node;