From 8ab66ab5cec20c7de8c5c8d06ca2afaa10903a4c Mon Sep 17 00:00:00 2001 From: Pranav K Date: Mon, 23 May 2016 14:35:03 -0700 Subject: [PATCH] Fix resource generation to work with dotnet test --- .../MvcTestFixture.cs | 31 ++----------- .../ResourceFile.cs | 29 +----------- .../SolutionPathUtility.cs | 45 +++++++++++++++++++ 3 files changed, 51 insertions(+), 54 deletions(-) create mode 100644 test/Microsoft.AspNetCore.Mvc.TestCommon/SolutionPathUtility.cs diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcTestFixture.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcTestFixture.cs index 026f90f464..73658b514c 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcTestFixture.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcTestFixture.cs @@ -2,25 +2,21 @@ // 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.IO; using System.Net.Http; using System.Reflection; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.Controllers; -using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.AspNetCore.Mvc.ViewComponents; using Microsoft.AspNetCore.TestHost; using Microsoft.AspNetCore.Testing; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.PlatformAbstractions; namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public class MvcTestFixture : IDisposable { - private const string SolutionName = "Mvc.sln"; private readonly TestServer _server; public MvcTestFixture() @@ -35,8 +31,11 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests // (DefaultRequestCulture) is consistent regardless of system configuration or personal preferences. using (new CultureReplacer()) { + var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly; + var contentRoot = SolutionPathUtility.GetProjectPath(solutionRelativePath, startupAssembly); + var builder = new WebHostBuilder() - .UseContentRoot(GetApplicationPath(solutionRelativePath)) + .UseContentRoot(contentRoot) .ConfigureServices(InitializeServices) .UseStartup(typeof(TStartup)); @@ -55,28 +54,6 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests _server.Dispose(); } - private static string GetApplicationPath(string solutionRelativePath) - { - var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly; - var applicationName = startupAssembly.GetName().Name; - var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath; - - 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) { var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly; diff --git a/test/Microsoft.AspNetCore.Mvc.TestCommon/ResourceFile.cs b/test/Microsoft.AspNetCore.Mvc.TestCommon/ResourceFile.cs index bf143fb98e..f1128e0f66 100644 --- a/test/Microsoft.AspNetCore.Mvc.TestCommon/ResourceFile.cs +++ b/test/Microsoft.AspNetCore.Mvc.TestCommon/ResourceFile.cs @@ -44,8 +44,6 @@ namespace Microsoft.AspNetCore.Mvc /// public static Stream GetResourceStream(Assembly assembly, string resourceName, bool sourceFile) { - // The DNX runtime compiles every file under the resources folder as a resource available at runtime with - // the same name as the file name. var fullName = $"{ assembly.GetName().Name }.{ resourceName.Replace('/', '.') }"; if (!Exists(assembly, fullName)) { @@ -190,10 +188,9 @@ namespace Microsoft.AspNetCore.Mvc if (!string.Equals(normalizedPreviousContent, normalizedContent, StringComparison.Ordinal)) { - // The DNX runtime compiles every file under the resources folder as a resource available at runtime + // The build system compiles every file under the resources folder as a resource available at runtime // with the same name as the file name. Need to update this file on disc. - var projectName = assembly.GetName().Name; - var projectPath = GetProjectPath(projectName); + var projectPath = SolutionPathUtility.GetProjectPath("test", assembly); var fullPath = Path.Combine(projectPath, resourceName); WriteFile(fullPath, content); } @@ -214,28 +211,6 @@ namespace Microsoft.AspNetCore.Mvc return false; } - private static string GetProjectPath(string projectName) - { - // Initial guess: Already in the project directory. - var projectPath = Path.GetFullPath("."); - - var currentDirectoryName = new DirectoryInfo(projectPath).Name; - if (!string.Equals(projectName, currentDirectoryName, StringComparison.Ordinal)) - { - // Not running from test project directory. Should be in "test" or solution directory. - if (string.Equals("test", currentDirectoryName, StringComparison.Ordinal)) - { - projectPath = Path.Combine(projectPath, projectName); - } - else - { - projectPath = Path.Combine(projectPath, "test", projectName); - } - } - - return projectPath; - } - private static void WriteFile(string fullPath, string content) { // Serialize writes to minimize contention for file handles and directory access. diff --git a/test/Microsoft.AspNetCore.Mvc.TestCommon/SolutionPathUtility.cs b/test/Microsoft.AspNetCore.Mvc.TestCommon/SolutionPathUtility.cs new file mode 100644 index 0000000000..3565d34262 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.TestCommon/SolutionPathUtility.cs @@ -0,0 +1,45 @@ +// 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.IO; +using System.Reflection; +using Microsoft.Extensions.PlatformAbstractions; + +namespace Microsoft.AspNetCore.Mvc +{ + public static class SolutionPathUtility + { + private const string SolutionName = "Mvc.sln"; + + /// + /// Gets the full path to the project. + /// + /// + /// The parent directory of the project. + /// e.g. samples, test, or test/Websites + /// + /// The project's assembly. + /// The full path to the project. + public static string GetProjectPath(string solutionRelativePath, Assembly assembly) + { + var projectName = assembly.GetName().Name; + var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath; + + var directoryInfo = new DirectoryInfo(applicationBasePath); + do + { + var solutionFileInfo = new FileInfo(Path.Combine(directoryInfo.FullName, SolutionName)); + if (solutionFileInfo.Exists) + { + return Path.GetFullPath(Path.Combine(directoryInfo.FullName, solutionRelativePath, projectName)); + } + + directoryInfo = directoryInfo.Parent; + } + while (directoryInfo.Parent != null); + + throw new Exception($"Solution root could not be located using application root {applicationBasePath}."); + } + } +}