Skip to content

Commit d3d910e

Browse files
authored
[wasm] Wasm.Build.Tests - some refactoring, and rationalizing (#88357)
1 parent 1dd0fa2 commit d3d910e

19 files changed

+661
-322
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
#nullable enable
5+
6+
namespace Wasm.Build.Tests;
7+
8+
public record AssertTestMainJsAppBundleOptions
9+
(
10+
string BundleDir,
11+
string ProjectName,
12+
string Config,
13+
string MainJS,
14+
bool HasV8Script,
15+
GlobalizationMode? GlobalizationMode,
16+
string PredefinedIcudt = "",
17+
bool UseWebcil = true,
18+
bool IsBrowserProject = true,
19+
bool IsPublish = false
20+
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ await BlazorRunForBuildWithDotnetRun("debug", onConsoleMessage: msg =>
5454
Assert.True(existsChecked, "File '/appsettings.json' wasn't found");
5555
Assert.True(contentChecked, "Content of '/appsettings.json' is not matched");
5656
}
57-
}
57+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public void DefaultTemplate_WithoutWorkload(string config)
3434

3535
// Build
3636
BlazorBuildInternal(id, config, publish: false);
37-
AssertBlazorBootJson(config, isPublish: false, isNet7AndBelow: false);
37+
AssertBlazorBootJson(config, isPublish: false);
3838

3939
// Publish
4040
BlazorBuildInternal(id, config, publish: true);
41-
AssertBlazorBootJson(config, isPublish: true, isNet7AndBelow: false);
41+
AssertBlazorBootJson(config, isPublish: true);
4242
}
4343

