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.
This commit is contained in:
Jorijn van der Graaf 2026-11-15 02:16:07 +01:00
commit 5795b666f5
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
42 changed files with 1427 additions and 5 deletions

View file

@ -0,0 +1,20 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
using namespace Crafter;
// Returns what the project beside it keeps to itself, which the load
// refuses: the library keeper's program is built on, or with --test the
// configuration of the program's test, or with --test-dependency the helper
// only that test is built on. keeper's own file returns none of the three.
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view> args) {
// The test loads this file from digger/, so the path resolves against
// it. No args, so that keeper is loaded once whatever this file was
// given.
Configuration* program = LocalProject({ .projectFile = "../keeper/project.cpp", .args = {} });
if (std::ranges::contains(args, std::string_view("--test"))) return { &program->tests[0].config };
if (std::ranges::contains(args, std::string_view("--test-dependency"))) return { program->tests[0].config.dependencies[1] };
return { program->dependencies[0] };
}

View file

@ -0,0 +1,39 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
using namespace Crafter;
// A project that returns one of the configurations it makes: the program.
// It keeps to itself the library the program is built on, the program's
// test, and a helper only that test is built on. None has a source: the
// test loads this file and builds nothing of it.
namespace {
// One new set per call, at addresses that never change.
std::deque<Configuration> Owned;
}
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view>) {
Configuration& kept = Owned.emplace_back();
kept.path = "./";
kept.name = "kept";
kept.outputName = "kept";
kept.type = ConfigurationType::LibraryStatic;
Configuration& helper = Owned.emplace_back();
helper.path = "./";
helper.name = "helper";
helper.outputName = "helper";
helper.type = ConfigurationType::LibraryStatic;
Configuration& program = Owned.emplace_back();
program.path = "./";
program.name = "program";
program.outputName = "program";
program.type = ConfigurationType::Executable;
program.dependencies = { &kept };
program.AddTest("KeptTest").Dependencies({ &program, &helper });
return { &program };
}

View file

@ -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);
}
}

View file

@ -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;
}

View file

@ -0,0 +1,60 @@
// 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;
// The project other projects load: a library, a program built on it, and a
// tool that shares nothing with either and declares a test and a script.
// Its `path` is relative, as a project file writes it, so a consumer only
// builds it into this directory once the loader has rebased it.
namespace {
// One new set per call, at addresses that never change: whoever loads
// this file keeps the pointers it returns.
std::deque<Configuration> Owned;
}
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view>) {
Configuration& greeter = Owned.emplace_back();
greeter.path = "./";
greeter.name = "greeter";
greeter.outputName = "greeter";
greeter.type = ConfigurationType::LibraryStatic;
{
std::array<fs::path, 1> interfaces = { "greeter/Greeter" };
std::array<fs::path, 0> implementations = {};
greeter.GetInterfacesAndImplementations(interfaces, implementations);
}
Configuration& hello = Owned.emplace_back();
hello.path = "./";
hello.name = "hello";
hello.outputName = "hello";
hello.type = ConfigurationType::Executable;
hello.dependencies = { &greeter };
{
std::array<fs::path, 0> interfaces = {};
std::array<fs::path, 1> implementations = { "hello/main" };
hello.GetInterfacesAndImplementations(interfaces, implementations);
}
Configuration& tool = Owned.emplace_back();
tool.path = "./";
tool.name = "tool";
tool.outputName = "tool";
tool.type = ConfigurationType::Executable;
{
std::array<fs::path, 0> interfaces = {};
std::array<fs::path, 1> implementations = { "tool/main" };
tool.GetInterfacesAndImplementations(interfaces, implementations);
}
tool.AddTest("ToolTest");
tool.scripts.push_back({"stamp", [](ScriptContext& ctx) {
std::ofstream(ctx.projectDir / "stamp.ran") << ctx.project.name;
return 0;
}});
return { &greeter, &hello, &tool };
}

View file

@ -0,0 +1,11 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
// Leaves a file where it ran: the directory of the project whose test run
// started it.
int main() {
std::ofstream("ToolTest.ran") << "ran";
return 0;
}

