Skip to content

Commit 79d1988

Browse files
authored
[wasm][wbt] Test that dotnet.js could be run from any current directory (#69441)
In #72275 we fixed locateFile to deal with relative file path or URL. We could also handle absolute path/URL. This PR is adding tests to prove it works.
1 parent 34ea135 commit 79d1988

File tree

5 files changed

+106
-5
lines changed

5 files changed

+106
-5
lines changed

eng/testing/scenarios/BuildWasmAppsJobsList.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Wasm.Build.Tests.BlazorWasmBuildPublishTests
66
Wasm.Build.Tests.BlazorWasmTests
77
Wasm.Build.Tests.BuildPublishTests
88
Wasm.Build.Tests.CleanTests
9+
Wasm.Build.Tests.ConfigSrcTests
910
Wasm.Build.Tests.InvariantGlobalizationTests
1011
Wasm.Build.Tests.LocalEMSDKTests
1112
Wasm.Build.Tests.MainWithArgsTests
@@ -17,4 +18,5 @@ Wasm.Build.Tests.SatelliteAssembliesTests
1718
Wasm.Build.Tests.WasmBuildAppTest
1819
Wasm.Build.Tests.WasmNativeDefaultsTests
1920
Wasm.Build.Tests.WorkloadTests
21+
Wasm.Build.Tests.WasmRunOutOfAppBundleTests
2022
Wasm.Build.Tests.WasmTemplateTests

src/mono/wasm/test-main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ function initRunArgs() {
166166
runArgs.enableGC = runArgs.enableGC === undefined ? true : runArgs.enableGC;
167167
runArgs.diagnosticTracing = runArgs.diagnosticTracing === undefined ? false : runArgs.diagnosticTracing;
168168
runArgs.debugging = runArgs.debugging === undefined ? false : runArgs.debugging;
169+
runArgs.configSrc = runArgs.configSrc === undefined ? './mono-config.json' : runArgs.configSrc;
169170
// default'ing to true for tests, unless debugging
170171
runArgs.forwardConsole = runArgs.forwardConsole === undefined ? !runArgs.debugging : runArgs.forwardConsole;
171172
}
@@ -217,6 +218,9 @@ function processQueryArguments(incomingArguments) {
217218
} else {
218219
console.warn("--fetch-random-delay only works on browser")
219220
}
221+
} else if (currentArg.startsWith("--config-src=")) {
222+
const arg = currentArg.substring("--config-src=".length);
223+
runArgs.configSrc = arg;
220224
} else {
221225
break;
222226
}
@@ -355,7 +359,7 @@ Promise.all([argsPromise, loadDotnetPromise]).then(async ([_, createDotnetRuntim
355359
return createDotnetRuntime(({ MONO, INTERNAL, BINDING, IMPORTS, EXPORTS, Module }) => ({
356360
disableDotnet6Compatibility: true,
357361
config: null,
358-
configSrc: "./mono-config.json",
362+
configSrc: runArgs.configSrc || "./mono-config.json",
359363
onConfigLoaded: (config) => {
360364
if (!Module.config) {
361365
const err = new Error("Could not find ./mono-config.json. Cancelling run");

src/tests/BuildWasmApps/Wasm.Build.Tests/BuildTestBase.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,13 @@ protected string RunAndTestWasmApp(BuildArgs buildArgs,
130130
string id,
131131
Action<string>? test=null,
132132
string? buildDir = null,
133+
string? bundleDir = null,
133134
int expectedExitCode = 0,
134135
string? args = null,
135136
Dictionary<string, string>? envVars = null,
136137
string targetFramework = DefaultTargetFramework,
137-
string? extraXHarnessMonoArgs = null)
138+
string? extraXHarnessMonoArgs = null,
139+
string jsRelativePath = "test-main.js")
138140
{
139141
buildDir ??= _projectDir;
140142
envVars ??= new();
@@ -151,13 +153,13 @@ protected string RunAndTestWasmApp(BuildArgs buildArgs,
151153
envVars[kvp.Key] = kvp.Value;
152154
}
153155

154-
string bundleDir = Path.Combine(GetBinDir(baseDir: buildDir, config: buildArgs.Config, targetFramework: targetFramework), "AppBundle");
156+
bundleDir ??= Path.Combine(GetBinDir(baseDir: buildDir, config: buildArgs.Config, targetFramework: targetFramework), "AppBundle");
155157

156158
// Use wasm-console.log to get the xharness output for non-browser cases
157159
(string testCommand, string extraXHarnessArgs, bool useWasmConsoleOutput) = host switch
158160
{
159-
RunHost.V8 => ("wasm test", "--js-file=test-main.js --engine=V8 -v trace", true),
160-
RunHost.NodeJS => ("wasm test", "--js-file=test-main.js --engine=NodeJS -v trace", true),
161+
RunHost.V8 => ("wasm test", $"--js-file={jsRelativePath} --engine=V8 -v trace", true),
162+
RunHost.NodeJS => ("wasm test", $"--js-file={jsRelativePath} --engine=NodeJS -v trace", true),
161163
_ => ("wasm test-browser", $"-v trace -b {host} --web-server-use-cop", false)
162164
};
163165

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.IO;
5+
using Xunit;
6+
using Xunit.Abstractions;
7+
8+
#nullable enable
9+
10+
namespace Wasm.Build.Tests;
11+
12+
public class ConfigSrcTests : BuildTestBase
13+
{
14+
public ConfigSrcTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext) : base(output, buildContext)
15+
{}
16+
17+
// NOTE: port number determinizes dynamically, so could not generate absolute URI
18+
[Theory]
19+
[BuildAndRun(host: RunHost.V8 | RunHost.NodeJS)]
20+
public void ConfigSrcAbsolutePath(BuildArgs buildArgs, RunHost host, string id)
21+
{
22+
buildArgs = buildArgs with { ProjectName = $"configsrcabsolute_{buildArgs.Config}_{buildArgs.AOT}" };
23+
buildArgs = ExpandBuildArgs(buildArgs);
24+
25+
BuildProject(buildArgs,
26+
id: id,
27+
new BuildProjectOptions(
28+
InitProject: () => File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), s_mainReturns42),
29+
DotnetWasmFromRuntimePack: !(buildArgs.AOT || buildArgs.Config == "Release")));
30+
31+
string binDir = GetBinDir(baseDir: _projectDir!, config: buildArgs.Config);
32+
string bundleDir = Path.Combine(binDir, "AppBundle");
33+
string configSrc = Path.GetFullPath(Path.Combine(bundleDir, "mono-config.json"));
34+
35+
RunAndTestWasmApp(buildArgs, expectedExitCode: 42, host: host, id: id, extraXHarnessMonoArgs: $"--config-src={configSrc}");
36+
}
37+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.IO;
5+
using Xunit;
6+
using Xunit.Abstractions;
7+
8+
#nullable enable
9+
10+
namespace Wasm.Build.Tests;
11+
12+
public class WasmRunOutOfAppBundleTests : BuildTestBase
13+
{
14+
public WasmRunOutOfAppBundleTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext) : base(output, buildContext)
15+
{}
16+
17+
[Theory]
18+
[BuildAndRun]
19+
public void RunOutOfAppBundle(BuildArgs buildArgs, RunHost host, string id)
20+
{
21+
buildArgs = buildArgs with { ProjectName = $"outofappbundle_{buildArgs.Config}_{buildArgs.AOT}" };
22+
buildArgs = ExpandBuildArgs(buildArgs);
23+
24+
BuildProject(buildArgs,
25+
id: id,
26+
new BuildProjectOptions(
27+
InitProject: () => File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), s_mainReturns42),
28+
DotnetWasmFromRuntimePack: !(buildArgs.AOT || buildArgs.Config == "Release")));
29+
30+
string binDir = GetBinDir(baseDir: _projectDir!, config: buildArgs.Config);
31+
string appBundleDir = Path.Combine(binDir, "AppBundle");
32+
string outerDir = Path.GetFullPath(Path.Combine(appBundleDir, ".."));
33+
34+
if (host is RunHost.Chrome)
35+
{
36+
string indexHtmlPath = Path.Combine(appBundleDir, "index.html");
37+
// Delete the original one, so we don't use that by accident
38+
if (File.Exists(indexHtmlPath))
39+
File.Delete(indexHtmlPath);
40+
41+
indexHtmlPath = Path.Combine(outerDir, "index.html");
42+
if (!File.Exists(indexHtmlPath))
43+
{
44+
var html = @"<html><body><script type=""module"" src=""./AppBundle/test-main.js""></script></body></html>";
45+
File.WriteAllText(indexHtmlPath, html);
46+
}
47+
}
48+
49+
RunAndTestWasmApp(buildArgs,
50+
expectedExitCode: 42,
51+
host: host,
52+
id: id,
53+
bundleDir: outerDir,
54+
jsRelativePath: "./AppBundle/test-main.js");
55+
}
56+
}

0 commit comments

Comments
 (0)