diff --git a/project.cpp b/project.cpp index 0f10375..a1b397d 100644 --- a/project.cpp +++ b/project.cpp @@ -273,6 +273,7 @@ extern "C" Configuration CrafterBuildProject(std::span a cfg.AddTest("WasiBrowserRuntimeExit").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasmVariants").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasmHelloRuns").Dependencies({ CrafterBuildLib.get() }); + cfg.AddTest("WasmTestRuns").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasmExceptions").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasiSysrootWithoutExceptions").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasmBuildRefusals").Dependencies({ CrafterBuildLib.get() }); diff --git a/tests/WasmTestRuns/fixture/tests/hello/main.cpp b/tests/WasmTestRuns/fixture/tests/hello/main.cpp new file mode 100644 index 0000000..05f0712 --- /dev/null +++ b/tests/WasmTestRuns/fixture/tests/hello/main.cpp @@ -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; +} diff --git a/tests/WasmTestRuns/main.cpp b/tests/WasmTestRuns/main.cpp new file mode 100644 index 0000000..1ee7254 --- /dev/null +++ b/tests/WasmTestRuns/main.cpp @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +#include +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// while the link had written +// .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//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; +}