mind-body forge probe: a root from the environment, and the test seam in the library build

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-10 19:19:14 +02:00
commit 5f38ac74be
2 changed files with 71 additions and 1 deletions

View file

@ -263,6 +263,19 @@ pub fn load_state(source: &impl ControlSource) -> Result<ControlState> {
/// let _ = mind_body::control::Grant::for_test;
/// ```
///
/// ```compile_fail
/// // the test seam that moves the root is not in the library build, public or not
/// use mind_body::control::with_root;
/// ```
///
/// ```compile_fail
/// use mind_body::control::TEST_ROOT;
/// ```
///
/// ```compile_fail
/// use mind_body::control::control_root;
/// ```
///
/// The same preamble with the real call compiles:
///
/// ```

View file

@ -1,7 +1,8 @@
//! The brake-grant rule seen from outside the crate: the only reader takes the
//! instance, so whatever a caller builds elsewhere (a store in a temporary
//! directory, a symlink to another instance's directory, a copy of another
//! instance's store, the relative root `.`) is never what a Grant is made from.
//! instance's store, the relative root `.`, a tree that every environment
//! variable points at) is never what a Grant is made from.
//! The signature half of the rule (no root argument, no source reader, no
//! struct literal, no test constructor) is the `compile_fail` examples on
//! `read_effective`; this is the behavioural half, committed because it is the
@ -63,4 +64,60 @@ fn no_forged_store_yields_a_grant() {
let relative = reads(FORGED);
std::env::set_current_dir(before).unwrap();
assert!(matches!(relative, Effective::Held { .. }), "relative root");
// A root named by the environment. The forged tree is laid out for a value
// used as the root itself and for one with `minds` joined on, and every
// variable the process has, with the names an implementation would likely
// read, is pointed at it. A variable of a name neither list holds is not
// reached; the library build having no `std::env` reader is the rest of it.
let environed = tempfile::tempdir().unwrap();
release(environed.path(), FORGED);
std::fs::create_dir_all(environed.path().join("minds")).unwrap();
release(&environed.path().join("minds"), FORGED);
let value = environed.path().as_os_str().to_owned();
let mut names: Vec<std::ffi::OsString> = std::env::vars_os().map(|(name, _)| name).collect();
names.extend(GUESSED_ROOT_VARIABLES.iter().map(Into::into));
let saved: Vec<_> = names.iter().map(|name| (name.clone(), std::env::var_os(name))).collect();
// SAFETY: this file's one test is the only thread touching the environment.
unsafe {
for name in &names {
std::env::set_var(name, &value);
}
}
let through_environment = reads(FORGED);
unsafe {
for (name, before) in saved {
match before {
Some(before) => std::env::set_var(name, before),
None => std::env::remove_var(name),
}
}
}
assert!(matches!(through_environment, Effective::Held { .. }), "root from an environment variable");
}
/// Names a caller-chosen root would plausibly be read from.
const GUESSED_ROOT_VARIABLES: [&str; 22] = [
"MIND_CONTROL_ROOT",
"MIND_BODY_ROOT",
"MIND_ROOT",
"MINDS_ROOT",
"MINDS_DIR",
"CONTROL_ROOT",
"CONTROL_DIR",
"GAMECULT_MINDS",
"GAMECULT_ROOT",
"HUGINN_ROOT",
"HUGINN_HOME",
"EUREKA_ROOT",
"EUREKA_HOME",
"EUREKA_INSTANCE_ROOT",
"STATE_DIRECTORY",
"CONFIGURATION_DIRECTORY",
"RUNTIME_DIRECTORY",
"XDG_CONFIG_HOME",
"XDG_STATE_HOME",
"XDG_DATA_HOME",
"HOME",
"TMPDIR",
];