// SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® 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 test is never packaged, whatever its Configuration asks for: the runner // starts the binary itself. Saying so in AddTest alone would miss a test // from AddMarchVariants, or one a project.cpp puts into cfg.tests itself: // it would follow its own packaging, Default only while that means the // executable as linked, and Packaging::Flatpak would be packaged, or, by a // native crafter-build, refused. So RunTests runs every test as linked, // and so does RunLint work out its compile command: the test's own module // PCMs are where its VariantId puts them, and the Flatpak variant, which // nothing builds, has none, so linting it with a rule that reads the AST // used to fail the run for want of them, after any number of test runs. int main() { // 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" / "FlatpakTestsRaw" / "fixture"); std::error_code ec; fs::remove_all("bin", ec); fs::remove_all("build", ec); Configuration project; project.path = "./"; project.name = "flatpak-tests-raw"; project.outputName = "flatpak-tests-raw"; project.target = HostTarget(); { std::array ifaces = { "tests/hello/Hello" }; project.AddTest("hello", ifaces); } project.tests.back().config.packaging = Packaging::Flatpak; 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 test passes (exit {}): {}", result.exitCode, result.output)); Check(result.output.contains("hello from a test"), "the output is the program's own"); } const Configuration& test = project.tests.back().config; Check(test.ResolvedPackaging() == Packaging::None, "the test is built as linked"); Check(!fs::exists(test.BinDir() / "files") && fs::is_regular_file(test.BinDir() / "hello"), "its bin directory holds the program, and no Flatpak files/"); // As `crafter-build lint` loads the project afresh: the test asks for a // Flatpak again, and main.cpp, which imports Hello, parses against the // PCMs the test run built. project.tests.back().config.packaging = Packaging::Flatpak; std::size_t parsed = 0; project.AddAstLintRule("parses", [&parsed](LintContext&) { ++parsed; }); const LintSummary lint = RunLint(project, RunLintOptions{}); Check(lint.errors == 0 && parsed == 2, std::format("lint parses the test's two files against those PCMs ({} errors, {} parsed)", lint.errors, parsed)); if (Failures > 0) { std::println(std::cerr, "{} assertions failed", Failures); return 1; } return 0; }