Files
cargoxx/tests/cmd_run.cpp

66 lines
2.1 KiB
C++

#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);
}