diff --git a/zebra-test/src/command.rs b/zebra-test/src/command.rs index 9bd10918..f0cffb19 100644 --- a/zebra-test/src/command.rs +++ b/zebra-test/src/command.rs @@ -223,7 +223,7 @@ impl TestOutput { pub fn stdout_contains(&self, regex: &str) -> Result<&Self> { let re = regex::Regex::new(regex)?; - let stdout = String::from_utf8_lossy(self.output.stdout.as_slice()); + let stdout = String::from_utf8_lossy(&self.output.stdout); for line in stdout.lines() { if re.is_match(line) { @@ -241,6 +241,37 @@ impl TestOutput { .with_section(stdout) } + pub fn stdout_equals(&self, s: &str) -> Result<&Self> { + let stdout = String::from_utf8_lossy(&self.output.stdout); + + if stdout == s { + return Ok(self); + } + + let command = || self.cmd.clone().header("Command:"); + let stdout = || stdout.into_owned().header("Stdout:"); + + Err(eyre!("stdout of command is not equal the given string")) + .with_section(command) + .with_section(stdout) + } + + pub fn stdout_matches(&self, regex: &str) -> Result<&Self> { + let re = regex::Regex::new(regex)?; + let stdout = String::from_utf8_lossy(&self.output.stdout); + + if re.is_match(&stdout) { + return Ok(self); + } + + let command = || self.cmd.clone().header("Command:"); + let stdout = || stdout.into_owned().header("Stdout:"); + + Err(eyre!("stdout of command is not equal to the given regex")) + .with_section(command) + .with_section(stdout) + } + /// Returns true if the program was killed, false if exit was by another reason. pub fn was_killed(&self) -> bool { #[cfg(unix)] diff --git a/zebrad/tests/acceptance.rs b/zebrad/tests/acceptance.rs index 7d88a779..0f12f595 100644 --- a/zebrad/tests/acceptance.rs +++ b/zebrad/tests/acceptance.rs @@ -29,6 +29,7 @@ fn generate_no_args() -> Result<()> { let output = child.wait_with_output()?; let output = output.assert_success()?; + // First line output.stdout_contains(r"# Default configuration for zebrad.")?; Ok(()) @@ -71,6 +72,10 @@ fn help_no_args() -> Result<()> { let output = child.wait_with_output()?; let output = output.assert_success()?; + // First line haves the version + output.stdout_contains(r"zebrad [0-9].[0-9].[0-9]")?; + + // Make sure we are in help by looking usage string output.stdout_contains(r"USAGE:")?; Ok(()) @@ -102,7 +107,7 @@ fn revhex_args() -> Result<()> { let output = child.wait_with_output()?; let output = output.assert_success()?; - output.stdout_contains(r"55ffee33")?; + output.stdout_equals("55ffee33\n")?; Ok(()) } @@ -213,7 +218,7 @@ fn version_no_args() -> Result<()> { let output = child.wait_with_output()?; let output = output.assert_success()?; - output.stdout_contains(r"zebrad [0-9].[0-9].[0-9]")?; + output.stdout_matches(r"^zebrad [0-9].[0-9].[0-9]-[A-Za-z]*.[0-9]\n$")?; Ok(()) }