From 5795b666f5a5d607b36deefc06dd703b85a5a053 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Sun, 15 Nov 2026 02:16:07 +0100 Subject: [PATCH] test: several configurations from one project file BuildAll builds configurations the test makes itself, on a fresh copy of its fixture outside the checkout. A library, a program built on it and an unrelated program all come out built, each in the record of the pass under its PcmDir, and a list that names one configuration twice builds it from nothing, what the build before left of it being removed first, and leaves it in the record of the pass once. A library built on another library of its own name, path and type, so that the two are built into one directory, builds, with both archives in that directory; returned beside a program that is built on it, the pair is refused, both by name, before the program is built or anything is recorded. Two libraries that are each built on a configuration of the other's directory are refused by name, and nothing of them is built or recorded. A wasm32 library that names no sysroot and a program for the host built on it leave the library in the record once, under the directory it has after the pass, and its refusal comes back once; it is refused for its -fno-exceptions, so the case needs no WASI sysroot. Of two programs built on a library that does not compile and a third that has nothing to do with it, the third is built, the two are not, and the library's error comes back once. Two configurations that would be built into one directory are refused, and neither is built. With a wasm executable that declares a variant among them, a program of one source is linked no earlier than the program and library given before it; the wasm one is refused for its -fno-exceptions before it builds anything, so the case needs no WASI sysroot. ProjectConfigurations loads fixture project files staged outside the checkout. A file with CrafterBuildConfigurations comes back as the three configurations it returns, with the pointer the file took from the second to the first intact, and as two when its args say so; LoadProject refuses it. A file with CrafterBuildProject comes back as a list of one whose test still depends on that Configuration, and that test builds and passes. A file with both entry points, one with neither, and ones whose CrafterBuildConfigurations returns nothing, a null pointer or one configuration twice are refused, each with a message that says which. Then crafter-build's verbs, through Run() and RunTests, on the file of three: a build builds all three; -r refuses to pick between its two executables, and runs the one executable there is when the file returns two configurations; RunTests runs the tests of the first and the third configuration in one run, and when the first declares the third's test as well, that test fails for both, naming the two, and is not built, while the first's own test passes, and a list that names the first configuration twice runs its test once; with a fourth configuration of the third's name and path, a test run and a build are each refused for the two that would be built into one directory, and neither writes the AGENTS.md that a build which is not refused writes from the template the test puts beside the project file; `run` reaches a script of the first and of the third configuration and hands each the configuration that declares it; install refuses the project while none of the three is packaged, bundle refuses it when two are, and with one packaged executable beside the library bundle does not take the library for the app. SiblingConfigurations loads such a file as another project's sibling. A spec without `configuration` gets the first, a spec that names one gets that one, and the three specs share one load: the dependency of the second is the first as LocalProject returned it. All three are rebased onto the sibling's directory. A name the sibling does not have throws with the names it has, and a name that two configurations of another fixture share throws too, while a spec without a name still gets the first of those two. Building the second builds the first with it and leaves the third unbuilt. A file that returns something a sibling keeps to itself is refused when it is loaded, the configuration and the sibling named: the library the sibling's program is built on, the configuration of that program's test, and a helper only that test is built on, none of which the sibling's file returns. A project that returns the sibling's third configuration beside one of its own builds both, cannot run the sibling's script and runs its own, and its test run leaves the sibling's test alone. DependencyNotes gains a project of two configurations and a fixture, outer, that builds against lib: two Direct configurations give what each of them names; a project a Direct one names is followed when a Transitive one leads to it; a configuration under None keeps out what only it is built against; with both under None, AGENTS.md is the template's text; and a returned configuration of another project is one of the projects the notes come from, followed further only under Transitive, which is also what a file that made no configuration gets. For outer's own AGENTS.md, written from the Configuration LocalProject returned and outer's project file, outer's own setting selects: lib under Direct, lib and base under Transitive, nothing under None. Script runs a script of each of two configurations, each handed the configuration that declares it, refuses a name both declare without running anything, and, given a list that names one configuration twice, runs the script asked for, which a name declared twice would have kept it from. Lint formats the sources of two configurations in one run, counts a rule both register as one, and runs a rule only one of them registers over the sources of both. Of two returned configurations that register a rule under one name the earlier one's runs, over the sources of all three, though the configuration returned ahead of both depends on the later one. --- project.cpp | 15 +- tests/BuildAll/fixture/broken/Broken.cppm | 10 + tests/BuildAll/fixture/first/main.cpp | 9 + tests/BuildAll/fixture/good/main.cpp | 9 + tests/BuildAll/fixture/greeter/Greeter.cppm | 11 + tests/BuildAll/fixture/hello/main.cpp | 10 + tests/BuildAll/fixture/second/main.cpp | 9 + tests/BuildAll/fixture/tool/main.cpp | 9 + tests/BuildAll/fixture/twins/Lower.cppm | 11 + tests/BuildAll/fixture/twins/Upper.cppm | 10 + tests/BuildAll/main.cpp | 273 ++++++++++++++++++ .../fixture/outer/AGENTS.consumer.md | 3 + .../DependencyNotes/fixture/outer/project.cpp | 21 ++ tests/DependencyNotes/main.cpp | 80 +++++ tests/Lint/main.cpp | 46 +++ .../fixture/both/project.cpp | 16 + .../fixture/empty/project.cpp | 11 + .../fixture/neither/project.cpp | 11 + .../fixture/null/project.cpp | 11 + .../fixture/several/greeter/Greeter.cppm | 11 + .../fixture/several/hello/main.cpp | 12 + .../fixture/several/project.cpp | 94 ++++++ .../several/tests/GreeterTest/main.cpp | 9 + .../fixture/several/tests/ToolTest/main.cpp | 11 + .../fixture/several/tool/main.cpp | 9 + .../fixture/single/Single.cppm | 11 + .../fixture/single/project.cpp | 22 ++ .../fixture/single/tests/SelfTest/main.cpp | 9 + .../fixture/twice/project.cpp | 17 ++ tests/ProjectConfigurations/main.cpp | 228 +++++++++++++++ tests/Script/main.cpp | 32 ++ .../fixture/digger/project.cpp | 20 ++ .../fixture/keeper/project.cpp | 39 +++ .../fixture/sibling/greeter/Greeter.cppm | 11 + .../fixture/sibling/hello/main.cpp | 10 + .../fixture/sibling/project.cpp | 60 ++++ .../fixture/sibling/tests/ToolTest/main.cpp | 11 + .../fixture/sibling/tool/main.cpp | 9 + .../fixture/twins/project.cpp | 24 ++ .../fixture/umbrella/main.cpp | 9 + .../fixture/umbrella/project.cpp | 34 +++ tests/SiblingConfigurations/main.cpp | 165 +++++++++++ 42 files changed, 1427 insertions(+), 5 deletions(-) create mode 100644 tests/BuildAll/fixture/broken/Broken.cppm create mode 100644 tests/BuildAll/fixture/first/main.cpp create mode 100644 tests/BuildAll/fixture/good/main.cpp create mode 100644 tests/BuildAll/fixture/greeter/Greeter.cppm create mode 100644 tests/BuildAll/fixture/hello/main.cpp create mode 100644 tests/BuildAll/fixture/second/main.cpp create mode 100644 tests/BuildAll/fixture/tool/main.cpp create mode 100644 tests/BuildAll/fixture/twins/Lower.cppm create mode 100644 tests/BuildAll/fixture/twins/Upper.cppm create mode 100644 tests/BuildAll/main.cpp create mode 100644 tests/DependencyNotes/fixture/outer/AGENTS.consumer.md create mode 100644 tests/DependencyNotes/fixture/outer/project.cpp create mode 100644 tests/ProjectConfigurations/fixture/both/project.cpp create mode 100644 tests/ProjectConfigurations/fixture/empty/project.cpp create mode 100644 tests/ProjectConfigurations/fixture/neither/project.cpp create mode 100644 tests/ProjectConfigurations/fixture/null/project.cpp create mode 100644 tests/ProjectConfigurations/fixture/several/greeter/Greeter.cppm create mode 100644 tests/ProjectConfigurations/fixture/several/hello/main.cpp create mode 100644 tests/ProjectConfigurations/fixture/several/project.cpp create mode 100644 tests/ProjectConfigurations/fixture/several/tests/GreeterTest/main.cpp create mode 100644 tests/ProjectConfigurations/fixture/several/tests/ToolTest/main.cpp create mode 100644 tests/ProjectConfigurations/fixture/several/tool/main.cpp create mode 100644 tests/ProjectConfigurations/fixture/single/Single.cppm create mode 100644 tests/ProjectConfigurations/fixture/single/project.cpp create mode 100644 tests/ProjectConfigurations/fixture/single/tests/SelfTest/main.cpp create mode 100644 tests/ProjectConfigurations/fixture/twice/project.cpp create mode 100644 tests/ProjectConfigurations/main.cpp create mode 100644 tests/SiblingConfigurations/fixture/digger/project.cpp create mode 100644 tests/SiblingConfigurations/fixture/keeper/project.cpp create mode 100644 tests/SiblingConfigurations/fixture/sibling/greeter/Greeter.cppm create mode 100644 tests/SiblingConfigurations/fixture/sibling/hello/main.cpp create mode 100644 tests/SiblingConfigurations/fixture/sibling/project.cpp create mode 100644 tests/SiblingConfigurations/fixture/sibling/tests/ToolTest/main.cpp create mode 100644 tests/SiblingConfigurations/fixture/sibling/tool/main.cpp create mode 100644 tests/SiblingConfigurations/fixture/twins/project.cpp create mode 100644 tests/SiblingConfigurations/fixture/umbrella/main.cpp create mode 100644 tests/SiblingConfigurations/fixture/umbrella/project.cpp create mode 100644 tests/SiblingConfigurations/main.cpp diff --git a/project.cpp b/project.cpp index 1452eb6..adaaddf 100644 --- a/project.cpp +++ b/project.cpp @@ -310,13 +310,13 @@ extern "C" Configuration CrafterBuildProject(std::span a cfg.AddTest("WasmBuildRefusals").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("WasmSysrootKey").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("RunSingleTestExit").Dependencies({ CrafterBuildLib.get() }); - // LoadProject dlopens the synthesized project.so, which references - // Crafter:: symbols (HostTarget, Configuration ctors) that have to be - // visible from the test exe — same wiring crafter-build itself uses - // for project.so. + // Loading a project file dlopens the project.so compiled from it, + // which references Crafter:: symbols (HostTarget, Configuration + // ctors) that have to be visible from the test exe — same wiring + // crafter-build itself uses for project.so. cfg.AddTest("ConcurrentCacheRace").Dependencies({ CrafterBuildLib.get() }) .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); - // Same LoadProject wiring as ConcurrentCacheRace above. + // Same wiring as ConcurrentCacheRace above. cfg.AddTest("HostCacheSourceStamp").Dependencies({ CrafterBuildLib.get() }) .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); cfg.AddTest("HostCacheToolchainKey").Dependencies({ CrafterBuildLib.get() }) @@ -337,10 +337,15 @@ extern "C" Configuration CrafterBuildProject(std::span a .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); cfg.AddTest("SiblingOwnPointers").Dependencies({ CrafterBuildLib.get() }) .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); + cfg.AddTest("ProjectConfigurations").Dependencies({ CrafterBuildLib.get() }) + .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); + cfg.AddTest("SiblingConfigurations").Dependencies({ CrafterBuildLib.get() }) + .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); cfg.AddTest("ConcurrentDependencyReset").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("JobGroup").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("JobLimit").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("ImportCycle").Dependencies({ CrafterBuildLib.get() }); + cfg.AddTest("BuildAll").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("Lint").Dependencies({ CrafterBuildLib.get() }); } diff --git a/tests/BuildAll/fixture/broken/Broken.cppm b/tests/BuildAll/fixture/broken/Broken.cppm new file mode 100644 index 0000000..45ade4e --- /dev/null +++ b/tests/BuildAll/fixture/broken/Broken.cppm @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module Broken; +import std; + +// Does not compile: the library two of the test's programs are built on. +export std::int32_t Value() { + return Undeclared; +} diff --git a/tests/BuildAll/fixture/first/main.cpp b/tests/BuildAll/fixture/first/main.cpp new file mode 100644 index 0000000..58a7a9c --- /dev/null +++ b/tests/BuildAll/fixture/first/main.cpp @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Broken; + +int main() { + return Value(); +} diff --git a/tests/BuildAll/fixture/good/main.cpp b/tests/BuildAll/fixture/good/main.cpp new file mode 100644 index 0000000..5736637 --- /dev/null +++ b/tests/BuildAll/fixture/good/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("good"); + return 0; +} diff --git a/tests/BuildAll/fixture/greeter/Greeter.cppm b/tests/BuildAll/fixture/greeter/Greeter.cppm new file mode 100644 index 0000000..4e0c093 --- /dev/null +++ b/tests/BuildAll/fixture/greeter/Greeter.cppm @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module Greeter; +import std; + +export namespace Greeter { + std::string Greet(std::string_view name) { + return std::format("hello {}", name); + } +} diff --git a/tests/BuildAll/fixture/hello/main.cpp b/tests/BuildAll/fixture/hello/main.cpp new file mode 100644 index 0000000..60df1dc --- /dev/null +++ b/tests/BuildAll/fixture/hello/main.cpp @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Greeter; + +int main() { + std::println("{}", Greeter::Greet("world")); + return 0; +} diff --git a/tests/BuildAll/fixture/second/main.cpp b/tests/BuildAll/fixture/second/main.cpp new file mode 100644 index 0000000..58a7a9c --- /dev/null +++ b/tests/BuildAll/fixture/second/main.cpp @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Broken; + +int main() { + return Value(); +} diff --git a/tests/BuildAll/fixture/tool/main.cpp b/tests/BuildAll/fixture/tool/main.cpp new file mode 100644 index 0000000..3d03672 --- /dev/null +++ b/tests/BuildAll/fixture/tool/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("tool"); + return 0; +} diff --git a/tests/BuildAll/fixture/twins/Lower.cppm b/tests/BuildAll/fixture/twins/Lower.cppm new file mode 100644 index 0000000..e5f6378 --- /dev/null +++ b/tests/BuildAll/fixture/twins/Lower.cppm @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module Lower; +import std; + +// Built by a configuration with the name, path and type of the one that +// builds Upper, which is built on this one. +export std::int32_t Lower() { + return 1; +} diff --git a/tests/BuildAll/fixture/twins/Upper.cppm b/tests/BuildAll/fixture/twins/Upper.cppm new file mode 100644 index 0000000..f8d1ceb --- /dev/null +++ b/tests/BuildAll/fixture/twins/Upper.cppm @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module Upper; +import std; +import Lower; + +export std::int32_t Upper() { + return Lower() + 1; +} diff --git a/tests/BuildAll/main.cpp b/tests/BuildAll/main.cpp new file mode 100644 index 0000000..85678aa --- /dev/null +++ b/tests/BuildAll/main.cpp @@ -0,0 +1,273 @@ +// 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; + +// BuildAll builds the configurations a project file returns, which need not +// have anything to do with each other, in one pass: every one of them, and +// what two of them share once. +// +// What it guards against is a configuration that goes missing or is built +// wrongly without a word: left unbuilt because another one failed, or +// because it shares an output directory with another and was taken for it; +// built twice at once because another one depends on it, each build +// writing what the other reads; and built at once with a wasm executable +// that declares variants, which rewrites the configurations it is built on +// while it builds. And against a build that never ends: a configuration +// waiting for a build of its own directory that is waiting for it. +namespace { + std::int32_t Failures = 0; + + void Check(bool cond, std::string_view msg) { + if (!cond) { + std::println(std::cerr, "FAIL: {}", msg); + ++Failures; + } + } + + std::size_t Count(std::string_view text, std::string_view needle) { + std::size_t count = 0; + for (std::size_t pos = text.find(needle); pos != std::string_view::npos; pos = text.find(needle, pos + needle.size())) ++count; + return count; + } + + // A library made of `dir`/`interface`.cppm. + void MakeLibrary(Configuration& cfg, const fs::path& dir, std::string_view name, std::string_view interface) { + cfg.path = dir; + cfg.name = name; + cfg.outputName = name; + cfg.target = HostTarget(); + cfg.type = ConfigurationType::LibraryStatic; + std::array interfaces = { interface }; + std::array implementations = {}; + cfg.GetInterfacesAndImplementations(interfaces, implementations); + } + + // A program made of `dir`/main.cpp. + void MakeProgram(Configuration& cfg, const fs::path& dir, std::string_view name) { + cfg.path = dir; + cfg.name = name; + cfg.outputName = name; + cfg.target = HostTarget(); + cfg.type = ConfigurationType::Executable; + std::array interfaces = {}; + std::array implementations = { "main" }; + cfg.GetInterfacesAndImplementations(interfaces, implementations); + } +} + +int main() { + // Staged: the cases below compare what a build left behind, so each run + // starts from sources alone. + const fs::path scratch = fs::temp_directory_path() / "crafter-build-build-all"; + std::error_code ec; + fs::remove_all(scratch, ec); + fs::copy(fs::current_path() / "tests" / "BuildAll" / "fixture", scratch, fs::copy_options::recursive); + + // Every one of them is built and left in the record of the pass, where + // a configuration that depends on it looks for it. hello depends on + // greeter, and greeter built from here as well as through hello was two + // builds of it at once, one of which archived the objects while the + // other was still writing them. + { + Configuration greeter; + MakeLibrary(greeter, scratch / "greeter", "greeter", "Greeter"); + Configuration hello; + hello.dependencies = { &greeter }; + MakeProgram(hello, scratch / "hello", "hello"); + Configuration tool; + MakeProgram(tool, scratch / "tool", "tool"); + + std::array all = { &greeter, &hello, &tool }; + std::unordered_map> depResults; + std::mutex depMutex; + const std::string failed = BuildAll(all, depResults, depMutex); + Check(failed.empty(), std::format("a library, a program built on it and an unrelated program build: {}", failed)); + for (const Configuration* cfg : all) { + Check(fs::exists(ExpectedOutput(*cfg)), std::format("{} was built", cfg->name)); + Check(depResults.contains(cfg->PcmDir()), std::format("{} is in the record of the pass, where a configuration that depends on it finds its build", cfg->name)); + } + + // A list that names one of them twice builds it like any other: + // once, from nothing here, since what the build above left of it is + // removed first. + fs::remove_all(tool.BinDir(), ec); + fs::remove_all(tool.BuildDir(), ec); + std::array twice = { &tool, &tool }; + std::unordered_map> twiceResults; + Check(BuildAll(twice, twiceResults, depMutex).empty() && fs::exists(ExpectedOutput(tool)), "a configuration the list names twice is built"); + Check(twiceResults.size() == 1 && twiceResults.contains(tool.PcmDir()), std::format("and is in the record of the pass once ({} entries)", twiceResults.size())); + } + + // A configuration built on another of its own name, path and type: the + // two are built into one directory, the dependency first, as they were + // when a project had one configuration and Build() was all there was. + // In the record of the pass while it is still being built, the first + // would be what the record hands its dependency, which would wait for + // it while it waits for the dependency: a build that never ends. + { + Configuration lower; + MakeLibrary(lower, scratch / "twins", "twins", "Lower"); + lower.outputName = "lower"; + Configuration upper; + upper.dependencies = { &lower }; + MakeLibrary(upper, scratch / "twins", "twins", "Upper"); + + std::array one = { &upper }; + std::unordered_map> depResults; + std::mutex depMutex; + const std::string failed = BuildAll(one, depResults, depMutex); + Check(failed.empty(), std::format("a configuration built on another of the same directory builds: {}", failed)); + Check(upper.PcmDir() == lower.PcmDir() && fs::exists(ExpectedOutput(upper)) && fs::exists(ExpectedOutput(lower)), "both into that one directory"); + + // With a program built on the upper one returned beside it, that + // directory holds two builds and the record of the pass one result: + // the program would be handed the lower one's, and be linked without + // the library it names. + Configuration program; + program.dependencies = { &upper }; + MakeProgram(program, scratch / "good", "on-twins"); + std::array both = { &program, &upper }; + std::unordered_map> bothResults; + const std::string refused = BuildAll(both, bothResults, depMutex); + Check(refused.contains("twins and a configuration it is built on would both be built into") && refused.contains("where on-twins looks"), std::format("a configuration built on that one, returned beside it, is refused, both by name: {}", refused)); + Check(bothResults.empty() && !fs::exists(program.BuildDir()) && !fs::exists(program.BinDir()), "and refused before anything is built"); + } + + // Two that are each built on something of the other's directory cannot + // be put in an order, and are refused before either is built. Nothing + // here has a source: the refusal comes first. + { + const auto named = [&](Configuration& cfg, std::string_view name) { + cfg.path = scratch / "crossed"; + cfg.name = name; + cfg.outputName = name; + cfg.target = HostTarget(); + cfg.type = ConfigurationType::LibraryStatic; + }; + Configuration left; + Configuration right; + Configuration leftAgain; + Configuration rightAgain; + named(left, "left"); + named(right, "right"); + named(leftAgain, "left"); + named(rightAgain, "right"); + left.dependencies = { &rightAgain }; + right.dependencies = { &leftAgain }; + + std::array crossed = { &left, &right }; + std::unordered_map> depResults; + std::mutex depMutex; + const std::string refused = BuildAll(crossed, depResults, depMutex); + Check(refused.contains("left, right") && refused.contains("none of them can be built first"), std::format("two configurations that each wait for the other's directory are refused, by name: {}", refused)); + Check(depResults.empty() && !fs::exists(scratch / "crossed" / "bin") && !fs::exists(scratch / "crossed" / "build"), "and refused before anything is built"); + } + + // A wasm32 library whose target is set here and not by + // ApplyStandardArgs, so that it names no sysroot, and a program built on + // it: the program finds the library's build in the record, under the + // directory the library has before its build and after it alike. The + // library has a flag no wasm build accepts, so its build is refused on + // every machine, with a WASI sysroot or without, and a refused build is + // recorded like any other. The program is one for the host, whose own + // build gets as far as asking for the library wherever the test runs. + { + Configuration library; + MakeLibrary(library, scratch / "greeter", "wasm-greeter", "Greeter"); + library.target = "wasm32-wasip1"; + library.compileFlags.push_back("-fno-exceptions"); + Configuration program; + program.dependencies = { &library }; + MakeProgram(program, scratch / "hello", "on-wasm-greeter"); + + std::array all = { &program, &library }; + std::unordered_map> depResults; + std::mutex depMutex; + const std::string failed = BuildAll(all, depResults, depMutex); + Check(Count(failed, "wasm-greeter: -fno-exceptions") == 1, std::format("the library's refusal comes back once, for the two of them: {}", failed)); + const auto recorded = std::ranges::count_if(depResults, [](const auto& entry) { return entry.first.filename().string().starts_with("wasm-greeter-"); }); + Check(recorded == 1 && depResults.contains(library.PcmDir()), std::format("and the library is in the record of the pass once, under the directory it has now ({} times)", recorded)); + } + + // One that fails stops none of the others, and what failed is said + // once: two programs built on a library that does not compile, and a + // third that has nothing to do with it. + { + Configuration broken; + MakeLibrary(broken, scratch / "broken", "broken", "Broken"); + Configuration first; + first.dependencies = { &broken }; + MakeProgram(first, scratch / "first", "first"); + Configuration second; + second.dependencies = { &broken }; + MakeProgram(second, scratch / "second", "second"); + Configuration good; + MakeProgram(good, scratch / "good", "good"); + + std::array all = { &first, &second, &good }; + std::unordered_map> depResults; + std::mutex depMutex; + const std::string failed = BuildAll(all, depResults, depMutex); + Check(failed.contains("Undeclared"), std::format("the library's compile error comes back: {}", failed)); + Check(Count(failed, "error:") == 1, std::format("a dependency that failed for two configurations is reported once: {}", failed)); + Check(fs::exists(ExpectedOutput(good)), "a configuration that has nothing to do with the failure is still built"); + Check(!fs::exists(ExpectedOutput(first)) && !fs::exists(ExpectedOutput(second)), "the two built on the broken library are not"); + + // Two configurations in one directory would be one piece of work to + // the pass, which would build the first and hand the second its + // result. + Configuration twin; + MakeProgram(twin, scratch / "good", "good"); + std::array clash = { &good, &twin }; + fs::remove_all(good.BinDir(), ec); + fs::remove_all(good.BuildDir(), ec); + std::unordered_map> clashResults; + const std::string refused = BuildAll(clash, clashResults, depMutex); + Check(refused.contains("would both be built into"), std::format("two configurations that build into one directory are refused: {}", refused)); + Check(!fs::exists(good.BinDir()), "and refused before either is built"); + } + + // A wasm executable with variants rewrites the configurations it is + // built on, once for each variant, so with one among them the + // configurations are built one after the other: built at once, a + // library two of them share would change under the other's build. The + // slow one comes first here, a program and the library it is built on, + // and the quick one, a single source, is only linked once the slow one + // is. The wasm one is refused before it builds anything, on every + // machine, for its -fno-exceptions, so no wasm build is waited for. + { + Configuration library; + MakeLibrary(library, scratch / "greeter", "ordered-greeter", "Greeter"); + Configuration slow; + slow.dependencies = { &library }; + MakeProgram(slow, scratch / "hello", "ordered-slow"); + Configuration quick; + MakeProgram(quick, scratch / "good", "ordered-quick"); + Configuration variants; + MakeProgram(variants, scratch / "good", "ordered-variants"); + variants.target = "wasm32-wasip1"; + variants.compileFlags.push_back("-fno-exceptions"); + EnableWasiRelaxedSimdVariant(variants); + + std::array all = { &slow, &quick, &variants }; + std::unordered_map> depResults; + std::mutex depMutex; + const std::string failed = BuildAll(all, depResults, depMutex); + Check(failed.contains("-fno-exceptions"), std::format("the wasm configuration is the one that is refused: {}", failed)); + const fs::file_time_type slowBuilt = fs::last_write_time(ExpectedOutput(slow), ec); + Check(!ec, "the slow configuration is built"); + const fs::file_time_type quickBuilt = fs::last_write_time(ExpectedOutput(quick), ec); + Check(!ec && quickBuilt >= slowBuilt, "with a wasm executable that declares variants among them, a configuration is built once the one before it is"); + } + + if (Failures > 0) { + std::println(std::cerr, "{} assertions failed", Failures); + return 1; + } + fs::remove_all(scratch, ec); + return 0; +} diff --git a/tests/DependencyNotes/fixture/outer/AGENTS.consumer.md b/tests/DependencyNotes/fixture/outer/AGENTS.consumer.md new file mode 100644 index 0000000..71c3eb9 --- /dev/null +++ b/tests/DependencyNotes/fixture/outer/AGENTS.consumer.md @@ -0,0 +1,3 @@ +## outer + +outer wraps lib. diff --git a/tests/DependencyNotes/fixture/outer/project.cpp b/tests/DependencyNotes/fixture/outer/project.cpp new file mode 100644 index 0000000..dbd05cd --- /dev/null +++ b/tests/DependencyNotes/fixture/outer/project.cpp @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Crafter.Build; +using namespace Crafter; + +// A project that builds against lib, and so against base one ring further +// out. app does not name it: the test loads it for the cases in which a +// project has several configurations, where it is what a second +// configuration names. +extern "C" Configuration CrafterBuildProject(std::span) { + Configuration cfg; + cfg.path = "./"; + cfg.name = "outer"; + cfg.outputName = "outer"; + cfg.type = ConfigurationType::LibraryStatic; + // Loaded from outer/, so this resolves against it. + cfg.dependencies = { LocalProject({ .projectFile = "../lib/project.cpp" }) }; + return cfg; +} diff --git a/tests/DependencyNotes/main.cpp b/tests/DependencyNotes/main.cpp index 58d9247..7481688 100644 --- a/tests/DependencyNotes/main.cpp +++ b/tests/DependencyNotes/main.cpp @@ -294,6 +294,86 @@ int main() { Check(Read(agents) == generated, "under None, AGENTS.md holds the template's text alone"); cfg.dependencyNotes = DependencyNotes::Transitive; + // A project file that returns several configurations: each one's setting + // selects among what that one is built against, and the notes are + // merged. `second` stands in for a second configuration of app's project + // file, and outer is a project that builds against lib, and so against + // base one ring further out. + { + Configuration* outer = nullptr; + try { + outer = LocalProject({ .projectFile = scratch / "outer" / "project.cpp" }); + } catch (const std::exception& e) { + std::println(std::cerr, "FAIL: loading outer threw: {}", e.what()); + return 1; + } + Configuration* const baseProject = cfg.dependencies[0]->dependencies[0]; + Configuration second; + std::array both = { &cfg, &second }; + // The projects the section names, in its order, once AGENTS.md is + // written for `configurations`. + const auto named = [&](std::span configurations) { + (void)UpdateAgentsMd(configurations, projectFile); + const std::string now = Read(agents); + constexpr std::string_view from = "", pos) - pos - from.size())); + } + return names; + }; + using Names = std::vector; + + cfg.dependencyNotes = DependencyNotes::Direct; + second.dependencies = { outer }; + second.dependencyNotes = DependencyNotes::Direct; + Check(named(both) == Names{"tool", "lib", "outer"}, std::format("two Direct configurations give what each of them names (got {})", named(both))); + // lib is in the first ring through app, which asks for no more. The + // second configuration reaches it through outer and asks for + // everything, so what lib builds against is in. + second.dependencyNotes = DependencyNotes::Transitive; + Check(named(both) == Names{"tool", "lib", "outer", "base"}, std::format("a project a Direct configuration names is followed when a Transitive one leads to it (got {})", named(both))); + cfg.dependencyNotes = DependencyNotes::None; + second.dependencies = { baseProject }; + Check(named(both) == Names{"tool", "base"}, std::format("a configuration under None keeps out what only it is built against (got {})", named(both))); + second.dependencyNotes = DependencyNotes::None; + Check(named(both).empty() && Read(agents) == generated, "with every configuration under None, AGENTS.md holds the template's text alone"); + + // A configuration the file loaded from another project and returns + // is one of the projects it is built against, and its own setting + // counts for nothing here: it goes by the widest of the others. + std::array withOuter = { &cfg, outer }; + cfg.dependencyNotes = DependencyNotes::Direct; + Check(named(withOuter) == Names{"tool", "lib", "outer"}, std::format("a returned configuration of another project is in the first ring, and under Direct no more of it (got {})", named(withOuter))); + cfg.dependencyNotes = DependencyNotes::Transitive; + Check(named(withOuter) == Names{"tool", "lib", "outer", "base"}, std::format("under Transitive what it builds against follows (got {})", named(withOuter))); + std::array onlyOuter = { outer }; + Check(named(onlyOuter) == Names{"tool", "outer", "lib", "base"}, std::format("a file that made no configuration itself gets everything (got {})", named(onlyOuter))); + + // The same Configuration, for outer's own AGENTS.md: outer's project + // file made it, so here its setting is the one that selects, though + // a loader holds it. Told apart by being held, it was another + // project's configuration to its own file, and got everything. + const fs::path outerFile = scratch / "outer" / "project.cpp"; + const fs::path outerAgents = scratch / "outer" / "AGENTS.md"; + Write(scratch / "outer" / "AGENTS.template.md", templateText); + const auto outerNamed = [&](DependencyNotes setting) { + outer->dependencyNotes = setting; + (void)UpdateAgentsMd(*outer, outerFile); + const std::string now = Read(outerAgents); + constexpr std::string_view from = "", pos) - pos - from.size())); + } + return names; + }; + Check(outerNamed(DependencyNotes::Direct) == Names{"lib"}, std::format("a project loaded by another gets what its own Direct selects in its own AGENTS.md (got {})", outerNamed(DependencyNotes::Direct))); + Check(outerNamed(DependencyNotes::Transitive) == Names{"lib", "base"}, std::format("and what its Transitive does (got {})", outerNamed(DependencyNotes::Transitive))); + Check(outerNamed(DependencyNotes::None).empty() && Read(outerAgents) == generated, "and under None the template's text alone"); + outer->dependencyNotes = DependencyNotes::Transitive; + } + // Linked to the template, AGENTS.md would have every build write the // file that is committed. fs::remove(agents); diff --git a/tests/Lint/main.cpp b/tests/Lint/main.cpp index 4e07d94..d5cffc5 100644 --- a/tests/Lint/main.cpp +++ b/tests/Lint/main.cpp @@ -246,6 +246,52 @@ int main() { Check(s.Read("a") == "hello \n", "Check mode never writes"); } + // The configurations a project file returns: one run over the rules and + // the sources of all of them. The second lies inside the first's + // directory, which is the project root when no project file is given. + { + Scratch first("several"); + Scratch second("several/second"); + first.Write("a", "one \n"); + second.Write("b", "two \n"); + Configuration one = first.Config({"a"}); + Configuration two = second.Config({"b"}); + AddTrimRule(one); + AddTrimRule(two); + std::int32_t noted = 0; + two.AddLintRule("note", [¬ed](LintContext&) { ++noted; }); + std::array both = { &one, &two }; + LintSummary sum = RunLint(both, Mode(LintMode::Apply)); + Check(first.Read("a") == "one\n" && second.Read("b") == "two\n", "several: the sources of every configuration are formatted"); + Check(sum.rulesRun == 2, "several: a rule two configurations register under one name is one rule"); + Check(noted == 2, "several: a rule one configuration registers runs over the sources of all"); + } + + // A rule two of the returned configurations register under one name is + // the one the first of them registered, in the order the file returned + // them, whatever depends on the other: app depends on tools, so a walk + // of one configuration's graph after another's meets tools before + // `other` and would run its rule. + { + Scratch appDir("order"); + Scratch otherDir("order/other"); + Scratch toolsDir("order/tools"); + appDir.Write("a", "app\n"); + otherDir.Write("b", "other\n"); + toolsDir.Write("c", "tools\n"); + Configuration app = appDir.Config({"a"}); + Configuration other = otherDir.Config({"b"}); + Configuration tools = toolsDir.Config({"c"}); + app.dependencies = { &tools }; + std::atomic otherRan = 0; + std::atomic toolsRan = 0; + other.AddLintRule("pick", [&otherRan](LintContext&) { ++otherRan; }); + tools.AddLintRule("pick", [&toolsRan](LintContext&) { ++toolsRan; }); + std::array roots = { &app, &other, &tools }; + const LintSummary sum = RunLint(roots, Mode(LintMode::Report)); + Check(sum.rulesRun == 1 && otherRan == 3 && toolsRan == 0, std::format("several: the rule of the first configuration that registers the name runs, over every source (rules {}, the first's {} times, the later one's {} times)", sum.rulesRun, otherRan.load(), toolsRan.load())); + } + // Chaining: rule2 sees rule1's output; both attributed in Report mode. { Scratch s("chain"); diff --git a/tests/ProjectConfigurations/fixture/both/project.cpp b/tests/ProjectConfigurations/fixture/both/project.cpp new file mode 100644 index 0000000..d330144 --- /dev/null +++ b/tests/ProjectConfigurations/fixture/both/project.cpp @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Crafter.Build; +using namespace Crafter; + +// Both entry points: whichever the loader called, what the other returns +// would go unbuilt. +extern "C" Configuration CrafterBuildProject(std::span) { + return {}; +} + +extern "C" std::vector CrafterBuildConfigurations(std::span) { + return {}; +} diff --git a/tests/ProjectConfigurations/fixture/empty/project.cpp b/tests/ProjectConfigurations/fixture/empty/project.cpp new file mode 100644 index 0000000..37a17eb --- /dev/null +++ b/tests/ProjectConfigurations/fixture/empty/project.cpp @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Crafter.Build; +using namespace Crafter; + +// Returns nothing to build. +extern "C" std::vector CrafterBuildConfigurations(std::span) { + return {}; +} diff --git a/tests/ProjectConfigurations/fixture/neither/project.cpp b/tests/ProjectConfigurations/fixture/neither/project.cpp new file mode 100644 index 0000000..ef78490 --- /dev/null +++ b/tests/ProjectConfigurations/fixture/neither/project.cpp @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Crafter.Build; +using namespace Crafter; + +// No entry point: the function has a name the loader does not look for. +extern "C" Configuration CrafterBuildProjects(std::span) { + return {}; +} diff --git a/tests/ProjectConfigurations/fixture/null/project.cpp b/tests/ProjectConfigurations/fixture/null/project.cpp new file mode 100644 index 0000000..7731811 --- /dev/null +++ b/tests/ProjectConfigurations/fixture/null/project.cpp @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Crafter.Build; +using namespace Crafter; + +// Returns a pointer to nothing. +extern "C" std::vector CrafterBuildConfigurations(std::span) { + return { nullptr }; +} diff --git a/tests/ProjectConfigurations/fixture/several/greeter/Greeter.cppm b/tests/ProjectConfigurations/fixture/several/greeter/Greeter.cppm new file mode 100644 index 0000000..4e0c093 --- /dev/null +++ b/tests/ProjectConfigurations/fixture/several/greeter/Greeter.cppm @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module Greeter; +import std; + +export namespace Greeter { + std::string Greet(std::string_view name) { + return std::format("hello {}", name); + } +} diff --git a/tests/ProjectConfigurations/fixture/several/hello/main.cpp b/tests/ProjectConfigurations/fixture/several/hello/main.cpp new file mode 100644 index 0000000..690dfe8 --- /dev/null +++ b/tests/ProjectConfigurations/fixture/several/hello/main.cpp @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Greeter; + +// -r runs this in its own directory, where it leaves what it would have +// printed for the test to find. +int main() { + std::ofstream("hello.ran") << Greeter::Greet("world"); + return 0; +} diff --git a/tests/ProjectConfigurations/fixture/several/project.cpp b/tests/ProjectConfigurations/fixture/several/project.cpp new file mode 100644 index 0000000..63d7b6a --- /dev/null +++ b/tests/ProjectConfigurations/fixture/several/project.cpp @@ -0,0 +1,94 @@ +// 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 of three configurations: a library, a program built on it, and +// a tool that shares nothing with either. The library and the tool each +// declare a test and a script. --no-tool leaves the tool out, which leaves +// one executable for -r to run. --packaged packages both programs as +// Flatpaks, for install and bundle to choose between, and makes them wasm +// builds, which a build refuses to package wherever the test runs: nothing +// is ever exported, let alone installed. --twin-test has the library +// declare the tool's test as well, which a test run refuses to build. +// --twin adds a fourth configuration of the tool's name and path, which +// would be built into the tool's directory. +namespace { + // One new set per call, at addresses that never change: whoever loads + // this file keeps the pointers it returns. + std::deque Owned; + + // Leaves `