60 lines
1.6 KiB
C++
60 lines
1.6 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
|
|
import cargoxx.resolver;
|
|
import std;
|
|
|
|
using cargoxx::resolver::last_failure_dir;
|
|
|
|
namespace {
|
|
|
|
struct EnvScope {
|
|
EnvScope(const char* k, std::optional<std::string> v) : key(k) {
|
|
if (auto* prior = std::getenv(key)) {
|
|
previous = std::string{prior};
|
|
}
|
|
if (v) {
|
|
setenv(key, v->c_str(), 1);
|
|
} else {
|
|
unsetenv(key);
|
|
}
|
|
}
|
|
~EnvScope() {
|
|
if (previous) {
|
|
setenv(key, previous->c_str(), 1);
|
|
} else {
|
|
unsetenv(key);
|
|
}
|
|
}
|
|
const char* key;
|
|
std::optional<std::string> previous;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("last_failure_dir honors XDG_CACHE_HOME when set",
|
|
"[resolver][last_failure_dir]") {
|
|
EnvScope xdg{"XDG_CACHE_HOME", "/tmp/xdg-test"};
|
|
|
|
auto p = last_failure_dir("sqlite");
|
|
REQUIRE(p.string() == "/tmp/xdg-test/cargoxx/last-failure/sqlite");
|
|
}
|
|
|
|
TEST_CASE("last_failure_dir falls back to $HOME/.cache when XDG is unset",
|
|
"[resolver][last_failure_dir]") {
|
|
EnvScope xdg{"XDG_CACHE_HOME", std::nullopt};
|
|
EnvScope home{"HOME", "/tmp/home-test"};
|
|
|
|
auto p = last_failure_dir("fmt");
|
|
REQUIRE(p.string() == "/tmp/home-test/.cache/cargoxx/last-failure/fmt");
|
|
}
|
|
|
|
TEST_CASE("last_failure_dir uses cwd-based fallback when neither var is set",
|
|
"[resolver][last_failure_dir]") {
|
|
EnvScope xdg{"XDG_CACHE_HOME", std::nullopt};
|
|
EnvScope home{"HOME", std::nullopt};
|
|
|
|
auto p = last_failure_dir("obscure");
|
|
REQUIRE(p.filename() == "obscure");
|
|
REQUIRE(p.parent_path().filename() == ".cargoxx-last-failure");
|
|
}
|