test: specs and ProjectDependency() in a project file's static initialisers

ProjectArgs gets a sibling that makes its own sibling's spec at
namespace scope. Before the fix that spec took the args the file was
first loaded with; now it takes none on every load. The checks go
through one CheckLoaded helper, which prints what each sibling was
given, so a failure says which args a wrong frame handed over.

ProjectDependency loads a fixture whose static initialiser asks for its
pragma dependency, in a child, because a throw there terminates the
process: before the fix it found none. The check that the dependency
gets none of the consumer's args moves into the first load, which now
passes the app an arg it ignores; the separate load could not fail its
own check, and its early return skipped every later one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jorijn van der Graaf 2026-10-31 05:10:02 +01:00
commit e6363b95fe
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
5 changed files with 102 additions and 36 deletions

View file

@ -15,7 +15,10 @@ using namespace Crafter;
// release while the project built --debug. The fixture is a chain
// app -> lib -> leaf of LocalProjects; app also loads lib with an empty and
// with a replaced list, and checks the GitProjectSpec default itself, which
// a clone would need a repository for.
// a clone would need a repository for. A spec made at namespace scope gets
// no args: made while dlopen runs the file's static initialisers, which
// happens on its first load only, it would otherwise keep that load's args
// for every later one.
namespace {
std::int32_t Failures = 0;
@ -26,10 +29,21 @@ namespace {
}
}
// The single sibling lib and leaf declare, or nullptr.
// The single sibling lib, static and leaf declare, or nullptr.
const Configuration* OnlyDependency(const Configuration& cfg) {
return cfg.dependencies.size() == 1 ? cfg.dependencies[0] : nullptr;
}
// What a sibling was loaded with, as ApplyStandardArgs split it up:
// --debug into cfg.debug, the uninterpreted rest into cfg.projectArgs.
// Printed either way, so a failure says which args the sibling read.
void CheckLoaded(const Configuration* cfg, bool debug, const std::vector<std::string>& rest, std::string_view what) {
if (!cfg) {
Check(false, std::format("{} (no such configuration)", what));
return;
}
Check(cfg->debug == debug && cfg->projectArgs == rest, std::format("{} (debug {}, projectArgs {})", what, cfg->debug, cfg->projectArgs));
}
}
int main() {
@ -60,24 +74,25 @@ int main() {
try {
Configuration cfg = LoadProject("project.cpp", args);
Check(std::ranges::contains(cfg.compileFlags, "-DGIT_DEFAULT=1"), "a GitProjectSpec's args default to the project's own");
Check(cfg.dependencies.size() == 3, std::format("the project declared its three siblings (got {})", cfg.dependencies.size()));
if (cfg.dependencies.size() == 3) {
const Configuration& inherited = *cfg.dependencies[0];
const Configuration& none = *cfg.dependencies[1];
const Configuration& replaced = *cfg.dependencies[2];
Check(inherited.debug && inherited.projectArgs == flavor, std::format("a LocalProject without .args gets the project's args (debug {}, projectArgs {})", inherited.debug, inherited.projectArgs));
Check(!none.debug && none.projectArgs.empty(), std::format("`.args = {{}}` forwards nothing (debug {}, projectArgs {})", none.debug, none.projectArgs));
Check(!replaced.debug && replaced.projectArgs == only, std::format("an explicit list replaces the args rather than adding to them (debug {}, projectArgs {})", replaced.debug, replaced.projectArgs));
Check(&inherited != &none && &inherited != &replaced && &none != &replaced, "each list loads its own configuration");
Check(cfg.dependencies.size() == 4, std::format("the project declared its four siblings (got {})", cfg.dependencies.size()));
if (cfg.dependencies.size() == 4) {
const Configuration* inherited = cfg.dependencies[0];
const Configuration* none = cfg.dependencies[1];
const Configuration* replaced = cfg.dependencies[2];
const Configuration* fromStatic = cfg.dependencies[3];
CheckLoaded(inherited, true, flavor, "a LocalProject without .args gets the project's args");
CheckLoaded(none, false, {}, "`.args = {}` forwards nothing");
CheckLoaded(replaced, false, only, "an explicit list replaces the args rather than adding to them");
Check(inherited != none && inherited != replaced && none != replaced, "each list loads its own configuration");
// One sibling further down, the same default passes on what the
// middle project was given, not what the top one was.
const Configuration* inheritedLeaf = OnlyDependency(inherited);
Check(inheritedLeaf && inheritedLeaf->debug && inheritedLeaf->projectArgs == flavor, "the project's args reach its sibling's sibling");
const Configuration* noneLeaf = OnlyDependency(none);
Check(noneLeaf && !noneLeaf->debug && noneLeaf->projectArgs.empty(), "a sibling loaded with nothing passes nothing on");
const Configuration* replacedLeaf = OnlyDependency(replaced);
Check(replacedLeaf && !replacedLeaf->debug && replacedLeaf->projectArgs == only, "a sibling passes on the list it was given");
CheckLoaded(OnlyDependency(*inherited), true, flavor, "the project's args reach its sibling's sibling");
CheckLoaded(OnlyDependency(*none), false, {}, "a sibling loaded with nothing passes nothing on");
CheckLoaded(OnlyDependency(*replaced), false, only, "a sibling passes on the list it was given");
CheckLoaded(fromStatic, true, flavor, "the sibling with a namespace-scope spec is itself loaded with the args");
CheckLoaded(OnlyDependency(*fromStatic), false, {}, "a spec made at namespace scope forwards nothing");
}
} catch (const std::exception& e) {
fs::current_path(cwd);