90 lines
2.7 KiB
C++
90 lines
2.7 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
|
|
import cargoxx.resolver;
|
|
import cargoxx.util;
|
|
import std;
|
|
|
|
using cargoxx::resolver::pc_scan;
|
|
using cargoxx::util::ErrorCode;
|
|
|
|
namespace {
|
|
|
|
auto fresh_store() -> std::filesystem::path {
|
|
auto d = std::filesystem::temp_directory_path() /
|
|
std::format("cargoxx-pc-scan-{}", std::random_device{}());
|
|
std::filesystem::create_directories(d / "lib" / "pkgconfig");
|
|
return d;
|
|
}
|
|
|
|
void touch_pc(const std::filesystem::path& store, std::string_view name,
|
|
std::string_view content =
|
|
"Name: x\nDescription: x\nLibs: -lx\n") {
|
|
std::ofstream{store / "lib" / "pkgconfig" / std::string{name}} << content;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("pc_scan picks the exact-name .pc when present",
|
|
"[resolver][pc_scan]") {
|
|
auto store = fresh_store();
|
|
touch_pc(store, "sqlite3.pc");
|
|
|
|
auto r = pc_scan(store, "sqlite3");
|
|
REQUIRE(r.has_value());
|
|
REQUIRE(r->pc_module == "sqlite3");
|
|
REQUIRE(r->pc_file.filename() == "sqlite3.pc");
|
|
}
|
|
|
|
TEST_CASE("pc_scan picks sqlite3.pc for nixpkgs name 'sqlite'",
|
|
"[resolver][pc_scan]") {
|
|
auto store = fresh_store();
|
|
touch_pc(store, "sqlite3.pc");
|
|
|
|
auto r = pc_scan(store, "sqlite");
|
|
REQUIRE(r.has_value());
|
|
REQUIRE(r->pc_module == "sqlite3");
|
|
}
|
|
|
|
TEST_CASE("pc_scan picks the best match among multiple .pc files",
|
|
"[resolver][pc_scan]") {
|
|
auto store = fresh_store();
|
|
touch_pc(store, "zlib.pc");
|
|
touch_pc(store, "sqlite3.pc");
|
|
touch_pc(store, "unrelated.pc");
|
|
|
|
auto r = pc_scan(store, "zlib");
|
|
REQUIRE(r.has_value());
|
|
REQUIRE(r->pc_module == "zlib");
|
|
}
|
|
|
|
TEST_CASE("pc_scan returns ResolutionUnknownPackage when nothing matches",
|
|
"[resolver][pc_scan]") {
|
|
auto store = fresh_store();
|
|
touch_pc(store, "totally-unrelated.pc");
|
|
|
|
auto r = pc_scan(store, "sqlite");
|
|
REQUIRE_FALSE(r.has_value());
|
|
REQUIRE(r.error().code == ErrorCode::ResolutionUnknownPackage);
|
|
}
|
|
|
|
TEST_CASE("pc_scan errors when lib/pkgconfig is missing",
|
|
"[resolver][pc_scan]") {
|
|
auto d = std::filesystem::temp_directory_path() /
|
|
std::format("cargoxx-pc-empty-{}", std::random_device{}());
|
|
std::filesystem::create_directories(d);
|
|
|
|
auto r = pc_scan(d, "sqlite");
|
|
REQUIRE_FALSE(r.has_value());
|
|
REQUIRE(r.error().code == ErrorCode::ResolutionUnknownPackage);
|
|
}
|
|
|
|
TEST_CASE("pc_scan skips .pc files that look like junk",
|
|
"[resolver][pc_scan]") {
|
|
auto store = fresh_store();
|
|
touch_pc(store, "sqlite3.pc", "this is not a real pc file\n");
|
|
|
|
auto r = pc_scan(store, "sqlite3");
|
|
REQUIRE_FALSE(r.has_value());
|
|
REQUIRE(r.error().code == ErrorCode::ResolutionUnknownPackage);
|
|
}
|