Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hosting DirectSettingsOverrideConfigurationSetting and ConfigurationSettingCanInfluenceEnvironment test failures in CI #79455

Merged
merged 1 commit into from
Jan 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -234,85 +234,123 @@ public void DisableDefaultIHostEnvironmentValues()
[InlineData(false)]
public void ConfigurationSettingCanInfluenceEnvironment(bool disableDefaults)
{
var tempPath = Path.GetTempPath();
var tempPath = CreateTempSubdirectory();

using var config = new ConfigurationManager();

config.AddInMemoryCollection(new KeyValuePair<string, string>[]
try
{
new(HostDefaults.ApplicationKey, "AppA" ),
new(HostDefaults.EnvironmentKey, "EnvA" ),
new(HostDefaults.ContentRootKey, tempPath)
});
using var config = new ConfigurationManager();

var builder = new HostApplicationBuilder(new HostApplicationBuilderSettings
{
DisableDefaults = disableDefaults,
Configuration = config,
});
config.AddInMemoryCollection(new KeyValuePair<string, string>[]
{
new(HostDefaults.ApplicationKey, "AppA" ),
new(HostDefaults.EnvironmentKey, "EnvA" ),
new(HostDefaults.ContentRootKey, tempPath)
});

Assert.Equal("AppA", builder.Configuration[HostDefaults.ApplicationKey]);
Assert.Equal("EnvA", builder.Configuration[HostDefaults.EnvironmentKey]);
Assert.Equal(tempPath, builder.Configuration[HostDefaults.ContentRootKey]);
var builder = new HostApplicationBuilder(new HostApplicationBuilderSettings
{
DisableDefaults = disableDefaults,
Configuration = config,
});

Assert.Equal("AppA", builder.Environment.ApplicationName);
Assert.Equal("EnvA", builder.Environment.EnvironmentName);
Assert.Equal(tempPath, builder.Environment.ContentRootPath);
var fileProviderFromBuilder = Assert.IsType<PhysicalFileProvider>(builder.Environment.ContentRootFileProvider);
Assert.Equal(tempPath, fileProviderFromBuilder.Root);
Assert.Equal("AppA", builder.Configuration[HostDefaults.ApplicationKey]);
Assert.Equal("EnvA", builder.Configuration[HostDefaults.EnvironmentKey]);
Assert.Equal(tempPath, builder.Configuration[HostDefaults.ContentRootKey]);

using IHost host = builder.Build();
Assert.Equal("AppA", builder.Environment.ApplicationName);
Assert.Equal("EnvA", builder.Environment.EnvironmentName);
Assert.Equal(tempPath, builder.Environment.ContentRootPath);
var fileProviderFromBuilder = Assert.IsType<PhysicalFileProvider>(builder.Environment.ContentRootFileProvider);
Assert.Equal(tempPath, fileProviderFromBuilder.Root);

var hostEnvironmentFromServices = host.Services.GetRequiredService<IHostEnvironment>();
Assert.Equal("AppA", hostEnvironmentFromServices.ApplicationName);
Assert.Equal("EnvA", hostEnvironmentFromServices.EnvironmentName);
Assert.Equal(tempPath, hostEnvironmentFromServices.ContentRootPath);
var fileProviderFromServices = Assert.IsType<PhysicalFileProvider>(hostEnvironmentFromServices.ContentRootFileProvider);
Assert.Equal(tempPath, fileProviderFromServices.Root);
using IHost host = builder.Build();

var hostEnvironmentFromServices = host.Services.GetRequiredService<IHostEnvironment>();
Assert.Equal("AppA", hostEnvironmentFromServices.ApplicationName);
Assert.Equal("EnvA", hostEnvironmentFromServices.EnvironmentName);
Assert.Equal(tempPath, hostEnvironmentFromServices.ContentRootPath);
var fileProviderFromServices = Assert.IsType<PhysicalFileProvider>(hostEnvironmentFromServices.ContentRootFileProvider);
Assert.Equal(tempPath, fileProviderFromServices.Root);
}
finally
{
Directory.Delete(tempPath);
}
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void DirectSettingsOverrideConfigurationSetting(bool disableDefaults)
{
var tempPath = Path.GetTempPath();

using var config = new ConfigurationManager();
var tempPath = CreateTempSubdirectory();

config.AddInMemoryCollection(new KeyValuePair<string, string>[]
try
{
new(HostDefaults.ApplicationKey, "AppA" ),
new(HostDefaults.EnvironmentKey, "EnvA" ),
});
using var config = new ConfigurationManager();

var builder = new HostApplicationBuilder(new HostApplicationBuilderSettings
{
DisableDefaults = disableDefaults,
Configuration = config,
ApplicationName = "AppB",
EnvironmentName = "EnvB",
ContentRootPath = tempPath,
});
config.AddInMemoryCollection(new KeyValuePair<string, string>[]
{
new(HostDefaults.ApplicationKey, "AppA" ),
new(HostDefaults.EnvironmentKey, "EnvA" ),
});

Assert.Equal("AppB", builder.Configuration[HostDefaults.ApplicationKey]);
Assert.Equal("EnvB", builder.Configuration[HostDefaults.EnvironmentKey]);
Assert.Equal(tempPath, builder.Configuration[HostDefaults.ContentRootKey]);
var builder = new HostApplicationBuilder(new HostApplicationBuilderSettings
{
DisableDefaults = disableDefaults,
Configuration = config,
ApplicationName = "AppB",
EnvironmentName = "EnvB",
ContentRootPath = tempPath,
});

Assert.Equal("AppB", builder.Environment.ApplicationName);
Assert.Equal("EnvB", builder.Environment.EnvironmentName);
Assert.Equal(tempPath, builder.Environment.ContentRootPath);
var fileProviderFromBuilder = Assert.IsType<PhysicalFileProvider>(builder.Environment.ContentRootFileProvider);
Assert.Equal(tempPath, fileProviderFromBuilder.Root);
Assert.Equal("AppB", builder.Configuration[HostDefaults.ApplicationKey]);
Assert.Equal("EnvB", builder.Configuration[HostDefaults.EnvironmentKey]);
Assert.Equal(tempPath, builder.Configuration[HostDefaults.ContentRootKey]);

using IHost host = builder.Build();
Assert.Equal("AppB", builder.Environment.ApplicationName);
Assert.Equal("EnvB", builder.Environment.EnvironmentName);
Assert.Equal(tempPath, builder.Environment.ContentRootPath);
var fileProviderFromBuilder = Assert.IsType<PhysicalFileProvider>(builder.Environment.ContentRootFileProvider);
Assert.Equal(tempPath, fileProviderFromBuilder.Root);

var hostEnvironmentFromServices = host.Services.GetRequiredService<IHostEnvironment>();
Assert.Equal("AppB", hostEnvironmentFromServices.ApplicationName);
Assert.Equal("EnvB", hostEnvironmentFromServices.EnvironmentName);
Assert.Equal(tempPath, hostEnvironmentFromServices.ContentRootPath);
var fileProviderFromServices = Assert.IsType<PhysicalFileProvider>(hostEnvironmentFromServices.ContentRootFileProvider);
Assert.Equal(tempPath, fileProviderFromServices.Root);
using IHost host = builder.Build();

var hostEnvironmentFromServices = host.Services.GetRequiredService<IHostEnvironment>();
Assert.Equal("AppB", hostEnvironmentFromServices.ApplicationName);
Assert.Equal("EnvB", hostEnvironmentFromServices.EnvironmentName);
Assert.Equal(tempPath, hostEnvironmentFromServices.ContentRootPath);
var fileProviderFromServices = Assert.IsType<PhysicalFileProvider>(hostEnvironmentFromServices.ContentRootFileProvider);
Assert.Equal(tempPath, fileProviderFromServices.Root);
}
finally
{
Directory.Delete(tempPath);
}
}

private static string CreateTempSubdirectory()
{
#if NETCOREAPP
DirectoryInfo directoryInfo = Directory.CreateTempSubdirectory();
#else
DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));
directoryInfo.Create();
#endif

// PhysicalFileProvider will always ensure the path has a trailing slash
return EnsureTrailingSlash(directoryInfo.FullName);
}

private static string EnsureTrailingSlash(string path)
{
if (!string.IsNullOrEmpty(path) &&
path[path.Length - 1] != Path.DirectorySeparatorChar)
{
return path + Path.DirectorySeparatorChar;
}

return path;
}

[Fact]
Expand Down