Crafter.Build/tests/ProjectConfigurations/main.cpp

228 lines
13 KiB
C++
Raw Normal View History

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.
2026-11-15 02:16:07 +01:00
// 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<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() };
}
std::vector<std::string> Names(std::span<Configuration* const> configurations) {
std::vector<std::string> 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<std::string_view, 0> noArgs = {};
std::array<std::string_view, 1> 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<Configuration*> 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<std::string>{"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<Configuration*>{several[0]}, "the pointers the file took between its configurations are the ones it returned");
Check(Names(LoadConfigurations("project.cpp", noTool)) == std::vector<std::string>{"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<Configuration*> single = LoadConfigurations("project.cpp", noArgs);
Check(Names(single) == std::vector<std::string>{"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<Configuration*>{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<std::pair<std::string_view, std::string_view>>{
{"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<std::string_view, 1> twinTest = { "--twin-test" };
const std::vector<Configuration*> 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<std::string> ran;
for (const TestResult& result : summary.results) ran.push_back(result.name);
std::ranges::sort(ran);
Check(ran == std::vector<std::string>{"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<Configuration*, 2> 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;
}