diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcSampleFixture.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcSampleFixture.cs index ea4594d24f..6a09db1a65 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcSampleFixture.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcSampleFixture.cs @@ -1,18 +1,12 @@ // 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.IO; - namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public class MvcSampleFixture : MvcTestFixture { public MvcSampleFixture() -#if NET451 - : base(Path.Combine("..", "..", "..", "..", "..", "..", "samples")) -#else - : base(Path.Combine("..", "..", "..", "..", "..", "samples")) -#endif + : base("samples") { } } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcTestFixture.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcTestFixture.cs index 7963c213ba..026f90f464 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcTestFixture.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcTestFixture.cs @@ -20,18 +20,15 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public class MvcTestFixture : IDisposable { + private const string SolutionName = "Mvc.sln"; private readonly TestServer _server; public MvcTestFixture() -#if NET451 - : this(Path.Combine("..", "..", "..", "..", "..", "WebSites")) -#else - : this(Path.Combine("..", "..", "..", "..", "WebSites")) -#endif + : this(Path.Combine("test", "WebSites")) { } - protected MvcTestFixture(string relativePath) + protected MvcTestFixture(string solutionRelativePath) { // RequestLocalizationOptions saves the current culture when constructed, potentially changing response // localization i.e. RequestLocalizationMiddleware behavior. Ensure the saved culture @@ -39,7 +36,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests using (new CultureReplacer()) { var builder = new WebHostBuilder() - .UseContentRoot(GetApplicationPath(relativePath)) + .UseContentRoot(GetApplicationPath(solutionRelativePath)) .ConfigureServices(InitializeServices) .UseStartup(typeof(TStartup)); @@ -58,12 +55,26 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests _server.Dispose(); } - private static string GetApplicationPath(string relativePath) + private static string GetApplicationPath(string solutionRelativePath) { var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly; var applicationName = startupAssembly.GetName().Name; 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)