208 lines
7.8 KiB
C++
208 lines
7.8 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
|
|
import cargoxx.cli;
|
|
import cargoxx.linkdb;
|
|
import cargoxx.manifest;
|
|
import cargoxx.lockfile;
|
|
import cargoxx.util;
|
|
import std;
|
|
|
|
using cargoxx::cli::cmd_build;
|
|
using cargoxx::cli::cmd_new;
|
|
using cargoxx::linkdb::Database;
|
|
using cargoxx::linkdb::Recipe;
|
|
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());
|
|
}
|
|
|
|
auto seed_recipe(const std::filesystem::path& overlay, const std::string& package,
|
|
const std::string& version_range, const Recipe& r) {
|
|
auto db = Database::open(overlay);
|
|
REQUIRE(db.has_value());
|
|
REQUIRE(db->add_manual(package, version_range, r).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, std::nullopt, 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, std::nullopt, 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 manually-seeded 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");
|
|
seed_recipe(overlay_path(parent), "fmt", ">=10.0.0",
|
|
Recipe{
|
|
.nixpkgs_attr = "fmt_10",
|
|
.find_package = "fmt CONFIG REQUIRED",
|
|
.targets = {"fmt::fmt"},
|
|
.source = "manual",
|
|
});
|
|
|
|
auto r = cmd_build(root, true, false, std::nullopt, 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 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");
|
|
seed_recipe(overlay_path(parent), "fmt", ">=10.0.0",
|
|
Recipe{
|
|
.nixpkgs_attr = "fmt_10",
|
|
.find_package = "fmt CONFIG REQUIRED",
|
|
.targets = {"fmt::fmt"},
|
|
.source = "manual",
|
|
});
|
|
seed_recipe(overlay_path(parent), "spdlog", "*",
|
|
Recipe{
|
|
.nixpkgs_attr = "spdlog",
|
|
.find_package = "spdlog CONFIG REQUIRED",
|
|
.targets = {"spdlog::spdlog"},
|
|
.source = "manual",
|
|
});
|
|
|
|
auto r = cmd_build(root, true, false, std::nullopt, overlay_path(parent));
|
|
REQUIRE(r.has_value());
|
|
|
|
auto lock = lockfile::parse(root / "Cargoxx.lock");
|
|
REQUIRE(lock.has_value());
|
|
REQUIRE(lock->packages.size() == 3);
|
|
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, std::nullopt, overlay_path(parent));
|
|
REQUIRE_FALSE(r.has_value());
|
|
REQUIRE(r.error().code == ErrorCode::LinkdbUnknownPackage);
|
|
}
|
|
|
|
TEST_CASE("cmd_build --release writes a release-typed build/CMakeLists",
|
|
"[cli][build]") {
|
|
// The CMAKE_BUILD_TYPE flag itself is set on the cmake command line, not in
|
|
// the generated CMakeLists.txt — but the same generation runs for both
|
|
// profiles, so just verify the --release path with --no-build doesn't error.
|
|
auto parent = fresh_dir();
|
|
REQUIRE(cmd_new("app", false, parent).has_value());
|
|
auto root = parent / "app";
|
|
auto r = cmd_build(root, true, true, std::nullopt, overlay_path(parent));
|
|
REQUIRE(r.has_value());
|
|
}
|
|
|
|
TEST_CASE("cmd_build accepts a --target argument under --no-build",
|
|
"[cli][build]") {
|
|
auto parent = fresh_dir();
|
|
REQUIRE(cmd_new("app", false, parent).has_value());
|
|
auto root = parent / "app";
|
|
auto r = cmd_build(root, true, false, std::string{"app_bin"},
|
|
overlay_path(parent));
|
|
REQUIRE(r.has_value());
|
|
}
|
|
|
|
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");
|
|
seed_recipe(overlay_path(parent), "fmt", ">=10.0.0",
|
|
Recipe{
|
|
.nixpkgs_attr = "fmt_10",
|
|
.find_package = "fmt CONFIG REQUIRED",
|
|
.targets = {"fmt::fmt"},
|
|
.source = "manual",
|
|
});
|
|
|
|
REQUIRE(cmd_build(root, true, false, std::nullopt, 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, std::nullopt, 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);
|
|
}
|