Skip to content

Commit 423d678

Browse files
ReubenBondeerhardt
andauthored
Account for standard environment variable prefixes when simulating launch profile (#7698)
Co-authored-by: Eric Erhardt <[email protected]>
1 parent 400866d commit 423d678

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Aspire.Hosting.Testing/DistributedApplicationFactory.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ private static void PreConfigureBuilderOptions(
162162
};
163163
applicationOptions.Args = hostBuilderOptions.Args;
164164

165-
hostBuilderOptions.EnvironmentName = Environments.Development;
166165
hostBuilderOptions.ApplicationName = entryPointAssembly.GetName().Name ?? string.Empty;
167166
applicationOptions.AssemblyName = entryPointAssembly.GetName().Name ?? string.Empty;
168167
applicationOptions.DisableDashboard = true;
@@ -264,6 +263,18 @@ private static void PostConfigureBuilderOptions(
264263
foreach (var (key, value) in envVars)
265264
{
266265
SetDefault(key, value);
266+
267+
// See https://github.com/dotnet/runtime/blob/8edaf7460777e791b6279b395a68a77533db2d20/src/libraries/Microsoft.Extensions.Hosting/src/HostApplicationBuilder.cs#L96
268+
if (key.StartsWith("DOTNET_", StringComparison.OrdinalIgnoreCase))
269+
{
270+
SetDefault(key["DOTNET_".Length..], value);
271+
}
272+
273+
// See https://github.com/dotnet/aspnetcore/blob/4ce2db7b8d85c07cad2c59242edc19af6a91b0d7/src/DefaultBuilder/src/WebApplicationBuilder.cs#L38
274+
if (key.StartsWith("ASPNETCORE_", StringComparison.OrdinalIgnoreCase))
275+
{
276+
SetDefault(key["ASPNETCORE_".Length..], value);
277+
}
267278
}
268279
}
269280
}

tests/Aspire.Hosting.Testing.Tests/TestingBuilderTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ public async Task CreateAsyncWithOptions(bool genericEntryPoint)
107107
}
108108
}
109109

110+
[Fact]
111+
public async Task CanSetEnvironment()
112+
{
113+
var builder = await DistributedApplicationTestingBuilder.CreateAsync<Projects.TestingAppHost1_AppHost>(["--environment=Testing"]);
114+
Assert.Equal("Testing", builder.Environment.EnvironmentName);
115+
}
116+
117+
[Fact]
118+
public async Task EnvironmentDefaultsToDevelopment()
119+
{
120+
var builder = await DistributedApplicationTestingBuilder.CreateAsync<Projects.TestingAppHost1_AppHost>();
121+
Assert.Equal(Environments.Development, builder.Environment.EnvironmentName);
122+
}
123+
110124
[Theory]
111125
[RequiresDocker]
112126
[InlineData(false)]

0 commit comments

Comments
 (0)