Crafter.Build/tests/SiblingOwnPointers/main.cpp

49 lines
2.2 KiB
C++
Raw Normal View History

// 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 may keep pointers to the Configuration it returns: a test
// that links the library the file describes is declared with
// cfg.AddTest("T").Dependencies({ &cfg }). Loaded by another project through
// LocalProject or GitProject, that Configuration has to stay where the
// project file built it.
//
// The loader moved it: out of the call's return value, and then into its
// cache. The test's dependency was left pointing at an object that no
// longer existed, and whatever followed it read freed memory.
// `crafter-build lint` follows it for a sibling inside the project's own
// directory, when it works out the compile command of each of the sibling's
// tests for a rule that needs an AST.
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 the load drops a build/ tree next to the project file.
const fs::path scratch = fs::temp_directory_path() / "crafter-build-sibling-own-pointers";
std::error_code ec;
fs::remove_all(scratch, ec);
fs::copy(fs::current_path() / "tests" / "SiblingOwnPointers" / "fixture", scratch, fs::copy_options::recursive);
Configuration* sibling = nullptr;
try {
sibling = LocalProject({ .projectFile = scratch / "project.cpp" });
} catch (const std::exception& e) {
std::println(std::cerr, "FAIL: loading the sibling threw: {}", e.what());
return 1;
}
// Compared, never followed: where the two differ, the pointer leads
// nowhere.
if (sibling->tests.size() != 1 || sibling->tests[0].config.dependencies != std::vector<Configuration*>{sibling}) {
std::println(std::cerr, "FAIL: the pointer a sibling's project file took to the Configuration it returns is not the Configuration its consumers get");
return 1;
}
fs::remove_all(scratch, ec);
return 0;
}