diff --git a/project.cpp b/project.cpp index dc636e7..976d76d 100644 --- a/project.cpp +++ b/project.cpp @@ -255,6 +255,8 @@ extern "C" Configuration CrafterBuildProject(std::span a cfg.AddTest("StdPcmRebuild").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("CompilerWarning").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("CleanProject").Dependencies({ CrafterBuildLib.get() }); + cfg.AddTest("FormatCheckGlob").Dependencies({ CrafterBuildLib.get() }) + .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); cfg.AddTest("ShaderCompile").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("ShaderVariantBuild").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("ShaderIncludeChange").Dependencies({ CrafterBuildLib.get() }); diff --git a/tests/FormatCheckGlob/fixture/main.cpp b/tests/FormatCheckGlob/fixture/main.cpp new file mode 100644 index 0000000..23c066a --- /dev/null +++ b/tests/FormatCheckGlob/fixture/main.cpp @@ -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; +} diff --git a/tests/FormatCheckGlob/fixture/project.cpp b/tests/FormatCheckGlob/fixture/project.cpp new file mode 100644 index 0000000..5b0c894 --- /dev/null +++ b/tests/FormatCheckGlob/fixture/project.cpp @@ -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 args) { + Configuration cfg; + cfg.path = "./"; + cfg.name = "format-check"; + cfg.outputName = "format-check"; + cfg.type = ConfigurationType::Executable; + ApplyStandardArgs(cfg, args); + std::array interfaces = {}; + std::array 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; +} diff --git a/tests/FormatCheckGlob/main.cpp b/tests/FormatCheckGlob/main.cpp new file mode 100644 index 0000000..c2952a3 --- /dev/null +++ b/tests/FormatCheckGlob/main.cpp @@ -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(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; +}