Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugging the testing setup #21

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions tests/async_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 2 additions & 14 deletions tests/call_chain_analysis_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 1 addition & 8 deletions tests/control_flow_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 7 additions & 12 deletions tests/external_annotation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@ mod helpers;
use helpers::*;

fn do_in_crate_dir<A, F: std::panic::UnwindSafe + FnOnce() -> A>(f: F) -> std::io::Result<A> {
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}.
Expand Down
12 changes: 1 addition & 11 deletions tests/forge_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@ fn do_in_crate_dir<A, F: std::panic::UnwindSafe + FnOnce() -> 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"
Expand Down
75 changes: 31 additions & 44 deletions tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<std::path::Path>,
A,
F: std::panic::UnwindSafe + FnOnce() -> A,
>(
p: P,
f: F,
) -> std::io::Result<A> {
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
Expand All @@ -70,27 +50,36 @@ pub fn use_rustc<A, F: FnOnce() -> 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<Path>) -> bool {
run_dfpp_with_graph_dump_and::<_, &str>(dir, [])
}

pub fn dfpp_command(dir: impl AsRef<Path>) -> 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
Expand All @@ -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<I, S>(extra: I) -> bool
pub fn run_dfpp_with_graph_dump_and<I, S>(dir: impl AsRef<Path>, extra: I) -> bool
where
I: IntoIterator<Item = S>,
S: AsRef<std::ffi::OsStr>,
{
std::process::Command::new("cargo")
.arg("dfpp")
dfpp_command(dir)
.arg("--abort-after-analysis")
.arg("--dump-serialized-non-transitive-graph")
.args(extra)
Expand All @@ -120,22 +108,21 @@ 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<Path>) -> bool {
run_dfpp_with_flow_graph_dump_and::<_, &str>(dir, [])
}

/// Run dfpp in the current directory, passing the
/// `--dump-serialized-flow-graph` which dumps the [`ProgramDescription`] as
/// JSON.
///
/// The result is suitable for reading with [`PreFrg::from_file_at`]
pub fn run_dfpp_with_flow_graph_dump_and<I, S>(extra: I) -> bool
pub fn run_dfpp_with_flow_graph_dump_and<I, S>(dir: impl AsRef<Path>, extra: I) -> bool
where
I: IntoIterator<Item = S>,
S: AsRef<std::ffi::OsStr>,
{
std::process::Command::new("cargo")
.arg("dfpp")
dfpp_command(dir)
.arg("--abort-after-analysis")
.arg("--dump-serialized-flow-graph")
.args(extra)
Expand Down
15 changes: 4 additions & 11 deletions tests/inconsequential_call_removal_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 2 additions & 11 deletions tests/inline_elision_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 2 additions & 11 deletions tests/inline_no_arg_closure_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 1 addition & 8 deletions tests/new_alias_analysis_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 2 additions & 11 deletions tests/non_transitive_graph_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading