Skip to content

Commit 56a9de3

Browse files
authored
Remove WasmFingerprintDotnetJs tests and add OverrideHtmlAssetPlaceholders tests (#112859)
* Fix passing information about `WasmFingerprintDotnetJs` value in WBT. * Uncomment logging - useful in such cases. * Feedback: no logging when not throwing. * Revert changes + ove dotnet JS fingerprinting to pure blazor + add tests for dotnet/sdk#46233. * Fix the name of property. * Remove problematic test. * Fix the assert. * Missing file update. * Rename WriteImportMapToHtml to OverrideHtmlAssetPlaceholders.
1 parent b4517e1 commit 56a9de3

10 files changed

+103
-45
lines changed

eng/testing/scenarios/BuildWasmAppsJobsList.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ Wasm.Build.Tests.WasmTemplateTests
4646
Wasm.Build.Tests.WorkloadTests
4747
Wasm.Build.Tests.MT.Blazor.SimpleMultiThreadedTests
4848
Wasm.Build.Tests.DebugLevelTests
49+
Wasm.Build.Tests.FingerprintingTests
4950
Wasm.Build.Tests.PreloadingTests

src/mono/wasm/Wasm.Build.Tests/Blazor/BuildPublishTests.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,6 @@ public void DefaultTemplate_AOT_WithWorkload(Configuration config, bool testUnic
5858
PublishProject(info, config, new PublishOptions(AOT: true, UseCache: false));
5959
}
6060

61-
[Theory]
62-
[InlineData(Configuration.Debug, false)]
63-
[InlineData(Configuration.Release, false)]
64-
public void DefaultTemplate_CheckFingerprinting(Configuration config, bool expectFingerprintOnDotnetJs)
65-
{
66-
var extraProperty = expectFingerprintOnDotnetJs ?
67-
"<WasmFingerprintDotnetJs>true</WasmFingerprintDotnetJs><WasmBuildNative>true</WasmBuildNative>" :
68-
"<WasmBuildNative>true</WasmBuildNative>";
69-
ProjectInfo info = CopyTestAsset(config, aot: false, TestAsset.BlazorBasicTestApp, "blz_checkfingerprinting", extraProperties: extraProperty);
70-
BlazorBuild(info, config, isNativeBuild: true);
71-
BlazorPublish(info, config, new PublishOptions(UseCache: false), isNativeBuild: true);
72-
}
73-
7461
// Disabling for now - publish folder can have more than one dotnet*hash*js, and not sure
7562
// how to pick which one to check, for the test
7663
//[Theory]

src/mono/wasm/Wasm.Build.Tests/Blazor/MiscTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.IO;
67
using System.Linq;
78
using System.Text.Json;
9+
using System.Threading.Tasks;
810
using Microsoft.NET.Sdk.WebAssembly;
911
using Xunit;
1012
using Xunit.Abstractions;
@@ -96,4 +98,23 @@ public void BugRegression_60479_WithRazorClassLib()
9698

9799
Assert.Contains(bootJson.resources.lazyAssembly.Keys, f => f.StartsWith(razorClassLibraryName));
98100
}
101+
102+
103+
[Fact]
104+
public async Task TestOverrideHtmlAssetPlaceholders()
105+
{
106+
var config = Configuration.Release;
107+
string extraProperties = "<OverrideHtmlAssetPlaceholders>true</OverrideHtmlAssetPlaceholders>";
108+
ProjectInfo info = CopyTestAsset(config, aot: false, TestAsset.BlazorBasicTestApp, "blz_import_map_html", extraProperties: extraProperties);
109+
UpdateFile(Path.Combine("wwwroot", "index.html"), new Dictionary<string, string> {
110+
{ """<base href="/" />""", """<script type="importmap"></script> <base href="/" />""" }
111+
});
112+
113+
BuildProject(info, config);
114+
BrowserRunOptions runOptions = new(config, TestScenario: "DotnetRun");
115+
await RunForBuildWithDotnetRun(runOptions);
116+
117+
PublishProject(info, config, new PublishOptions(UseCache: false));
118+
await RunForPublishWithWebServer(runOptions);
119+
}
99120
}

