[M3] add cargoxx build --no-build
This commit is contained in:
@@ -18,3 +18,4 @@ cargoxx_add_test(linkdb_overlay)
|
||||
cargoxx_add_test(codegen_flake)
|
||||
cargoxx_add_test(codegen_cmake)
|
||||
cargoxx_add_test(cmd_new)
|
||||
cargoxx_add_test(cmd_build)
|
||||
|
||||
176
tests/cmd_build.cpp
Normal file
176
tests/cmd_build.cpp
Normal file
@@ -0,0 +1,176 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
import cargoxx.cli;
|
||||
import cargoxx.manifest;
|
||||
import cargoxx.lockfile;
|
||||
import cargoxx.util;
|
||||
import std;
|
||||
|
||||
using cargoxx::cli::cmd_build;
|
||||
using cargoxx::cli::cmd_new;
|
||||
using cargoxx::util::ErrorCode;
|
||||
namespace manifest = cargoxx::manifest;
|
||||
namespace lockfile = cargoxx::lockfile;
|
||||
|
||||
namespace {
|
||||
|
||||
auto fresh_dir() -> std::filesystem::path {
|
||||
auto d = std::filesystem::temp_directory_path() /
|
||||
std::format("cargoxx-build-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 read_file(const std::filesystem::path& p) -> std::string {
|
||||
std::ifstream in{p};
|
||||
return std::string{std::istreambuf_iterator<char>(in), {}};
|
||||
}
|
||||
|
||||
auto add_dep(const std::filesystem::path& root, const std::string& name,
|
||||
const std::string& version_spec, std::vector<std::string> components = {}) {
|
||||
auto path = root / "Cargoxx.toml";
|
||||
auto m = manifest::parse(path);
|
||||
REQUIRE(m.has_value());
|
||||
m->dependencies.push_back(manifest::Dependency{
|
||||
.name = name,
|
||||
.version_spec = version_spec,
|
||||
.components = std::move(components),
|
||||
});
|
||||
REQUIRE(manifest::write(*m, path).has_value());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("cmd_build generates files for a no-deps binary project",
|
||||
"[cli][build]") {
|
||||
auto parent = fresh_dir();
|
||||
REQUIRE(cmd_new("hello", false, parent).has_value());
|
||||
auto root = parent / "hello";
|
||||
|
||||
auto r = cmd_build(root, true, false, overlay_path(parent));
|
||||
REQUIRE(r.has_value());
|
||||
|
||||
REQUIRE(std::filesystem::exists(root / "flake.nix"));
|
||||
REQUIRE(std::filesystem::exists(root / "build" / "CMakeLists.txt"));
|
||||
REQUIRE(std::filesystem::exists(root / "Cargoxx.lock"));
|
||||
|
||||
auto cmake_text = read_file(root / "build" / "CMakeLists.txt");
|
||||
REQUIRE(cmake_text.find("project(hello LANGUAGES CXX)") != std::string::npos);
|
||||
REQUIRE(cmake_text.find("add_executable(hello_bin ../src/main.cpp)") !=
|
||||
std::string::npos);
|
||||
|
||||
auto flake_text = read_file(root / "flake.nix");
|
||||
REQUIRE(flake_text.find("description = \"hello\";") != std::string::npos);
|
||||
REQUIRE(flake_text.find("github:NixOS/nixpkgs/nixos-unstable") != std::string::npos);
|
||||
}
|
||||
|
||||
TEST_CASE("cmd_build generates files for a library project", "[cli][build]") {
|
||||
auto parent = fresh_dir();
|
||||
REQUIRE(cmd_new("widget", true, parent).has_value());
|
||||
auto root = parent / "widget";
|
||||
|
||||
auto r = cmd_build(root, true, false, overlay_path(parent));
|
||||
REQUIRE(r.has_value());
|
||||
|
||||
auto cmake_text = read_file(root / "build" / "CMakeLists.txt");
|
||||
REQUIRE(cmake_text.find("add_library(widget STATIC)") != std::string::npos);
|
||||
REQUIRE(cmake_text.find("../src/lib.cppm") != std::string::npos);
|
||||
REQUIRE(cmake_text.find("add_executable") == std::string::npos);
|
||||
}
|
||||
|
||||
TEST_CASE("cmd_build resolves a curated dep into find_package + targets",
|
||||
"[cli][build]") {
|
||||
auto parent = fresh_dir();
|
||||
REQUIRE(cmd_new("app", false, parent).has_value());
|
||||
auto root = parent / "app";
|
||||
add_dep(root, "fmt", "10.2.0");
|
||||
|
||||
auto r = cmd_build(root, true, false, overlay_path(parent));
|
||||
REQUIRE(r.has_value());
|
||||
|
||||
auto cmake_text = read_file(root / "build" / "CMakeLists.txt");
|
||||
REQUIRE(cmake_text.find("find_package(fmt CONFIG REQUIRED)") != std::string::npos);
|
||||
REQUIRE(cmake_text.find("fmt::fmt") != std::string::npos);
|
||||
|
||||
auto flake_text = read_file(root / "flake.nix");
|
||||
REQUIRE(flake_text.find("pkgs.fmt_10") != std::string::npos);
|
||||
}
|
||||
|
||||
TEST_CASE("cmd_build resolves a componentized dep", "[cli][build]") {
|
||||
auto parent = fresh_dir();
|
||||
REQUIRE(cmd_new("app", false, parent).has_value());
|
||||
auto root = parent / "app";
|
||||
add_dep(root, "boost", "1.84.0", {"filesystem", "system"});
|
||||
|
||||
auto r = cmd_build(root, true, false, overlay_path(parent));
|
||||
REQUIRE(r.has_value());
|
||||
|
||||
auto cmake_text = read_file(root / "build" / "CMakeLists.txt");
|
||||
REQUIRE(cmake_text.find("find_package(Boost REQUIRED COMPONENTS filesystem system)") !=
|
||||
std::string::npos);
|
||||
REQUIRE(cmake_text.find("Boost::filesystem") != std::string::npos);
|
||||
REQUIRE(cmake_text.find("Boost::system") != std::string::npos);
|
||||
}
|
||||
|
||||
TEST_CASE("cmd_build synthesizes a lockfile entry per dep", "[cli][build]") {
|
||||
auto parent = fresh_dir();
|
||||
REQUIRE(cmd_new("app", false, parent).has_value());
|
||||
auto root = parent / "app";
|
||||
add_dep(root, "fmt", "10.2.0");
|
||||
add_dep(root, "spdlog", "1.13.0");
|
||||
|
||||
auto r = cmd_build(root, true, false, overlay_path(parent));
|
||||
REQUIRE(r.has_value());
|
||||
|
||||
auto lock = lockfile::parse(root / "Cargoxx.lock");
|
||||
REQUIRE(lock.has_value());
|
||||
REQUIRE(lock->packages.size() == 3); // root + fmt + spdlog
|
||||
REQUIRE(lock->packages[0].name == "app");
|
||||
REQUIRE(lock->packages[0].dependencies.size() == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("cmd_build fails for an unknown dep", "[cli][build]") {
|
||||
auto parent = fresh_dir();
|
||||
REQUIRE(cmd_new("app", false, parent).has_value());
|
||||
auto root = parent / "app";
|
||||
add_dep(root, "obscurelib", "0.0.1");
|
||||
|
||||
auto r = cmd_build(root, true, false, overlay_path(parent));
|
||||
REQUIRE_FALSE(r.has_value());
|
||||
REQUIRE(r.error().code == ErrorCode::LinkdbUnknownPackage);
|
||||
}
|
||||
|
||||
TEST_CASE("cmd_build without --no-build returns NotImplemented", "[cli][build]") {
|
||||
auto parent = fresh_dir();
|
||||
REQUIRE(cmd_new("app", false, parent).has_value());
|
||||
auto root = parent / "app";
|
||||
|
||||
auto r = cmd_build(root, false, false, overlay_path(parent));
|
||||
REQUIRE_FALSE(r.has_value());
|
||||
REQUIRE(r.error().code == ErrorCode::NotImplemented);
|
||||
// Files are still generated before the build step would run
|
||||
REQUIRE(std::filesystem::exists(root / "flake.nix"));
|
||||
REQUIRE(std::filesystem::exists(root / "build" / "CMakeLists.txt"));
|
||||
}
|
||||
|
||||
TEST_CASE("cmd_build is idempotent — second run produces identical files",
|
||||
"[cli][build]") {
|
||||
auto parent = fresh_dir();
|
||||
REQUIRE(cmd_new("app", false, parent).has_value());
|
||||
auto root = parent / "app";
|
||||
add_dep(root, "fmt", "10.2.0");
|
||||
|
||||
REQUIRE(cmd_build(root, true, false, overlay_path(parent)).has_value());
|
||||
auto first_cmake = read_file(root / "build" / "CMakeLists.txt");
|
||||
auto first_flake = read_file(root / "flake.nix");
|
||||
auto first_lock = read_file(root / "Cargoxx.lock");
|
||||
|
||||
REQUIRE(cmd_build(root, true, false, overlay_path(parent)).has_value());
|
||||
REQUIRE(read_file(root / "build" / "CMakeLists.txt") == first_cmake);
|
||||
REQUIRE(read_file(root / "flake.nix") == first_flake);
|
||||
REQUIRE(read_file(root / "Cargoxx.lock") == first_lock);
|
||||
}
|
||||
Reference in New Issue
Block a user