View file

@ -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;
}

View file

@ -0,0 +1,24 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
using namespace Crafter;
// Two configurations under one name, which a consumer cannot pick between
// by it. Their output names tell the test which of them it was handed.
namespace {
std::deque<Configuration> Owned;
}
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view>) {
Configuration& first = Owned.emplace_back();
first.path = "./first/";
first.name = "twin";
first.outputName = "first";
Configuration& second = Owned.emplace_back();
second.path = "./second/";
second.name = "twin";
second.outputName = "second";
return { &first, &second };
}

View file

@ -0,0 +1,9 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
int main() {
std::println("umbrella");
return 0;
}

View file

@ -0,0 +1,34 @@
// 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 that returns a configuration of another project beside one of
// its own: the tool of ../sibling, picked by name, and a program made here.
// The tool is built with the program, and its test and its script stay the
// other project's.
namespace {
std::deque<Configuration> Owned;
}
extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view>) {
Configuration* tool = LocalProject({ .projectFile = "../sibling/project.cpp", .configuration = "tool" });
Configuration& app = Owned.emplace_back();
app.path = "./";
app.name = "umbrella";
app.outputName = "umbrella";
app.type = ConfigurationType::Executable;
std::array<fs::path, 0> interfaces = {};
std::array<fs::path, 1> implementations = { "main" };
app.GetInterfacesAndImplementations(interfaces, implementations);
app.scripts.push_back({"own", [](ScriptContext& ctx) {
std::ofstream(ctx.projectDir / "own.ran") << ctx.project.name;
return 0;
}});
return { tool, &app };
}

View file

