-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Update BootJsonData generation to account for runtimeconfig.dev.json #130825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c0d0b71
Update BootJsonData generation to account for runtimeconfig.dev.json
Copilot 93bef74
Move tests
tmat 0280de9
Potential fix for pull request finding
tmat 4ee1d49
Cleanup
tmat 642b3e3
Move test project
tmat 6cf2687
Cleanup
tmat b757191
Move tests to src/tasks.tests
tmat 3978b53
Revert Subsets.props change from #130825
maraf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
src/tasks.tests/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests/GenerateWasmBootJsonTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Text.Json; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.NET.Sdk.WebAssembly.Tests; | ||
|
|
||
| public class GenerateWasmBootJsonTests | ||
| { | ||
| [Fact] | ||
| public void ReadRuntimeConfigFiles_NullMainConfigPath_ReturnsNull() | ||
| { | ||
| var result = GenerateWasmBootJson.ReadRuntimeConfigFiles(null, null); | ||
|
|
||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReadRuntimeConfigFiles_MainConfigNotExists_ReturnsNull() | ||
| { | ||
| using var dir = new TempDirectory(); | ||
| var nonExistentPath = Path.Combine(dir.Path, "does-not-exist.runtimeconfig.json"); | ||
| var result = GenerateWasmBootJson.ReadRuntimeConfigFiles(nonExistentPath, null); | ||
|
|
||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReadRuntimeConfigFiles_DevConfigPreservesBooleanAndNumericTypes() | ||
| { | ||
| using var dir = new TempDirectory(); | ||
| var mainConfig = WriteRuntimeConfig(dir.Path, "App.runtimeconfig.json", | ||
| configProperties: new() { ["key1"] = "value1" }); | ||
| // Write dev config with native JSON boolean and number (not string) values. | ||
| var devConfigPath = Path.Combine(dir.Path, "App.runtimeconfig.dev.json"); | ||
| File.WriteAllText(devConfigPath, """ | ||
| { | ||
| "runtimeOptions": { | ||
| "configProperties": { | ||
| "System.HotReload.Enable": true, | ||
| "System.HotReload.MaxRetries": 10 | ||
| } | ||
| } | ||
| } | ||
| """); | ||
|
|
||
| var result = GenerateWasmBootJson.ReadRuntimeConfigFiles(mainConfig, devConfigPath); | ||
|
|
||
| Assert.NotNull(result?.runtimeOptions?.configProperties); | ||
| var props = result!.runtimeOptions!.configProperties!; | ||
| Assert.Equal(JsonValueKind.True, ((JsonElement)props["System.HotReload.Enable"]).ValueKind); | ||
| Assert.Equal(JsonValueKind.Number, ((JsonElement)props["System.HotReload.MaxRetries"]).ValueKind); | ||
| Assert.Equal(10, ((JsonElement)props["System.HotReload.MaxRetries"]).GetInt32()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReadRuntimeConfigFiles_MainConfigOnly_ReturnsMainProperties() | ||
| { | ||
| using var dir = new TempDirectory(); | ||
| var mainConfig = WriteRuntimeConfig(dir.Path, "App.runtimeconfig.json", | ||
| configProperties: new() { ["key1"] = "value1", ["key2"] = "42" }); | ||
|
|
||
| var result = GenerateWasmBootJson.ReadRuntimeConfigFiles(mainConfig, null); | ||
|
|
||
| Assert.NotNull(result); | ||
| Assert.NotNull(result.runtimeOptions?.configProperties); | ||
| Assert.Equal("value1", result.runtimeOptions!.configProperties!["key1"].ToString()); | ||
| Assert.Equal("42", result.runtimeOptions.configProperties["key2"].ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReadRuntimeConfigFiles_DevConfigNotExists_ReturnsMainPropertiesUnchanged() | ||
| { | ||
| using var dir = new TempDirectory(); | ||
| var mainConfig = WriteRuntimeConfig(dir.Path, "App.runtimeconfig.json", | ||
| configProperties: new() { ["key1"] = "value1" }); | ||
| var devConfigPath = Path.Combine(dir.Path, "App.runtimeconfig.dev.json"); | ||
|
|
||
| var result = GenerateWasmBootJson.ReadRuntimeConfigFiles(mainConfig, devConfigPath); | ||
|
|
||
| Assert.NotNull(result); | ||
| Assert.Equal("value1", result.runtimeOptions?.configProperties?["key1"].ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReadRuntimeConfigFiles_DevConfigAddsNewProperty() | ||
| { | ||
| using var dir = new TempDirectory(); | ||
| var mainConfig = WriteRuntimeConfig(dir.Path, "App.runtimeconfig.json", | ||
| configProperties: new() { ["key1"] = "value1" }); | ||
| var devConfig = WriteRuntimeConfig(dir.Path, "App.runtimeconfig.dev.json", | ||
| configProperties: new() { ["key2"] = "value2" }); | ||
|
|
||
| var result = GenerateWasmBootJson.ReadRuntimeConfigFiles(mainConfig, devConfig); | ||
|
|
||
| Assert.NotNull(result?.runtimeOptions?.configProperties); | ||
| Assert.Equal("value1", result!.runtimeOptions!.configProperties!["key1"].ToString()); | ||
| Assert.Equal("value2", result.runtimeOptions.configProperties["key2"].ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReadRuntimeConfigFiles_DevConfigOverridesMainProperty() | ||
| { | ||
| using var dir = new TempDirectory(); | ||
| var mainConfig = WriteRuntimeConfig(dir.Path, "App.runtimeconfig.json", | ||
| configProperties: new() { ["System.Runtime.Feature"] = "false" }); | ||
| var devConfig = WriteRuntimeConfig(dir.Path, "App.runtimeconfig.dev.json", | ||
| configProperties: new() { ["System.Runtime.Feature"] = "true" }); | ||
|
|
||
| var result = GenerateWasmBootJson.ReadRuntimeConfigFiles(mainConfig, devConfig); | ||
|
|
||
| Assert.NotNull(result?.runtimeOptions?.configProperties); | ||
| Assert.Equal("true", result!.runtimeOptions!.configProperties!["System.Runtime.Feature"].ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReadRuntimeConfigFiles_DevConfigMergesWhenMainHasNoConfigProperties() | ||
| { | ||
| using var dir = new TempDirectory(); | ||
| var mainConfig = WriteRuntimeConfig(dir.Path, "App.runtimeconfig.json", | ||
| configProperties: null); | ||
| var devConfig = WriteRuntimeConfig(dir.Path, "App.runtimeconfig.dev.json", | ||
| configProperties: new() { ["System.HotReload.Enable"] = "true" }); | ||
|
|
||
| var result = GenerateWasmBootJson.ReadRuntimeConfigFiles(mainConfig, devConfig); | ||
|
|
||
| Assert.NotNull(result?.runtimeOptions?.configProperties); | ||
| Assert.Equal("true", result!.runtimeOptions!.configProperties!["System.HotReload.Enable"].ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReadRuntimeConfigFiles_DevConfigEmptyProperties_DoesNotAlterResult() | ||
| { | ||
| using var dir = new TempDirectory(); | ||
| var mainConfig = WriteRuntimeConfig(dir.Path, "App.runtimeconfig.json", | ||
| configProperties: new() { ["key1"] = "value1" }); | ||
| var devConfig = WriteRuntimeConfig(dir.Path, "App.runtimeconfig.dev.json", | ||
| configProperties: new()); | ||
|
|
||
| var result = GenerateWasmBootJson.ReadRuntimeConfigFiles(mainConfig, devConfig); | ||
|
|
||
| Assert.NotNull(result?.runtimeOptions?.configProperties); | ||
| Assert.Single(result!.runtimeOptions!.configProperties!); | ||
| Assert.Equal("value1", result.runtimeOptions.configProperties["key1"].ToString()); | ||
| } | ||
|
|
||
| private static string WriteRuntimeConfig(string dir, string fileName, Dictionary<string, string>? configProperties) | ||
| { | ||
| var path = Path.Combine(dir, fileName); | ||
| using var stream = File.OpenWrite(path); | ||
| using var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true }); | ||
| writer.WriteStartObject(); | ||
| writer.WritePropertyName("runtimeOptions"); | ||
| writer.WriteStartObject(); | ||
| if (configProperties is not null) | ||
| { | ||
| writer.WritePropertyName("configProperties"); | ||
| writer.WriteStartObject(); | ||
| foreach (var (key, value) in configProperties) | ||
| writer.WriteString(key, value); | ||
| writer.WriteEndObject(); | ||
| } | ||
| writer.WriteEndObject(); | ||
| writer.WriteEndObject(); | ||
| return path; | ||
| } | ||
|
|
||
| private sealed class TempDirectory : System.IDisposable | ||
| { | ||
| public string Path { get; } = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName()); | ||
|
|
||
| public TempDirectory() => Directory.CreateDirectory(Path); | ||
|
|
||
| public void Dispose() | ||
| { | ||
| // Silently ignore cleanup failures to avoid masking actual test failures. | ||
| try { Directory.Delete(Path, recursive: true); } catch { } | ||
| } | ||
| } | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
...ET.Sdk.WebAssembly.Pack.Tasks.Tests/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.Tests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>$(NetCoreAppToolCurrent)</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Build.Framework" Version="$(MicrosoftBuildFrameworkVersion)" /> | ||
| <PackageReference Include="Microsoft.Build.Utilities.Core" Version="$(MicrosoftBuildUtilitiesCoreVersion)" /> | ||
| <ProjectReference Include="..\..\tasks\Microsoft.NET.Sdk.WebAssembly.Pack.Tasks\Microsoft.NET.Sdk.WebAssembly.Pack.Tasks.csproj" /> | ||
| </ItemGroup> | ||
|
tmat marked this conversation as resolved.
|
||
|
|
||
| </Project> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.