From 6c5405ccf836ced04461c0fab0fbe0c1b7a0c118 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Mon, 18 Oct 2021 16:59:17 +0100 Subject: [PATCH] [tests] Get the run_* functions to log what they're doing. --- tests/cache_check.rs | 2 +- tests/cache_dump.rs | 2 +- tests/cache_repair.rs | 2 +- tests/cache_restore.rs | 2 +- tests/common/process.rs | 79 +++++++++++++++++++++++++++++++---- tests/common/program.rs | 3 +- tests/common/target.rs | 63 ++++++++++++++++------------ tests/common/test_dir.rs | 6 +++ tests/thin_check.rs | 13 +++++- tests/thin_delta.rs | 2 +- tests/thin_dump.rs | 2 +- tests/thin_metadata_pack.rs | 2 +- tests/thin_metadata_unpack.rs | 2 +- tests/thin_repair.rs | 2 +- tests/thin_restore.rs | 2 +- tests/thin_rmap.rs | 2 +- 16 files changed, 137 insertions(+), 49 deletions(-) diff --git a/tests/cache_check.rs b/tests/cache_check.rs index 45c1995..b3e9c9a 100644 --- a/tests/cache_check.rs +++ b/tests/cache_check.rs @@ -41,7 +41,7 @@ impl<'a> Program<'a> for CacheCheck { "cache_check" } - fn cmd(args: I) -> duct::Expression + fn cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, diff --git a/tests/cache_dump.rs b/tests/cache_dump.rs index 63c5933..7cea5b0 100644 --- a/tests/cache_dump.rs +++ b/tests/cache_dump.rs @@ -41,7 +41,7 @@ impl<'a> Program<'a> for CacheDump { "cache_dump" } - fn cmd(args: I) -> duct::Expression + fn cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, diff --git a/tests/cache_repair.rs b/tests/cache_repair.rs index 95c08cc..524b376 100644 --- a/tests/cache_repair.rs +++ b/tests/cache_repair.rs @@ -37,7 +37,7 @@ impl<'a> Program<'a> for CacheRepair { "cache_repair" } - fn cmd(args: I) -> duct::Expression + fn cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, diff --git a/tests/cache_restore.rs b/tests/cache_restore.rs index 4eaa51c..aebc6d1 100644 --- a/tests/cache_restore.rs +++ b/tests/cache_restore.rs @@ -39,7 +39,7 @@ impl<'a> Program<'a> for CacheRestore { "thin_restore" } - fn cmd(args: I) -> duct::Expression + fn cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, diff --git a/tests/common/process.rs b/tests/common/process.rs index 2d74d1c..0338c60 100644 --- a/tests/common/process.rs +++ b/tests/common/process.rs @@ -1,4 +1,7 @@ use anyhow::Result; +use std::ffi::OsString; +use std::fmt; +use std::process; //------------------------------------------ @@ -13,11 +16,65 @@ macro_rules! args { }; } +/// Holds a set of arguments for a shell command +#[derive(Debug)] +pub struct Command { + program: OsString, + args: Vec, +} + +#[macro_export] +macro_rules! cmd { + ( $program:expr $(, $arg:expr )* $(,)? ) => { + { + // use std::ffi::OsString; + let args: &[OsString] = &[$( Into::::into($arg) ),*]; + Command::new($program, args) + } + }; +} + +impl Command { + pub fn new(program: OsString, args: Vec) -> Self { + Command { program, args } + } + + fn to_expr(&self) -> duct::Expression { + duct::cmd(&self.program, &self.args) + } +} + +impl fmt::Display for Command { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.program.clone().into_string().unwrap())?; + for a in &self.args { + write!(f, " {}", a.clone().into_string().unwrap())?; + } + Ok(()) + } +} + +fn log_output(output: &process::Output) { + use std::str::from_utf8; + + if output.stdout.len() > 0 { + eprintln!("stdout: {:?}", from_utf8(&output.stdout)); + } + if output.stderr.len() > 0 { + eprintln!("stderr: {:?}", from_utf8(&output.stderr)); + } +} + // Returns stdout. The command must return zero. -pub fn run_ok(command: duct::Expression) -> Result { - let command = command.stdout_capture().stderr_capture(); +pub fn run_ok(command: Command) -> Result { + eprintln!("run_ok: {}", command); + + let command = command.to_expr().stdout_capture().stderr_capture(); let output = command.run()?; + + log_output(&output); assert!(output.status.success()); + let stdout = std::str::from_utf8(&output.stdout[..]) .unwrap() .trim_end_matches(|c| c == '\n' || c == '\r') @@ -27,26 +84,32 @@ pub fn run_ok(command: duct::Expression) -> Result { } // Returns the entire output. The command must return zero. -pub fn run_ok_raw(command: duct::Expression) -> Result { - let command = command.stdout_capture().stderr_capture(); +pub fn run_ok_raw(command: Command) -> Result { + eprintln!("run_ok_raw: {}", command); + let command = command.to_expr().stdout_capture().stderr_capture(); let output = command.run()?; + log_output(&output); assert!(output.status.success()); Ok(output) } // Returns stderr, a non zero status must be returned -pub fn run_fail(command: duct::Expression) -> Result { - let command = command.stdout_capture().stderr_capture(); +pub fn run_fail(command: Command) -> Result { + eprintln!("run_fail: {}", command); + let command = command.to_expr().stdout_capture().stderr_capture(); let output = command.unchecked().run()?; + log_output(&output); assert!(!output.status.success()); let stderr = std::str::from_utf8(&output.stderr[..]).unwrap().to_string(); Ok(stderr) } // Returns the entire output, a non zero status must be returned -pub fn run_fail_raw(command: duct::Expression) -> Result { - let command = command.stdout_capture().stderr_capture(); +pub fn run_fail_raw(command: Command) -> Result { + eprintln!("run_fail_raw: {}", command); + let command = command.to_expr().stdout_capture().stderr_capture(); let output = command.unchecked().run()?; + log_output(&output); assert!(!output.status.success()); Ok(output) } diff --git a/tests/common/program.rs b/tests/common/program.rs index fd15639..f006202 100644 --- a/tests/common/program.rs +++ b/tests/common/program.rs @@ -1,6 +1,7 @@ use anyhow::Result; use std::path::PathBuf; +pub use crate::common::process::*; use crate::common::test_dir::TestDir; //------------------------------------------ @@ -12,7 +13,7 @@ pub enum ArgType { pub trait Program<'a> { fn name() -> &'a str; - fn cmd(args: I) -> duct::Expression + fn cmd(args: I) -> Command where I: IntoIterator, I::Item: Into; diff --git a/tests/common/target.rs b/tests/common/target.rs index 965f94e..6a91930 100644 --- a/tests/common/target.rs +++ b/tests/common/target.rs @@ -1,20 +1,28 @@ use std::ffi::OsString; use std::path::PathBuf; +use crate::common::process::*; + //------------------------------------------ -pub fn cpp_cmd(cmd: S, args: I) -> duct::Expression +pub fn cpp_cmd(cmd: S, args: I) -> Command where S: Into, I: IntoIterator, I::Item: Into, { - let mut bin = PathBuf::from("./bin"); + let mut bin = PathBuf::from("bin"); bin.push(Into::::into(cmd)); - duct::cmd(bin.as_path(), args) + + let mut args_ = Vec::new(); + for a in args { + args_.push(Into::::into(a)); + } + + Command::new(Into::::into(bin.as_path()), args_) } -pub fn rust_cmd(cmd: S, args: I) -> duct::Expression +pub fn rust_cmd(cmd: S, args: I) -> Command where S: Into, I: IntoIterator, @@ -27,10 +35,10 @@ where all_args.push(Into::::into(a)); } - duct::cmd(RUST_PATH, &all_args) + Command::new(Into::::into(RUST_PATH), all_args) } -pub fn thin_check_cmd(args: I) -> duct::Expression +pub fn thin_check_cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, @@ -38,7 +46,7 @@ where rust_cmd("thin_check", args) } -pub fn thin_rmap_cmd(args: I) -> duct::Expression +pub fn thin_rmap_cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, @@ -46,7 +54,7 @@ where cpp_cmd("thin_rmap", args) } -pub fn thin_generate_metadata_cmd(args: I) -> duct::Expression +pub fn thin_generate_metadata_cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, @@ -54,7 +62,7 @@ where cpp_cmd("thin_generate_metadata", args) } -pub fn thin_generate_mappings_cmd(args: I) -> duct::Expression +pub fn thin_generate_mappings_cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, @@ -62,7 +70,7 @@ where cpp_cmd("thin_generate_mappings", args) } -pub fn thin_generate_damage_cmd(args: I) -> duct::Expression +pub fn thin_generate_damage_cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, @@ -70,7 +78,16 @@ where cpp_cmd("thin_generate_damage", args) } -pub fn thin_restore_cmd(args: I) -> duct::Expression +pub fn thin_restore_cmd(args: I) -> Command +where + I: IntoIterator, + I::Item: Into, +{ + // rust_cmd("thin_restore", args) + cpp_cmd("thin_restore", args) +} + +pub fn thin_repair_cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, @@ -78,15 +95,7 @@ where rust_cmd("thin_restore", args) } -pub fn thin_repair_cmd(args: I) -> duct::Expression -where - I: IntoIterator, - I::Item: Into, -{ - rust_cmd("thin_restore", args) -} - -pub fn thin_dump_cmd(args: I) -> duct::Expression +pub fn thin_dump_cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, @@ -94,7 +103,7 @@ where rust_cmd("thin_dump", args) } -pub fn thin_delta_cmd(args: I) -> duct::Expression +pub fn thin_delta_cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, @@ -102,7 +111,7 @@ where cpp_cmd("thin_delta", args) } -pub fn thin_metadata_pack_cmd(args: I) -> duct::Expression +pub fn thin_metadata_pack_cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, @@ -110,7 +119,7 @@ where rust_cmd("thin_metadata_pack", args) } -pub fn thin_metadata_unpack_cmd(args: I) -> duct::Expression +pub fn thin_metadata_unpack_cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, @@ -118,7 +127,7 @@ where rust_cmd("thin_metadata_unpack", args) } -pub fn cache_check_cmd(args: I) -> duct::Expression +pub fn cache_check_cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, @@ -126,7 +135,7 @@ where rust_cmd("cache_check", args) } -pub fn cache_dump_cmd(args: I) -> duct::Expression +pub fn cache_dump_cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, @@ -134,7 +143,7 @@ where rust_cmd("cache_dump", args) } -pub fn cache_restore_cmd(args: I) -> duct::Expression +pub fn cache_restore_cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, @@ -142,7 +151,7 @@ where rust_cmd("cache_restore", args) } -pub fn cache_repair_cmd(args: I) -> duct::Expression +pub fn cache_repair_cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, diff --git a/tests/common/test_dir.rs b/tests/common/test_dir.rs index a71525a..3a0f837 100644 --- a/tests/common/test_dir.rs +++ b/tests/common/test_dir.rs @@ -36,6 +36,10 @@ impl TestDir { }) } + pub fn dont_clean_up(&mut self) { + self.clean_up = false; + } + pub fn mk_path(&mut self, file: &str) -> PathBuf { let mut p = PathBuf::new(); p.push(&self.dir); @@ -54,6 +58,8 @@ impl Drop for TestDir { let _ignore = fs::remove_file(f); } fs::remove_dir(&self.dir).expect("couldn't remove test directory"); + } else { + eprintln!("leaving test directory: {:?}", self.dir); } } } diff --git a/tests/thin_check.rs b/tests/thin_check.rs index d89e497..52688cf 100644 --- a/tests/thin_check.rs +++ b/tests/thin_check.rs @@ -45,7 +45,7 @@ impl<'a> Program<'a> for ThinCheck { "thin_check" } - fn cmd(args: I) -> duct::Expression + fn cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, @@ -315,17 +315,26 @@ fn fatal_errors_cant_be_ignored() -> Result<()> { #[test] fn auto_repair() -> Result<()> { let mut td = TestDir::new()?; + td.dont_clean_up(); let md = prep_metadata(&mut td)?; + eprintln!("here 0"); + // auto-repair should have no effect on good metadata. ensure_untouched(&md, || { - run_ok(thin_check_cmd(args!["--auto-repair", &md]))?; + // run_ok(thin_check_cmd(args!["--auto-repair", &md]))?; + run_ok(thin_check_cmd(args![&md]))?; Ok(()) })?; + eprintln!("here 0.5"); generate_metadata_leaks(&md, 16, 0, 1)?; + eprintln!("here 1"); run_fail(thin_check_cmd(args![&md]))?; + eprintln!("here 2"); run_ok(thin_check_cmd(args!["--auto-repair", &md]))?; + eprintln!("here 3"); + run_ok(thin_check_cmd(args![&md]))?; Ok(()) } diff --git a/tests/thin_delta.rs b/tests/thin_delta.rs index 4b9cba0..b2d57a7 100644 --- a/tests/thin_delta.rs +++ b/tests/thin_delta.rs @@ -29,7 +29,7 @@ impl<'a> Program<'a> for ThinDelta { "thin_delta" } - fn cmd(args: I) -> duct::Expression + fn cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, diff --git a/tests/thin_dump.rs b/tests/thin_dump.rs index 1ce6926..990a3bb 100644 --- a/tests/thin_dump.rs +++ b/tests/thin_dump.rs @@ -35,7 +35,7 @@ impl<'a> Program<'a> for ThinDump { "thin_dump" } - fn cmd(args: I) -> duct::Expression + fn cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, diff --git a/tests/thin_metadata_pack.rs b/tests/thin_metadata_pack.rs index 23e573f..86c7856 100644 --- a/tests/thin_metadata_pack.rs +++ b/tests/thin_metadata_pack.rs @@ -38,7 +38,7 @@ impl<'a> Program<'a> for ThinMetadataPack { "thin_metadata_pack" } - fn cmd(args: I) -> duct::Expression + fn cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, diff --git a/tests/thin_metadata_unpack.rs b/tests/thin_metadata_unpack.rs index bdbadb1..2e5d6c3 100644 --- a/tests/thin_metadata_unpack.rs +++ b/tests/thin_metadata_unpack.rs @@ -40,7 +40,7 @@ impl<'a> Program<'a> for ThinMetadataUnpack { "thin_metadata_pack" } - fn cmd(args: I) -> duct::Expression + fn cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, diff --git a/tests/thin_repair.rs b/tests/thin_repair.rs index e99a0ea..f8264e2 100644 --- a/tests/thin_repair.rs +++ b/tests/thin_repair.rs @@ -33,7 +33,7 @@ impl<'a> Program<'a> for ThinRepair { "thin_repair" } - fn cmd(args: I) -> duct::Expression + fn cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, diff --git a/tests/thin_restore.rs b/tests/thin_restore.rs index 6eae528..4d88352 100644 --- a/tests/thin_restore.rs +++ b/tests/thin_restore.rs @@ -34,7 +34,7 @@ impl<'a> Program<'a> for ThinRestore { "thin_restore" } - fn cmd(args: I) -> duct::Expression + fn cmd(args: I) -> Command where I: IntoIterator, I::Item: Into, diff --git a/tests/thin_rmap.rs b/tests/thin_rmap.rs index 699e85f..f30bf32 100644 --- a/tests/thin_rmap.rs +++ b/tests/thin_rmap.rs @@ -29,7 +29,7 @@ impl<'a> Program<'a> for ThinRmap { "thin_rmap" } - fn cmd(args: I) -> duct::Expression + fn cmd(args: I) -> Command where I: IntoIterator, I::Item: Into,