@ -0,0 +1,165 @@
// 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 that returns several configurations, loaded by another
// project: LocalProject and GitProject give the first one unless the spec
// names one, and a project file may return what they gave it beside
// configurations of its own.
//
// What this guards against is a consumer that gets the wrong thing: another
// configuration than it asked for; one whose sources resolve against the
// consumer's directory, because only the first was rebased; a second load
// of the sibling for every name asked, which leaves a consumer of two of
// its configurations with two builds of what they share; or everything the
// sibling describes built when one part of it was asked for. And against a
// sibling's configuration becoming part of the project that returns it: its
// script run as the project's, its test in the project's test run, whether
// the sibling's file returned it or kept it to itself.
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<char>(in)), std::istreambuf_iterator<char>());
}
// What `call` throws, or "" when it returns.
std::string Thrown(const std::function<void()>& 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<std::string> args) {
std::vector<char*> 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<std::int32_t>(argv.size()), argv.data());
std::cerr.rdbuf(saved);
return { status, err.str() };
}
}
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-sibling-configurations";
std::error_code ec;
fs::remove_all(scratch, ec);
fs::copy(home / "tests" / "SiblingConfigurations" / "fixture", scratch, fs::copy_options::recursive);
// The first configuration unless the spec names one.
const fs::path sibling = scratch / "sibling" / "project.cpp";
Configuration* greeter = nullptr;
Configuration* hello = nullptr;
Configuration* tool = nullptr;
try {
greeter = LocalProject({ .projectFile = sibling });
hello = LocalProject({ .projectFile = sibling, .configuration = "hello" });
tool = LocalProject({ .projectFile = sibling, .configuration = "tool" });
} catch (const std::exception& e) {
std::println(std::cerr, "FAIL: loading the sibling threw: {}", e.what());
return 1;
}
Check(greeter->name == "greeter", std::format("a spec without a name gets the first configuration (got {})", greeter->name));
Check(hello->name == "hello" && tool->name == "tool", "a spec that names one gets that one");
Check(hello->dependencies == std::vector<Configuration*>{greeter}, "specs that differ only in the configuration they name share one load of the file");
for (const Configuration* cfg : {greeter, hello, tool}) {
Check(cfg->path.is_absolute() && fs::equivalent(cfg->path, scratch / "sibling", ec), std::format("{} is rebased onto its project's directory, like every configuration the file returns (path {})", cfg->name, cfg->path.string()));
}
{
const std::string thrown = Thrown([&] { (void)LocalProject({ .projectFile = sibling, .configuration = "nope" }); });
Check(thrown.contains("no configuration named 'nope'") && thrown.contains("greeter, hello, tool"), std::format("a name the project does not have is refused, listing the ones it has (got '{}')", thrown));
const fs::path twins = scratch / "twins" / "project.cpp";
const std::string ambiguous = Thrown([&] { (void)LocalProject({ .projectFile = twins, .configuration = "twin" }); });
Check(ambiguous.contains("two of its configurations are named 'twin'"), std::format("a name two configurations share picks neither (got '{}')", ambiguous));
Check(LocalProject({ .projectFile = twins })->outputName == "first", "without a name the first of the two is still the one a consumer gets");
}
// A consumer builds what it names and what that is built on.
{
std::array<Configuration*, 1> named = { hello };
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
const std::string failed = BuildAll(named, depResults, depMutex);
Check(failed.empty(), std::format("a sibling's second configuration builds: {}", failed));
Check(fs::exists(ExpectedOutput(*hello)) && fs::exists(ExpectedOutput(*greeter)), "with the library it is built on");
Check(!fs::exists(ExpectedOutput(*tool)), "and the configuration nobody named stays unbuilt");
}
// A configuration a sibling keeps to itself, one its project file does
// not return, is not another project's to return. It is not rebased,
// and returned it would count as the returning project's own, tests and
// scripts with it. digger returns what keeper's program leads to: the
// library it is built on, the configuration of its test, and a helper
// only that test is built on.
fs::current_path(scratch / "digger");
for (const auto& [arg, kept] : std::initializer_list<std::pair<std::string_view, std::string_view>>{
{"", "kept"},
{"--test", "KeptTest"},
{"--test-dependency", "helper"},
}) {
std::vector<std::string_view> args;
if (!arg.empty()) args.push_back(arg);
const std::string thrown = Thrown([&] { (void)LoadConfigurations("project.cpp", args); });
Check(thrown.contains(std::format("returned {}, which keeper keeps to itself", kept)), std::format("a file that returns {}, which its sibling keeps to itself, is refused, naming the two (got '{}')", kept, thrown));
}
// A file that returns another project's configuration beside its own:
// it is built, and everything else about it stays that project's. From
// the project's own directory, as crafter-build runs it.
fs::current_path(scratch / "umbrella");
Check(RunWith({"crafter-build", "-q"}).status == 0, "a project that returns a sibling's configuration builds");
{
std::array<std::string_view, 0> noArgs = {};
const std::vector<Configuration*> umbrella = LoadConfigurations("project.cpp", noArgs);
Check(umbrella.size() == 2 && umbrella[0] == tool, "the sibling's configuration is the one every spec for it gets");
Check(fs::exists(ExpectedOutput(*tool)), "the returned sibling's configuration is built");
Check(umbrella.size() == 2 && fs::exists(ExpectedOutput(*umbrella[1])), "and so is the project's own");
}
{
const Ran run = RunWith({"crafter-build", "-q", "run", "stamp"});
Check(run.status == 1 && run.err.contains("No script named 'stamp'"), std::format("the sibling's script is not the project's to run (exit {}): {}", run.status, run.err));
Check(!fs::exists(scratch / "sibling" / "stamp.ran") && !fs::exists(scratch / "umbrella" / "stamp.ran"), "and it did not run");
Check(RunWith({"crafter-build", "-q", "run", "own"}).status == 0 && Read(scratch / "umbrella" / "own.ran") == "umbrella", "the project's own script runs");
}
Check(RunWith({"crafter-build", "-q", "test"}).status == 0, "a test run of the project succeeds");
Check(!fs::exists(scratch / "umbrella" / "ToolTest.ran") && !fs::exists(scratch / "sibling" / "ToolTest.ran"), "and leaves the sibling's test alone");
fs::current_path(home);
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
fs::remove_all(scratch, ec);
return 0;
}