[M5+] add resolver::nixpkgs_probe (nix eval wrapper)

This commit is contained in:
2026-05-10 09:52:06 +00:00
parent f3d18b7939
commit 1c7ff39f64
7 changed files with 239 additions and 0 deletions

View File

@@ -24,3 +24,5 @@ cargoxx_add_test(cmd_run)
cargoxx_add_test(cmd_clean)
cargoxx_add_test(cmd_add)
cargoxx_add_test(cmd_remove)
cargoxx_add_test(nixpkgs_probe_parse)
cargoxx_add_test(nixpkgs_probe_live)

View File

@@ -0,0 +1,40 @@
// Network/nix-eval-gated integration test for resolver::nixpkgs_probe.
// Skipped unless CARGOXX_NETWORK_TESTS=1 is set in the environment.
#include <catch2/catch_test_macros.hpp>
import cargoxx.resolver;
import cargoxx.util;
import std;
namespace {
auto network_tests_enabled() -> bool {
auto* env = std::getenv("CARGOXX_NETWORK_TESTS");
return env != nullptr && std::string_view{env} == "1";
}
} // namespace
TEST_CASE("nixpkgs_probe finds 'hello'", "[resolver][network]") {
if (!network_tests_enabled()) {
SKIP("CARGOXX_NETWORK_TESTS != 1");
}
auto r = cargoxx::resolver::nixpkgs_probe("hello");
REQUIRE(r.has_value());
REQUIRE(r->attr == "hello");
REQUIRE_FALSE(r->out_path.empty());
REQUIRE(r->out_path.starts_with("/nix/store/"));
// GNU hello has a stable, conventional version field.
REQUIRE_FALSE(r->version.empty());
}
TEST_CASE("nixpkgs_probe rejects an unknown attribute", "[resolver][network]") {
if (!network_tests_enabled()) {
SKIP("CARGOXX_NETWORK_TESTS != 1");
}
auto r = cargoxx::resolver::nixpkgs_probe(
"definitely_not_a_real_pkg_cargoxx_xyzzy");
REQUIRE_FALSE(r.has_value());
REQUIRE(r.error().code == cargoxx::util::ErrorCode::ResolutionUnknownPackage);
}

View File

@@ -0,0 +1,55 @@
#include <catch2/catch_test_macros.hpp>
import cargoxx.resolver;
import cargoxx.util;
import std;
using cargoxx::resolver::parse_nix_eval_json;
using cargoxx::util::ErrorCode;
TEST_CASE("parse_nix_eval_json extracts version and outPath", "[resolver][nixpkgs]") {
constexpr std::string_view input = R"({
"version": "3.7.0",
"path": "/nix/store/abc-simdjson-3.7.0"
})";
auto r = parse_nix_eval_json("simdjson", input);
REQUIRE(r.has_value());
REQUIRE(r->attr == "simdjson");
REQUIRE(r->version == "3.7.0");
REQUIRE(r->out_path == "/nix/store/abc-simdjson-3.7.0");
}
TEST_CASE("parse_nix_eval_json accepts an empty version field",
"[resolver][nixpkgs]") {
constexpr std::string_view input = R"({
"version": "",
"path": "/nix/store/xyz-foo"
})";
auto r = parse_nix_eval_json("foo", input);
REQUIRE(r.has_value());
REQUIRE(r->version.empty());
REQUIRE(r->out_path == "/nix/store/xyz-foo");
}
TEST_CASE("parse_nix_eval_json fails when outPath is missing",
"[resolver][nixpkgs]") {
constexpr std::string_view input = R"({"version": "1.0"})";
auto r = parse_nix_eval_json("foo", input);
REQUIRE_FALSE(r.has_value());
REQUIRE(r.error().code == ErrorCode::ResolutionNetworkError);
}
TEST_CASE("parse_nix_eval_json fails on garbage input", "[resolver][nixpkgs]") {
auto r = parse_nix_eval_json("foo", "not-json-at-all");
REQUIRE_FALSE(r.has_value());
REQUIRE(r.error().code == ErrorCode::ResolutionNetworkError);
}
TEST_CASE("parse_nix_eval_json fails on a non-object root",
"[resolver][nixpkgs]") {
auto r = parse_nix_eval_json("foo", "[]");
REQUIRE_FALSE(r.has_value());
REQUIRE(r.error().code == ErrorCode::ResolutionNetworkError);
}