src/mono/wasm/Wasm.Build.Tests/BrowserStructures/AssertBundleOptions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public record AssertBundleOptions(
1414
string BinFrameworkDir,
1515
bool ExpectSymbolsFile = true,
1616
bool AssertIcuAssets = true,
17-
bool AssertSymbolsFile = true
17+
bool AssertSymbolsFile = true,
18+
bool? ExpectDotnetJsFingerprinting = false
1819
);

src/mono/wasm/Wasm.Build.Tests/Common/EnvironmentVariables.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ internal static class EnvironmentVariables
2323
internal static readonly bool ShowBuildOutput = IsRunningOnCI || Environment.GetEnvironmentVariable("SHOW_BUILD_OUTPUT") is not null;
2424
internal static readonly bool UseWebcil = Environment.GetEnvironmentVariable("USE_WEBCIL_FOR_TESTS") is "true";
2525
internal static readonly bool UseFingerprinting = Environment.GetEnvironmentVariable("USE_FINGERPRINTING_FOR_TESTS") is "true";
26-
internal static readonly bool UseFingerprintingDotnetJS = Environment.GetEnvironmentVariable("WASM_FINGERPRINT_DOTNET_JS") is "true";
2726
internal static readonly string? SdkDirName = Environment.GetEnvironmentVariable("SDK_DIR_NAME");
2827
internal static readonly string? WasiSdkPath = Environment.GetEnvironmentVariable("WASI_SDK_PATH");
2928
internal static readonly bool WorkloadsTestPreviousVersions = Environment.GetEnvironmentVariable("WORKLOADS_TEST_PREVIOUS_VERSIONS") is "true";
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
using Xunit.Abstractions;
10+
using Xunit;
11+
12+
#nullable enable
13+
14+
namespace Wasm.Build.Tests;
15+
16+
public class FingerprintingTests : WasmTemplateTestsBase
17+
{
18+
public FingerprintingTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext)
19+
: base(output, buildContext)
20+
{
21+
}
22+
23+
[Fact]
24+
public async Task TestOverrideHtmlAssetPlaceholdersWithFingerprinting()
25+
{
26+
var config = Configuration.Release;
27+
string extraProperties = "<OverrideHtmlAssetPlaceholders>true</OverrideHtmlAssetPlaceholders>";
28+
ProjectInfo info = CopyTestAsset(config, aot: false, TestAsset.WasmBasicTestApp, "OverrideHtmlAssetPlaceholders", extraProperties: extraProperties);
29+
UpdateFile(Path.Combine("wwwroot", "index.html"), new Dictionary<string, string> {
30+
{ """<script type='module' src="./main.js"></script>""", """<script type="importmap"></script> <script type='module' src="./main.js"></script>""" }
31+
});
32+
BuildProject(info, config, wasmFingerprintDotnetJs: true);
33+
BrowserRunOptions runOptions = new(config, TestScenario: "DotnetRun");
34+
await RunForBuildWithDotnetRun(runOptions);
35+
36+
PublishProject(info, config, new PublishOptions(UseCache: false), wasmFingerprintDotnetJs: true);
37+
await RunForPublishWithWebServer(runOptions);
38+
}
39+
}

