[M4] add cargoxx run/test/clean

This commit is contained in:
2026-05-10 00:12:25 +00:00
parent f6e8699a72
commit 0a398d1c31
10 changed files with 349 additions and 0 deletions

View File

@@ -20,3 +20,5 @@ cargoxx_add_test(codegen_flake)
cargoxx_add_test(codegen_cmake)
cargoxx_add_test(cmd_new)
cargoxx_add_test(cmd_build)
cargoxx_add_test(cmd_run)
cargoxx_add_test(cmd_clean)

44
tests/cmd_clean.cpp Normal file
View File

@@ -0,0 +1,44 @@
#include <catch2/catch_test_macros.hpp>
import cargoxx.cli;
import cargoxx.util;
import std;
using cargoxx::cli::cmd_clean;
namespace {
auto fresh_dir() -> std::filesystem::path {
auto d = std::filesystem::temp_directory_path() /
std::format("cargoxx-clean-test-{}", std::random_device{}());
std::filesystem::create_directories(d);
return d;
}
} // namespace
TEST_CASE("cmd_clean removes the build directory", "[cli][clean]") {
auto root = fresh_dir();
std::filesystem::create_directories(root / "build" / "debug");
std::ofstream{root / "build" / "debug" / "stale.txt"} << "x";
REQUIRE(cmd_clean(root).has_value());
REQUIRE_FALSE(std::filesystem::exists(root / "build"));
}
TEST_CASE("cmd_clean is a no-op when build/ does not exist", "[cli][clean]") {
auto root = fresh_dir();
REQUIRE(cmd_clean(root).has_value());
}
TEST_CASE("cmd_clean leaves Cargoxx.lock and flake.lock alone", "[cli][clean]") {
auto root = fresh_dir();
std::filesystem::create_directories(root / "build");
std::ofstream{root / "Cargoxx.lock"} << "version = 1\n";
std::ofstream{root / "flake.lock"} << "{}\n";
REQUIRE(cmd_clean(root).has_value());
REQUIRE_FALSE(std::filesystem::exists(root / "build"));
REQUIRE(std::filesystem::exists(root / "Cargoxx.lock"));
REQUIRE(std::filesystem::exists(root / "flake.lock"));
}

65
tests/cmd_run.cpp Normal file
View File

@@ -0,0 +1,65 @@
#include <catch2/catch_test_macros.hpp>
import cargoxx.cli;
import cargoxx.util;
import std;
using cargoxx::cli::cmd_new;
using cargoxx::cli::cmd_run;
using cargoxx::util::ErrorCode;
namespace {
auto fresh_dir() -> std::filesystem::path {
auto d = std::filesystem::temp_directory_path() /
std::format("cargoxx-run-test-{}", std::random_device{}());
std::filesystem::create_directories(d);
return d;
}
auto overlay_path(const std::filesystem::path& dir) -> std::filesystem::path {
return dir / "overlay.sqlite";
}
auto add_extra_bin(const std::filesystem::path& root, std::string_view name) {
auto p = root / "src" / "bin" / std::format("{}.cpp", name);
std::filesystem::create_directories(p.parent_path());
std::ofstream{p} << "int main() { return 0; }\n";
}
} // namespace
TEST_CASE("cmd_run errors when no binary is present", "[cli][run]") {
// A library-only project has no binary to run.
auto parent = fresh_dir();
REQUIRE(cmd_new("widget", true, parent).has_value());
auto root = parent / "widget";
auto r = cmd_run(root, false, std::nullopt, {}, overlay_path(parent));
REQUIRE_FALSE(r.has_value());
REQUIRE(r.error().code == ErrorCode::LayoutNoTarget);
}
TEST_CASE("cmd_run errors when --bin doesn't match any target", "[cli][run]") {
auto parent = fresh_dir();
REQUIRE(cmd_new("app", false, parent).has_value());
auto root = parent / "app";
auto r = cmd_run(root, false, std::string{"missing"}, {}, overlay_path(parent));
REQUIRE_FALSE(r.has_value());
REQUIRE(r.error().code == ErrorCode::LayoutNoTarget);
}
TEST_CASE("cmd_run requires --bin when multiple binaries exist", "[cli][run]") {
auto parent = fresh_dir();
REQUIRE(cmd_new("app", false, parent).has_value());
auto root = parent / "app";
add_extra_bin(root, "tool");
auto r = cmd_run(root, false, std::nullopt, {}, overlay_path(parent));
REQUIRE_FALSE(r.has_value());
REQUIRE(r.error().code == ErrorCode::LayoutNoTarget);
// Hint should list both target names.
REQUIRE(r.error().hint.find("app") != std::string::npos);
REQUIRE(r.error().hint.find("tool") != std::string::npos);
}