Skip to content

Commit

Permalink
Fix failing Hosting tests (#79455)
Browse files Browse the repository at this point in the history
Ensure the temp directory used is always empty, so it doesn't pick up appsettings.json files randomly.

Fix #79453
  • Loading branch information
eerhardt authored Jan 3, 2023
1 parent 5b64d38 commit 795fcec
Showing 1 changed file with 96 additions and 58 deletions.
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

0 comments on commit 795fcec

Please sign in to comment.