Skip to content

Commit 801d39f

Browse files
eerhardthalter73
authored andcommitted
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
1 parent b0886bb commit 801d39f

File tree

1 file changed

+96
-58
lines changed

1 file changed

+96
-58
lines changed

src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostApplicationBuilderTests.cs

Lines changed: 96 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -234,85 +234,123 @@ public void DisableDefaultIHostEnvironmentValues()
234234
[InlineData(false)]
235235
public void ConfigurationSettingCanInfluenceEnvironment(bool disableDefaults)
236236
{
237-
var tempPath = Path.GetTempPath();
237+
var tempPath = CreateTempSubdirectory();
238238

239-
using var config = new ConfigurationManager();
240-
241-
config.AddInMemoryCollection(new KeyValuePair<string, string>[]
239+
try
242240
{
243-
new(HostDefaults.ApplicationKey, "AppA" ),
244-
new(HostDefaults.EnvironmentKey, "EnvA" ),
245-
new(HostDefaults.ContentRootKey, tempPath)
246-
});
241+
using var config = new ConfigurationManager();
247242

248-
var builder = new HostApplicationBuilder(new HostApplicationBuilderSettings
249-
{
250-
DisableDefaults = disableDefaults,
251-
Configuration = config,
252-
});
243+
config.AddInMemoryCollection(new KeyValuePair<string, string>[]
244+
{
245+
new(HostDefaults.ApplicationKey, "AppA" ),
246+
new(HostDefaults.EnvironmentKey, "EnvA" ),
247+
new(HostDefaults.ContentRootKey, tempPath)
248+
});
253249

254-
Assert.Equal("AppA", builder.Configuration[HostDefaults.ApplicationKey]);
255-
Assert.Equal("EnvA", builder.Configuration[HostDefaults.EnvironmentKey]);
256-
Assert.Equal(tempPath, builder.Configuration[HostDefaults.ContentRootKey]);
250+
var builder = new HostApplicationBuilder(new HostApplicationBuilderSettings
251+
{
252+
DisableDefaults = disableDefaults,
253+
Configuration = config,
254+
});
257255

258-
Assert.Equal("AppA", builder.Environment.ApplicationName);
259-
Assert.Equal("EnvA", builder.Environment.EnvironmentName);
260-
Assert.Equal(tempPath, builder.Environment.ContentRootPath);
261-
var fileProviderFromBuilder = Assert.IsType<PhysicalFileProvider>(builder.Environment.ContentRootFileProvider);
262-
Assert.Equal(tempPath, fileProviderFromBuilder.Root);
256+
Assert.Equal("AppA", builder.Configuration[HostDefaults.ApplicationKey]);
257+
Assert.Equal("EnvA", builder.Configuration[HostDefaults.EnvironmentKey]);
258+
Assert.Equal(tempPath, builder.Configuration[HostDefaults.ContentRootKey]);
263259

264-
using IHost host = builder.Build();
260+
Assert.Equal("AppA", builder.Environment.ApplicationName);
261+
Assert.Equal("EnvA", builder.Environment.EnvironmentName);
262+
Assert.Equal(tempPath, builder.Environment.ContentRootPath);
263+
var fileProviderFromBuilder = Assert.IsType<PhysicalFileProvider>(builder.Environment.ContentRootFileProvider);
264+
Assert.Equal(tempPath, fileProviderFromBuilder.Root);
265265

266-
var hostEnvironmentFromServices = host.Services.GetRequiredService<IHostEnvironment>();
267-
Assert.Equal("AppA", hostEnvironmentFromServices.ApplicationName);
268-
Assert.Equal("EnvA", hostEnvironmentFromServices.EnvironmentName);
269-
Assert.Equal(tempPath, hostEnvironmentFromServices.ContentRootPath);
270-
var fileProviderFromServices = Assert.IsType<PhysicalFileProvider>(hostEnvironmentFromServices.ContentRootFileProvider);
271-
Assert.Equal(tempPath, fileProviderFromServices.Root);
266+
using IHost host = builder.Build();
267+
268+
var hostEnvironmentFromServices = host.Services.GetRequiredService<IHostEnvironment>();
269+
Assert.Equal("AppA", hostEnvironmentFromServices.ApplicationName);
270+
Assert.Equal("EnvA", hostEnvironmentFromServices.EnvironmentName);
271+
Assert.Equal(tempPath, hostEnvironmentFromServices.ContentRootPath);
272+
var fileProviderFromServices = Assert.IsType<PhysicalFileProvider>(hostEnvironmentFromServices.ContentRootFileProvider);
273+
Assert.Equal(tempPath, fileProviderFromServices.Root);
274+
}
275+
finally
276+
{
277+
Directory.Delete(tempPath);
278+
}
272279
}
273280

