Craft Anything: understand how any software works, and craft your own.
Rust
Test sessions find only the engines their settings name (#466)
Bently7bf82200066f
diff --git a/crates/engine/src/settings.rs b/crates/engine/src/settings.rsindex 23b868bb..dd0d1bac 100644--- a/crates/engine/src/settings.rs+++ b/crates/engine/src/settings.rs@@ -76,6 +76,11 @@ pub struct EngineSettings {pub store_dir: Option<PathBuf>,/// URL of a user-run JEB MCP server, e.g. `http://127.0.0.1:8425/mcp` (loopback, plain HTTP only).pub jeb_url: Option<String>,+ /// Find engines only where these settings and the managed store say, never on PATH or in the+ /// usual install folders. Never saved: test sessions set it so they don't find what this+ /// computer has installed ([`crate::testing::session`]).+ #[serde(skip)]+ pub only_configured: bool,}#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]@@ -377,7 +382,12 @@ impl Settings {/// Ghidra's headless launcher, when Deep mode can really use Ghidra (`None`: not installed).pub fn ghidra_launcher(&self) -> Option<PathBuf> {- craft_anything_engines::ghidra::get::headless(self.engines.ghidra.install_dir.as_deref(), &self.engine_store_dir())+ let (dir, store) = (self.engines.ghidra.install_dir.as_deref(), self.engine_store_dir());+ if self.engines.only_configured {+ craft_anything_engines::ghidra::get::headless_configured(dir, &store)+ } else {+ craft_anything_engines::ghidra::get::headless(dir, &store)+ }}/// The Java runtime Ghidra runs on (`None`: let Ghidra find its own).@@ -394,7 +404,11 @@ impl Settings {if id == craft_anything_engines::ghidra::get::ID {return self.ghidra_launcher();}- craft_anything_engines::locate(id, &self.engines.tools, &self.engine_store_dir())+ if self.engines.only_configured {+ craft_anything_engines::locate_configured(id, &self.engines.tools, &self.engine_store_dir())+ } else {+ craft_anything_engines::locate(id, &self.engines.tools, &self.engine_store_dir())+ }}}@@ -417,6 +431,39 @@ auto_mode = trueuse super::*;+ #[test]+ fn only_configured_finds_no_engine_from_this_computer_but_keeps_the_ones_set() {+ let dir = tempfile::tempdir().unwrap();+ let mut s = Settings::default();+ s.engines.store_dir = Some(dir.path().join("engines"));+ s.engines.only_configured = true;+ assert_eq!(s.ghidra_launcher(), None, "PATH and the usual install folders aren't searched");+ assert_eq!(s.engine_path("java"), None);+ // The folder the person picked still counts, and so does the managed store.+ let launcher_name = if cfg!(windows) { "analyzeHeadless.bat" } else { "analyzeHeadless" };+ let picked = dir.path().join("picked").join("ghidra_12_PUBLIC");+ std::fs::create_dir_all(picked.join("support")).unwrap();+ std::fs::write(picked.join("support").join(launcher_name), "").unwrap();+ s.engines.ghidra.install_dir = Some(dir.path().join("picked"));+ assert_eq!(s.ghidra_launcher(), Some(picked.join("support").join(launcher_name)));+ assert_eq!(s.engine_path("ghidra"), s.ghidra_launcher());+ s.engines.ghidra.install_dir = None;+ let managed = dir.path().join("engines").join("ghidra").join("ghidra_12_PUBLIC").join("support");+ std::fs::create_dir_all(&managed).unwrap();+ std::fs::write(managed.join(launcher_name), "").unwrap();+ assert_eq!(s.ghidra_launcher(), Some(managed.join(launcher_name)));+ // It is never written to config.toml, and a loaded config looks around the computer.+ let text = toml::to_string_pretty(&s).unwrap();+ assert!(!text.contains("only_configured"), "{text}");+ let loaded: Settings = toml::from_str(&text).unwrap();+ assert!(!loaded.engines.only_configured);+ }++ #[test]+ fn the_test_session_looks_only_where_its_settings_say() {+ assert!(crate::testing::session().settings.engines.only_configured);+ }+#[test]fn path_overrides_match_the_engine_table_exactly() {use craft_anything_engines::ToolPathLookup;diff --git a/crates/engine/src/testing.rs b/crates/engine/src/testing.rsindex 5d0048c6..b3ba6bff 100644--- a/crates/engine/src/testing.rs+++ b/crates/engine/src/testing.rs@@ -16,9 +16,12 @@ pub fn temp_dir() -> PathBuf {/// A session whose config, plugin caches, engine store and projects live in a fresh temp folder./// It is in Deep mode, so tools may look inside the sample targets tests open; a test of Clean room-/// turns `deep_mode` off.+/// turns `deep_mode` off. It finds no engine this computer has on PATH or in the usual install+/// folders (`engines.only_configured`), so a test behaves the same whether Ghidra or adb is+/// installed; a test that needs an engine points the settings or the store at one.pub fn session() -> Session {let mut s = Session::isolated(&temp_dir());s.settings.deep_mode = true;+ s.settings.engines.only_configured = true;s}diff --git a/crates/engines/src/ghidra/get.rs b/crates/engines/src/ghidra/get.rsindex 16c4df8b..e31956d6 100644--- a/crates/engines/src/ghidra/get.rs+++ b/crates/engines/src/ghidra/get.rs@@ -28,6 +28,14 @@ pub fn can_install() -> bool {/// Ghidra's headless launcher: in the folder the person picked (Ghidra's own folder, or one that/// holds it), then the managed install, then PATH, then where system packages put it.pub fn headless(install_dir: Option<&Path>, store: &Path) -> Option<PathBuf> {+ headless_configured(install_dir, store)+ .or_else(|| crate::find_on_path(&["analyzeHeadless.bat", "analyzeHeadless"]))+ .or_else(|| crate::well_known(ID).into_iter().find(|p| p.is_file()))+}++/// Ghidra's headless launcher in the folder the person picked or the managed install only: what+/// [`headless`] finds before it looks around the computer.+pub fn headless_configured(install_dir: Option<&Path>, store: &Path) -> Option<PathBuf> {if let Some(dir) = install_dir {if dir.is_file() {return Some(dir.to_path_buf());@@ -37,8 +45,6 @@ pub fn headless(install_dir: Option<&Path>, store: &Path) -> Option<PathBuf> {}}in_child(&store.join(ID), launcher)- .or_else(|| crate::find_on_path(&["analyzeHeadless.bat", "analyzeHeadless"]))- .or_else(|| crate::well_known(ID).into_iter().find(|p| p.is_file()))}/// The Java runtime to run Ghidra with: the one the person set, else the managed one. `None` letsdiff --git a/crates/engines/src/lib.rs b/crates/engines/src/lib.rsindex 9e393bb6..8d593fea 100644--- a/crates/engines/src/lib.rs+++ b/crates/engines/src/lib.rs@@ -47,6 +47,15 @@ pub fn display_name(id: &str) -> &'static str {/// Find an engine: user override, then the managed store (`<store>/<id>/bin/<exe>`), then PATH,/// then its standard install folders.pub fn locate(id: &str, overrides: &dyn ToolPathLookup, store: &Path) -> Option<PathBuf> {+ locate_configured(id, overrides, store).or_else(|| {+ let e = engine(id)?;+ find_on_path(e.host_exes()).or_else(|| e.known_install_paths().into_iter().find(|p| p.is_file()))+ })+}++/// Find an engine where the settings or the managed store say only: what [`locate`] finds before+/// it looks around the computer (PATH and standard install folders).+pub fn locate_configured(id: &str, overrides: &dyn ToolPathLookup, store: &Path) -> Option<PathBuf> {if let Some(p) = overrides.override_for(id)&& p.exists(){@@ -65,7 +74,7 @@ pub fn locate(id: &str, overrides: &dyn ToolPathLookup, store: &Path) -> Option<return Some(store.join(id).join(name));}}- find_on_path(exes).or_else(|| e.known_install_paths().into_iter().find(|p| p.is_file()))+ None}/// An executable in a managed install folder: `<dir>/<exe>`, `<dir>/bin/<exe>` ordiff --git a/docs/pitfalls.md b/docs/pitfalls.mdindex 77bd0ef8..0ee36fea 100644--- a/docs/pitfalls.md+++ b/docs/pitfalls.md@@ -71,6 +71,10 @@ before running anything, and run new tests on a machine without the program.It happened again when Clean room began refusing tools before their program is looked for: a"missing adb" test returned early on a machine with adb and failed in CI without it. A test ofwhat happens without a program builds its session in the mode the tool runs in.+It happened a third time with Ghidra: the test session's engine store was empty, but the lookup+still searched PATH, `~/.local/bin` and `/opt/ghidra`, so two Deep-mode tests got Ghidra's answer+on a machine that had it. `testing::session()` now sets `engines.only_configured`, so it finds only+the engines its settings or store name.**A failing crate hid the next crate's tests.** `cargo test -p a -p b` stopped after a failure in`a`, so `b`'s tests never ran; the rerun was filtered to the failing test, and a broken engine id