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:
parent
2c3d0b0d1f
commit
4b1585c709
4 changed files with 99 additions and 0 deletions
9
tests/FormatCheckGlob/fixture/main.cpp
Normal file
9
tests/FormatCheckGlob/fixture/main.cpp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
import std;
|
||||
|
||||
int main() {
|
||||
std::println("format-check");
|
||||
return 0;
|
||||
}
|
||||
24
tests/FormatCheckGlob/fixture/project.cpp
Normal file
24
tests/FormatCheckGlob/fixture/project.cpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
import std;
|
||||
import Crafter.Build;
|
||||
using namespace Crafter;
|
||||
|
||||
// One source and one transform, named "format", that would change it: it
|
||||
// ends every file with a marker line the fixture's main.cpp lacks.
|
||||
extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> args) {
|
||||
Configuration cfg;
|
||||
cfg.path = "./";
|
||||
cfg.name = "format-check";
|
||||
cfg.outputName = "format-check";
|
||||
cfg.type = ConfigurationType::Executable;
|
||||
ApplyStandardArgs(cfg, args);
|
||||
std::array<std::filesystem::path, 0> interfaces = {};
|
||||
std::array<std::filesystem::path, 1> implementations = { "main" };
|
||||
cfg.GetInterfacesAndImplementations(interfaces, implementations);
|
||||
cfg.AddLintRule("format", [](LintContext& ctx) {
|
||||
if (!ctx.content.ends_with("// formatted\n")) ctx.SetContent(ctx.content + "// formatted\n");
|
||||
});
|
||||
return cfg;
|
||||
}
|
||||
64
tests/FormatCheckGlob/main.cpp
Normal file
64
tests/FormatCheckGlob/main.cpp
Normal 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;
|
||||
}
|
||||
Loading…
Reference in a new issue