[tests] Refine the test naming and error messages

- Make the naming of test cases less ambiguous, e.g., rename
  "missing_input_file" to "missing_input_arg" or "input_file_not_found"
- Unify the error messages on input/output options
This commit is contained in:
Ming-Hung Tsai 2021-06-23 17:42:41 +08:00
parent d00388f68a
commit 6660fde3c4
7 changed files with 49 additions and 28 deletions

View File

@ -71,8 +71,11 @@ void
file_utils::check_file_exists(string const &file, bool must_be_regular_file) { file_utils::check_file_exists(string const &file, bool must_be_regular_file) {
struct stat info; struct stat info;
int r = ::stat(file.c_str(), &info); int r = ::stat(file.c_str(), &info);
if (r) if (r) {
throw runtime_error("Couldn't stat file"); ostringstream msg;
msg << file << ": " << base::error_string(errno);
throw runtime_error(msg.str());
}
if (must_be_regular_file && !S_ISREG(info.st_mode)) if (must_be_regular_file && !S_ISREG(info.st_mode))
throw runtime_error("Not a regular file"); throw runtime_error("Not a regular file");
@ -116,8 +119,11 @@ file_utils::get_file_length(string const &file) {
uint64_t nr_bytes; uint64_t nr_bytes;
int r = ::stat(file.c_str(), &info); int r = ::stat(file.c_str(), &info);
if (r) if (r) {
throw runtime_error("Couldn't stat path"); ostringstream msg;
msg << file << ": " << base::error_string(errno);
throw runtime_error(msg.str());
}
if (S_ISREG(info.st_mode)) if (S_ISREG(info.st_mode))
// It's okay to cast st_size to a uint64_t value. // It's okay to cast st_size to a uint64_t value.
@ -136,9 +142,11 @@ file_utils::get_file_length(string const &file) {
throw runtime_error("ioctl BLKGETSIZE64 failed"); throw runtime_error("ioctl BLKGETSIZE64 failed");
} }
::close(fd); ::close(fd);
} else } else {
// FIXME: needs a better message ostringstream msg;
throw runtime_error("bad path"); msg << file << ": " << "Not a block device or regular file";
throw runtime_error(msg.str());
}
return nr_bytes; return nr_bytes;
} }

View File

@ -82,9 +82,12 @@
(define-scenario (cache-restore missing-input-file) (define-scenario (cache-restore missing-input-file)
"the input file can't be found" "the input file can't be found"
(with-empty-metadata (md) (with-empty-metadata (md)
(run-fail-rcv (_ stderr) (cache-restore "-i no-such-file -o" md) (let ((bad-path "no-such-file"))
(assert-superblock-all-zeroes md) (run-fail-rcv (_ stderr) (cache-restore "-i" bad-path "-o" md)
(assert-starts-with "Couldn't stat file" stderr)))) (assert-superblock-all-zeroes md)
(assert-starts-with
(string-append bad-path ": No such file or directory")
stderr)))))
(define-scenario (cache-restore garbage-input-file) (define-scenario (cache-restore garbage-input-file)
"the input file is just zeroes" "the input file is just zeroes"
@ -264,9 +267,12 @@
(define-scenario (cache-repair missing-input-file) (define-scenario (cache-repair missing-input-file)
"the input file can't be found" "the input file can't be found"
(with-empty-metadata (md) (with-empty-metadata (md)
(run-fail-rcv (_ stderr) (cache-repair "-i no-such-file -o" md) (let ((bad-path "no-such-file"))
(assert-superblock-all-zeroes md) (run-fail-rcv (_ stderr) (cache-repair "-i no-such-file -o" md)
(assert-starts-with "Couldn't stat path" stderr)))) (assert-superblock-all-zeroes md)
(assert-starts-with
(string-append bad-path ": No such file or directory")
stderr)))))
(define-scenario (cache-repair garbage-input-file) (define-scenario (cache-repair garbage-input-file)
"the input file is just zeroes" "the input file is just zeroes"

View File

@ -152,9 +152,12 @@
(define-scenario (era-restore missing-input-file) (define-scenario (era-restore missing-input-file)
"the input file can't be found" "the input file can't be found"
(with-empty-metadata (md) (with-empty-metadata (md)
(run-fail-rcv (_ stderr) (era-restore "-i no-such-file -o" md) (let ((bad-path "no-such-file"))
(assert-superblock-all-zeroes md) (run-fail-rcv (_ stderr) (era-restore "-i no-such-file -o" md)
(assert-starts-with "Couldn't stat file" stderr)))) (assert-superblock-all-zeroes md)
(assert-starts-with
(string-append bad-path ": No such file or directory")
stderr)))))
(define-scenario (era-restore garbage-input-file) (define-scenario (era-restore garbage-input-file)
"the input file is just zeroes" "the input file is just zeroes"
@ -197,7 +200,9 @@
(with-empty-metadata (md) (with-empty-metadata (md)
(run-fail-rcv (stdout stderr) (era-restore "--quiet" "-i" bad-xml "-o" md) (run-fail-rcv (stdout stderr) (era-restore "--quiet" "-i" bad-xml "-o" md)
(assert-eof stdout) (assert-eof stdout)
(assert-starts-with "Couldn't stat file" stderr))))) (assert-starts-with
(string-append bad-xml ": No such file or directory")
stderr)))))
(define-scenario (era-restore q-fail) (define-scenario (era-restore q-fail)
"No output with --q(failing)" "No output with --q(failing)"
@ -205,7 +210,9 @@
(with-empty-metadata (md) (with-empty-metadata (md)
(run-fail-rcv (stdout stderr) (era-restore "-q" "-i" bad-xml "-o" md) (run-fail-rcv (stdout stderr) (era-restore "-q" "-i" bad-xml "-o" md)
(assert-eof stdout) (assert-eof stdout)
(assert-starts-with "Couldn't stat file" stderr))))) (assert-starts-with
(string-append bad-xml ": No such file or directory")
stderr)))))
;;;----------------------------------------------------------- ;;;-----------------------------------------------------------
;;; era_dump scenarios ;;; era_dump scenarios

