test: a wasm test declared with AddTest runs under wasmtime

WasmTestRuns declares a wasm32-wasip1 test on a fixture project and runs
it through RunTests, which builds it and hands it to the wasmtime runner
its target derives. The test has to pass and print its own output.
Before the fix every such test failed with "failed to open wasm module",
because RunTests left the .wasm off. It skips where the WASI sysroot
with exceptions or wasmtime is missing, as WasmHelloRuns does, so it
runs in the Flatpak and skips on a development box without them.
This commit is contained in:
Jorijn van der Graaf 2026-11-01 18:40:38 +01:00
commit 3044ac34a1
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
3 changed files with 87 additions and 0 deletions

View file

@ -273,6 +273,7 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
cfg.AddTest("WasiBrowserRuntimeExit").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasiBrowserRuntimeExit").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("WasmVariants").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasmVariants").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("WasmHelloRuns").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasmHelloRuns").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("WasmTestRuns").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("WasmExceptions").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasmExceptions").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("WasiSysrootWithoutExceptions").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasiSysrootWithoutExceptions").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("WasmBuildRefusals").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasmBuildRefusals").Dependencies({ CrafterBuildLib.get() });

View file

@ -0,0 +1,9 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
int main() {
std::println("hello from a wasm test");
return 0;
}

View file

@ -0,0 +1,77 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
#include <stdlib.h>
import std;
import Crafter.Build;
namespace fs = std::filesystem;
using namespace Crafter;
namespace {
std::int32_t Failures = 0;
void Check(bool cond, std::string_view msg) {
if (!cond) {
std::println(std::cerr, "FAIL: {}", msg);
++Failures;
}
}
}
// A wasm32 test declared with AddTest is built, handed to wasmtime by the
// runner its target derives, and passes. RunTests used to work the binary's
// path out on its own, adding .exe for Windows triples and nothing for wasm,
// so it gave wasmtime bin/<variant>/<name> while the link had written
// <name>.wasm beside it: every wasm AddTest failed with "failed to open wasm
// module", inside the Flatpak as well, where wasmtime is bundled. Skips where
// the sysroot or wasmtime is missing, as WasmHelloRuns does.
int main() {
// The test changes into its fixture below, after the toolchain is read:
// a relative CRAFTER_BUILD_WASI_SYSROOT, which names a directory from
// where the suite runs, is made absolute first.
if (const std::string sysroot = std::getenv("CRAFTER_BUILD_WASI_SYSROOT") ? std::getenv("CRAFTER_BUILD_WASI_SYSROOT") : ""; !sysroot.empty()) {
setenv("CRAFTER_BUILD_WASI_SYSROOT", fs::absolute(sysroot).string().c_str(), 1);
}
const Toolchain& tc = GetToolchain();
const fs::path libDir = tc.wasiSysroot / "lib" / "wasm32-wasip1";
if (!fs::exists(libDir / "eh" / "libunwind.a") && !fs::exists(libDir / "libunwind.a")) {
std::println(std::cerr, "SKIP: no exception-enabled WASI sysroot at {}", tc.wasiSysroot.string());
return 77;
}
if (std::system("which wasmtime > /dev/null 2>&1") != 0) {
std::println(std::cerr, "SKIP: wasmtime not on PATH");
return 77;
}
// AddTest resolves tests/<name>/main.cpp against the working directory,
// and the test's outputs land below it, in the fixture's own bin/ and
// build/.
fs::current_path(fs::current_path() / "tests" / "WasmTestRuns" / "fixture");
std::error_code ec;
fs::remove_all("bin", ec);
fs::remove_all("build", ec);
Configuration project;
project.path = "./";
project.name = "wasm-test-runs";
project.outputName = "wasm-test-runs";
project.target = "wasm32-wasip1";
project.AddTest("hello");
RunTestsOptions opts;
opts.jobs = 1;
const TestSummary summary = RunTests(project, opts);
Check(summary.results.size() == 1, std::format("one test ran, got {}", summary.results.size()));
if (!summary.results.empty()) {
const TestResult& result = summary.results.front();
Check(result.outcome == TestOutcome::Pass, std::format("the wasm test passes (exit {}): {}", result.exitCode, result.output));
Check(result.output.contains("hello from a wasm test"), "the output is the program's own");
}
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
return 0;
}