[tests] Pull out common tests on i/o options into reusable modules

- Introduce modules for testing input/output options
- Provide macros for generating test cases
- Hide details of subprocess execution
This commit is contained in:
Ming-Hung Tsai
2021-07-01 22:27:37 +08:00
parent 6660fde3c4
commit 12ef69c31b
13 changed files with 1077 additions and 784 deletions

View File

@@ -1,54 +1,36 @@
use anyhow::Result;
use thinp::version::tools_version;
mod common;
use common::common_args::*;
use common::test_dir::*;
use common::*;
//------------------------------------------
#[test]
fn accepts_v() -> Result<()> {
let stdout = thin_rmap!("-V").read()?;
assert!(stdout.contains(tools_version()));
Ok(())
}
const USAGE: &str = "Usage: thin_rmap [options] {device|file}\n\
Options:\n \
{-h|--help}\n \
{-V|--version}\n \
{--region <block range>}*\n\
Where:\n \
<block range> is of the form <begin>..<one-past-the-end>\n \
for example 5..45 denotes blocks 5 to 44 inclusive, but not block 45";
#[test]
fn accepts_version() -> Result<()> {
let stdout = thin_rmap!("--version").read()?;
assert!(stdout.contains(tools_version()));
Ok(())
}
//------------------------------------------
const USAGE: &str = "Usage: thin_rmap [options] {device|file}\nOptions:\n {-h|--help}\n {-V|--version}\n {--region <block range>}*\nWhere:\n <block range> is of the form <begin>..<one-past-the-end>\n for example 5..45 denotes blocks 5 to 44 inclusive, but not block 45";
test_accepts_help!(THIN_RMAP, USAGE);
test_accepts_version!(THIN_RMAP);
test_rejects_bad_option!(THIN_RMAP);
#[test]
fn accepts_h() -> Result<()> {
let stdout = thin_rmap!("-h").read()?;
assert_eq!(stdout, USAGE);
Ok(())
}
#[test]
fn accepts_help() -> Result<()> {
let stdout = thin_rmap!("--help").read()?;
assert_eq!(stdout, USAGE);
Ok(())
}
#[test]
fn rejects_bad_option() -> Result<()> {
let stderr = run_fail(thin_rmap!("--hedgehogs-only"))?;
assert!(stderr.contains("unrecognized option \'--hedgehogs-only\'"));
Ok(())
}
//------------------------------------------
#[test]
fn valid_region_format_should_pass() -> Result<()> {
let mut td = TestDir::new()?;
let md = mk_valid_md(&mut td)?;
thin_rmap!("--region", "23..7890", &md).run()?;
let md_path = md.to_str().unwrap();
run_ok(THIN_RMAP, &["--region", "23..7890", md_path])?;
Ok(())
}
@@ -67,7 +49,7 @@ fn invalid_regions_should_fail() -> Result<()> {
for r in &invalid_regions {
let mut td = TestDir::new()?;
let md = mk_valid_md(&mut td)?;
run_fail(thin_rmap!(r, &md))?;
run_fail(THIN_RMAP, &[&r.to_string(), md.to_str().unwrap()])?;
}
Ok(())
}
@@ -76,7 +58,16 @@ fn invalid_regions_should_fail() -> Result<()> {
fn multiple_regions_should_pass() -> Result<()> {
let mut td = TestDir::new()?;
let md = mk_valid_md(&mut td)?;
thin_rmap!("--region", "1..23", "--region", "45..78", &md).run()?;
run_ok(
THIN_RMAP,
&[
"--region",
"1..23",
"--region",
"45..78",
md.to_str().unwrap(),
],
)?;
Ok(())
}
@@ -84,7 +75,7 @@ fn multiple_regions_should_pass() -> Result<()> {
fn junk_input() -> Result<()> {
let mut td = TestDir::new()?;
let xml = mk_valid_xml(&mut td)?;
run_fail(thin_rmap!("--region", "0..-1", &xml))?;
run_fail(THIN_RMAP, &["--region", "0..-1", xml.to_str().unwrap()])?;
Ok(())
}