Updated to use 0.4.0 version of IntegrationTesting package

This commit is contained in:
Kiran Challa 2017-04-14 14:46:17 -07:00
parent cc1b5f4d5b
commit c18724a275
17 changed files with 77 additions and 122 deletions

3
.gitignore vendored
View File

@ -38,4 +38,5 @@ node_modules
*launchSettings.json
*.orig
BuildInfo.generated.cs
msbuild.log
msbuild.log
global.json

View File

@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<AspNetCoreVersion>2.0.0-*</AspNetCoreVersion>
<AspNetCoreLabsVersion>0.3.0-*</AspNetCoreLabsVersion>
<AspNetCoreIntegrationTestingVersion>0.4.0-*</AspNetCoreIntegrationTestingVersion>
<CoreFxVersion>4.3.0</CoreFxVersion>
<InternalAspNetCoreSdkVersion>2.0.0-*</InternalAspNetCoreSdkVersion>
<NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>

1
global.json Normal file
View File

@ -0,0 +1 @@
{ "sdk": { "version": "2.0.0-preview1-005783" } }

View File

@ -19,13 +19,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
[Fact]
public async Task ConsumingClassLibrariesWithPrecompiledViewsWork()
{
// Arrange
var deploymentResult = Fixture.CreateDeployment();
// Act
var response = await Fixture.HttpClient.GetStringWithRetryAsync(
deploymentResult.ApplicationBaseUri + "Manage/Home",
Fixture.Logger);
var response = await Fixture.HttpClient.GetStringWithRetryAsync("Manage/Home", Fixture.Logger);
// Assert
TestEmbeddedResource.AssertContent("ApplicationConsumingPrecompiledViews.Manage.Home.Index.txt", response);

View File

@ -19,12 +19,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
[Fact]
public async Task Precompilation_WorksForViewsUsingRelativePath()
{
// Arrange
var deploymentResult = Fixture.CreateDeployment();
// Act
var response = await Fixture.HttpClient.GetStringWithRetryAsync(
deploymentResult.ApplicationBaseUri,
Fixture.DeploymentResult.ApplicationBaseUri,
Fixture.Logger);
// Assert
@ -34,12 +31,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
[Fact]
public async Task Precompilation_WorksForViewsUsingDirectoryTraversal()
{
// Arrange
var deploymentResult = Fixture.CreateDeployment();
// Act
var response = await Fixture.HttpClient.GetStringWithRetryAsync(
deploymentResult.ApplicationBaseUri,
Fixture.DeploymentResult.ApplicationBaseUri,
Fixture.Logger);
// Assert

View File

@ -19,12 +19,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
[Fact]
public async Task Precompilation_RunsConfiguredCompilationCallbacks()
{
// Arrange
var deploymentResult = Fixture.CreateDeployment();
// Act
var response = await Fixture.HttpClient.GetStringWithRetryAsync(
deploymentResult.ApplicationBaseUri,
Fixture.DeploymentResult.ApplicationBaseUri,
Fixture.Logger);
// Assert
@ -34,12 +31,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
[Fact]
public async Task Precompilation_UsesConfiguredParseOptions()
{
// Arrange
var deploymentResult = Fixture.CreateDeployment();
// Act
var response = await Fixture.HttpClient.GetStringWithRetryAsync(
deploymentResult.ApplicationBaseUri + "Home/ViewWithPreprocessor",
"Home/ViewWithPreprocessor",
Fixture.Logger);
// Assert

View File

@ -26,11 +26,10 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests
{
// Arrange
var expectedText = "Hello Index!";
var deploymentResult = Fixture.CreateDeployment();
// Act
var response = await Fixture.HttpClient.GetStringWithRetryAsync(
deploymentResult.ApplicationBaseUri,
Fixture.DeploymentResult.ApplicationBaseUri,
Fixture.Logger);
// Assert
@ -46,11 +45,10 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests
"/Views/Home/About.cshtml",
"/Views/Home/Index.cshtml",
};
var deploymentResult = Fixture.CreateDeployment();
// Act
var response2 = await Fixture.HttpClient.GetStringWithRetryAsync(
$"{deploymentResult.ApplicationBaseUri}Home/GetPrecompiledResourceNames",
"Home/GetPrecompiledResourceNames",
Fixture.Logger);
// Assert
@ -73,8 +71,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests
{
"NotIncluded.cshtml",
};
var deploymentResult = Fixture.CreateDeployment();
var viewsDirectory = Path.Combine(deploymentResult.ContentRoot, "Views", "Home");
var viewsDirectory = Path.Combine(Fixture.DeploymentResult.ContentRoot, "Views", "Home");
// Act & Assert
foreach (var file in viewsPublished)

View File

@ -4,58 +4,42 @@
using System;
using System.IO;
using System.Linq;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.Extensions.Logging.Testing;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
{
public class ApplicationWithParseErrorsTest :
IClassFixture<ApplicationWithParseErrorsTest.ApplicationWithParseErrorsFixture>
public class ApplicationWithParseErrorsTest
{
public ApplicationWithParseErrorsTest(ApplicationWithParseErrorsFixture fixture)
{
Fixture = fixture;
}
public ApplicationWithParseErrorsFixture Fixture { get; }
[Fact]
public void PublishingPrintsParseErrors()
public async Task PublishingPrintsParseErrors()
{
var indexPath = Path.Combine(Fixture.ApplicationPath, "Views", "Home", "Index.cshtml");
var viewImportsPath = Path.Combine(Fixture.ApplicationPath, "Views", "Home", "About.cshtml");
// Arrange
var applicationPath = ApplicationPaths.GetTestAppDirectory("ApplicationWithParseErrors");
var indexPath = Path.Combine(applicationPath, "Views", "Home", "Index.cshtml");
var viewImportsPath = Path.Combine(applicationPath, "Views", "Home", "About.cshtml");
var expectedErrors = new[]
{
indexPath + " (0): The code block is missing a closing \"}\" character. Make sure you have a matching \"}\" character for all the \"{\" characters within this block, and that none of the \"}\" characters are being interpreted as markup.",
viewImportsPath + " (1): A space or line break was encountered after the \"@\" character. Only valid identifiers, keywords, comments, \"(\" and \"{\" are valid at the start of a code block and they must occur immediately following \"@\" with no space in between.",
};
// Act & Assert
Assert.Throws<Exception>(() => Fixture.CreateDeployment());
// Assert
var output = Fixture.TestSink.Writes.Select(w => w.State.ToString().Trim()).ToList();
foreach (var error in expectedErrors)
var testSink = new TestSink();
var deploymentParameters = ApplicationTestFixture.GetDeploymentParameters(applicationPath);
var loggerFactory = new TestLoggerFactory(testSink, enabled: true);
using (var deployer = ApplicationDeployerFactory.Create(deploymentParameters, loggerFactory))
{
Assert.Contains(error, output);
}
}
// Act
await Assert.ThrowsAsync<Exception>(() => deployer.DeployAsync());
public class ApplicationWithParseErrorsFixture : ApplicationTestFixture
{
public ApplicationWithParseErrorsFixture()
: base("ApplicationWithParseErrors")
{
}
public TestSink TestSink { get; } = new TestSink();
public override ILogger CreateLogger()
{
return new TestLoggerFactory(TestSink, enabled: true).CreateLogger($"{ApplicationName}");
// Assert
var logs = testSink.Writes.Select(w => w.State.ToString().Trim()).ToList();
foreach (var expectedError in expectedErrors)
{
Assert.Contains(logs, log => log.Contains(expectedError));
}
}
}
}

View File

@ -31,12 +31,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
[MemberData(nameof(ApplicationWithTagHelpersData))]
public async Task Precompilation_WorksForViewsThatUseTagHelpers(string url)
{
// Arrange
var deploymentResult = Fixture.CreateDeployment();
// Act
var response = await Fixture.HttpClient.GetStringWithRetryAsync(
$"{deploymentResult.ApplicationBaseUri}Home/{url}",
$"Home/{url}",
Fixture.Logger);
// Assert

View File

@ -13,51 +13,41 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
public abstract class ApplicationTestFixture : IDisposable
{
public const string DotnetCLITelemetryOptOut = "DOTNET_CLI_TELEMETRY_OPTOUT";
private readonly object _deploymentLock = new object();
private IApplicationDeployer _deployer;
private DeploymentResult _deploymentResult;
protected ApplicationTestFixture(string applicationName)
{
ApplicationName = applicationName;
DeploymentResult = CreateDeployment();
HttpClient = new HttpClient();
HttpClient.BaseAddress = new Uri(DeploymentResult.ApplicationBaseUri);
}
public string ApplicationName { get; }
public string ApplicationPath => ApplicationPaths.GetTestAppDirectory(ApplicationName);
public HttpClient HttpClient { get; } = new HttpClient();
public HttpClient HttpClient { get; }
public ILogger Logger { get; private set; }
public DeploymentResult CreateDeployment()
{
lock (_deploymentLock)
{
if (_deployer != null)
{
return _deploymentResult;
}
public ILoggerFactory LoggerFactory { get; private set; }
Logger = CreateLogger();
var deploymentParameters = GetDeploymentParameters();
var deployer = ApplicationDeployerFactory.Create(deploymentParameters, Logger);
_deploymentResult = deployer.Deploy();
_deployer = deployer;
return _deploymentResult;
}
}
public DeploymentResult DeploymentResult { get; private set; }
public virtual DeploymentParameters GetDeploymentParameters()
{
return GetDeploymentParameters(ApplicationPath);
}
public static DeploymentParameters GetDeploymentParameters(string applicationPath)
{
var telemetryOptOut = new KeyValuePair<string, string>(
DotnetCLITelemetryOptOut,
"1");
var deploymentParameters = new DeploymentParameters(
ApplicationPath,
applicationPath,
ServerType.Kestrel,
RuntimeFlavor.CoreClr,
RuntimeArchitecture.x64)
@ -82,11 +72,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
return deploymentParameters;
}
public virtual ILogger CreateLogger()
public virtual ILoggerFactory CreateLoggerFactory()
{
return new LoggerFactory()
.AddConsole()
.CreateLogger($"{ApplicationName}");
return new LoggerFactory().AddConsole();
}
public void Dispose()
@ -106,5 +94,15 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
// Ignore delete failures.
}
}
private DeploymentResult CreateDeployment()
{
LoggerFactory = CreateLoggerFactory();
Logger = LoggerFactory.CreateLogger(ApplicationName);
var deploymentParameters = GetDeploymentParameters();
_deployer = ApplicationDeployerFactory.Create(deploymentParameters, LoggerFactory);
return _deployer.DeployAsync().Result;
}
}
}

View File

@ -12,6 +12,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
{
public static class HttpClientExtensions
{
public static Task<string> GetStringWithRetryAsync(
this HttpClient httpClient,
ILogger logger)
{
return GetStringWithRetryAsync(httpClient, httpClient.BaseAddress.AbsoluteUri, logger);
}
public static async Task<string> GetStringWithRetryAsync(
this HttpClient httpClient,
string url,

View File

@ -12,7 +12,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Server.IntegrationTesting" Version="$(AspNetCoreLabsVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Server.IntegrationTesting" Version="$(AspNetCoreIntegrationTestingVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Testing" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Testing" Version="$(AspNetCoreVersion)" />

View File

@ -32,15 +32,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests
"/Views/Home/Index.cshtml",
};
var expectedText = "Hello Index!";
var deploymentResult = Fixture.CreateDeployment();
var assemblyPath = Path.Combine(
deploymentResult.DeploymentParameters.PublishedApplicationRootPath,
Fixture.DeploymentResult.DeploymentParameters.PublishedApplicationRootPath,
$"{ApplicationName}.PrecompiledViews.dll");
// Act - 1
var response1 = await Fixture.HttpClient.GetStringWithRetryAsync(
$"{deploymentResult.ApplicationBaseUri}Home/Index",
"Home/Index",
Fixture.Logger);
// Assert - 1
@ -48,7 +47,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.FunctionalTests
// Act - 2
var response2 = await Fixture.HttpClient.GetStringWithRetryAsync(
$"{deploymentResult.ApplicationBaseUri}Home/GetPrecompiledResourceNames",
"Home/GetPrecompiledResourceNames",
Fixture.Logger);
// Assert - 2

View File

@ -18,12 +18,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
[Fact]
public async Task Precompilation_WorksForSimpleApps()
{
// Arrange
var deploymentResult = Fixture.CreateDeployment();
// Act
var response = await Fixture.HttpClient.GetStringWithRetryAsync(
deploymentResult.ApplicationBaseUri,
Fixture.DeploymentResult.ApplicationBaseUri,
Fixture.Logger);
// Assert

View File

@ -19,12 +19,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
[Fact]
public async Task Precompilation_WorksForSimpleApps()
{
// Arrange
var deploymentResult = Fixture.CreateDeployment();
// Act
var response = await Fixture.HttpClient.GetStringWithRetryAsync(
deploymentResult.ApplicationBaseUri,
Fixture.DeploymentResult.ApplicationBaseUri,
Fixture.Logger);
// Assert

View File

@ -18,12 +18,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
[Fact]
public async Task PrecompiledAssembliesUseSameStrongNameAsApplication()
{
// Arrange
var deploymentResult = Fixture.CreateDeployment();
// Act
var response = await Fixture.HttpClient.GetStringWithRetryAsync(
deploymentResult.ApplicationBaseUri,
Fixture.DeploymentResult.ApplicationBaseUri,
Fixture.Logger);
// Assert

View File

@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Xunit;
@ -20,26 +21,22 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
[Fact]
public void Precompilation_PreventsRefAssembliesFromBeingPublished()
{
// Arrange & Act
var deploymentResult = Fixture.CreateDeployment();
// Assert
Assert.False(Directory.Exists(Path.Combine(deploymentResult.ContentRoot, "refs")));
// Act & Assert
Assert.False(Directory.Exists(Path.Combine(Fixture.DeploymentResult.ContentRoot, "refs")));
}
[Fact]
public void PublishingWithOption_AllowsPublishingRefAssemblies()
public async Task PublishingWithOption_AllowsPublishingRefAssemblies()
{
// Arrange
var deploymentParameters = Fixture.GetDeploymentParameters();
deploymentParameters.PublishEnvironmentVariables.Add(
new KeyValuePair<string, string>("MvcRazorExcludeRefAssembliesFromPublish", "false"));
var logger = Fixture.CreateLogger();
using (var deployer = ApplicationDeployerFactory.Create(deploymentParameters, logger))
using (var deployer = ApplicationDeployerFactory.Create(deploymentParameters, Fixture.LoggerFactory))
{
// Act
var deploymentResult = deployer.Deploy();
var deploymentResult = await deployer.DeployAsync();
// Assert
Assert.True(Directory.Exists(Path.Combine(deploymentResult.ContentRoot, "refs")));