Allow functional tests with content to run from VS

Fixes #4543
This commit is contained in:
Pranav K 2016-05-18 15:47:02 -07:00
parent 61883b5101
commit 43c75c922e
2 changed files with 21 additions and 16 deletions

View File

@ -1,18 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
namespace Microsoft.AspNetCore.Mvc.FunctionalTests namespace Microsoft.AspNetCore.Mvc.FunctionalTests
{ {
public class MvcSampleFixture<TStartup> : MvcTestFixture<TStartup> public class MvcSampleFixture<TStartup> : MvcTestFixture<TStartup>
{ {
public MvcSampleFixture() public MvcSampleFixture()
#if NET451 : base("samples")
: base(Path.Combine("..", "..", "..", "..", "..", "..", "samples"))
#else
: base(Path.Combine("..", "..", "..", "..", "..", "samples"))
#endif
{ {
} }
} }

View File

@ -20,18 +20,15 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
{ {
public class MvcTestFixture<TStartup> : IDisposable public class MvcTestFixture<TStartup> : IDisposable
{ {
private const string SolutionName = "Mvc.sln";
private readonly TestServer _server; private readonly TestServer _server;
public MvcTestFixture() public MvcTestFixture()
#if NET451 : this(Path.Combine("test", "WebSites"))
: this(Path.Combine("..", "..", "..", "..", "..", "WebSites"))
#else
: this(Path.Combine("..", "..", "..", "..", "WebSites"))
#endif
{ {
} }
protected MvcTestFixture(string relativePath) protected MvcTestFixture(string solutionRelativePath)
{ {
// RequestLocalizationOptions saves the current culture when constructed, potentially changing response // RequestLocalizationOptions saves the current culture when constructed, potentially changing response
// localization i.e. RequestLocalizationMiddleware behavior. Ensure the saved culture // localization i.e. RequestLocalizationMiddleware behavior. Ensure the saved culture
@ -39,7 +36,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
using (new CultureReplacer()) using (new CultureReplacer())
{ {
var builder = new WebHostBuilder() var builder = new WebHostBuilder()
.UseContentRoot(GetApplicationPath(relativePath)) .UseContentRoot(GetApplicationPath(solutionRelativePath))
.ConfigureServices(InitializeServices) .ConfigureServices(InitializeServices)
.UseStartup(typeof(TStartup)); .UseStartup(typeof(TStartup));
@ -58,12 +55,26 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
_server.Dispose(); _server.Dispose();
} }
private static string GetApplicationPath(string relativePath) private static string GetApplicationPath(string solutionRelativePath)
{ {
var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly; var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly;
var applicationName = startupAssembly.GetName().Name; var applicationName = startupAssembly.GetName().Name;
var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath; var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath;
return Path.GetFullPath(Path.Combine(applicationBasePath, relativePath, applicationName));
var directoryInfo = new DirectoryInfo(applicationBasePath);
do
{
var solutionFileInfo = new FileInfo(Path.Combine(directoryInfo.FullName, SolutionName));
if (solutionFileInfo.Exists)
{
return Path.Combine(directoryInfo.FullName, solutionRelativePath, applicationName);
}
directoryInfo = directoryInfo.Parent;
}
while (directoryInfo.Parent != null);
throw new Exception($"Solution root could not be located using application root {applicationBasePath}.");
} }
protected virtual void InitializeServices(IServiceCollection services) protected virtual void InitializeServices(IServiceCollection services)