165 lines
8.6 KiB
C++
165 lines
8.6 KiB
C++
|
|
// 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;
|
||
|
|
}
|