// 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; // A project file describes one configuration, returned by value from // CrafterBuildProject, or several that need not have anything to do with // each other, returned as pointers from CrafterBuildConfigurations, and // crafter-build's verbs act on every one of them. // // Before the second entry point a project had one configuration, and // whatever else it wanted built had to be made a dependency of that one. // What this guards against is one of several going missing without a word: // left out because the file returned it twice by mistake, or because a verb // only looks at the first, so that its tests never run and its scripts // cannot be reached; a verb that needs one of them picking one by guessing; // and a test that two of them declare being built for both at once, in the // one directory a test of that name has. And the form with one // configuration keeps what it relied on all along: a pointer the file took // to the Configuration it returns, which a test that links the project's // library holds, still points at it once the file is loaded. namespace { std::int32_t Failures = 0; void Check(bool cond, std::string_view msg) { if (!cond) { std::println(std::cerr, "FAIL: {}", msg); ++Failures; } } std::string Read(const fs::path& file) { std::ifstream in(file, std::ios::binary); return std::string((std::istreambuf_iterator(in)), std::istreambuf_iterator()); } // What `call` throws, or "" when it returns. std::string Thrown(const std::function& call) { try { call(); } catch (const std::exception& e) { return e.what(); } return ""; } // Run() as the command line reaches it, and what it wrote to stderr. struct Ran { std::int32_t status = 0; std::string err; }; Ran RunWith(std::vector args) { std::vector argv; for (std::string& arg : args) argv.push_back(arg.data()); std::ostringstream err; std::streambuf* const saved = std::cerr.rdbuf(err.rdbuf()); const std::int32_t status = Run(static_cast(argv.size()), argv.data()); std::cerr.rdbuf(saved); return { status, err.str() }; } std::vector Names(std::span configurations) { std::vector names; for (const Configuration* cfg : configurations) names.push_back(cfg->name); return names; } } int main() { const fs::path installedHome = GetCrafterBuildHome(); if (!fs::exists(installedHome / "Crafter.Build.cppm")) { std::println(std::cerr, "SKIP: no module sources at {}", installedHome.string()); return 77; } // Staged because every load drops a build/ tree next to its project // file. const fs::path home = fs::current_path(); const fs::path scratch = fs::temp_directory_path() / "crafter-build-project-configurations"; std::error_code ec; fs::remove_all(scratch, ec); fs::copy(home / "tests" / "ProjectConfigurations" / "fixture", scratch, fs::copy_options::recursive); std::array noArgs = {}; std::array noTool = { "--no-tool" }; // The loader: what each entry point comes back as. From the project's // own directory, as crafter-build runs it: the fixtures name their // sources relative to it. fs::current_path(scratch / "several"); std::vector several; try { several = LoadConfigurations("project.cpp", noArgs); } catch (const std::exception& e) { std::println(std::cerr, "FAIL: loading a project of several configurations threw: {}", e.what()); return 1; } Check(Names(several) == std::vector{"greeter", "hello", "tool"}, std::format("CrafterBuildConfigurations comes back as the file returned it (got {})", Names(several))); // Every case below reads the three of them. if (several.size() != 3) return 1; Check(several[1]->dependencies == std::vector{several[0]}, "the pointers the file took between its configurations are the ones it returned"); Check(Names(LoadConfigurations("project.cpp", noTool)) == std::vector{"greeter", "hello"}, "the file decides what it returns from its args"); Check(Thrown([&] { (void)LoadProject("project.cpp", noArgs); }).contains("LoadConfigurations"), "LoadProject refuses a file of several configurations and names the function that loads it"); fs::current_path(scratch / "single"); { const std::vector single = LoadConfigurations("project.cpp", noArgs); Check(Names(single) == std::vector{"single"}, "CrafterBuildProject comes back as a list of one"); // The file handed its test `&cfg`. Moved after the call returned // it, the Configuration would leave that pointer behind. Check(single.size() == 1 && single[0]->tests.size() == 1 && single[0]->tests[0].config.dependencies == std::vector{single[0]}, "a pointer the file took to the Configuration it returns points at the one that is loaded"); // And the test builds through that pointer. RunTestsOptions opts; opts.targetFilter = HostTarget(); const TestSummary summary = RunTests(single, opts); Check(summary.passed == 1 && summary.AllPassed(), std::format("a test that links the project's own library through that pointer builds and passes ({} passed, {} failed, {} crashed)", summary.passed, summary.failed, summary.crashed)); Check(LoadProject("project.cpp", noArgs).name == "single", "LoadProject still returns a CrafterBuildProject's Configuration by value"); } // A file the loader cannot make sense of says so when it is loaded. By // now this process holds project files with each entry point, so these // also show that one file's entry point is never taken for another's. for (const auto& [project, expected] : std::initializer_list>{ {"both", "defines both CrafterBuildProject and CrafterBuildConfigurations"}, {"neither", "defines neither CrafterBuildProject nor CrafterBuildConfigurations"}, {"empty", "returned no configuration"}, {"null", "returned a null pointer"}, {"twice", "returned same twice"}, }) { const std::string thrown = Thrown([&] { (void)LoadConfigurations(scratch / project / "project.cpp", noArgs); }); Check(thrown.contains(expected), std::format("{}/project.cpp is refused, saying '{}' (got '{}')", project, expected, thrown)); } // The verbs, over the project crafter-build is pointed at. A build // builds every one of them. fs::current_path(scratch / "several"); Check(RunWith({"crafter-build", "-q"}).status == 0, "a build of the project succeeds"); for (const Configuration* cfg : several) { Check(fs::exists(ExpectedOutput(*cfg)), std::format("the build built {}", cfg->name)); } { const Ran run = RunWith({"crafter-build", "-q", "-r"}); Check(run.status == 1 && run.err.contains("hello, tool are executables"), std::format("-r runs one program and says which two it will not pick between (exit {}): {}", run.status, run.err)); } { // -r leaves this process in the program's directory. const fs::path ran = fs::absolute(several[1]->ProgramDir()) / "hello.ran"; fs::remove(ran, ec); const Ran run = RunWith({"crafter-build", "-q", "--no-tool", "-r"}); fs::current_path(scratch / "several"); Check(run.status == 0 && Read(ran) == "hello world", std::format("with one executable among the configurations, -r runs it (exit {}): {}", run.status, run.err)); } { // The library declares the tool's test as well: one tests/ToolTest // and one directory to build it in, for two configurations. Built // for both at once, that directory would end up holding either. std::array twinTest = { "--twin-test" }; const std::vector twins = LoadConfigurations("project.cpp", twinTest); RunTestsOptions opts; opts.targetFilter = HostTarget(); const TestSummary summary = RunTests(twins, opts); std::int32_t refused = 0; for (const TestResult& result : summary.results) { if (result.name == "ToolTest" && result.outcome == TestOutcome::Fail && result.output.contains("greeter, tool each declare a test named ToolTest")) ++refused; } Check(refused == 2 && summary.failed == 2 && summary.passed == 1, std::format("a test two configurations declare fails for both, naming them, and the library's own test still passes ({} refused, {} failed, {} passed)", refused, summary.failed, summary.passed)); Check(twins.size() == 3 && twins[2]->tests.size() == 1 && !fs::exists(ExpectedOutput(twins[2]->tests[0].config)), "and it is built for neither"); } { RunTestsOptions opts; opts.targetFilter = HostTarget(); const TestSummary summary = RunTests(several, opts); std::vector ran; for (const TestResult& result : summary.results) ran.push_back(result.name); std::ranges::sort(ran); Check(ran == std::vector{"GreeterTest", "ToolTest"} && summary.passed == 2, std::format("the tests of every configuration run in one run (ran {}, {} passed)", ran, summary.passed)); // Named twice in the list, a configuration's tests would each be // built twice at once. std::array same = { several[0], several[0] }; const TestSummary once = RunTests(same, opts); Check(once.results.size() == 1 && once.passed == 1, std::format("a list that names one configuration twice runs its tests once ({} results, {} passed)", once.results.size(), once.passed)); } { // Two returned configurations of one directory are refused by a // test run as by a build, though a test run never builds the // configurations themselves: a test built on the one would be // handed the other's build. // Refused from the loaded project alone, so before anything is // built or written: with a template beside the project file, a // build or a test run that gets as far as building writes AGENTS.md. const fs::path templateFile = scratch / "several" / "AGENTS.template.md"; std::ofstream(templateFile) << "# several\n"; const Ran test = RunWith({"crafter-build", "-q", "--twin", "test"}); Check(test.status == 1 && test.err.contains("two configurations named tool would both be built into"), std::format("a test run refuses two configurations that would be built into one directory (exit {}): {}", test.status, test.err)); const Ran build = RunWith({"crafter-build", "-q", "--twin"}); Check(build.status == 1 && build.err.contains("two configurations named tool would both be built into"), std::format("and so does a build (exit {}): {}", build.status, build.err)); Check(!fs::exists(scratch / "several" / "AGENTS.md"), "and neither has written AGENTS.md"); Check(RunWith({"crafter-build", "-q"}).status == 0 && fs::exists(scratch / "several" / "AGENTS.md"), "which a build that is not refused writes"); fs::remove(templateFile, ec); fs::remove(scratch / "several" / "AGENTS.md", ec); } Check(RunWith({"crafter-build", "-q", "run", "stamp"}).status == 0 && Read(scratch / "several" / "stamp.ran") == "tool", "a script of the third configuration runs, handed the configuration that declares it"); Check(RunWith({"crafter-build", "-q", "run", "greet"}).status == 0 && Read(scratch / "several" / "greet.ran") == "greeter", "and so does one of the first"); // install and bundle export one app: the configuration packaged as a // Flatpak. The fixture's packaged programs are wasm builds, which no // build packages, so the one case that gets past the choice ends there // or, where the host's flatpak is out of reach, before it. { const Ran none = RunWith({"crafter-build", "-q", "install"}); Check(none.status == 1 && none.err.contains("none of greeter, hello, tool is packaged as a Flatpak"), std::format("install refuses a project none of whose configurations is packaged (exit {}): {}", none.status, none.err)); const Ran two = RunWith({"crafter-build", "-q", "--packaged", "bundle"}); Check(two.status == 1 && two.err.contains("hello, tool are each packaged as a Flatpak"), std::format("bundle refuses to pick between two packaged configurations (exit {}): {}", two.status, two.err)); const Ran one = RunWith({"crafter-build", "-q", "--packaged", "--no-tool", "bundle"}); Check(one.status == 1 && !one.err.contains("none of") && !one.err.contains("are each packaged") && !one.err.contains("greeter is not packaged"), std::format("with one packaged configuration among them, bundle takes that one (exit {}): {}", one.status, one.err)); } fs::current_path(home); if (Failures > 0) { std::println(std::cerr, "{} assertions failed", Failures); return 1; } fs::remove_all(scratch, ec); return 0; }