From bfb96bf67d8f79a4372a7c94ed29d3caa30e0c1c Mon Sep 17 00:00:00 2001 From: xermicus Date: Sun, 23 Mar 2025 15:36:49 +0100 Subject: [PATCH] inject workdir Signed-off-by: xermicus --- Cargo.lock | 8 ++++++++ Cargo.toml | 1 + crates/config/src/lib.rs | 15 +++++---------- crates/core/Cargo.toml | 1 + crates/core/src/main.rs | 9 ++++++++- crates/node/Cargo.toml | 3 +++ crates/node/src/geth.rs | 20 ++++++++++++++++---- 7 files changed, 42 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ecc2e94..a8c93a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2886,6 +2886,7 @@ dependencies = [ "semver 1.0.26", "serde", "serde_json", + "temp-dir", ] [[package]] @@ -2909,6 +2910,7 @@ dependencies = [ "revive-dt-config", "revive-dt-node-interaction", "serde_json", + "temp-dir", ] [[package]] @@ -3527,6 +3529,12 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" +[[package]] +name = "temp-dir" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc1ee6eef34f12f765cb94725905c6312b6610ab2b0940889cfe58dae7bc3c72" + [[package]] name = "tempfile" version = "3.19.1" diff --git a/Cargo.toml b/Cargo.toml index f6e43a0..27d502a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ semver = { version = "1.0", features = ["serde"] } serde = { version = "1.0", default-features = false, features = ["derive"] } serde_json = { version = "1.0", default-features = false, features = ["arbitrary_precision", "std"] } sha2 = { version = "0.10.8" } +temp-dir = { version = "0.1.14" } tokio = { version = "1", default-features = false, features = ["rt-multi-thread"] } # revive compiler diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index 4adf45d..b604c50 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -1,6 +1,6 @@ //! The global configuration used accross all revive differential testing crates. -use std::{env, path::PathBuf}; +use std::path::PathBuf; use clap::Parser; @@ -18,8 +18,10 @@ pub struct Arguments { pub corpus: Vec, /// A place to store temporary artifacts during test execution. - #[arg(long = "workdir", short, default_value_t = cwd())] - pub working_directory: String, + /// + /// Creates a temporary dir if not specified. + #[arg(long = "workdir", short)] + pub working_directory: Option, /// The path to the `geth` executable. /// @@ -40,13 +42,6 @@ pub struct Arguments { pub genesis_file: Option, } -fn cwd() -> String { - env::current_dir() - .expect("should be able to access current woring directory") - .to_string_lossy() - .to_string() -} - impl Default for Arguments { fn default() -> Self { Arguments::parse_from(["retester"]) diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index b35d2be..a75ed83 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -27,3 +27,4 @@ rayon = { workspace = true } semver = { workspace = true } serde = { workspace = true, features = [ "derive" ] } serde_json = { workspace = true } +temp-dir = { workspace = true } diff --git a/crates/core/src/main.rs b/crates/core/src/main.rs index 26196ba..e289f4e 100644 --- a/crates/core/src/main.rs +++ b/crates/core/src/main.rs @@ -6,15 +6,22 @@ use rayon::prelude::*; use revive_dt_config::*; use revive_dt_core::driver::compiler::build_evm; use revive_dt_format::corpus::Corpus; +use temp_dir::TempDir; fn main() -> anyhow::Result<()> { env_logger::init(); - let config = Arguments::parse(); + let mut config = Arguments::parse(); + if config.corpus.is_empty() { anyhow::bail!("no test corpus specified"); } + let temporary_directory = TempDir::new()?; + config + .working_directory + .get_or_insert_with(|| temporary_directory.path().into()); + for path in config.corpus.iter().collect::>() { log::trace!("attempting corpus {path:?}"); let corpus = Corpus::try_from_path(path)?; diff --git a/crates/node/Cargo.toml b/crates/node/Cargo.toml index 67c509d..23edde0 100644 --- a/crates/node/Cargo.toml +++ b/crates/node/Cargo.toml @@ -15,3 +15,6 @@ serde_json = { workspace = true } revive-dt-node-interaction = { workspace = true } revive-dt-config = { workspace = true } + +[dev-dependencies] +temp-dir = { workspace = true } diff --git a/crates/node/src/geth.rs b/crates/node/src/geth.rs index 5ad9652..4866d29 100644 --- a/crates/node/src/geth.rs +++ b/crates/node/src/geth.rs @@ -54,7 +54,11 @@ impl Instance { /// Create a new uninitialized instance. pub fn new(config: &Arguments) -> anyhow::Result { - let geth_directory = PathBuf::from(&config.working_directory).join(Self::BASE_DIRECTORY); + let geth_directory = config + .working_directory + .as_ref() + .ok_or_else(|| anyhow::anyhow!("config did not provide working directory"))? + .join(Self::BASE_DIRECTORY); let id = NODE_COUNT.fetch_add(1, Ordering::SeqCst); let base_directory = geth_directory.join(id.to_string()); @@ -218,16 +222,24 @@ impl Drop for Instance { #[cfg(test)] mod tests { - use revive_dt_config::Arguments; + use temp_dir::TempDir; use crate::{GENESIS_JSON, Node}; use super::Instance; + fn test_config() -> (Arguments, TempDir) { + let mut config = Arguments::default(); + let temp_dir = TempDir::new().unwrap(); + config.working_directory = temp_dir.path().to_path_buf().into(); + + (config, temp_dir) + } + #[test] fn init_works() { - Instance::new(&Arguments::default()) + Instance::new(&test_config().0) .unwrap() .init(GENESIS_JSON.to_string()) .unwrap(); @@ -235,7 +247,7 @@ mod tests { #[test] fn spawn_works() { - Instance::new(&Arguments::default()) + Instance::new(&test_config().0) .unwrap() .spawn(GENESIS_JSON.to_string()) .unwrap();