Added some tests utils for future refactoring

This commit is contained in:
Gilad Naaman 2021-04-10 21:24:30 +03:00
parent eb4971e6f4
commit cd3dba2481
2 changed files with 55 additions and 41 deletions

View file

@ -1,40 +1,3 @@
/// Assertion helper macro for [`CmdResult`] types
///
/// [`CmdResult`]: crate::tests::common::util::CmdResult
#[macro_export]
macro_rules! assert_empty_stderr(
($cond:expr) => (
if $cond.stderr.len() > 0 {
panic!("stderr: {}", $cond.stderr_str())
}
);
);
/// Assertion helper macro for [`CmdResult`] types
///
/// [`CmdResult`]: crate::tests::common::util::CmdResult
#[macro_export]
macro_rules! assert_empty_stdout(
($cond:expr) => (
if $cond.stdout.len() > 0 {
panic!("stdout: {}", $cond.stdout_str())
}
);
);
/// Assertion helper macro for [`CmdResult`] types
///
/// [`CmdResult`]: crate::tests::common::util::CmdResult
#[macro_export]
macro_rules! assert_no_error(
($cond:expr) => (
assert!($cond.success);
if $cond.stderr.len() > 0 {
panic!("stderr: {}", $cond.stderr_str())
}
);
);
/// Platform-independent helper for constructing a PathBuf from individual elements /// Platform-independent helper for constructing a PathBuf from individual elements
#[macro_export] #[macro_export]
macro_rules! path_concat { macro_rules! path_concat {

View file

@ -130,6 +130,11 @@ impl CmdResult {
self.code.expect("Program must be run first") self.code.expect("Program must be run first")
} }
pub fn code_is(&self, expected_code: i32) -> &CmdResult {
assert_eq!(self.code(), expected_code);
self
}
/// Returns the program's TempDir /// Returns the program's TempDir
/// Panics if not present /// Panics if not present
pub fn tmpd(&self) -> Rc<TempDir> { pub fn tmpd(&self) -> Rc<TempDir> {
@ -146,13 +151,25 @@ impl CmdResult {
/// asserts that the command resulted in a success (zero) status code /// asserts that the command resulted in a success (zero) status code
pub fn success(&self) -> &CmdResult { pub fn success(&self) -> &CmdResult {
assert!(self.success); if !self.success {
panic!(
"Command was expected to succeed.\nstdout = {}\n stderr = {}",
self.stdout_str(),
self.stderr_str()
);
}
self self
} }
/// asserts that the command resulted in a failure (non-zero) status code /// asserts that the command resulted in a failure (non-zero) status code
pub fn failure(&self) -> &CmdResult { pub fn failure(&self) -> &CmdResult {
assert!(!self.success); if self.success {
panic!(
"Command was expected to fail.\nstdout = {}\n stderr = {}",
self.stdout_str(),
self.stderr_str()
);
}
self self
} }
@ -168,7 +185,12 @@ impl CmdResult {
/// 1. you can not know exactly what stdout will be or /// 1. you can not know exactly what stdout will be or
/// 2. you know that stdout will also be empty /// 2. you know that stdout will also be empty
pub fn no_stderr(&self) -> &CmdResult { pub fn no_stderr(&self) -> &CmdResult {
assert!(self.stderr.is_empty()); if !self.stderr.is_empty() {
panic!(
"Expected stderr to be empty, but it's:\n{}",
self.stderr_str()
);
}
self self
} }
@ -179,7 +201,12 @@ impl CmdResult {
/// 1. you can not know exactly what stderr will be or /// 1. you can not know exactly what stderr will be or
/// 2. you know that stderr will also be empty /// 2. you know that stderr will also be empty
pub fn no_stdout(&self) -> &CmdResult { pub fn no_stdout(&self) -> &CmdResult {
assert!(self.stdout.is_empty()); if !self.stdout.is_empty() {
panic!(
"Expected stdout to be empty, but it's:\n{}",
self.stderr_str()
);
}
self self
} }
@ -281,6 +308,30 @@ impl CmdResult {
assert!(self.stderr_str().contains(cmp.as_ref())); assert!(self.stderr_str().contains(cmp.as_ref()));
self self
} }
pub fn stdout_does_not_contain<T: AsRef<str>>(&self, cmp: T) -> &CmdResult {
assert!(!self.stdout_str().contains(cmp.as_ref()));
self
}
pub fn stderr_does_not_contain<T: AsRef<str>>(&self, cmp: &T) -> &CmdResult {
assert!(!self.stderr_str().contains(cmp.as_ref()));
self
}
pub fn stdout_matches(&self, regex: &regex::Regex) -> &CmdResult {
if !regex.is_match(self.stdout_str()) {
panic!("Stdout does not match regex:\n{}", self.stdout_str())
}
self
}
pub fn stdout_does_not_match(&self, regex: &regex::Regex) -> &CmdResult {
if regex.is_match(self.stdout_str()) {
panic!("Stdout matches regex:\n{}", self.stdout_str())
}
self
}
} }
pub fn log_info<T: AsRef<str>, U: AsRef<str>>(msg: T, par: U) { pub fn log_info<T: AsRef<str>, U: AsRef<str>>(msg: T, par: U) {