View File

@ -40,16 +40,16 @@ fn accepts_help() -> Result<()> {
} }
#[test] #[test]
fn missing_metadata() -> Result<()> { fn missing_input_arg() -> Result<()> {
let stderr = run_fail(cache_check!())?; let stderr = run_fail(cache_check!())?;
assert!(stderr.contains(msg::MISSING_INPUT_ARG)); assert!(stderr.contains(msg::MISSING_INPUT_ARG));
Ok(()) Ok(())
} }
#[test] #[test]
fn no_such_metadata() -> Result<()> { fn input_file_not_found() -> Result<()> {
let stderr = run_fail(cache_check!("/arbitrary/filename"))?; let stderr = run_fail(cache_check!("/arbitrary/filename"))?;
assert!(stderr.contains("No such file or directory")); assert!(stderr.contains(msg::FILE_NOT_FOUND));
Ok(()) Ok(())
} }

View File

@ -20,7 +20,7 @@ use test_dir::TestDir;
#[cfg(not(feature = "rust_tests"))] #[cfg(not(feature = "rust_tests"))]
pub mod msg { pub mod msg {
pub const FILE_NOT_FOUND: &str = "Couldn't stat file"; pub const FILE_NOT_FOUND: &str = "No such file or directory";
pub const MISSING_INPUT_ARG: &str = "No input file provided"; pub const MISSING_INPUT_ARG: &str = "No input file provided";
pub const MISSING_OUTPUT_ARG: &str = "No output file provided"; pub const MISSING_OUTPUT_ARG: &str = "No output file provided";
} }

View File

@ -48,13 +48,13 @@ fn dont_repair_xml() -> Result<()> {
} }
#[test] #[test]
fn missing_input_file() -> Result<()> { fn input_file_not_found() -> Result<()> {
let mut td = TestDir::new()?; let mut td = TestDir::new()?;
let md = mk_zeroed_md(&mut td)?; let md = mk_zeroed_md(&mut td)?;
let stderr = run_fail(thin_repair!("-i", "no-such-file", "-o", &md))?; let stderr = run_fail(thin_repair!("-i", "no-such-file", "-o", &md))?;
assert!(superblock_all_zeroes(&md)?); assert!(superblock_all_zeroes(&md)?);
// TODO: replace with msg::FILE_NOT_FOUND once the rust version is ready // TODO: replace with msg::FILE_NOT_FOUND once the rust version is ready
assert!(stderr.contains("Couldn't stat file")); assert!(stderr.contains("No such file or directory"));
Ok(()) Ok(())
} }
@ -69,7 +69,7 @@ fn garbage_input_file() -> Result<()> {
} }
#[test] #[test]
fn missing_output_file() -> Result<()> { fn missing_output_arg() -> Result<()> {
let mut td = TestDir::new()?; let mut td = TestDir::new()?;
let md = mk_valid_md(&mut td)?; let md = mk_valid_md(&mut td)?;
let stderr = run_fail(thin_repair!("-i", &md))?; let stderr = run_fail(thin_repair!("-i", &md))?;

View File

@ -40,7 +40,7 @@ fn accepts_help() -> Result<()> {
} }
#[test] #[test]
fn no_input_file() -> Result<()> { fn missing_input_arg() -> Result<()> {
let mut td = TestDir::new()?; let mut td = TestDir::new()?;
let md = mk_zeroed_md(&mut td)?; let md = mk_zeroed_md(&mut td)?;
let stderr = run_fail(thin_restore!("-o", &md))?; let stderr = run_fail(thin_restore!("-o", &md))?;
@ -49,7 +49,7 @@ fn no_input_file() -> Result<()> {
} }
#[test] #[test]
fn missing_input_file() -> Result<()> { fn input_file_not_found() -> Result<()> {
let mut td = TestDir::new()?; let mut td = TestDir::new()?;
let md = mk_zeroed_md(&mut td)?; let md = mk_zeroed_md(&mut td)?;
let stderr = run_fail(thin_restore!("-i", "no-such-file", "-o", &md))?; let stderr = run_fail(thin_restore!("-i", "no-such-file", "-o", &md))?;
@ -69,7 +69,7 @@ fn garbage_input_file() -> Result<()> {
} }
#[test] #[test]
fn no_output_file() -> Result<()> { fn missing_output_arg() -> Result<()> {
let mut td = TestDir::new()?; let mut td = TestDir::new()?;
let xml = mk_valid_xml(&mut td)?; let xml = mk_valid_xml(&mut td)?;
let stderr = run_fail(thin_restore!("-i", &xml))?; let stderr = run_fail(thin_restore!("-i", &xml))?;