// SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® export module Crafter.Build:Test; import std; import :Clang; export namespace Crafter { // One row in a SIMD-march fan-out: a single Test variant compiles the // shared source with `-march= -mtune=`. Passed by span to // Configuration::AddMarchVariants — see Crafter.Math/project.cpp. struct MarchTier { std::string march; std::string mtune; }; // Fluent helper returned by Configuration::AddTest. Holds a back-pointer // to the parent Configuration and the index of the just-pushed Test, so // mutations survive any vector reallocation that subsequent AddTest // calls might trigger. Each setter returns `*this` to support chaining. // // Note: source files (interfaces + implementations) are fixed at // AddTest-time — pass them via the AddTest overload that takes // interfaces. The builder only mutates compile state, deps, runtime // args, and requires; it deliberately does NOT expose Sources() to // avoid the double-`GetInterfacesAndImplementations` footgun (calling // it twice would list the same .cppm files twice, and the build would // make a Module entry of each listing). struct TestBuilder { Configuration* parent; std::size_t index; Test& Ref() const { return parent->tests[index]; } // Override the path the test's sources resolve against. Defaults to // "./" (project root, where tests//main.cpp lives). Override // only when the test source layout sits under a subdir. TestBuilder& Path(std::filesystem::path p); TestBuilder& Target(std::string t); TestBuilder& March(std::string m); TestBuilder& Mtune(std::string m); TestBuilder& Sysroot(std::filesystem::path s); TestBuilder& Debug(bool d = true); TestBuilder& Define(std::string name, std::string value = ""); TestBuilder& Timeout(std::chrono::seconds s); // Replaces any previously-set args. TestBuilder& Args(std::vector a); // Appends one require entry ("tool:foo" / "file:..." / "env:VAR"). TestBuilder& Requires(std::string r); // Replaces the dependency list. Pointers must outlive the build. TestBuilder& Dependencies(std::vector deps); // Append a single linker flag (-l..., -L..., etc.). TestBuilder& LinkFlag(std::string f); // Append a single compile flag. TestBuilder& CompileFlag(std::string f); }; struct RunTestsOptions { std::vector globs; // Tests built and run at once, from --jobs=; 0 is one per hardware // thread. It counts tests, not build steps: the steps of their builds // run on JobPool::Process() under that pool's own limit, and a test // that is running is not one of them. std::int32_t jobs = 0; std::optional timeoutOverride; bool listOnly = false; // Single-target run: only tests whose Configuration::target matches // are included. Empty (default) = run every distinct target declared // across discovered tests, plus the host target. Set from --target=... // (when omitted, the harness sweeps all declared targets so cross-arch // tests run by default without the user having to know which targets // exist). std::string targetFilter; // CLI override for --runner=: applies to every test in the run. // Target scoping is unnecessary because targetFilter ensures the run // contains only one target's tests. std::optional runnerOverride; }; struct TestSummary { std::int32_t passed = 0; std::int32_t failed = 0; std::int32_t crashed = 0; std::int32_t timedOut = 0; std::int32_t skipped = 0; std::vector results; bool AllPassed() const { return failed == 0 && crashed == 0 && timedOut == 0; } }; TestSummary RunTests(Configuration& projectCfg, const RunTestsOptions& opts, std::span projectArgs = {}); // The tests of several configurations, those a project file returns, as // one run: one sweep over the targets they declare, one summary, and // one build pass per target, so a library that tests of two of them link // is built once. The log a failed, crashed or timed-out test leaves // goes under the path of the configuration that declares it. A test // belongs to one configuration: where two of them each declare one that // would be built into the same directory, which two tests of one name // compiled alike are, both fail without being built and name the // configurations. A configuration the list names twice counts once. TestSummary RunTests(std::span configurations, const RunTestsOptions& opts, std::span projectArgs = {}); TestResult RunSingleTest(const Test& test, const std::filesystem::path& binary, std::chrono::seconds timeout); // The configurations of the project crafter-build was pointed at, by // name. Run() hands SetParentProject what the project file returned, // once it has loaded. ParentLib() then finds a Configuration* by // Configuration::name among them and what they depend on, searching the // dependency graph of each in the order the file returned them, and // throws when none has that name or no project is set. void SetParentProject(Configuration* parent); void SetParentProject(std::span parents); Configuration* ParentLib(std::string_view name); } namespace Crafter { // A test is never packaged, whatever its Configuration asks for: the // runner starts the binary itself. Sets every test of `cfg` so, for one // from AddTest or AddMarchVariants and one a project.cpp put into // cfg.tests itself alike. Every reader of cfg.tests that builds a test // or works out its compile command, RunTests and RunLint, calls it // first, so that they agree on the test's VariantId, and so on where its // module PCMs are. void UnpackageTests(Configuration& cfg); }