@@ -229,66 +229,128 @@ public void DisableDefaultIHostEnvironmentValues()
229
229
Assert . IsAssignableFrom < PhysicalFileProvider > ( env . ContentRootFileProvider ) ;
230
230
}
231
231
232
- [ Fact ]
233
- public void ConfigurationSettingCanInfluenceEnvironment ( )
232
+ [ Theory ]
233
+ [ InlineData ( true ) ]
234
+ [ InlineData ( false ) ]
235
+ public void ConfigurationSettingCanInfluenceEnvironment ( bool disableDefaults )
234
236
{
235
- using var config = new ConfigurationManager ( ) ;
237
+ var tempPath = CreateTempSubdirectory ( ) ;
236
238
237
- config . AddInMemoryCollection ( new KeyValuePair < string , string > [ ]
239
+ try
238
240
{
239
- new ( HostDefaults . ApplicationKey , "AppA" ) ,
240
- new ( HostDefaults . EnvironmentKey , "EnvA" ) ,
241
- } ) ;
241
+ using var config = new ConfigurationManager ( ) ;
242
242
243
- var builder = new HostApplicationBuilder ( new HostApplicationBuilderSettings
244
- {
245
- DisableDefaults = true ,
246
- Configuration = config ,
247
- } ) ;
243
+ config . AddInMemoryCollection ( new KeyValuePair < string , string > [ ]
244
+ {
245
+ new ( HostDefaults . ApplicationKey , "AppA" ) ,
246
+ new ( HostDefaults . EnvironmentKey , "EnvA" ) ,
247
+ new ( HostDefaults . ContentRootKey , tempPath )
248
+ } ) ;
249
+
250
+ var builder = new HostApplicationBuilder ( new HostApplicationBuilderSettings
251
+ {
252
+ DisableDefaults = disableDefaults ,
253
+ Configuration = config ,
254
+ } ) ;
248
255
249
- Assert . Equal ( "AppA" , builder . Configuration [ HostDefaults . ApplicationKey ] ) ;
250
- Assert . Equal ( "EnvA" , builder . Configuration [ HostDefaults . EnvironmentKey ] ) ;
256
+ Assert . Equal ( "AppA" , builder . Configuration [ HostDefaults . ApplicationKey ] ) ;
257
+ Assert . Equal ( "EnvA" , builder . Configuration [ HostDefaults . EnvironmentKey ] ) ;
258
+ Assert . Equal ( tempPath , builder . Configuration [ HostDefaults . ContentRootKey ] ) ;
251
259
252
- Assert . Equal ( "AppA" , builder . Environment . ApplicationName ) ;
253
- Assert . Equal ( "EnvA" , builder . Environment . EnvironmentName ) ;
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 ) ;
254
265
255
- using IHost host = builder . Build ( ) ;
266
+ using IHost host = builder . Build ( ) ;
256
267
257
- var hostEnvironmentFromServices = host . Services . GetRequiredService < IHostEnvironment > ( ) ;
258
- Assert . Equal ( "AppA" , hostEnvironmentFromServices . ApplicationName ) ;
259
- Assert . Equal ( "EnvA" , hostEnvironmentFromServices . EnvironmentName ) ;
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
+ }
260
279
}
261
280
262
- [ Fact ]
263
- public void DirectSettingsOverrideConfigurationSetting ( )
281
+ [ Theory ]
282
+ [ InlineData ( true ) ]
283
+ [ InlineData ( false ) ]
284
+ public void DirectSettingsOverrideConfigurationSetting ( bool disableDefaults )
264
285
{
265
- using var config = new ConfigurationManager ( ) ;
286
+ var tempPath = CreateTempSubdirectory ( ) ;
266
287
267
- config . AddInMemoryCollection ( new KeyValuePair < string , string > [ ]
288
+ try
268
289
{
269
- new ( HostDefaults . ApplicationKey , "AppA" ) ,
270
- new ( HostDefaults . EnvironmentKey , "EnvA" ) ,
271
- } ) ;
290
+ using var config = new ConfigurationManager ( ) ;
272
291
273
- var builder = new HostApplicationBuilder ( new HostApplicationBuilderSettings
274
- {
275
- DisableDefaults = true ,
276
- Configuration = config ,
277
- ApplicationName = "AppB" ,
278
- EnvironmentName = "EnvB" ,
279
- } ) ;
292
+ config . AddInMemoryCollection ( new KeyValuePair < string , string > [ ]
293
+ {
294
+ new ( HostDefaults . ApplicationKey , "AppA" ) ,
295
+ new ( HostDefaults . EnvironmentKey , "EnvA" ) ,
296
+ } ) ;
280
297
281
- Assert . Equal ( "AppB" , builder . Configuration [ HostDefaults . ApplicationKey ] ) ;
282
- Assert . Equal ( "EnvB" , builder . Configuration [ HostDefaults . EnvironmentKey ] ) ;
298
+ var builder = new HostApplicationBuilder ( new HostApplicationBuilderSettings
299
+ {
300
+ DisableDefaults = disableDefaults ,
301
+ Configuration = config ,
302
+ ApplicationName = "AppB" ,
303
+ EnvironmentName = "EnvB" ,
304
+ ContentRootPath = tempPath ,
305
+ } ) ;
283
306
284
- Assert . Equal ( "AppB" , builder . Environment . ApplicationName ) ;
285
- Assert . Equal ( "EnvB" , builder . Environment . EnvironmentName ) ;
307
+ Assert . Equal ( "AppB" , builder . Configuration [ HostDefaults . ApplicationKey ] ) ;
308
+ Assert . Equal ( "EnvB" , builder . Configuration [ HostDefaults . EnvironmentKey ] ) ;
309
+ Assert . Equal ( tempPath , builder . Configuration [ HostDefaults . ContentRootKey ] ) ;
286
310
287
- 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 ) ;
288
316
289
- var hostEnvironmentFromServices = host . Services . GetRequiredService < IHostEnvironment > ( ) ;
290
- Assert . Equal ( "AppB" , hostEnvironmentFromServices . ApplicationName ) ;
291
- Assert . Equal ( "EnvB" , hostEnvironmentFromServices . EnvironmentName ) ;
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 ;
292
354
}
293
355
294
356
[ Fact ]
0 commit comments