macro_rules! assert_command_stderr_matches {
    ($a_command:expr, $b_matcher:expr $(,)?) => { ... };
    ($a_command:expr, $b_matcher:expr, $($message:tt)+) => { ... };
}
Expand description

Assert a command stderr string is a match to a regex.

  • If true, return ().

  • Otherwise, call panic! with a message and the values of the expressions with their debug representations.

Examples

use std::process::Command;
use regex::Regex;

// Return Ok
let mut command = Command::new("printf");
let matcher = Regex::new(r"usage").unwrap();
assert_command_stderr_matches!(command, matcher);
//-> ()

// Panic with error message
let result = panic::catch_unwind(|| {
let mut command = Command::new("printf");
let matcher = Regex::new(r"xyz").unwrap();
assert_command_stderr_matches!(command, matcher);
//-> panic!
});
assert!(result.is_err());
let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
let expect = concat!(
    "assertion failed: `assert_command_stderr_matches!(left_command, right_matcher)`\n",
    " left_command label: `command`,\n",
    " left_command debug: `\"printf\"`,\n",
    "  right_matcher label: `matcher`,\n",
    "  right_matcher debug: `xyz`,\n",
    "               left: `\"usage: printf format [arguments ...]\\n\"`,\n",
    "              right: `xyz`"
);
assert_eq!(actual, expect);

// Panic with error message
let result = panic::catch_unwind(|| {
let mut command = Command::new("printf");
let matcher = Regex::new(r"xyz").unwrap();
assert_command_stderr_matches!(command, matcher, "message");
//-> panic!
});
assert!(result.is_err());
let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
let expect = "message";
assert_eq!(actual, expect);