Files
lemonade/test/cpp/test_state_isolation_guard.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

57 lines
1.6 KiB
C++

#include "support/test_state_isolation.h"
#include "lemon/utils/path_utils.h"
#include <filesystem>
#include <iostream>
#include <string>
namespace fs = std::filesystem;
namespace {
bool is_within(const fs::path& child, const fs::path& root) {
if (child == root) {
return true;
}
const fs::path relative = child.lexically_relative(root);
if (relative.empty()) {
return false;
}
const auto first = relative.begin();
return first != relative.end() && *first != "..";
}
bool check_inside(const char* label, const std::string& value, const fs::path& root) {
const fs::path path = lemon::utils::path_from_utf8(value).lexically_normal();
if (is_within(path, root.lexically_normal())) {
return true;
}
std::cerr << label << " escaped test sandbox: " << value << '\n';
return false;
}
} // namespace
int main() {
const fs::path root = lemon::test::state_dir();
bool ok = true;
ok &= check_inside("cache", lemon::utils::get_cache_dir(), root);
ok &= check_inside("config", lemon::utils::get_config_dir(), root);
// Tests such as config-dir migration intentionally exercise default path
// resolution. Clearing an override must not expose the developer's real dirs.
lemon::utils::set_cache_dir("");
lemon::utils::set_config_dir("");
ok &= check_inside("cache fallback", lemon::utils::get_cache_dir(), root);
ok &= check_inside("config fallback", lemon::utils::get_config_dir(), root);
if (!ok) {
return 1;
}
std::cout << "state isolation guard passed\n";
return 0;
}