From 6b9dd7518c62c28d1e40c23339339bc6f2cc2b19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Thu, 19 Jun 2025 18:37:16 +0200 Subject: [PATCH 1/3] Pass enviroment to runtime config only when defined --- src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts b/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts index 9eff6375548c..8904618f088a 100644 --- a/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts +++ b/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts @@ -138,9 +138,12 @@ function prepareRuntimeConfig(options: Partial, onConfi const config: MonoConfig = { maxParallelDownloads: 1000000, // disable throttling parallel downloads enableDownloadRetry: false, // disable retry downloads - applicationEnvironment: options.environment, }; + if (options.environment) { + config.applicationEnvironment = options.environment; + } + const onConfigLoaded = async (loadedConfig: MonoConfig) => { if (!loadedConfig.environmentVariables) { loadedConfig.environmentVariables = {}; From c56b3db701e9c3be0515f251722a7af13f2f4a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Fri, 20 Jun 2025 09:17:43 +0200 Subject: [PATCH 2/3] E2E test with non-default value on CI --- .../test/E2ETest/Tests/WebAssemblyConfigurationTest.cs | 6 +++--- .../test/testassets/BasicTestApp/BasicTestApp.csproj | 3 ++- .../BasicTestApp/wwwroot/appsettings.Staging.json | 4 ++++ 3 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 src/Components/test/testassets/BasicTestApp/wwwroot/appsettings.Staging.json diff --git a/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs b/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs index b231545db2aa..78c56106cf37 100644 --- a/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs +++ b/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs @@ -40,13 +40,13 @@ public void WebAssemblyConfiguration_Works() if (_serverFixture.TestTrimmedOrMultithreadingApps) { // Verify that the environment gets detected as 'Production'. - Browser.Equal("Production", () => _appElement.FindElement(By.Id("environment")).Text); + Browser.Equal("Staging", () => _appElement.FindElement(By.Id("environment")).Text); // Verify values overriden by an environment specific 'appsettings.$(Environment).json are read - Assert.Equal("Prod key2-value", _appElement.FindElement(By.Id("key2")).Text); + Assert.Equal("Staging key2-value", _appElement.FindElement(By.Id("key2")).Text); // Lastly for sanity, make sure values specified in an environment specific 'appsettings.$(Environment).json are read - Assert.Equal("Prod key3-value", _appElement.FindElement(By.Id("key3")).Text); + Assert.Equal("Staging key3-value", _appElement.FindElement(By.Id("key3")).Text); } else { diff --git a/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj b/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj index d3be5f099dfa..842b1ebd06eb 100644 --- a/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj +++ b/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj @@ -13,12 +13,13 @@ true true - + <_BlazorBrotliCompressionLevel>NoCompression + Staging diff --git a/src/Components/test/testassets/BasicTestApp/wwwroot/appsettings.Staging.json b/src/Components/test/testassets/BasicTestApp/wwwroot/appsettings.Staging.json new file mode 100644 index 000000000000..2f676f9a26a2 --- /dev/null +++ b/src/Components/test/testassets/BasicTestApp/wwwroot/appsettings.Staging.json @@ -0,0 +1,4 @@ +{ + "key2": "Staging key2-value", + "key3": "Staging key3-value" +} From 5f1db88bbd9736ae974ba77dc8e1fbdac929c3ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Fri, 20 Jun 2025 13:35:19 +0200 Subject: [PATCH 3/3] Move the E2E test to a different test app to reduce the impact --- .../Tests/WebAssemblyConfigurationTest.cs | 6 ++-- .../Tests/WebAssemblyEnvironmentTest.cs | 32 +++++++++++++++++++ .../BasicTestApp/BasicTestApp.csproj | 3 +- .../wwwroot/appsettings.Staging.json | 4 --- .../testassets/GlobalizationWasmApp/App.razor | 2 ++ .../GlobalizationWasmApp.csproj | 2 ++ .../WebAssemblyEnvironment.razor | 3 ++ 7 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 src/Components/test/E2ETest/Tests/WebAssemblyEnvironmentTest.cs delete mode 100644 src/Components/test/testassets/BasicTestApp/wwwroot/appsettings.Staging.json create mode 100644 src/Components/test/testassets/GlobalizationWasmApp/WebAssemblyEnvironment.razor diff --git a/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs b/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs index 78c56106cf37..b231545db2aa 100644 --- a/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs +++ b/src/Components/test/E2ETest/Tests/WebAssemblyConfigurationTest.cs @@ -40,13 +40,13 @@ public void WebAssemblyConfiguration_Works() if (_serverFixture.TestTrimmedOrMultithreadingApps) { // Verify that the environment gets detected as 'Production'. - Browser.Equal("Staging", () => _appElement.FindElement(By.Id("environment")).Text); + Browser.Equal("Production", () => _appElement.FindElement(By.Id("environment")).Text); // Verify values overriden by an environment specific 'appsettings.$(Environment).json are read - Assert.Equal("Staging key2-value", _appElement.FindElement(By.Id("key2")).Text); + Assert.Equal("Prod key2-value", _appElement.FindElement(By.Id("key2")).Text); // Lastly for sanity, make sure values specified in an environment specific 'appsettings.$(Environment).json are read - Assert.Equal("Staging key3-value", _appElement.FindElement(By.Id("key3")).Text); + Assert.Equal("Prod key3-value", _appElement.FindElement(By.Id("key3")).Text); } else { diff --git a/src/Components/test/E2ETest/Tests/WebAssemblyEnvironmentTest.cs b/src/Components/test/E2ETest/Tests/WebAssemblyEnvironmentTest.cs new file mode 100644 index 000000000000..8a80853521cf --- /dev/null +++ b/src/Components/test/E2ETest/Tests/WebAssemblyEnvironmentTest.cs @@ -0,0 +1,32 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Globalization; +using GlobalizationWasmApp; +using Microsoft.AspNetCore.Components.E2ETest.Infrastructure; +using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures; +using Microsoft.AspNetCore.E2ETesting; +using OpenQA.Selenium; +using Xunit.Abstractions; + +namespace Microsoft.AspNetCore.Components.E2ETest.Tests; + +public class WebAssemblyEnvironmentTest : ServerTestBase> +{ + public WebAssemblyEnvironmentTest( + BrowserFixture browserFixture, + ToggleExecutionModeServerFixture serverFixture, + ITestOutputHelper output) + : base(browserFixture, serverFixture, output) + { + } + + [Fact] + public void WebAssemblyEnvironment_Works() + { + Navigate($"{ServerPathBase}/"); + + // Verify that the environment gets detected as 'Staging'. + Browser.Equal("Staging", () => Browser.FindElement(By.Id("environment")).Text); + } +} diff --git a/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj b/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj index 842b1ebd06eb..d3be5f099dfa 100644 --- a/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj +++ b/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj @@ -13,13 +13,12 @@ true true - + <_BlazorBrotliCompressionLevel>NoCompression - Staging diff --git a/src/Components/test/testassets/BasicTestApp/wwwroot/appsettings.Staging.json b/src/Components/test/testassets/BasicTestApp/wwwroot/appsettings.Staging.json deleted file mode 100644 index 2f676f9a26a2..000000000000 --- a/src/Components/test/testassets/BasicTestApp/wwwroot/appsettings.Staging.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "key2": "Staging key2-value", - "key3": "Staging key3-value" -} diff --git a/src/Components/test/testassets/GlobalizationWasmApp/App.razor b/src/Components/test/testassets/GlobalizationWasmApp/App.razor index 18684c00a742..8ee3c33fdcb1 100644 --- a/src/Components/test/testassets/GlobalizationWasmApp/App.razor +++ b/src/Components/test/testassets/GlobalizationWasmApp/App.razor @@ -6,3 +6,5 @@

DateTime: @(new DateTime(2020, 09, 02))

Localized string: @Loc["Hello"]

+ + \ No newline at end of file diff --git a/src/Components/test/testassets/GlobalizationWasmApp/GlobalizationWasmApp.csproj b/src/Components/test/testassets/GlobalizationWasmApp/GlobalizationWasmApp.csproj index ffcaad706f56..25d78a0f1ff9 100644 --- a/src/Components/test/testassets/GlobalizationWasmApp/GlobalizationWasmApp.csproj +++ b/src/Components/test/testassets/GlobalizationWasmApp/GlobalizationWasmApp.csproj @@ -7,6 +7,8 @@ false + + Staging diff --git a/src/Components/test/testassets/GlobalizationWasmApp/WebAssemblyEnvironment.razor b/src/Components/test/testassets/GlobalizationWasmApp/WebAssemblyEnvironment.razor new file mode 100644 index 000000000000..42852ced7446 --- /dev/null +++ b/src/Components/test/testassets/GlobalizationWasmApp/WebAssemblyEnvironment.razor @@ -0,0 +1,3 @@ +@inject Microsoft.AspNetCore.Components.WebAssembly.Hosting.IWebAssemblyHostEnvironment HostEnvironment + +
@HostEnvironment.Environment