From d2678fdf27204a47d29de20cfe0a7b2f70f40138 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Wed, 5 Aug 2020 09:09:18 +0100 Subject: [PATCH] [functional-tests] port thin_metadata_pack tests to Rust. cargo test --- functional-tests/thin-functional-tests.scm | 43 ------------ tests/common/mod.rs | 22 +++++++ tests/thin_metadata_pack.rs | 76 ++++++++++++++++++++++ 3 files changed, 98 insertions(+), 43 deletions(-) create mode 100644 tests/thin_metadata_pack.rs diff --git a/functional-tests/thin-functional-tests.scm b/functional-tests/thin-functional-tests.scm index 920f0ff..0f2fd09 100644 --- a/functional-tests/thin-functional-tests.scm +++ b/functional-tests/thin-functional-tests.scm @@ -67,49 +67,6 @@ ;; to run. (define (register-thin-tests) #t) - ;;;----------------------------------------------------------- - ;;; thin_metadata_pack scenarios - ;;;----------------------------------------------------------- - - (define-scenario (thin-metadata-pack version) - "accepts --version" - (run-ok-rcv (stdout _) (thin-metadata-pack "--version") - (assert-equal "thin_metadata_pack 0.9.0-rc2" stdout))) - - (define-scenario (thin-metadata-pack h) - "accepts -h" - (run-ok-rcv (stdout _) (thin-metadata-pack "-h") - (assert-equal thin-metadata-pack-help stdout))) - - (define-scenario (thin-metadata-pack help) - "accepts --help" - (run-ok-rcv (stdout _) (thin-metadata-pack "--help") - (assert-equal thin-metadata-pack-help stdout))) - - (define-scenario (thin-metadata-pack unrecognised-option) - "Unrecognised option should cause failure" - (with-valid-metadata (md) - (run-fail-rcv (stdout stderr) (thin-metadata-pack "--unleash-the-hedgehogs") - (assert-starts-with "error: Found argument '--unleash-the-hedgehogs'" stderr)))) - - (define-scenario (thin-metadata-pack missing-input-file) - "the input file wasn't specified" - (with-empty-metadata (md) - (run-fail-rcv (_ stderr) (thin-metadata-pack "-o " md) - (assert-starts-with "error: The following required arguments were not provided:\n -i " stderr)))) - - (define-scenario (thin-metadata-pack no-such-input-file) - "the input file can't be found" - (with-empty-metadata (md) - (run-fail-rcv (_ stderr) (thin-metadata-pack "-i no-such-file -o" md) - (assert-starts-with "Couldn't find input file" stderr)))) - - (define-scenario (thin-metadata-pack missing-output-file) - "the output file wasn't specified" - (with-empty-metadata (md) - (run-fail-rcv (_ stderr) (thin-metadata-pack "-i" md) - (assert-starts-with "error: The following required arguments were not provided:\n -o " stderr)))) - ;;;----------------------------------------------------------- ;;; thin_metadata_unpack scenarios ;;;----------------------------------------------------------- diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 0c35c95..21c5526 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -81,6 +81,28 @@ macro_rules! thin_delta { }; } +#[macro_export] +macro_rules! thin_metadata_pack { + ( $( $arg: expr ),* ) => { + { + use std::ffi::OsString; + let args: &[OsString] = &[$( Into::::into($arg) ),*]; + duct::cmd("bin/thin_metadata_pack", args).stdout_capture().stderr_capture() + } + }; +} + +#[macro_export] +macro_rules! thin_metadata_unpack { + ( $( $arg: expr ),* ) => { + { + use std::ffi::OsString; + let args: &[OsString] = &[$( Into::::into($arg) ),*]; + duct::cmd("bin/thin_metadata_unpack", args).stdout_capture().stderr_capture() + } + }; +} + //------------------------------------------ pub struct TestDir { diff --git a/tests/thin_metadata_pack.rs b/tests/thin_metadata_pack.rs new file mode 100644 index 0000000..ee9926b --- /dev/null +++ b/tests/thin_metadata_pack.rs @@ -0,0 +1,76 @@ +use anyhow::Result; +use thinp::version::TOOLS_VERSION; + +mod common; +use common::*; + +//------------------------------------------ + +#[test] +fn accepts_v() -> Result<()> { + let stdout = thin_metadata_pack!("-V").read()?; + assert!(stdout.contains(TOOLS_VERSION)); + Ok(()) +} + +#[test] +fn accepts_version() -> Result<()> { + let stdout = thin_metadata_pack!("--version").read()?; + assert!(stdout.contains(TOOLS_VERSION)); + Ok(()) +} + +const USAGE: &str = "thin_metadata_pack 0.9.0-rc2\nProduces a compressed file of thin metadata. Only packs metadata blocks that are actually used.\n\nUSAGE:\n thin_metadata_pack -i -o \n\nFLAGS:\n -h, --help Prints help information\n -V, --version Prints version information\n\nOPTIONS:\n -i Specify thinp metadata binary device/file\n -o Specify packed output file"; + +#[test] +fn accepts_h() -> Result<()> { + let stdout = thin_metadata_pack!("-h").read()?; + assert_eq!(stdout, USAGE); + Ok(()) +} + +#[test] +fn accepts_help() -> Result<()> { + let stdout = thin_metadata_pack!("--help").read()?; + assert_eq!(stdout, USAGE); + Ok(()) +} + +#[test] +fn rejects_bad_option() -> Result<()> { + let stderr = run_fail(thin_metadata_pack!("--hedgehogs-only"))?; + assert!(stderr.contains("Found argument \'--hedgehogs-only\'")); + Ok(()) +} + +#[test] +fn missing_input_file() -> Result<()> { + let mut td = TestDir::new()?; + let md = mk_zeroed_md(&mut td)?; + let stderr = run_fail(thin_metadata_pack!("-o", &md))?; + assert!( + stderr.contains("error: The following required arguments were not provided:\n -i ") + ); + Ok(()) +} + +#[test] +fn no_such_input_file() -> Result<()> { + let mut td = TestDir::new()?; + let md = mk_zeroed_md(&mut td)?; + let stderr = run_fail(thin_metadata_pack!("-i", "no-such-file", "-o", &md))?; + assert!(stderr.contains("Couldn't find input file")); + Ok(()) +} + +#[test] +fn missing_output_file() -> Result<()> { + let mut td = TestDir::new()?; + let md = mk_zeroed_md(&mut td)?; + let stderr = run_fail(thin_metadata_pack!("-i", &md))?; + assert!(stderr + .contains("error: The following required arguments were not provided:\n -o ")); + Ok(()) +} + +//------------------------------------------