From 4cadf9ad741fb1f0c1b09967c62aef01a8c152ee Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Fri, 22 Jul 2022 05:06:33 +0200 Subject: [PATCH] [wasm] Return int from console template (#72623) Also update the template tests to test that it propagates correctly as an exit code from the node process. --- src/mono/wasm/templates/templates/console/Program.cs | 2 ++ src/mono/wasm/templates/templates/console/app-support.mjs | 2 +- src/mono/wasm/templates/templates/console/main.mjs | 2 +- .../BuildWasmApps/Wasm.Build.Tests/WasmTemplateTests.cs | 8 +++----- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/mono/wasm/templates/templates/console/Program.cs b/src/mono/wasm/templates/templates/console/Program.cs index edd4616471ece..39e2e566379c0 100644 --- a/src/mono/wasm/templates/templates/console/Program.cs +++ b/src/mono/wasm/templates/templates/console/Program.cs @@ -3,6 +3,8 @@ Console.WriteLine("Hello, Console!"); +return 0; + public partial class MyClass { [JSExport] diff --git a/src/mono/wasm/templates/templates/console/app-support.mjs b/src/mono/wasm/templates/templates/console/app-support.mjs index 6affa5b5448af..0645f743a54ed 100644 --- a/src/mono/wasm/templates/templates/console/app-support.mjs +++ b/src/mono/wasm/templates/templates/console/app-support.mjs @@ -166,7 +166,7 @@ try { if (runArgs.runtimeArgs.length > 0) INTERNAL.mono_wasm_set_runtime_options(runArgs.runtimeArgs); - Object.assign(App, { MONO, BINDING, IMPORTS, Module, runArgs }); + Object.assign(App, { MONO, INTERNAL, BINDING, IMPORTS, Module, runArgs }); try { if (App.main) { diff --git a/src/mono/wasm/templates/templates/console/main.mjs b/src/mono/wasm/templates/templates/console/main.mjs index cd90d5b0fc79f..7ed16057db0e5 100644 --- a/src/mono/wasm/templates/templates/console/main.mjs +++ b/src/mono/wasm/templates/templates/console/main.mjs @@ -12,5 +12,5 @@ App.main = async function (applicationArguments) { const text = exports.MyClass.Greeting(); console.log(text); - await App.MONO.mono_run_main("console.0.dll", applicationArguments); + return await App.MONO.mono_run_main("console.0.dll", applicationArguments); } diff --git a/src/tests/BuildWasmApps/Wasm.Build.Tests/WasmTemplateTests.cs b/src/tests/BuildWasmApps/Wasm.Build.Tests/WasmTemplateTests.cs index e3edd4d60cd82..3a6e40561b4a4 100644 --- a/src/tests/BuildWasmApps/Wasm.Build.Tests/WasmTemplateTests.cs +++ b/src/tests/BuildWasmApps/Wasm.Build.Tests/WasmTemplateTests.cs @@ -3,14 +3,11 @@ using System; using System.IO; -using System.Text; using System.Threading.Tasks; -using System.Text.RegularExpressions; using Xunit; using Xunit.Abstractions; using Xunit.Sdk; - #nullable enable namespace Wasm.Build.Tests @@ -32,6 +29,7 @@ private void updateProgramCS() { 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); } @@ -162,7 +160,7 @@ public void ConsoleBuildAndRun(string config) AssertDotNetJsSymbols(Path.Combine(GetBinDir(config), "AppBundle"), fromRuntimePack: true); (int exitCode, string output) = RunProcess(s_buildEnv.DotNet, _testOutput, args: $"run --no-build -c {config} x y z", workingDir: _projectDir); - Assert.Equal(0, exitCode); + Assert.Equal(42, exitCode); Assert.Contains("args[0] = x", output); Assert.Contains("args[1] = y", output); Assert.Contains("args[2] = z", output); @@ -209,7 +207,7 @@ public void ConsolePublishAndRun(string config, bool aot) var res = new RunCommand(s_buildEnv, _testOutput, label: id) .WithWorkingDirectory(_projectDir!) .ExecuteWithCapturedOutput(runArgs) - .EnsureSuccessful(); + .EnsureExitCode(42); if (aot) Assert.Contains($"AOT: image '{Path.GetFileNameWithoutExtension(projectFile)}' found", res.Output);