Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix failing Hosting tests (#79455)
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 and halter73 committed Jan 3, 2023
commit 801d39fb11a43f045196ff514d736ce45859feff
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