// 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; 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 ReadFile(const fs::path& path) { std::ifstream in(path, std::ios::binary); return std::string(std::istreambuf_iterator(in), std::istreambuf_iterator()); } } // `format --check` writes nothing, a rule-name glob after it included. A // second `format`, the glob that names a rule called "format", was read as // the verb again, which set the mode back to applying and recorded no // glob, so `crafter-build format --check format` ran every transform and // rewrote the project's sources, and exited 0. The fixture's one rule, // named "format", would change its main.cpp: the run has to say so, exit 1 // as a check with a file to change does, and leave main.cpp as it was. 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 format rewrites files, and LoadProject drops a build/ // tree next to the project file it compiles. const fs::path scratch = fs::temp_directory_path() / "crafter-build-format-check-glob"; std::error_code ec; fs::remove_all(scratch, ec); fs::copy(fs::current_path() / "tests" / "FormatCheckGlob" / "fixture", scratch, fs::copy_options::recursive); const std::string before = ReadFile(scratch / "main.cpp"); // From the project's own directory, as crafter-build runs it. const fs::path cwd = fs::current_path(); fs::current_path(scratch); std::array words = { "crafter-build", "format", "--check", "format" }; std::array argv = { words[0].data(), words[1].data(), words[2].data(), words[3].data() }; const std::int32_t status = Run(static_cast(argv.size()), argv.data()); fs::current_path(cwd); Check(ReadFile(scratch / "main.cpp") == before, "format --check format leaves main.cpp as it was"); Check(status == 1, std::format("and exits 1, a file the rule would change (exit {})", status)); fs::remove_all(scratch, ec); if (Failures > 0) { std::println(std::cerr, "{} assertions failed", Failures); return 1; } return 0; }