Skip to content

Commit

Permalink
Add Unit test to run WasmApp outside of AppBundle directory
Browse files Browse the repository at this point in the history
V8: Could not determine scriptDirectory (always empty string). You must explicitly specify the base path within locateFile.
Chrome: Failed to load dotnet.wasm
  • Loading branch information
yamachu committed May 21, 2022
1 parent a1c3c7a commit 97b2991
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/mono/wasm/test-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ loadDotnet("./dotnet.js").then((createDotnetRuntime) => {
return createDotnetRuntime(({ MONO, INTERNAL, BINDING, Module }) => ({
disableDotnet6Compatibility: true,
config: null,
configSrc: "./mono-config.json",
configSrc: processedArguments.run_deep_working_dir ? "./AppBundle/mono-config.json" : "./mono-config.json",
onConfigLoaded: (config) => {
if (!Module.config) {
const err = new Error("Could not find ./mono-config.json. Cancelling run");
Expand Down Expand Up @@ -304,6 +304,7 @@ function processArguments(incomingArguments) {
let enable_gc = true;
let diagnostic_tracing = false;
let working_dir = '/';
let run_deep_working_dir = false;
while (incomingArguments && incomingArguments.length > 0) {
const currentArg = incomingArguments[0];
if (currentArg.startsWith("--profile=")) {
Expand Down Expand Up @@ -343,6 +344,9 @@ function processArguments(incomingArguments) {
} else {
console.warn("--fetch-random-delay only works on browser")
}
} else if (currentArg.startsWith("--run-deep-work-dir=")) {
const arg = currentArg.substring("--run-deep-work-dir=".length);
run_deep_working_dir = arg === 'true';
} else {
break;
}
Expand All @@ -363,6 +367,7 @@ function processArguments(incomingArguments) {
enable_gc,
diagnostic_tracing,
working_dir,
run_deep_working_dir,
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/tests/BuildWasmApps/Wasm.Build.Tests/BuildTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,20 @@ public BuildTestBase(ITestOutputHelper output, SharedBuildPerTestClassFixture bu
envVars[kvp.Key] = kvp.Value;
}

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

string bundleDir = Path.Combine(GetBinDir(baseDir: buildDir, config: buildArgs.Config, targetFramework: targetFramework), "AppBundle");
(string testCommand, string extraXHarnessArgs) = host switch
(string testCommand, string extraXHarnessArgs) = (host, runDeepWorkDir) switch
{
RunHost.V8 => ("wasm test", "--js-file=test-main.js --engine=V8 -v trace"),
RunHost.NodeJS => ("wasm test", "--js-file=test-main.js --engine=NodeJS -v trace"),
_ => ("wasm test-browser", $"-v trace -b {host}")
(RunHost.V8, false) => ("wasm test", "--js-file=test-main.js --engine=V8 -v trace"),
(RunHost.V8, true) => ("wasm test", "--js-file=AppBundle/test-main.js --engine=V8 -v trace"),
(RunHost.NodeJS, false) => ("wasm test", "--js-file=test-main.js --engine=NodeJS -v trace"),
(RunHost.NodeJS, true) => ("wasm test", "--js-file=AppBundle/test-main.js --engine=NodeJS -v trace"),
_ => ("wasm test-browser", $"-v trace -b {host}")
};

string testLogPath = Path.Combine(_logPath, host.ToString());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.IO;
using Xunit;
using Xunit.Abstractions;

#nullable enable

namespace Wasm.Build.Tests
{
public class WasmRunOutOfAppBundleTests : BuildTestBase
{
public WasmRunOutOfAppBundleTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext) : base(output, buildContext)
{}

public static IEnumerable<object?[]> MainMethodTestData(bool aot, RunHost host)
=> ConfigWithAOTData(aot)
.WithRunHosts(host)
.UnwrapItemsAsArrays();

[Theory]
// NOTE: V8 and Chrome now fail
// Ref: https://github.com/dotnet/runtime/issues/69259
[MemberData(nameof(MainMethodTestData), parameters: new object[] { /*aot*/ true, RunHost.NodeJS })]
[MemberData(nameof(MainMethodTestData), parameters: new object[] { /*aot*/ false, RunHost.NodeJS })]
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")));

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=""text/javascript"" src=""AppBundle/test-main.js""></script></body></html>";
File.WriteAllText(indexHtmlPath, html);
}
}

RunAndTestWasmApp(buildArgs, expectedExitCode: 42, host: host, id: id, extraXHarnessMonoArgs: "--run-deep-work-dir=true");
}
}
}

0 comments on commit 97b2991

Please sign in to comment.