-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
WasmTemplateTests.cs
304 lines (251 loc) · 13.3 KB
/
WasmTemplateTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
#nullable enable
namespace Wasm.Build.Tests
{
public class WasmTemplateTests : BuildTestBase
{
public WasmTemplateTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext)
: base(output, buildContext)
{
}
private void updateProgramCS() {
string programText = """
Console.WriteLine("Hello, Console!");
for (int i = 0; i < args.Length; i ++)
Console.WriteLine ($"args[{i}] = {args[i]}");
""";
var path = Path.Combine(_projectDir!, "Program.cs");
string text = File.ReadAllText(path);
text = text.Replace(@"Console.WriteLine(""Hello, Console!"");", programText);
text = text.Replace("return 0;", "return 42;");
File.WriteAllText(path, text);
}
[Theory]
[InlineData("Debug")]
[InlineData("Release")]
public void BrowserBuildThenPublish(string config)
{
string id = $"browser_{config}_{Path.GetRandomFileName()}";
string projectFile = CreateWasmTemplateProject(id, "wasmbrowser");
string projectName = Path.GetFileNameWithoutExtension(projectFile);
var buildArgs = new BuildArgs(projectName, config, false, id, null);
buildArgs = ExpandBuildArgs(buildArgs);
BuildProject(buildArgs,
id: id,
new BuildProjectOptions(
DotnetWasmFromRuntimePack: true,
CreateProject: false,
HasV8Script: false,
MainJS: "main.js",
Publish: false,
TargetFramework: "net7.0"
));
AssertDotNetJsSymbols(Path.Combine(GetBinDir(config), "AppBundle"), fromRuntimePack: true);
if (!_buildContext.TryGetBuildFor(buildArgs, out BuildProduct? product))
throw new XunitException($"Test bug: could not get the build product in the cache");
File.Move(product!.LogFile, Path.ChangeExtension(product.LogFile!, ".first.binlog"));
_testOutput.WriteLine($"{Environment.NewLine}Publishing with no changes ..{Environment.NewLine}");
_testOutput.WriteLine($"{Environment.NewLine}Publishing with no changes ..{Environment.NewLine}");
bool expectRelinking = config == "Release";
BuildProject(buildArgs,
id: id,
new BuildProjectOptions(
DotnetWasmFromRuntimePack: !expectRelinking,
CreateProject: false,
HasV8Script: false,
MainJS: "main.js",
Publish: true,
TargetFramework: "net7.0",
UseCache: false));
AssertDotNetJsSymbols(Path.Combine(GetBinDir(config), "AppBundle"), fromRuntimePack: !expectRelinking);
}
[Theory]
[InlineData("Debug")]
[InlineData("Release")]
public void ConsoleBuildThenPublish(string config)
{
string id = $"{config}_{Path.GetRandomFileName()}";
string projectFile = CreateWasmTemplateProject(id, "wasmconsole");
string projectName = Path.GetFileNameWithoutExtension(projectFile);
var buildArgs = new BuildArgs(projectName, config, false, id, null);
buildArgs = ExpandBuildArgs(buildArgs);
BuildProject(buildArgs,
id: id,
new BuildProjectOptions(
DotnetWasmFromRuntimePack: true,
CreateProject: false,
HasV8Script: false,
MainJS: "main.mjs",
Publish: false,
TargetFramework: "net7.0"
));
AssertDotNetJsSymbols(Path.Combine(GetBinDir(config), "AppBundle"), fromRuntimePack: true);
(int exitCode, string output) = RunProcess(s_buildEnv.DotNet, _testOutput, args: $"run --no-build -c {config}", workingDir: _projectDir);
Assert.Equal(0, exitCode);
Assert.Contains("Hello, Console!", output);
if (!_buildContext.TryGetBuildFor(buildArgs, out BuildProduct? product))
throw new XunitException($"Test bug: could not get the build product in the cache");
File.Move(product!.LogFile, Path.ChangeExtension(product.LogFile!, ".first.binlog"));
_testOutput.WriteLine($"{Environment.NewLine}Publishing with no changes ..{Environment.NewLine}");
bool expectRelinking = config == "Release";
BuildProject(buildArgs,
id: id,
new BuildProjectOptions(
DotnetWasmFromRuntimePack: !expectRelinking,
CreateProject: false,
HasV8Script: false,
MainJS: "main.mjs",
Publish: true,
TargetFramework: "net7.0",
UseCache: false));
AssertDotNetJsSymbols(Path.Combine(GetBinDir(config), "AppBundle"), fromRuntimePack: !expectRelinking);
}
[ConditionalTheory(typeof(BuildTestBase), nameof(IsUsingWorkloads))]
[InlineData("Debug", false)]
[InlineData("Debug", true)]
[InlineData("Release", false)]
[InlineData("Release", true)]
public void ConsoleBuildAndRun(string config, bool relinking)
{
string id = $"{config}_{Path.GetRandomFileName()}";
string projectFile = CreateWasmTemplateProject(id, "wasmconsole");
string projectName = Path.GetFileNameWithoutExtension(projectFile);
updateProgramCS();
if (relinking)
AddItemsPropertiesToProject(projectFile, "<WasmBuildNative>true</WasmBuildNative>");
var buildArgs = new BuildArgs(projectName, config, false, id, null);
buildArgs = ExpandBuildArgs(buildArgs);
BuildProject(buildArgs,
id: id,
new BuildProjectOptions(
DotnetWasmFromRuntimePack: !relinking,
CreateProject: false,
HasV8Script: false,
MainJS: "main.mjs",
Publish: false,
TargetFramework: "net7.0"
));
AssertDotNetJsSymbols(Path.Combine(GetBinDir(config), "AppBundle"), fromRuntimePack: !relinking);
(int exitCode, string output) = RunProcess(s_buildEnv.DotNet, _testOutput, args: $"run --no-build -c {config} x y z", workingDir: _projectDir);
Assert.Equal(42, exitCode);
Assert.Contains("args[0] = x", output);
Assert.Contains("args[1] = y", output);
Assert.Contains("args[2] = z", output);
}
public static TheoryData<string, bool, bool> TestDataForConsolePublishAndRun()
{
var data = new TheoryData<string, bool, bool>();
data.Add("Debug", false, false);
data.Add("Debug", false, false);
data.Add("Debug", false, true);
data.Add("Release", false, false); // Release relinks by default
// [ActiveIssue("https://github.com/dotnet/runtime/issues/71887", TestPlatforms.Windows)]
if (!OperatingSystem.IsWindows())
{
data.Add("Debug", true, false);
data.Add("Release", true, false);
}
return data;
}
[ConditionalTheory(typeof(BuildTestBase), nameof(IsUsingWorkloads))]
[MemberData(nameof(TestDataForConsolePublishAndRun))]
public void ConsolePublishAndRun(string config, bool aot, bool relinking)
{
string id = $"{config}_{Path.GetRandomFileName()}";
string projectFile = CreateWasmTemplateProject(id, "wasmconsole");
string projectName = Path.GetFileNameWithoutExtension(projectFile);
updateProgramCS();
if (aot)
AddItemsPropertiesToProject(projectFile, "<RunAOTCompilation>true</RunAOTCompilation>");
else if (relinking)
AddItemsPropertiesToProject(projectFile, "<WasmBuildNative>true</WasmBuildNative>");
var buildArgs = new BuildArgs(projectName, config, aot, id, null);
buildArgs = ExpandBuildArgs(buildArgs);
bool expectRelinking = config == "Release" || aot || relinking;
BuildProject(buildArgs,
id: id,
new BuildProjectOptions(
DotnetWasmFromRuntimePack: !expectRelinking,
CreateProject: false,
HasV8Script: false,
MainJS: "main.mjs",
Publish: true,
TargetFramework: "net7.0",
UseCache: false));
if (!aot)
{
// These are disabled for AOT explicitly
AssertDotNetJsSymbols(Path.Combine(GetBinDir(config), "AppBundle"), fromRuntimePack: !expectRelinking);
}
else
{
AssertFilesDontExist(Path.Combine(GetBinDir(config), "AppBundle"), new[] { "dotnet.js.symbols" });
}
// FIXME: pass envvars via the environment, once that is supported
string runArgs = $"run --no-build -c {config}";
if (aot)
runArgs += $" --setenv=MONO_LOG_MASK=aot --setenv=MONO_LOG_LEVEL=debug";
runArgs += " x y z";
var res = new RunCommand(s_buildEnv, _testOutput, label: id)
.WithWorkingDirectory(_projectDir!)
.ExecuteWithCapturedOutput(runArgs)
.EnsureExitCode(42);
if (aot)
Assert.Contains($"AOT: image '{Path.GetFileNameWithoutExtension(projectFile)}' found", res.Output);
Assert.Contains("args[0] = x", res.Output);
Assert.Contains("args[1] = y", res.Output);
Assert.Contains("args[2] = z", res.Output);
}
[ConditionalFact(typeof(BuildTestBase), nameof(IsUsingWorkloads))]
public async Task BlazorRunTest()
{
string config = "Debug";
string id = $"blazor_{config}_{Path.GetRandomFileName()}";
string projectFile = CreateWasmTemplateProject(id, "blazorwasm");
// string projectName = Path.GetFileNameWithoutExtension(projectFile);
// var buildArgs = new BuildArgs(projectName, config, false, id, null);
// buildArgs = ExpandBuildArgs(buildArgs);
new DotNetCommand(s_buildEnv, _testOutput)
.WithWorkingDirectory(_projectDir!)
.Execute($"build -c {config} -bl:{Path.Combine(s_buildEnv.LogRootPath, $"{id}.binlog")}")
.EnsureSuccessful();
using var runCommand = new RunCommand(s_buildEnv, _testOutput)
.WithWorkingDirectory(_projectDir!);
await using var runner = new BrowserRunner();
var page = await runner.RunAsync(runCommand, $"run -c {config} --no-build");
await page.Locator("text=Counter").ClickAsync();
var txt = await page.Locator("p[role='status']").InnerHTMLAsync();
Assert.Equal("Current count: 0", txt);
await page.Locator("text=\"Click me\"").ClickAsync();
txt = await page.Locator("p[role='status']").InnerHTMLAsync();
Assert.Equal("Current count: 1", txt);
}
[ConditionalFact(typeof(BuildTestBase), nameof(IsUsingWorkloads))]
public async Task BrowserTest()
{
string config = "Debug";
string id = $"browser_{config}_{Path.GetRandomFileName()}";
CreateWasmTemplateProject(id, "wasmbrowser");
// var buildArgs = new BuildArgs(projectName, config, false, id, null);
// buildArgs = ExpandBuildArgs(buildArgs);
new DotNetCommand(s_buildEnv, _testOutput)
.WithWorkingDirectory(_projectDir!)
.Execute($"build -c {config} -bl:{Path.Combine(s_buildEnv.LogRootPath, $"{id}.binlog")}")
.EnsureSuccessful();
using var runCommand = new RunCommand(s_buildEnv, _testOutput)
.WithWorkingDirectory(_projectDir!);
await using var runner = new BrowserRunner();
var page = await runner.RunAsync(runCommand, $"run -c {config} --no-build -r browser-wasm --forward-console");
await runner.WaitForExitMessageAsync(TimeSpan.FromMinutes(2));
Assert.Contains("Hello, Browser!", string.Join(Environment.NewLine, runner.OutputLines));
}
}
}