test(cli): format --check with a rule name writes nothing

`crafter-build format --check format` read the second `format` as the
verb again, which set the mode back to applying and recorded no glob, so
it ran every transform and rewrote the project's sources, and exited 0,
though `--check` writes nothing. FormatCheckGlob drives Run() on a
staged project whose one rule, named "format", would change its
main.cpp, and holds the run to exiting 1, the check with a file to
change, with main.cpp as it was. It fails before the verb enum of the
commit before this one and passes with it.
This commit is contained in:
Jorijn van der Graaf 2026-11-08 23:31:46 +01:00
commit 4b1585c709
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
4 changed files with 99 additions and 0 deletions

View file

@ -0,0 +1,64 @@
// 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<char>(in), std::istreambuf_iterator<char>());
}
}
// `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<std::string, 4> words = { "crafter-build", "format", "--check", "format" };
std::array<char*, 4> argv = { words[0].data(), words[1].data(), words[2].data(), words[3].data() };
const std::int32_t status = Run(static_cast<std::int32_t>(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;
}