4444
[Theory]

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ public void NativeBuild_WithDeployOnBuild_UsedByVS(string config, bool nativeRel
3939

4040
var expectedFileType = nativeRelink ? NativeFilesType.Relinked : NativeFilesType.AOT;
4141

42-
AssertDotNetNativeFiles(expectedFileType, config, forPublish: true, targetFramework: DefaultTargetFrameworkForBlazor);
43-
AssertBlazorBundle(config, isPublish: true, dotnetWasmFromRuntimePack: false);
42+
AssertBlazorBundle(new BlazorBuildOptions
43+
(
44+
Id: id,
45+
Config: config,
46+
ExpectedFileType: expectedFileType
47+
), isPublish: true);
4448

4549
if (expectedFileType == NativeFilesType.AOT)
4650
{

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

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -66,70 +66,4 @@ private CommandResult PublishForRequiresWorkloadTest(string config, string extra
6666
$"-bl:{publishLogPath}",
6767
$"-p:Configuration={config}");
6868
}
69-
70-
[Theory]
71-
[InlineData("Debug")]
72-
[InlineData("Release")]
73-
public void Net50Projects_NativeReference(string config)
74-
=> BuildNet50Project(config, aot: false, expectError: true, @"<NativeFileReference Include=""native-lib.o"" />");
75-
76-
public static TheoryData<string, bool, bool> Net50TestData = new()
77-
{
78-
{ "Debug", /*aot*/ true, /*expectError*/ true },
79-
{ "Debug", /*aot*/ false, /*expectError*/ false },
80-
{ "Release", /*aot*/ true, /*expectError*/ true },
81-
{ "Release", /*aot*/ false, /*expectError*/ false }
82-
};
83-
84-
// FIXME: test for WasmBuildNative=true?
85-
[Theory]
86-
[MemberData(nameof(Net50TestData))]
87-
public void Net50Projects_AOT(string config, bool aot, bool expectError)
88-
=> BuildNet50Project(config, aot: aot, expectError: expectError);
89-
90-
private void BuildNet50Project(string config, bool aot, bool expectError, string? extraItems=null)
91-
{
92-
string id = $"Blazor_net50_{config}_{aot}_{Path.GetRandomFileName()}";
93-
InitBlazorWasmProjectDir(id);
94-
95-
string directoryBuildTargets = @"<Project>
96-
<Target Name=""PrintAllProjects"" BeforeTargets=""Build"">
97-
<Message Text=""** UsingBrowserRuntimeWorkload: '$(UsingBrowserRuntimeWorkload)'"" Importance=""High"" />
98-
</Target>
99-
</Project>";
100-
101-
File.WriteAllText(Path.Combine(_projectDir!, "Directory.Build.props"), "<Project />");
102-
File.WriteAllText(Path.Combine(_projectDir!, "Directory.Build.targets"), directoryBuildTargets);
103-
104-
string logPath = Path.Combine(s_buildEnv.LogRootPath, id);
105-
Utils.DirectoryCopy(Path.Combine(BuildEnvironment.TestAssetsPath, "Blazor_net50"), Path.Combine(_projectDir!));
106-
107-
string projectFile = Path.Combine(_projectDir!, "Blazor_net50.csproj");
108-
AddItemsPropertiesToProject(projectFile, extraItems: extraItems);
109-
110-
string publishLogPath = Path.Combine(logPath, $"{id}.binlog");
111-
CommandResult result = new DotNetCommand(s_buildEnv, _testOutput)
112-
.WithWorkingDirectory(_projectDir!)
113-
.WithEnvironmentVariable("NUGET_PACKAGES", _nugetPackagesDir)
114-
.ExecuteWithCapturedOutput("publish",
115-
$"-bl:{publishLogPath}",
116-
(aot ? "-p:RunAOTCompilation=true" : ""),
117-
$"-p:Configuration={config}");
118-
119-
if (expectError)
120-
{
121-
result.EnsureExitCode(1);
122-
Assert.Contains("are only supported for projects targeting net6.0+", result.Output);
123-
}
124-
else
125-
{
126-
result.EnsureSuccessful();
127-
Assert.Contains("** UsingBrowserRuntimeWorkload: 'false'", result.Output);
128-
129-
string binFrameworkDir = FindBlazorBinFrameworkDir(config, forPublish: true, framework: "net5.0");
130-
AssertBlazorBootJson(config, isPublish: true, isNet7AndBelow: true, binFrameworkDir: binFrameworkDir);
131-
// dotnet.wasm here would be from 5.0 nuget like:
132-
// /Users/radical/.nuget/packages/microsoft.netcore.app.runtime.browser-wasm/5.0.9/runtimes/browser-wasm/native/dotnet.wasm
133-
}
134-
}
13569
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.Data;
7+
using System.IO;
8+
using System.Linq;
9+
using Xunit;
10+
using Xunit.Abstractions;
11+
using System.Runtime.Serialization.Json;
12+
using Microsoft.NET.Sdk.WebAssembly;
13+
14+
#nullable enable
15+
16+
namespace Wasm.Build.Tests;
17+
18+
public class BlazorWasmProjectProvider(string projectDir, ITestOutputHelper testOutput)
19+
: WasmSdkBasedProjectProvider(projectDir, testOutput)
20+
{
21+
public void AssertBlazorBootJson(
22+
string binFrameworkDir,
23+
bool expectFingerprintOnDotnetJs = false,
24+
bool isPublish = false,
25+
RuntimeVariant runtimeType = RuntimeVariant.SingleThreaded)
26+
{
27+
string bootJsonPath = Path.Combine(binFrameworkDir, "blazor.boot.json");
28+
Assert.True(File.Exists(bootJsonPath), $"Expected to find {bootJsonPath}");
29+
30+
BootJsonData bootJson = ParseBootData(bootJsonPath);
31+
var bootJsonEntries = bootJson.resources.runtime.Keys.Where(k => k.StartsWith("dotnet.", StringComparison.Ordinal)).ToArray();
32+
33+
var expectedEntries = new SortedDictionary<string, Action<string>>();
34+
IReadOnlySet<string> expected = GetDotNetFilesExpectedSet(runtimeType, isPublish);
35+
36+
var knownSet = GetAllKnownDotnetFilesToFingerprintMap(runtimeType);
37+
foreach (string expectedFilename in expected)
38+
{
39+
if (Path.GetExtension(expectedFilename) == ".map")
40+
continue;
41+
42+
bool expectFingerprint = knownSet[expectedFilename];
43+
expectedEntries[expectedFilename] = item =>
44+
{
45+
string prefix = Path.GetFileNameWithoutExtension(expectedFilename);
46+
string extension = Path.GetExtension(expectedFilename).Substring(1);
47+
48+
if (ShouldCheckFingerprint(expectedFilename: expectedFilename,
49+
expectFingerprintOnDotnetJs: expectFingerprintOnDotnetJs,
50+
expectFingerprintForThisFile: expectFingerprint))
51+
{
52+
Assert.Matches($"{prefix}{s_dotnetVersionHashRegex}{extension}", item);
53+
}
54+
else
55+
{
56+
Assert.Equal(expectedFilename, item);
57+
}
58+
59+
string absolutePath = Path.Combine(binFrameworkDir, item);
60+
Assert.True(File.Exists(absolutePath), $"Expected to find '{absolutePath}'");
61+
};
62+
}
63+
// FIXME: maybe use custom code so the details can show up in the log
64+
Assert.Collection(bootJsonEntries.Order(), expectedEntries.Values.ToArray());
65+
}
66+
67+
public static BootJsonData ParseBootData(string bootJsonPath)
68+
{
69+
using FileStream stream = File.OpenRead(bootJsonPath);
70+
stream.Position = 0;
71+
var serializer = new DataContractJsonSerializer(
72+
typeof(BootJsonData),
73+
new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true });
74+
75+
var config = (BootJsonData?)serializer.ReadObject(stream);
76+
Assert.NotNull(config);
77+
return config;
78+
}
79+
80+
public string FindBlazorBinFrameworkDir(string config, bool forPublish, string framework)
81+
{
82+
string basePath = Path.Combine(ProjectDir, "bin", config, framework);
83+
if (forPublish)
84+
basePath = FindSubDirIgnoringCase(basePath, "publish");
85+
86+
return Path.Combine(basePath, "wwwroot", "_framework");
87+
}
88+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
7+
#nullable enable
8+
9+
namespace Wasm.Build.Tests;
10+
11+
public record BuildProjectOptions
12+
(
13+
Action? InitProject = null,
14+
bool? DotnetWasmFromRuntimePack = null,
15+
GlobalizationMode? GlobalizationMode = null,
16+
string? PredefinedIcudt = null,
17+
bool UseCache = true,
18+
bool ExpectSuccess = true,
19+
bool AssertAppBundle = true,
20+
bool CreateProject = true,
21+
bool Publish = true,
22+
bool BuildOnlyAfterPublish = true,
23+
bool HasV8Script = true,
24+
string? Verbosity = null,
25+
string? Label = null,
26+
string? TargetFramework = null,
27+
string? MainJS = null,
28+
bool IsBrowserProject = true,
29+
IDictionary<string, string>? ExtraBuildEnvironmentVariables = null
30+
);

0 commit comments

Comments
 (0)