From 1a9b7aa1c95740956603e711c46cfb208104411a Mon Sep 17 00:00:00 2001 From: Justus Adam Date: Wed, 26 Jul 2023 10:49:30 -0700 Subject: [PATCH] Debugging the testing setup (#21) This fixes the way dfpp is run for the testing setup. Now it instead manipulates the `PATH` passed to the `cargo dfpp` command by adding the directory in which cargo has stored `cargo-dfpp` and it also sets the directory for running `cargo dfpp` not my changing into it but by setting `current_dir` in `Command` --- tests/async_tests.rs | 12 +--- tests/call_chain_analysis_tests.rs | 16 +---- tests/control_flow_tests.rs | 9 +-- tests/external_annotation_tests.rs | 19 ++---- tests/forge_tests.rs | 12 +--- tests/helpers/mod.rs | 75 +++++++++------------ tests/inconsequential_call_removal_tests.rs | 15 ++--- tests/inline_elision_tests.rs | 13 +--- tests/inline_no_arg_closure_tests.rs | 13 +--- tests/new_alias_analysis_tests.rs | 9 +-- tests/non_transitive_graph_tests.rs | 13 +--- 11 files changed, 54 insertions(+), 152 deletions(-) diff --git a/tests/async_tests.rs b/tests/async_tests.rs index baa7135a18..897d84bfe7 100644 --- a/tests/async_tests.rs +++ b/tests/async_tests.rs @@ -9,17 +9,7 @@ use helpers::*; const CRATE_DIR: &str = "tests/async-tests"; lazy_static! { - static ref TEST_CRATE_ANALYZED: bool = *helpers::DFPP_INSTALLED - && with_current_directory(CRATE_DIR, || { - run_dfpp_with_graph_dump_and(["--drop-poll"]) - }) - .map_or_else( - |e| { - println!("io err {}", e); - false - }, - |t| t - ); + static ref TEST_CRATE_ANALYZED: bool = run_dfpp_with_graph_dump_and(CRATE_DIR, ["--drop-poll"]); } macro_rules! define_test { diff --git a/tests/call_chain_analysis_tests.rs b/tests/call_chain_analysis_tests.rs index ced5774fd5..c1f720983e 100644 --- a/tests/call_chain_analysis_tests.rs +++ b/tests/call_chain_analysis_tests.rs @@ -10,20 +10,8 @@ use helpers::*; const TEST_CRATE_NAME: &str = "tests/call-chain-analysis-tests"; lazy_static! { - static ref TEST_CRATE_ANALYZED: bool = { - let crate_dir: std::path::PathBuf = TEST_CRATE_NAME.to_string().into(); - *DFPP_INSTALLED - && cwd_and_use_rustc_in(crate_dir, || { - run_dfpp_with_flow_graph_dump_and(["--drop-clone"]) - }) - .map_or_else( - |e| { - println!("io err {}", e); - false - }, - |t| t, - ) - }; + static ref TEST_CRATE_ANALYZED: bool = + run_dfpp_with_flow_graph_dump_and(TEST_CRATE_NAME, ["--drop-clone"]); } macro_rules! define_test { diff --git a/tests/control_flow_tests.rs b/tests/control_flow_tests.rs index 3fccd613a9..4bd60ac798 100644 --- a/tests/control_flow_tests.rs +++ b/tests/control_flow_tests.rs @@ -9,14 +9,7 @@ use helpers::*; const CRATE_DIR: &str = "tests/control-flow-tests"; lazy_static! { - static ref TEST_CRATE_ANALYZED: bool = *helpers::DFPP_INSTALLED - && with_current_directory(CRATE_DIR, || { run_dfpp_with_graph_dump() }).map_or_else( - |e| { - println!("io err {}", e); - false - }, - |t| t - ); + static ref TEST_CRATE_ANALYZED: bool = run_dfpp_with_graph_dump(CRATE_DIR); } macro_rules! define_test { diff --git a/tests/external_annotation_tests.rs b/tests/external_annotation_tests.rs index 730da0dec7..61692224b6 100644 --- a/tests/external_annotation_tests.rs +++ b/tests/external_annotation_tests.rs @@ -6,21 +6,16 @@ mod helpers; use helpers::*; fn do_in_crate_dir A>(f: F) -> std::io::Result { - with_current_directory("tests/external-annotation-tests", f) + with_current_directory(CRATE_DIR, f) } +const CRATE_DIR: &str = "tests/external-annotation-tests"; + lazy_static! { - static ref TEST_CRATE_ANALYZED: bool = *helpers::DFPP_INSTALLED - && do_in_crate_dir(|| { - run_dfpp_with_graph_dump_and(["--external-annotations", "external-annotations.toml"]) - }) - .map_or_else( - |e| { - println!("io err {}", e); - false - }, - |t| t - ); + static ref TEST_CRATE_ANALYZED: bool = run_dfpp_with_graph_dump_and( + CRATE_DIR, + ["--external-annotations", "external-annotations.toml"] + ); } // This will create a forge file with the name "test_{test_name}.frg" // that test expects that running {property} for Flows is {result}. diff --git a/tests/forge_tests.rs b/tests/forge_tests.rs index aeb821b73c..9cb9da4a0e 100644 --- a/tests/forge_tests.rs +++ b/tests/forge_tests.rs @@ -13,17 +13,7 @@ fn do_in_crate_dir A>(f: F) -> std::i } lazy_static! { - static ref TEST_CRATE_ANALYZED: bool = { - let crate_dir: std::path::PathBuf = TEST_CRATE_NAME.to_string().into(); - *DFPP_INSTALLED - && cwd_and_use_rustc_in(crate_dir, run_dfpp_with_flow_graph_dump).map_or_else( - |e| { - println!("io err {}", e); - false - }, - |t| t, - ) - }; + static ref TEST_CRATE_ANALYZED: bool = run_dfpp_with_flow_graph_dump(TEST_CRATE_NAME); } // This will create a forge file with the name "test_{test_name}.frg" diff --git a/tests/helpers/mod.rs b/tests/helpers/mod.rs index 39f4dba0bf..4f6427bd30 100644 --- a/tests/helpers/mod.rs +++ b/tests/helpers/mod.rs @@ -17,10 +17,10 @@ use either::Either; use dfpp::utils::outfile_pls; use std::borrow::Cow; use std::io::prelude::*; +use std::path::Path; lazy_static! { pub static ref CWD_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(()); - pub static ref DFPP_INSTALLED: bool = install_dfpp(); } /// Run an action `F` in the directory `P`, ensuring that afterwards the @@ -42,26 +42,6 @@ pub fn with_current_directory< Ok(res) } -/// Run the action `F` in directory `P` and with rustc data structures -/// initialized (e.g. using [`Symbol`] works), taking care to lock the directory -/// change and resetting the working directory after. -/// -/// Be aware that any [`Symbol`] created in `F` will **not** compare equal to -/// [`Symbol`]s created after `F` and may cause dereference errors. -pub fn cwd_and_use_rustc_in< - P: AsRef, - A, - F: std::panic::UnwindSafe + FnOnce() -> A, ->( - p: P, - f: F, -) -> std::io::Result { - with_current_directory(p, || { - eprintln!("creating session"); - rustc_span::create_default_session_if_not_set_then(|_| f()) - }) -} - /// Initialize rustc data structures (e.g. [`Symbol`] works) and run `F` /// /// Be aware that any [`Symbol`] created in `F` will **not** compare equal to @@ -70,27 +50,36 @@ pub fn use_rustc A>(f: F) -> A { rustc_span::create_default_session_if_not_set_then(|_| f()) } -pub fn install_dfpp() -> bool { - std::process::Command::new("cargo") - .arg("install") - .arg("--locked") - .arg("--offline") - .arg("--path") - .arg(".") - .arg("--debug") - .status() - .unwrap() - .success() -} - /// Run dfpp in the current directory, passing the /// `--dump-serialized-non-transitive-graph` flag, which dumps a /// [`CallOnlyFlow`](dfpp::ir::flows::CallOnlyFlow) for each controller. /// /// The result is suitable for reading with /// [`read_non_transitive_graph_and_body`](dfpp::dbg::read_non_transitive_graph_and_body). -pub fn run_dfpp_with_graph_dump() -> bool { - run_dfpp_with_graph_dump_and::<_, &str>([]) +pub fn run_dfpp_with_graph_dump(dir: impl AsRef) -> bool { + run_dfpp_with_graph_dump_and::<_, &str>(dir, []) +} + +pub fn dfpp_command(dir: impl AsRef) -> std::process::Command { + let mut cmd = std::process::Command::new("cargo"); + let path = std::env::var("PATH").unwrap_or_else(|_| Default::default()); + // Cargo gives us the path where it wrote `cargo-dfpp` to + let cargo_dfpp_path = std::path::Path::new(env!("CARGO_BIN_EXE_cargo-dfpp")); + let mut new_path = + std::ffi::OsString::with_capacity(path.len() + cargo_dfpp_path.as_os_str().len() + 1); + // We then append the parent (e.g. its directory) to the search path. THat + // directory (we presume) contains both `dfpp` and `cargo-dfpp`. + new_path.push(cargo_dfpp_path.parent().unwrap_or_else(|| { + panic!( + "cargo-dfpp path {} had no parent", + cargo_dfpp_path.display() + ) + })); + new_path.push(":"); + new_path.push(path); + cmd.arg("dfpp").env("PATH", new_path).current_dir(dir); + eprintln!("Command is {cmd:?}"); + cmd } /// Run dfpp in the current directory, passing the @@ -101,13 +90,12 @@ pub fn run_dfpp_with_graph_dump() -> bool { /// [`read_non_transitive_graph_and_body`](dfpp::dbg::read_non_transitive_graph_and_body). /// /// Allows for additional arguments to be passed to dfpp -pub fn run_dfpp_with_graph_dump_and(extra: I) -> bool +pub fn run_dfpp_with_graph_dump_and(dir: impl AsRef, extra: I) -> bool where I: IntoIterator, S: AsRef, { - std::process::Command::new("cargo") - .arg("dfpp") + dfpp_command(dir) .arg("--abort-after-analysis") .arg("--dump-serialized-non-transitive-graph") .args(extra) @@ -120,8 +108,8 @@ where /// JSON. /// /// The result is suitable for reading with [`PreFrg::from_file_at`] -pub fn run_dfpp_with_flow_graph_dump() -> bool { - run_dfpp_with_flow_graph_dump_and::<_, &str>([]) +pub fn run_dfpp_with_flow_graph_dump(dir: impl AsRef) -> bool { + run_dfpp_with_flow_graph_dump_and::<_, &str>(dir, []) } /// Run dfpp in the current directory, passing the @@ -129,13 +117,12 @@ pub fn run_dfpp_with_flow_graph_dump() -> bool { /// JSON. /// /// The result is suitable for reading with [`PreFrg::from_file_at`] -pub fn run_dfpp_with_flow_graph_dump_and(extra: I) -> bool +pub fn run_dfpp_with_flow_graph_dump_and(dir: impl AsRef, extra: I) -> bool where I: IntoIterator, S: AsRef, { - std::process::Command::new("cargo") - .arg("dfpp") + dfpp_command(dir) .arg("--abort-after-analysis") .arg("--dump-serialized-flow-graph") .args(extra) diff --git a/tests/inconsequential_call_removal_tests.rs b/tests/inconsequential_call_removal_tests.rs index 14210ba870..a3c15e9d72 100644 --- a/tests/inconsequential_call_removal_tests.rs +++ b/tests/inconsequential_call_removal_tests.rs @@ -9,17 +9,10 @@ use helpers::*; const CRATE_DIR: &str = "tests/inconsequential-call-removal-tests"; lazy_static! { - static ref TEST_CRATE_ANALYZED: bool = *helpers::DFPP_INSTALLED - && with_current_directory(CRATE_DIR, || { - run_dfpp_with_graph_dump_and(["--remove-inconsequential-calls", "conservative"]) - }) - .map_or_else( - |e| { - println!("io err {}", e); - false - }, - |t| t - ); + static ref TEST_CRATE_ANALYZED: bool = run_dfpp_with_graph_dump_and( + CRATE_DIR, + ["--remove-inconsequential-calls", "conservative"] + ); } macro_rules! define_test { diff --git a/tests/inline_elision_tests.rs b/tests/inline_elision_tests.rs index 69a3654fd5..c3f16beab3 100644 --- a/tests/inline_elision_tests.rs +++ b/tests/inline_elision_tests.rs @@ -9,17 +9,8 @@ use helpers::*; const CRATE_DIR: &str = "tests/inline-elision-tests"; lazy_static! { - static ref TEST_CRATE_ANALYZED: bool = *helpers::DFPP_INSTALLED - && with_current_directory(CRATE_DIR, || { - run_dfpp_with_graph_dump_and(["--inline-elision"]) - }) - .map_or_else( - |e| { - println!("io err {}", e); - false - }, - |t| t - ); + static ref TEST_CRATE_ANALYZED: bool = + run_dfpp_with_graph_dump_and(CRATE_DIR, ["--inline-elision"]); } macro_rules! define_test { diff --git a/tests/inline_no_arg_closure_tests.rs b/tests/inline_no_arg_closure_tests.rs index 1b509e2b2b..11092d4e58 100644 --- a/tests/inline_no_arg_closure_tests.rs +++ b/tests/inline_no_arg_closure_tests.rs @@ -9,17 +9,8 @@ use helpers::*; const CRATE_DIR: &str = "tests/inline-no-arg-closure-tests"; lazy_static! { - static ref TEST_CRATE_ANALYZED: bool = *helpers::DFPP_INSTALLED - && with_current_directory(CRATE_DIR, || { - run_dfpp_with_graph_dump_and(["--inline-no-arg-closures"]) - }) - .map_or_else( - |e| { - println!("io err {}", e); - false - }, - |t| t - ); + static ref TEST_CRATE_ANALYZED: bool = + run_dfpp_with_graph_dump_and(CRATE_DIR, ["--inline-no-arg-closures"]); } macro_rules! define_test { diff --git a/tests/new_alias_analysis_tests.rs b/tests/new_alias_analysis_tests.rs index 47ead69405..9d099f912c 100644 --- a/tests/new_alias_analysis_tests.rs +++ b/tests/new_alias_analysis_tests.rs @@ -9,14 +9,7 @@ use helpers::*; const CRATE_DIR: &str = "tests/new-alias-analysis-tests"; lazy_static! { - static ref TEST_CRATE_ANALYZED: bool = *helpers::DFPP_INSTALLED - && with_current_directory(CRATE_DIR, || { run_dfpp_with_graph_dump() }).map_or_else( - |e| { - println!("io err {}", e); - false - }, - |t| t - ); + static ref TEST_CRATE_ANALYZED: bool = run_dfpp_with_graph_dump(CRATE_DIR); } macro_rules! define_test { diff --git a/tests/non_transitive_graph_tests.rs b/tests/non_transitive_graph_tests.rs index f92f93966c..a74f17ad5a 100644 --- a/tests/non_transitive_graph_tests.rs +++ b/tests/non_transitive_graph_tests.rs @@ -9,17 +9,8 @@ use helpers::*; const CRATE_DIR: &str = "tests/non-transitive-graph-tests"; lazy_static! { - static ref TEST_CRATE_ANALYZED: bool = *helpers::DFPP_INSTALLED - && with_current_directory(CRATE_DIR, || { - run_dfpp_with_graph_dump_and(["--no-cross-function-analysis"]) - }) - .map_or_else( - |e| { - println!("io err {}", e); - false - }, - |t| t - ); + static ref TEST_CRATE_ANALYZED: bool = + run_dfpp_with_graph_dump_and(CRATE_DIR, ["--no-cross-function-analysis"]); } macro_rules! define_test {