[tests] Get the run_* functions to log what they're doing.

This commit is contained in:
Joe Thornber 2021-10-18 16:59:17 +01:00
parent 024554c987
commit 6c5405ccf8
16 changed files with 137 additions and 49 deletions

View File

@ -41,7 +41,7 @@ impl<'a> Program<'a> for CacheCheck {
"cache_check"
}
fn cmd<I>(args: I) -> duct::Expression
fn cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<std::ffi::OsString>,

View File

@ -41,7 +41,7 @@ impl<'a> Program<'a> for CacheDump {
"cache_dump"
}
fn cmd<I>(args: I) -> duct::Expression
fn cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<std::ffi::OsString>,

View File

@ -37,7 +37,7 @@ impl<'a> Program<'a> for CacheRepair {
"cache_repair"
}
fn cmd<I>(args: I) -> duct::Expression
fn cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<std::ffi::OsString>,

View File

@ -39,7 +39,7 @@ impl<'a> Program<'a> for CacheRestore {
"thin_restore"
}
fn cmd<I>(args: I) -> duct::Expression
fn cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<std::ffi::OsString>,

View File

@ -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<OsString>,
}
#[macro_export]
macro_rules! cmd {
( $program:expr $(, $arg:expr )* $(,)? ) => {
{
// use std::ffi::OsString;
let args: &[OsString] = &[$( Into::<OsString>::into($arg) ),*];
Command::new($program, args)
}
};
}
impl Command {
pub fn new(program: OsString, args: Vec<OsString>) -> 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<String> {
let command = command.stdout_capture().stderr_capture();
pub fn run_ok(command: Command) -> Result<String> {
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<String> {
}
// Returns the entire output. The command must return zero.
pub fn run_ok_raw(command: duct::Expression) -> Result<std::process::Output> {
let command = command.stdout_capture().stderr_capture();
pub fn run_ok_raw(command: Command) -> Result<std::process::Output> {
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<String> {
let command = command.stdout_capture().stderr_capture();
pub fn run_fail(command: Command) -> Result<String> {
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<std::process::Output> {
let command = command.stdout_capture().stderr_capture();
pub fn run_fail_raw(command: Command) -> Result<std::process::Output> {
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)
}

View File

@ -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<I>(args: I) -> duct::Expression
fn cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<std::ffi::OsString>;

View File

@ -1,20 +1,28 @@
use std::ffi::OsString;
use std::path::PathBuf;
use crate::common::process::*;
//------------------------------------------
pub fn cpp_cmd<S, I>(cmd: S, args: I) -> duct::Expression
pub fn cpp_cmd<S, I>(cmd: S, args: I) -> Command
where
S: Into<OsString>,
I: IntoIterator,
I::Item: Into<OsString>,
{
let mut bin = PathBuf::from("./bin");
let mut bin = PathBuf::from("bin");
bin.push(Into::<OsString>::into(cmd));
duct::cmd(bin.as_path(), args)
let mut args_ = Vec::new();
for a in args {
args_.push(Into::<OsString>::into(a));
}
Command::new(Into::<OsString>::into(bin.as_path()), args_)
}
pub fn rust_cmd<S, I>(cmd: S, args: I) -> duct::Expression
pub fn rust_cmd<S, I>(cmd: S, args: I) -> Command
where
S: Into<OsString>,
I: IntoIterator,
@ -27,10 +35,10 @@ where
all_args.push(Into::<OsString>::into(a));
}
duct::cmd(RUST_PATH, &all_args)
Command::new(Into::<OsString>::into(RUST_PATH), all_args)
}
pub fn thin_check_cmd<I>(args: I) -> duct::Expression
pub fn thin_check_cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<OsString>,
@ -38,7 +46,7 @@ where
rust_cmd("thin_check", args)
}
pub fn thin_rmap_cmd<I>(args: I) -> duct::Expression
pub fn thin_rmap_cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<OsString>,
@ -46,7 +54,7 @@ where
cpp_cmd("thin_rmap", args)
}
pub fn thin_generate_metadata_cmd<I>(args: I) -> duct::Expression
pub fn thin_generate_metadata_cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<OsString>,
@ -54,7 +62,7 @@ where
cpp_cmd("thin_generate_metadata", args)
}
pub fn thin_generate_mappings_cmd<I>(args: I) -> duct::Expression
pub fn thin_generate_mappings_cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<OsString>,
@ -62,7 +70,7 @@ where
cpp_cmd("thin_generate_mappings", args)
}
pub fn thin_generate_damage_cmd<I>(args: I) -> duct::Expression
pub fn thin_generate_damage_cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<OsString>,
@ -70,7 +78,16 @@ where
cpp_cmd("thin_generate_damage", args)
}
pub fn thin_restore_cmd<I>(args: I) -> duct::Expression
pub fn thin_restore_cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<OsString>,
{
// rust_cmd("thin_restore", args)
cpp_cmd("thin_restore", args)
}
pub fn thin_repair_cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<OsString>,
@ -78,15 +95,7 @@ where
rust_cmd("thin_restore", args)
}
pub fn thin_repair_cmd<I>(args: I) -> duct::Expression
where
I: IntoIterator,
I::Item: Into<OsString>,
{
rust_cmd("thin_restore", args)
}
pub fn thin_dump_cmd<I>(args: I) -> duct::Expression
pub fn thin_dump_cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<OsString>,
@ -94,7 +103,7 @@ where
rust_cmd("thin_dump", args)
}
pub fn thin_delta_cmd<I>(args: I) -> duct::Expression
pub fn thin_delta_cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<OsString>,
@ -102,7 +111,7 @@ where
cpp_cmd("thin_delta", args)
}
pub fn thin_metadata_pack_cmd<I>(args: I) -> duct::Expression
pub fn thin_metadata_pack_cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<OsString>,
@ -110,7 +119,7 @@ where
rust_cmd("thin_metadata_pack", args)
}
pub fn thin_metadata_unpack_cmd<I>(args: I) -> duct::Expression
pub fn thin_metadata_unpack_cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<OsString>,
@ -118,7 +127,7 @@ where
rust_cmd("thin_metadata_unpack", args)
}
pub fn cache_check_cmd<I>(args: I) -> duct::Expression
pub fn cache_check_cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<OsString>,
@ -126,7 +135,7 @@ where
rust_cmd("cache_check", args)
}
pub fn cache_dump_cmd<I>(args: I) -> duct::Expression
pub fn cache_dump_cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<OsString>,
@ -134,7 +143,7 @@ where
rust_cmd("cache_dump", args)
}
pub fn cache_restore_cmd<I>(args: I) -> duct::Expression
pub fn cache_restore_cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<OsString>,
@ -142,7 +151,7 @@ where
rust_cmd("cache_restore", args)
}
pub fn cache_repair_cmd<I>(args: I) -> duct::Expression
pub fn cache_repair_cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<OsString>,

