Add ability to override the testing web content root using environment variables

This commit is contained in:
Remco 2018-07-11 18:57:03 -07:00 committed by Pranav K
parent d4beab5d09
commit 28c0c4d128
No known key found for this signature in database
GPG Key ID: 1963DA6D96C3057A
1 changed files with 23 additions and 0 deletions

View File

@ -128,6 +128,11 @@ namespace Microsoft.AspNetCore.Mvc.Testing
private void SetContentRoot(IWebHostBuilder builder)
{
if (SetContentRootFromSetting(builder))
{
return;
}
var metadataAttributes = GetContentRootMetadataAttributes(
typeof(TEntryPoint).Assembly.FullName,
typeof(TEntryPoint).Assembly.GetName().Name);
@ -161,6 +166,24 @@ namespace Microsoft.AspNetCore.Mvc.Testing
}
}
private static bool SetContentRootFromSetting(IWebHostBuilder builder)
{
// Attempt to look for TEST_CONTENTROOT_APPNAME in settings. This should result in looking for
// ASPNETCORE_TEST_CONTENTROOT_APPNAME environment variable.
var assemblyName = typeof(TEntryPoint).Assembly.GetName().Name;
var settingSuffix = assemblyName.ToUpperInvariant().Replace(".", "_");
var settingName = $"TEST_CONTENTROOT_{settingSuffix}";
var settingValue = builder.GetSetting(settingName);
if (settingValue == null)
{
return false;
}
builder.UseContentRoot(settingValue);
return true;
}
private WebApplicationFactoryContentRootAttribute[] GetContentRootMetadataAttributes(
string tEntryPointAssemblyFullName,
string tEntryPointAssemblyName)