Files
lemonade/test/cpp/support/test_state_isolation.cpp
f2208e49ed test: isolate every C++ test from the real config dir (#3612)
* test: isolate every C++ test from the real config dir

user_models.json and friends moved from the cache dir to the config dir in
#3028, but the tests that isolate themselves only via set_cache_dir() were not
updated. Since then, running the suite locally writes fixtures straight into
the developer's ~/.config/lemonade/user_models.json: one run of
test_model_manager_collection_validation registers 8 router/classifier models
there, and test_model_registry adds 2 more. CI never notices because a fresh
runner has no registry worth corrupting.

Rather than add the missing call to each test, make isolation automatic.
add_cpp_ci_test() is already the only supported way to declare a test, so it
now links lemonade-test-isolation into the test executable, which points both
dirs at a throwaway directory before main() runs. A test with its own layout to
exercise (test_config_dir_migration) still overrides it by calling the setters
itself, so there is no opt-out list to maintain.

Drops the per-test temp-dir scaffolding that existed only for this, and roots
the fixture dirs that are still needed at the isolated state dir.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* fix(cmake): guard the test isolation library the way every test block does

Two defects in the previous commit, both caught by CI:

The .dockerignore excludes test/ entirely, so the Docker build configures a
tree with no test sources at all. Every test block already guards on
EXISTS "${_FOO_TEST_SRC}" for that reason; add_library() for the isolation TU
did not, and failed the configure with "Cannot find source file". Guard it the
same way. A tree that carries tests but not the isolation source is a
contradiction rather than a case to tolerate, so add_cpp_ci_test() now fails
loudly instead of silently linking nothing.

Only 52 of the 81 test executables link lemonade-server-core, and
get_config_dir() lives there. Linking the isolation TU into the other 29 left
set_cache_dir/set_config_dir undefined at link time. Those tests also cannot
reach the real config dir at all, so that link condition is the exact set that
needs redirecting, not an approximation.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* test: harden C++ state isolation

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Florian Reinle <[email protected]>
2026-10-14 12:53:47 +00:00

95 lines
2.9 KiB
C++

#include "test_state_isolation.h"
#include "lemon/utils/path_utils.h"
#include <atomic>
#include <chrono>
#include <stdexcept>
#include <system_error>
namespace fs = std::filesystem;
namespace lemon {
namespace test {
namespace {
fs::path claim_state_dir() {
const auto stamp = std::chrono::steady_clock::now().time_since_epoch().count();
// create_directory() reports whether this process created the directory, so
// a losing attempt retries instead of sharing a root with a test binary
// ctest started in parallel.
for (int attempt = 0; attempt < 64; ++attempt) {
fs::path dir = fs::temp_directory_path() /
("lemonade-test-" + std::to_string(stamp) + "-" +
std::to_string(attempt));
std::error_code ec;
if (fs::create_directory(dir, ec)) {
return dir;
}
if (ec && !fs::exists(dir)) {
throw std::runtime_error("could not create a test state dir: " + ec.message());
}
}
throw std::runtime_error("could not claim a unique test state dir");
}
void install_sandbox_environment(const fs::path& root) {
const fs::path home = root / "home";
fs::create_directories(home);
const std::string home_utf8 = utils::path_to_utf8(home);
utils::set_environment_variable_utf8("HOME", home_utf8);
utils::set_environment_variable_utf8("USERPROFILE", home_utf8);
// Keep higher-priority overrides logically unset so tests can exercise
// XDG/systemd precedence without inheriting the developer's environment.
utils::unset_environment_variable_utf8("LEMONADE_CACHE_DIR");
utils::unset_environment_variable_utf8("CACHE_DIRECTORY");
utils::unset_environment_variable_utf8("STATE_DIRECTORY");
utils::unset_environment_variable_utf8("XDG_CACHE_HOME");
utils::unset_environment_variable_utf8("XDG_CONFIG_HOME");
}
struct Isolation {
fs::path dir;
Isolation() : dir(claim_state_dir()) {
install_sandbox_environment(dir);
const std::string dir_utf8 = utils::path_to_utf8(dir);
utils::set_cache_dir(dir_utf8);
utils::set_config_dir(dir_utf8);
}
~Isolation() {
std::error_code ec;
fs::remove_all(dir, ec);
}
};
Isolation& isolation() {
static Isolation instance;
return instance;
}
// Constructed before main(), so a test with its own directory layout to exercise
// (test_config_dir_migration) overrides this simply by calling the setters
// itself, with no opt-out list to keep in sync.
const bool installed = (isolation(), true);
} // namespace
const fs::path& state_dir() {
(void)installed;
return isolation().dir;
}
fs::path make_scratch_dir(const std::string& prefix) {
static std::atomic<unsigned> counter{0};
fs::path dir = state_dir() / (prefix + "-" + std::to_string(counter++));
fs::create_directories(dir);
return dir;
}
} // namespace test
} // namespace lemon