View File

@ -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);
}
}
}

View File

@ -45,7 +45,7 @@ impl<'a> Program<'a> for ThinCheck {
"thin_check"
}
fn cmd<I>(args: I) -> duct::Expression
fn cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<std::ffi::OsString>,
@ -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(())
}

View File

@ -29,7 +29,7 @@ impl<'a> Program<'a> for ThinDelta {
"thin_delta"
}
fn cmd<I>(args: I) -> duct::Expression
fn cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<std::ffi::OsString>,

View File

@ -35,7 +35,7 @@ impl<'a> Program<'a> for ThinDump {
"thin_dump"
}
fn cmd<I>(args: I) -> duct::Expression
fn cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<std::ffi::OsString>,

View File

@ -38,7 +38,7 @@ impl<'a> Program<'a> for ThinMetadataPack {
"thin_metadata_pack"
}
fn cmd<I>(args: I) -> duct::Expression
fn cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<std::ffi::OsString>,

View File

@ -40,7 +40,7 @@ impl<'a> Program<'a> for ThinMetadataUnpack {
"thin_metadata_pack"
}
fn cmd<I>(args: I) -> duct::Expression
fn cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<std::ffi::OsString>,

View File

@ -33,7 +33,7 @@ impl<'a> Program<'a> for ThinRepair {
"thin_repair"
}
fn cmd<I>(args: I) -> duct::Expression
fn cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<std::ffi::OsString>,

View File

@ -34,7 +34,7 @@ impl<'a> Program<'a> for ThinRestore {
"thin_restore"
}
fn cmd<I>(args: I) -> duct::Expression
fn cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<std::ffi::OsString>,

View File

@ -29,7 +29,7 @@ impl<'a> Program<'a> for ThinRmap {
"thin_rmap"
}
fn cmd<I>(args: I) -> duct::Expression
fn cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<std::ffi::OsString>,