src/mono/wasm/Wasm.Build.Tests/ProjectProviderBase.cs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ public abstract class ProjectProviderBase(ITestOutputHelper _testOutput, string?
4141

4242
public bool IsFingerprintingEnabled => EnvironmentVariables.UseFingerprinting;
4343

44-
public bool IsFingerprintingOnDotnetJsEnabled => EnvironmentVariables.UseFingerprintingDotnetJS;
45-
4644
// Returns the actual files on disk
4745
public IReadOnlyDictionary<string, DotNetFileName> AssertBasicBundle(AssertBundleOptions assertOptions)
4846
{
@@ -82,7 +80,6 @@ public IReadOnlyDictionary<string, DotNetFileName> FindAndAssertDotnetFiles(Asse
8280
{
8381
EnsureProjectDirIsSet();
8482
return FindAndAssertDotnetFiles(binFrameworkDir: assertOptions.BinFrameworkDir,
85-
expectFingerprintOnDotnetJs: IsFingerprintingOnDotnetJsEnabled,
8683
assertOptions,
8784
superSet: GetAllKnownDotnetFilesToFingerprintMap(assertOptions),
8885
expected: GetDotNetFilesExpectedSet(assertOptions)
@@ -94,11 +91,11 @@ public IReadOnlyDictionary<string, DotNetFileName> FindAndAssertDotnetFiles(Asse
9491

9592
public IReadOnlyDictionary<string, DotNetFileName> FindAndAssertDotnetFiles(
9693
string binFrameworkDir,
97-
bool expectFingerprintOnDotnetJs,
9894
AssertBundleOptions assertOptions,
9995
IReadOnlyDictionary<string, bool> superSet,
10096
IReadOnlySet<string> expected)
10197
{
98+
var expectFingerprintOnDotnetJs = assertOptions.ExpectDotnetJsFingerprinting;
10299
EnsureProjectDirIsSet();
103100
var actual = new SortedDictionary<string, DotNetFileName>();
104101

@@ -109,6 +106,9 @@ public IReadOnlyDictionary<string, DotNetFileName> FindAndAssertDotnetFiles(
109106
SearchOption.TopDirectoryOnly)
110107
.Order()
111108
.ToList();
109+
110+
var comparisonLogging = new List<string>();
111+
112112
foreach ((string expectedFilename, bool expectFingerprint) in superSet.OrderByDescending(kvp => kvp.Key))
113113
{
114114
string prefix = Path.GetFileNameWithoutExtension(expectedFilename);
@@ -121,7 +121,7 @@ public IReadOnlyDictionary<string, DotNetFileName> FindAndAssertDotnetFiles(
121121
return false;
122122

123123
string actualFilename = Path.GetFileName(actualFile);
124-
// _testOutput.WriteLine($"Comparing {expectedFilename} with {actualFile}, expectFingerprintOnDotnetJs: {expectFingerprintOnDotnetJs}, expectFingerprint: {expectFingerprint}");
124+
comparisonLogging.Add($"Comparing {expectedFilename} with {actualFile}, expectFingerprint: {expectFingerprint}");
125125
if (ShouldCheckFingerprint(expectedFilename: expectedFilename,
126126
expectFingerprintOnDotnetJs: expectFingerprintOnDotnetJs,
127127
expectFingerprintForThisFile: expectFingerprint))
@@ -149,12 +149,18 @@ public IReadOnlyDictionary<string, DotNetFileName> FindAndAssertDotnetFiles(
149149
}).ToList();
150150
}
151151

152-
// _testOutput.WriteLine($"Accepted count: {actual.Count}");
153-
// foreach (var kvp in actual)
154-
// _testOutput.WriteLine($"Accepted: \t[{kvp.Key}] = {kvp.Value}");
155-
156152
if (dotnetFiles.Any())
157153
{
154+
foreach (var message in comparisonLogging)
155+
{
156+
_testOutput.WriteLine(message);
157+
}
158+
_testOutput.WriteLine($"Accepted count: {actual.Count}");
159+
foreach (var kvp in actual)
160+
{
161+
_testOutput.WriteLine($"Accepted: \t[{kvp.Key}] = {kvp.Value}");
162+
}
163+
158164
throw new XunitException($"Found unknown files in {binFrameworkDir}:{Environment.NewLine} " +
159165
$"{string.Join($"{Environment.NewLine} ", dotnetFiles.Select(f => Path.GetRelativePath(binFrameworkDir, f)))}{Environment.NewLine}" +
160166
$"Add these to {nameof(GetAllKnownDotnetFilesToFingerprintMap)} method{Environment.NewLine}" +
@@ -173,7 +179,7 @@ private void AssertDotNetFilesSet(
173179
IReadOnlySet<string> expected,
174180
IReadOnlyDictionary<string, bool> superSet,
175181
IReadOnlyDictionary<string, DotNetFileName> actualReadOnly,
176-
bool expectFingerprintOnDotnetJs,
182+
bool? expectFingerprintOnDotnetJs,
177183
string bundleDir)
178184
{
179185
EnsureProjectDirIsSet();
@@ -399,8 +405,8 @@ private string[] GetFilesMatchingNameConsideringFingerprinting(string filePath,
399405
return dict;
400406
}
401407

402-
public bool ShouldCheckFingerprint(string expectedFilename, bool expectFingerprintOnDotnetJs, bool expectFingerprintForThisFile)
403-
=> IsFingerprintingEnabled && ((expectedFilename == "dotnet.js" && expectFingerprintOnDotnetJs) || expectFingerprintForThisFile);
408+
public bool ShouldCheckFingerprint(string expectedFilename, bool? expectFingerprintOnDotnetJs, bool expectFingerprintForThisFile)
409+
=> IsFingerprintingEnabled && ((expectedFilename == "dotnet.js" && expectFingerprintOnDotnetJs == true) || expectFingerprintForThisFile);
404410

405411

406412
public static void AssertRuntimePackPath(string buildOutput, string targetFramework, RuntimeVariant runtimeType = RuntimeVariant.SingleThreaded)
@@ -577,7 +583,7 @@ public BootJsonData AssertBootJson(AssertBundleOptions options)
577583
string extension = Path.GetExtension(expectedFilename).Substring(1);
578584

579585
if (ShouldCheckFingerprint(expectedFilename: expectedFilename,
580-
expectFingerprintOnDotnetJs: IsFingerprintingOnDotnetJsEnabled,
586+
expectFingerprintOnDotnetJs: options.ExpectDotnetJsFingerprinting,
581587
expectFingerprintForThisFile: expectFingerprint))
582588
{
583589
return Regex.Match(item, $"{prefix}{s_dotnetVersionHashRegex}{extension}").Success;

src/mono/wasm/Wasm.Build.Tests/Templates/WasmTemplateTestsBase.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,39 +119,45 @@ private void UpdateProjectFile(string projectFilePath, bool runAnalyzers, string
119119
public virtual (string projectDir, string buildOutput) PublishProject(
120120
ProjectInfo info,
121121
Configuration configuration,
122-
bool? isNativeBuild = null) => // null for WasmBuildNative unset
123-
BuildProjectCore(info, configuration, _defaultPublishOptions, isNativeBuild);
122+
bool? isNativeBuild = null,
123+
bool? wasmFingerprintDotnetJs = null) => // null for unset properties
124+
BuildProjectCore(info, configuration, _defaultPublishOptions, isNativeBuild, wasmFingerprintDotnetJs);
124125

125126
public virtual (string projectDir, string buildOutput) PublishProject(
126127
ProjectInfo info,
127128
Configuration configuration,
128129
PublishOptions publishOptions,
129-
bool? isNativeBuild = null) =>
130+
bool? isNativeBuild = null,
131+
bool? wasmFingerprintDotnetJs = null) =>
130132
BuildProjectCore(
131133
info,
132134
configuration,
133135
publishOptions with { ExtraMSBuildArgs = $"{_extraBuildArgsPublish} {publishOptions.ExtraMSBuildArgs}" },
134-
isNativeBuild
136+
isNativeBuild,
137+
wasmFingerprintDotnetJs
135138
);
136139

137140
public virtual (string projectDir, string buildOutput) BuildProject(
138141
ProjectInfo info,
139142
Configuration configuration,
140-
bool? isNativeBuild = null) => // null for WasmBuildNative unset
141-
BuildProjectCore(info, configuration, _defaultBuildOptions, isNativeBuild);
143+
bool? isNativeBuild = null,
144+
bool? wasmFingerprintDotnetJs = null) => // null for unset properties
145+
BuildProjectCore(info, configuration, _defaultBuildOptions, isNativeBuild, wasmFingerprintDotnetJs);
142146

143147
public virtual (string projectDir, string buildOutput) BuildProject(
144148
ProjectInfo info,
145149
Configuration configuration,
146150
BuildOptions buildOptions,
147-
bool? isNativeBuild = null) =>
148-
BuildProjectCore(info, configuration, buildOptions, isNativeBuild);
151+
bool? isNativeBuild = null,
152+
bool? wasmFingerprintDotnetJs = null) =>
153+
BuildProjectCore(info, configuration, buildOptions, isNativeBuild, wasmFingerprintDotnetJs);
149154

150155
private (string projectDir, string buildOutput) BuildProjectCore(
151156
ProjectInfo info,
152157
Configuration configuration,
153158
MSBuildOptions buildOptions,
154-
bool? isNativeBuild = null)
159+
bool? isNativeBuild = null,
160+
bool? wasmFingerprintDotnetJs = null)
155161
{
156162
if (buildOptions.AOT)
157163
{
@@ -182,7 +188,7 @@ public virtual (string projectDir, string buildOutput) BuildProject(
182188

183189
if (buildOptions.AssertAppBundle)
184190
{
185-
_provider.AssertWasmSdkBundle(configuration, buildOptions, IsUsingWorkloads, isNativeBuild, res.Output);
191+
_provider.AssertWasmSdkBundle(configuration, buildOptions, IsUsingWorkloads, isNativeBuild, wasmFingerprintDotnetJs, res.Output);
186192
}
187193
return (_projectDir, res.Output);
188194
}

src/mono/wasm/Wasm.Build.Tests/Wasm.Build.Tests.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,6 @@
116116
<RunScriptCommands Condition="'$(OS)' != 'Windows_NT'" Include="export WASM_FINGERPRINT_ASSETS=$(WasmFingerprintAssets)" />
117117
<RunScriptCommands Condition="'$(OS)' == 'Windows_NT'" Include="set WASM_FINGERPRINT_ASSETS=$(WasmFingerprintAssets)" />
118118

119-
<RunScriptCommands Condition="'$(OS)' != 'Windows_NT'" Include="export WASM_FINGERPRINT_DOTNET_JS=$(WasmFingerprintDotnetJs)" />
120-
<RunScriptCommands Condition="'$(OS)' == 'Windows_NT'" Include="set WASM_FINGERPRINT_DOTNET_JS=$(WasmFingerprintDotnetJs)" />
121-
122119
<RunScriptCommands Condition="'$(OS)' != 'Windows_NT'" Include="export &quot;XUnitTraitArg=$(_XUnitTraitArg)&quot;" />
123120
<RunScriptCommands Condition="'$(OS)' == 'Windows_NT'" Include="set &quot;XUnitTraitArg=$(_XUnitTraitArg)&quot;" />
124121

src/mono/wasm/Wasm.Build.Tests/WasmSdkBasedProjectProvider.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public NativeFilesType GetExpectedFileType(Configuration config, bool isAOT, boo
9090
(config == Configuration.Release) ? NativeFilesType.Relinked :
9191
NativeFilesType.FromRuntimePack;
9292

93-
public void AssertBundle(Configuration config, MSBuildOptions buildOptions, bool isUsingWorkloads, bool? isNativeBuild = null)
93+
public void AssertBundle(Configuration config, MSBuildOptions buildOptions, bool isUsingWorkloads, bool? isNativeBuild = null, bool? wasmFingerprintDotnetJs = null)
9494
{
9595
string frameworkDir = string.IsNullOrEmpty(buildOptions.NonDefaultFrameworkDir) ?
9696
GetBinFrameworkDir(config, buildOptions.IsPublish, _defaultTargetFramework) :
@@ -103,7 +103,8 @@ public void AssertBundle(Configuration config, MSBuildOptions buildOptions, bool
103103
BinFrameworkDir: frameworkDir,
104104
ExpectSymbolsFile: true,
105105
AssertIcuAssets: true,
106-
AssertSymbolsFile: false
106+
AssertSymbolsFile: false,
107+
ExpectDotnetJsFingerprinting: wasmFingerprintDotnetJs
107108
));
108109
}
109110

@@ -172,14 +173,14 @@ private void AssertBundle(AssertBundleOptions assertOptions)
172173
}
173174
}
174175

175-
public void AssertWasmSdkBundle(Configuration config, MSBuildOptions buildOptions, bool isUsingWorkloads, bool? isNativeBuild = null, string? buildOutput = null)
176+
public void AssertWasmSdkBundle(Configuration config, MSBuildOptions buildOptions, bool isUsingWorkloads, bool? isNativeBuild = null, bool? wasmFingerprintDotnetJs = null, string? buildOutput = null)
176177
{
177178
if (isUsingWorkloads && buildOutput is not null)
178179
{
179180
// In no-workload case, the path would be from a restored nuget
180181
ProjectProviderBase.AssertRuntimePackPath(buildOutput, buildOptions.TargetFramework ?? _defaultTargetFramework, buildOptions.RuntimeType);
181182
}
182-
AssertBundle(config, buildOptions, isUsingWorkloads, isNativeBuild);
183+
AssertBundle(config, buildOptions, isUsingWorkloads, isNativeBuild, wasmFingerprintDotnetJs);
183184
}
184185

185186
public BuildPaths GetBuildPaths(Configuration configuration, bool forPublish)

0 commit comments

Comments
 (0)