77 lines
3 KiB
C++
77 lines
3 KiB
C++
|
|
// 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;
|
||
|
|
}
|