diff --git a/functional-tests/era-functional-tests.scm b/functional-tests/era-functional-tests.scm index a81b41b..f15c599 100644 --- a/functional-tests/era-functional-tests.scm +++ b/functional-tests/era-functional-tests.scm @@ -43,83 +43,6 @@ (define (register-era-tests) #t) - ;;;----------------------------------------------------------- - ;;; era_check scenarios - ;;;----------------------------------------------------------- - (define-scenario (era-check v) - "era_check -V" - (run-ok-rcv (stdout _) (era-check "-V") - (assert-equal tools-version stdout))) - - (define-scenario (era-check version) - "era_check --version" - (run-ok-rcv (stdout _) (era-check "--version") - (assert-equal tools-version stdout))) - - (define-scenario (era-check h) - "era_check -h" - (run-ok-rcv (stdout _) (era-check "-h") - (assert-equal era-check-help stdout))) - - (define-scenario (era-check help) - "era_check --help" - (run-ok-rcv (stdout _) (era-check "--help") - (assert-equal era-check-help stdout))) - - (define-scenario (era-check no-device-specified) - "Fail if no device specified" - (run-fail-rcv (_ stderr) (era-check) - (assert-starts-with "No input file provided." stderr))) - - (define-scenario (era-check dev-not-exist) - "Fail if specified device doesn't exist" - (run-fail-rcv (_ stderr) (era-check "/dev/unlikely") - (assert-starts-with "/dev/unlikely: No such file or directory" stderr))) - - (define-scenario (era-check dev-is-a-directory) - "Fail if given a directory instead of a file or device" - (run-fail-rcv (_ stderr) (era-check "/tmp") - (assert-starts-with "/tmp: Not a block device or regular file" stderr))) - - (define-scenario (era-check bad-permissions) - "Fail if given a device with inadequate access permissions" - (with-temp-file-sized ((md "era.bin" (meg 4))) - (run-ok "chmod -r" md) - (run-fail-rcv (_ stderr) (era-check md) - (assert-starts-with "syscall 'open' failed: Permission denied" stderr)))) - - (define-scenario (era-check empty-dev) - "Fail if given a file of zeroes" - (with-empty-metadata (md) - (run-fail (era-check md)))) - - (define-scenario (era-check quiet) - "Fail should give no output if --quiet" - (with-empty-metadata (md) - (run-fail-rcv (stdout stderr) (era-check "--quiet" md) - (assert-eof stdout) - (assert-eof stderr)))) - - (define-scenario (era-check q) - "Fail should give no output if -q" - (with-empty-metadata (md) - (run-fail-rcv (stdout stderr) (era-check "-q" md) - (assert-eof stdout) - (assert-eof stderr)))) - - (define-scenario (era-check tiny-metadata) - "Prints helpful message in case tiny metadata given" - (with-temp-file-sized ((md "era.bin" 1024)) - (run-fail-rcv (_ stderr) (era-check md) - (assert-starts-with "Metadata device/file too small. Is this binary metadata?" stderr)))) - - (define-scenario (era-check spot-accidental-xml-data) - "Prints helpful message if XML metadata given" - (with-era-xml (xml) - (system (fmt #f "man bash >> " xml)) - (run-fail-rcv (_ stderr) (era-check xml) - (assert-matches ".*This looks like XML. era_check only checks the binary metadata format." stderr)))) - ;;;----------------------------------------------------------- ;;; era_restore scenarios ;;;----------------------------------------------------------- diff --git a/tests/common/target.rs b/tests/common/target.rs index 6a91930..fa8ffc0 100644 --- a/tests/common/target.rs +++ b/tests/common/target.rs @@ -159,6 +159,38 @@ where rust_cmd("cache_repair", args) } +pub fn era_check_cmd(args: I) -> Command +where + I: IntoIterator, + I::Item: Into, +{ + rust_cmd("era_check", args) +} + +pub fn era_dump_cmd(args: I) -> Command +where + I: IntoIterator, + I::Item: Into, +{ + rust_cmd("era_dump", args) +} + +pub fn era_restore_cmd(args: I) -> Command +where + I: IntoIterator, + I::Item: Into, +{ + rust_cmd("era_restore", args) +} + +pub fn era_repair_cmd(args: I) -> Command +where + I: IntoIterator, + I::Item: Into, +{ + rust_cmd("era_repair", args) +} + //------------------------------------------ pub mod msg { diff --git a/tests/era_check.rs b/tests/era_check.rs new file mode 100644 index 0000000..774991f --- /dev/null +++ b/tests/era_check.rs @@ -0,0 +1,118 @@ +use anyhow::Result; + +mod common; + +use common::cache::*; +use common::common_args::*; +use common::fixture::*; +use common::input_arg::*; +use common::process::*; +use common::program::*; +use common::target::*; +use common::test_dir::*; + +//------------------------------------------ + +const USAGE: &str = "era_check 0.9.0 + +USAGE: + era_check [FLAGS] + +FLAGS: + --ignore-non-fatal-errors Only return a non-zero exit code if a fatal error is found. + -q, --quiet Suppress output messages, return only exit code. + --super-block-only Only check the superblock. + -h, --help Prints help information + -V, --version Prints version information + +ARGS: + Specify the input device to check"; + +//------------------------------------------ + +struct EraCheck; + +impl<'a> Program<'a> for EraCheck { + fn name() -> &'a str { + "era_check" + } + + fn cmd(args: I) -> Command + where + I: IntoIterator, + I::Item: Into, + { + era_check_cmd(args) + } + + fn usage() -> &'a str { + USAGE + } + + fn arg_type() -> ArgType { + ArgType::InputArg + } + + fn bad_option_hint(option: &str) -> String { + msg::bad_option_hint(option) + } +} + +impl<'a> InputProgram<'a> for EraCheck { + fn mk_valid_input(td: &mut TestDir) -> Result { + mk_valid_md(td) + } + + fn file_not_found() -> &'a str { + msg::FILE_NOT_FOUND + } + + fn missing_input_arg() -> &'a str { + msg::MISSING_INPUT_ARG + } + + fn corrupted_input() -> &'a str { + msg::BAD_SUPERBLOCK + } +} + +impl<'a> MetadataReader<'a> for EraCheck {} + +//------------------------------------------ + +test_accepts_help!(EraCheck); +test_accepts_version!(EraCheck); +test_rejects_bad_option!(EraCheck); + +test_missing_input_arg!(EraCheck); +test_input_file_not_found!(EraCheck); +test_input_cannot_be_a_directory!(EraCheck); +test_unreadable_input_file!(EraCheck); + +test_help_message_for_tiny_input_file!(EraCheck); +test_spot_xml_data!(EraCheck); +test_corrupted_input_data!(EraCheck); + +//------------------------------------------ + +#[test] +fn failing_q() -> Result<()> { + let mut td = TestDir::new()?; + let md = mk_zeroed_md(&mut td)?; + let output = run_fail_raw(era_check_cmd(args!["-q", &md]))?; + assert_eq!(output.stdout.len(), 0); + assert_eq!(output.stderr.len(), 0); + Ok(()) +} + +#[test] +fn failing_quiet() -> Result<()> { + let mut td = TestDir::new()?; + let md = mk_zeroed_md(&mut td)?; + let output = run_fail_raw(era_check_cmd(args!["--quiet", &md]))?; + assert_eq!(output.stdout.len(), 0); + assert_eq!(output.stderr.len(), 0); + Ok(()) +} + +//------------------------------------------