Fix resource generation to work with dotnet test
This commit is contained in:
parent
ff534da70f
commit
8ab66ab5ce
|
|
@ -2,25 +2,21 @@
|
||||||
// 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;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
||||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
|
||||||
using Microsoft.AspNetCore.Mvc.ViewComponents;
|
using Microsoft.AspNetCore.Mvc.ViewComponents;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
using Microsoft.AspNetCore.Testing;
|
using Microsoft.AspNetCore.Testing;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.PlatformAbstractions;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
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()
|
||||||
|
|
@ -35,8 +31,11 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
// (DefaultRequestCulture) is consistent regardless of system configuration or personal preferences.
|
// (DefaultRequestCulture) is consistent regardless of system configuration or personal preferences.
|
||||||
using (new CultureReplacer())
|
using (new CultureReplacer())
|
||||||
{
|
{
|
||||||
|
var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly;
|
||||||
|
var contentRoot = SolutionPathUtility.GetProjectPath(solutionRelativePath, startupAssembly);
|
||||||
|
|
||||||
var builder = new WebHostBuilder()
|
var builder = new WebHostBuilder()
|
||||||
.UseContentRoot(GetApplicationPath(solutionRelativePath))
|
.UseContentRoot(contentRoot)
|
||||||
.ConfigureServices(InitializeServices)
|
.ConfigureServices(InitializeServices)
|
||||||
.UseStartup(typeof(TStartup));
|
.UseStartup(typeof(TStartup));
|
||||||
|
|
||||||
|
|
@ -55,28 +54,6 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
_server.Dispose();
|
_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)
|
protected virtual void InitializeServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly;
|
var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly;
|
||||||
|
|
|
||||||
|
|
@ -44,8 +44,6 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
/// </exception>
|
/// </exception>
|
||||||
public static Stream GetResourceStream(Assembly assembly, string resourceName, bool sourceFile)
|
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('/', '.') }";
|
var fullName = $"{ assembly.GetName().Name }.{ resourceName.Replace('/', '.') }";
|
||||||
if (!Exists(assembly, fullName))
|
if (!Exists(assembly, fullName))
|
||||||
{
|
{
|
||||||
|
|
@ -190,10 +188,9 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
|
|
||||||
if (!string.Equals(normalizedPreviousContent, normalizedContent, StringComparison.Ordinal))
|
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.
|
// with the same name as the file name. Need to update this file on disc.
|
||||||
var projectName = assembly.GetName().Name;
|
var projectPath = SolutionPathUtility.GetProjectPath("test", assembly);
|
||||||
var projectPath = GetProjectPath(projectName);
|
|
||||||
var fullPath = Path.Combine(projectPath, resourceName);
|
var fullPath = Path.Combine(projectPath, resourceName);
|
||||||
WriteFile(fullPath, content);
|
WriteFile(fullPath, content);
|
||||||
}
|
}
|
||||||
|
|
@ -214,28 +211,6 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
return false;
|
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)
|
private static void WriteFile(string fullPath, string content)
|
||||||
{
|
{
|
||||||
// Serialize writes to minimize contention for file handles and directory access.
|
// Serialize writes to minimize contention for file handles and directory access.
|
||||||
|
|
|
||||||
|
|
@ -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";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the full path to the project.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="solutionRelativePath">
|
||||||
|
/// The parent directory of the project.
|
||||||
|
/// e.g. samples, test, or test/Websites
|
||||||
|
/// </param>
|
||||||
|
/// <param name="assembly">The project's assembly.</param>
|
||||||
|
/// <returns>The full path to the project.</returns>
|
||||||
|
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}.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue