From 23568aaa11d05ddbe4c7dfc2fd53838e827bdf51 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Wed, 5 Aug 2020 08:24:52 +0100 Subject: [PATCH] [functional-tests] port thin_delta tests to rust. cargo test --- functional-tests/thin-functional-tests.scm | 44 -------------- tests/common/mod.rs | 11 ++++ tests/thin_delta.rs | 70 ++++++++++++++++++++++ 3 files changed, 81 insertions(+), 44 deletions(-) create mode 100644 tests/thin_delta.rs diff --git a/functional-tests/thin-functional-tests.scm b/functional-tests/thin-functional-tests.scm index c921b87..920f0ff 100644 --- a/functional-tests/thin-functional-tests.scm +++ b/functional-tests/thin-functional-tests.scm @@ -67,50 +67,6 @@ ;; to run. (define (register-thin-tests) #t) - ;;;----------------------------------------------------------- - ;;; thin_delta scenarios - ;;;----------------------------------------------------------- - (define-scenario (thin-delta v) - "thin_delta accepts -V" - (run-ok-rcv (stdout _) (thin-delta "-V") - (assert-equal tools-version stdout))) - - (define-scenario (thin-delta version) - "thin_delta accepts --version" - (run-ok-rcv (stdout _) (thin-delta "--version") - (assert-equal tools-version stdout))) - - (define-scenario (thin-delta h) - "thin_delta accepts -h" - (run-ok-rcv (stdout _) (thin-delta "-h") - (assert-equal thin-delta-help stdout))) - - (define-scenario (thin-delta help) - "thin_delta accepts --help" - (run-ok-rcv (stdout _) (thin-delta "--help") - (assert-equal thin-delta-help stdout))) - - (define-scenario (thin-delta unrecognised-option) - "Unrecognised option should cause failure" - (with-valid-metadata (md) - (run-fail-rcv (stdout stderr) (thin-delta "--unleash-the-hedgehogs") - (assert-matches ".*thin_delta: unrecognized option '--unleash-the-hedgehogs" stderr)))) - - (define-scenario (thin-delta snap1-unspecified) - "Fails without --snap1 fails" - (run-fail-rcv (_ stderr) (thin-delta "--snap2 45 foo") - (assert-starts-with "--snap1 not specified." stderr))) - - (define-scenario (thin-delta snap2-unspecified) - "Fails without --snap2 fails" - (run-fail-rcv (_ stderr) (thin-delta "--snap1 45 foo") - (assert-starts-with "--snap2 not specified." stderr))) - - (define-scenario (thin-delta device-unspecified) - "Fails if no device given" - (run-fail-rcv (_ stderr) (thin-delta "--snap1 45 --snap2 46") - (assert-starts-with "No input device provided." stderr))) - ;;;----------------------------------------------------------- ;;; thin_metadata_pack scenarios ;;;----------------------------------------------------------- diff --git a/tests/common/mod.rs b/tests/common/mod.rs index bf24132..0c35c95 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -70,6 +70,17 @@ macro_rules! thin_repair { }; } +#[macro_export] +macro_rules! thin_delta { + ( $( $arg: expr ),* ) => { + { + use std::ffi::OsString; + let args: &[OsString] = &[$( Into::::into($arg) ),*]; + duct::cmd("bin/thin_delta", args).stdout_capture().stderr_capture() + } + }; +} + //------------------------------------------ pub struct TestDir { diff --git a/tests/thin_delta.rs b/tests/thin_delta.rs new file mode 100644 index 0000000..7c1442b --- /dev/null +++ b/tests/thin_delta.rs @@ -0,0 +1,70 @@ +use anyhow::Result; +use thinp::version::TOOLS_VERSION; + +mod common; +use common::*; + +//------------------------------------------ + +#[test] +fn accepts_v() -> Result<()> { + let stdout = thin_delta!("-V").read()?; + assert_eq!(stdout, TOOLS_VERSION); + Ok(()) +} + +#[test] +fn accepts_version() -> Result<()> { + let stdout = thin_delta!("--version").read()?; + assert_eq!(stdout, TOOLS_VERSION); + Ok(()) +} + +const USAGE: &str = "Usage: thin_delta [options] \nOptions:\n {--thin1, --snap1}\n {--thin2, --snap2}\n {-m, --metadata-snap} [block#]\n {--verbose}\n {-h|--help}\n {-V|--version}"; + +#[test] +fn accepts_h() -> Result<()> { + let stdout = thin_delta!("-h").read()?; + assert_eq!(stdout, USAGE); + Ok(()) +} + +#[test] +fn accepts_help() -> Result<()> { + let stdout = thin_delta!("--help").read()?; + assert_eq!(stdout, USAGE); + Ok(()) +} + +#[test] +fn rejects_bad_option() -> Result<()> { + let stderr = run_fail(thin_delta!("--hedgehogs-only"))?; + assert!(stderr.contains("unrecognized option \'--hedgehogs-only\'")); + Ok(()) +} + +#[test] +fn snap1_unspecified() -> Result<()> { + let mut td = TestDir::new()?; + let md = mk_valid_md(&mut td)?; + let stderr = run_fail(thin_delta!("--snap2", "45", &md))?; + assert!(stderr.contains("--snap1 not specified")); + Ok(()) +} + +#[test] +fn snap2_unspecified() -> Result<()> { + let mut td = TestDir::new()?; + let md = mk_valid_md(&mut td)?; + let stderr = run_fail(thin_delta!("--snap1", "45", &md))?; + assert!(stderr.contains("--snap2 not specified")); + Ok(()) +} + +#[test] +fn dev_unspecified() -> Result<()> { + let stderr = run_fail(thin_delta!("--snap1", "45", "--snap2", "46"))?; + assert!(stderr.contains("No input device provided")); + Ok(()) +} +