Skip to content
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

[wasm][wbt] Test that dotnet.js could be run from any current directory #69441

Merged
merged 10 commits into from Jul 28, 2022
9 changes: 8 additions & 1 deletion src/mono/wasm/test-main.js
Expand Up @@ -166,6 +166,7 @@ function initRunArgs() {
runArgs.enableGC = runArgs.enableGC === undefined ? true : runArgs.enableGC;
runArgs.diagnosticTracing = runArgs.diagnosticTracing === undefined ? false : runArgs.diagnosticTracing;
runArgs.debugging = runArgs.debugging === undefined ? false : runArgs.debugging;
runArgs.configSrc = runArgs.configSrc === undefined ? './mono-config.json' : runArgs.configSrc;
// default'ing to true for tests, unless debugging
runArgs.forwardConsole = runArgs.forwardConsole === undefined ? !runArgs.debugging : runArgs.forwardConsole;
}
Expand Down Expand Up @@ -217,6 +218,12 @@ function processQueryArguments(incomingArguments) {
} else {
console.warn("--fetch-random-delay only works on browser")
}
} else if (currentArg.startsWith("--config-src=")) {
const arg = currentArg.substring("--config-src=".length);
runArgs.configSrc = arg;
} else if (currentArg == ("--deep-work-dir")) {
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
// PR: https://github.com/dotnet/runtime/pull/69441
// NOP
} else {
break;
}
Expand Down Expand Up @@ -355,7 +362,7 @@ Promise.all([argsPromise, loadDotnetPromise]).then(async ([_, createDotnetRuntim
return createDotnetRuntime(({ MONO, INTERNAL, BINDING, IMPORTS, EXPORTS, Module }) => ({
disableDotnet6Compatibility: true,
config: null,
configSrc: "./mono-config.json",
configSrc: runArgs.configSrc || "./mono-config.json",
onConfigLoaded: (config) => {
if (!Module.config) {
const err = new Error("Could not find ./mono-config.json. Cancelling run");
Expand Down
16 changes: 12 additions & 4 deletions src/tests/BuildWasmApps/Wasm.Build.Tests/BuildTestBase.cs
Expand Up @@ -151,14 +151,22 @@ public BuildTestBase(ITestOutputHelper output, SharedBuildPerTestClassFixture bu
envVars[kvp.Key] = kvp.Value;
}

bool runDeepWorkDir = false;
if (extraXHarnessMonoArgs?.Contains("--deep-work-dir") ?? false)
{
runDeepWorkDir = true;
}

string bundleDir = Path.Combine(GetBinDir(baseDir: buildDir, config: buildArgs.Config, targetFramework: targetFramework), "AppBundle");

// Use wasm-console.log to get the xharness output for non-browser cases
(string testCommand, string extraXHarnessArgs, bool useWasmConsoleOutput) = host switch
(string testCommand, string extraXHarnessArgs, bool useWasmConsoleOutput) = (host, runDeepWorkDir) switch
{
RunHost.V8 => ("wasm test", "--js-file=test-main.js --engine=V8 -v trace", true),
RunHost.NodeJS => ("wasm test", "--js-file=test-main.js --engine=NodeJS -v trace", true),
_ => ("wasm test-browser", $"-v trace -b {host} --web-server-use-cop", false)
(RunHost.V8, false) => ("wasm test", "--js-file=test-main.js --engine=V8 -v trace", true),
(RunHost.V8, true) => ("wasm test", "--js-file=AppBundle/test-main.js --engine=V8 -v trace", true),
(RunHost.NodeJS, false) => ("wasm test", "--js-file=test-main.js --engine=NodeJS -v trace", true),
(RunHost.NodeJS, true) => ("wasm test", "--js-file=AppBundle/test-main.js --engine=NodeJS -v trace", true),
_ => ("wasm test-browser", $"-v trace -b {host} --web-server-use-cop", false)
};

string testLogPath = Path.Combine(_logPath, host.ToString());
Expand Down
41 changes: 41 additions & 0 deletions src/tests/BuildWasmApps/Wasm.Build.Tests/ConfigSrcTests.cs
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO;
using Xunit;
using Xunit.Abstractions;

#nullable enable

namespace Wasm.Build.Tests
radical marked this conversation as resolved.
Show resolved Hide resolved
{
public class ConfigSrcTests : BuildTestBase
{
public ConfigSrcTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext) : base(output, buildContext)
{}

// NOTE: port number determinizes dynamically, so could not generate absolute URI
[Theory]
[BuildAndRun(aot: false, host: RunHost.V8)]
[BuildAndRun(aot: true, host: RunHost.V8)]
[BuildAndRun(aot: false, host: RunHost.NodeJS)]
[BuildAndRun(aot: true, host: RunHost.NodeJS)]
radical marked this conversation as resolved.
Show resolved Hide resolved
public void ConfigSrcAbsolutePath(BuildArgs buildArgs, RunHost host, string id)
{
buildArgs = buildArgs with { ProjectName = $"configsrcabsolute_{buildArgs.Config}_{buildArgs.AOT}" };
buildArgs = ExpandBuildArgs(buildArgs);

BuildProject(buildArgs,
id: id,
new BuildProjectOptions(
InitProject: () => File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), s_mainReturns42),
DotnetWasmFromRuntimePack: !(buildArgs.AOT || buildArgs.Config == "Release")));

string binDir = GetBinDir(baseDir: _projectDir!, config: buildArgs.Config);
string bundleDir = Path.Combine(binDir, "AppBundle");
string configSrc = Path.GetFullPath(Path.Combine(bundleDir, "mono-config.json"));

RunAndTestWasmApp(buildArgs, expectedExitCode: 42, host: host, id: id, extraXHarnessMonoArgs: $"--config-src={configSrc}");
}
}
}
@@ -0,0 +1,60 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO;
using Xunit;
using Xunit.Abstractions;

#nullable enable

namespace Wasm.Build.Tests
radical marked this conversation as resolved.
Show resolved Hide resolved
{
public class WasmRunOutOfAppBundleTests : BuildTestBase
{
public WasmRunOutOfAppBundleTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext) : base(output, buildContext)
{}

[Theory]
[BuildAndRun(aot: false, host: RunHost.V8)]
[BuildAndRun(aot: true, host: RunHost.V8)]
[BuildAndRun(aot: false, host: RunHost.Chrome)]
[BuildAndRun(aot: true, host: RunHost.Chrome)]
[BuildAndRun(aot: false, host: RunHost.NodeJS)]
[BuildAndRun(aot: true, host: RunHost.NodeJS)]
radical marked this conversation as resolved.
Show resolved Hide resolved
public void RunOutOfAppBundle(BuildArgs buildArgs, RunHost host, string id)
{
buildArgs = buildArgs with { ProjectName = $"outofappbundle_{buildArgs.Config}_{buildArgs.AOT}" };
buildArgs = ExpandBuildArgs(buildArgs);

BuildProject(buildArgs,
id: id,
new BuildProjectOptions(
InitProject: () => File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), s_mainReturns42),
DotnetWasmFromRuntimePack: !(buildArgs.AOT || buildArgs.Config == "Release"),
UseCache: false));
radical marked this conversation as resolved.
Show resolved Hide resolved

string binDir = GetBinDir(baseDir: _projectDir!, config: buildArgs.Config);
string baseBundleDir = Path.Combine(binDir, "AppBundle");
string tmpBundleDir = Path.Combine(binDir, "AppBundleTmp");
string deepBundleDir = Path.Combine(baseBundleDir, "AppBundle");

Directory.Move(baseBundleDir, tmpBundleDir);
Directory.CreateDirectory(baseBundleDir);

// Create $binDir/AppBundle/AppBundle
Directory.Move(tmpBundleDir, deepBundleDir);

if (host == RunHost.Chrome)
{
string indexHtmlPath = Path.Combine(baseBundleDir, "index.html");
if (!File.Exists(indexHtmlPath))
{
var html = @"<html><body><script type=""module"" src=""AppBundle/test-main.js""></script></body></html>";
File.WriteAllText(indexHtmlPath, html);
}
}

RunAndTestWasmApp(buildArgs, expectedExitCode: 42, host: host, id: id, extraXHarnessMonoArgs: "--deep-work-dir");
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
}
}
}