274281
[Theory]
275282
[InlineData(true)]
276283
[InlineData(false)]
277284
public void DirectSettingsOverrideConfigurationSetting(bool disableDefaults)
278285
{
279-
var tempPath = Path.GetTempPath();
280-
281-
using var config = new ConfigurationManager();
286+
var tempPath = CreateTempSubdirectory();
282287

283-
config.AddInMemoryCollection(new KeyValuePair<string, string>[]
288+
try
284289
{
285-
new(HostDefaults.ApplicationKey, "AppA" ),
286-
new(HostDefaults.EnvironmentKey, "EnvA" ),
287-
});
290+
using var config = new ConfigurationManager();
288291

289-
var builder = new HostApplicationBuilder(new HostApplicationBuilderSettings
290-
{
291-
DisableDefaults = disableDefaults,
292-
Configuration = config,
293-
ApplicationName = "AppB",
294-
EnvironmentName = "EnvB",
295-
ContentRootPath = tempPath,
296-
});
292+
config.AddInMemoryCollection(new KeyValuePair<string, string>[]
293+
{
294+
new(HostDefaults.ApplicationKey, "AppA" ),
295+
new(HostDefaults.EnvironmentKey, "EnvA" ),
296+
});
297297

298-
Assert.Equal("AppB", builder.Configuration[HostDefaults.ApplicationKey]);
299-
Assert.Equal("EnvB", builder.Configuration[HostDefaults.EnvironmentKey]);
300-
Assert.Equal(tempPath, builder.Configuration[HostDefaults.ContentRootKey]);
298+
var builder = new HostApplicationBuilder(new HostApplicationBuilderSettings
299+
{
300+
DisableDefaults = disableDefaults,
301+
Configuration = config,
302+
ApplicationName = "AppB",
303+
EnvironmentName = "EnvB",
304+
ContentRootPath = tempPath,
305+
});
301306

302-
Assert.Equal("AppB", builder.Environment.ApplicationName);
303-
Assert.Equal("EnvB", builder.Environment.EnvironmentName);
304-
Assert.Equal(tempPath, builder.Environment.ContentRootPath);
305-
var fileProviderFromBuilder = Assert.IsType<PhysicalFileProvider>(builder.Environment.ContentRootFileProvider);
306-
Assert.Equal(tempPath, fileProviderFromBuilder.Root);
307+
Assert.Equal("AppB", builder.Configuration[HostDefaults.ApplicationKey]);
308+
Assert.Equal("EnvB", builder.Configuration[HostDefaults.EnvironmentKey]);
309+
Assert.Equal(tempPath, builder.Configuration[HostDefaults.ContentRootKey]);
307310

308-
using IHost host = builder.Build();
311+
Assert.Equal("AppB", builder.Environment.ApplicationName);
312+
Assert.Equal("EnvB", builder.Environment.EnvironmentName);
313+
Assert.Equal(tempPath, builder.Environment.ContentRootPath);
314+
var fileProviderFromBuilder = Assert.IsType<PhysicalFileProvider>(builder.Environment.ContentRootFileProvider);
315+
Assert.Equal(tempPath, fileProviderFromBuilder.Root);
309316

310-
var hostEnvironmentFromServices = host.Services.GetRequiredService<IHostEnvironment>();
311-
Assert.Equal("AppB", hostEnvironmentFromServices.ApplicationName);
312-
Assert.Equal("EnvB", hostEnvironmentFromServices.EnvironmentName);
313-
Assert.Equal(tempPath, hostEnvironmentFromServices.ContentRootPath);
314-
var fileProviderFromServices = Assert.IsType<PhysicalFileProvider>(hostEnvironmentFromServices.ContentRootFileProvider);
315-
Assert.Equal(tempPath, fileProviderFromServices.Root);
317+
using IHost host = builder.Build();
318+
319+
var hostEnvironmentFromServices = host.Services.GetRequiredService<IHostEnvironment>();
320+
Assert.Equal("AppB", hostEnvironmentFromServices.ApplicationName);
321+
Assert.Equal("EnvB", hostEnvironmentFromServices.EnvironmentName);
322+
Assert.Equal(tempPath, hostEnvironmentFromServices.ContentRootPath);
323+
var fileProviderFromServices = Assert.IsType<PhysicalFileProvider>(hostEnvironmentFromServices.ContentRootFileProvider);
324+
Assert.Equal(tempPath, fileProviderFromServices.Root);
325+
}
326+
finally
327+
{
328+
Directory.Delete(tempPath);
329+
}
330+
}
331+
332+
private static string CreateTempSubdirectory()
333+
{
334+
#if NETCOREAPP
335+
DirectoryInfo directoryInfo = Directory.CreateTempSubdirectory();
336+
#else
337+
DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));
338+
directoryInfo.Create();
339+
#endif
340+
341+
// PhysicalFileProvider will always ensure the path has a trailing slash
342+
return EnsureTrailingSlash(directoryInfo.FullName);
343+
}
344+
345+
private static string EnsureTrailingSlash(string path)
346+
{
347+
if (!string.IsNullOrEmpty(path) &&
348+
path[path.Length - 1] != Path.DirectorySeparatorChar)
349+
{
350+
return path + Path.DirectorySeparatorChar;
351+
}
352+
353+
return path;
316354
}
317355

318356
[Fact]

0 commit comments

Comments
 (0)