Module assert_command

Source
Expand description

Assert for comparing commands and their stdout & stderr.

These macros help with calling external commands, then capturing the standard output stream and standard error stream. See tutorial below.

These macros have corresponding the macros in the module assert_program_args.

§Macros for command standard output

Compare command standard output to another command standard output:

Compare command standard output to an expression:

Assert command standard output as a string:

§Macros for command standard error

Compare command standard error to another command standard error:

Compare command standard error to an expression:

Assert standard error as a string:

§Example

use assertables::*;
use std::process::Command;

let mut a = Command::new("bin/printf-stdout");
a.args(["%s", "alfa"]);
let mut b = Command::new("bin/printf-stdout");
b.args(["%s%s%s%s", "a", "l", "f", "a"]);
assert_command_stdout_eq!(a, b);

§Tutorial

Rust programs can call system commands.

For example this system command will print the word hello:

printf %s hello

Rust can create a system command then capture its standard output, by using:

let mut command = Command::new("printf"); command.args(["%s", "hello"]);
let stdout = command.output().unwrap().stdout;

Rust can compare a command’s standard output to another command’s standard output, by using:

let mut a_command = Command::new("printf"); a_command.args(["%s", "hello"]);
let mut b_command = Command::new("printf"); a_command.args(["%s", "world"]);
let a_stdout = a_command.output().unwrap().stdout;
let b_stdout = b_command.output().unwrap().stdout;
assert_ne!(a_stdout, b_stdout);

The assertables crate provides macros that do the same kind of processing, by automatically converting each command into standard output:

let mut a_command = Command::new("printf"); a_command.args(["%s", "hello"]);
let mut b_command = Command::new("printf"); a_command.args(["%s", "world"]);
assert_command_stdout_ne!(a_command, b_command);

Modules§

assert_command_stderr_contains
Assert a command stderr string contains a given containee.
assert_command_stderr_eq
Assert a command stderr string is equal to another.
assert_command_stderr_eq_x
Assert a command stderr string is equal to an expression.
assert_command_stderr_ge
Assert a command stderr string is greater than or equal to another.
assert_command_stderr_ge_x
Assert a command stderr string is greater than or equal to an expression.
assert_command_stderr_gt
Assert a command stderr string is greater than another.
assert_command_stderr_gt_x
Assert a command stderr string is greater than an expression.
assert_command_stderr_is_match
Assert a command stderr string is a match to a regex.
assert_command_stderr_le
Assert a command stderr string is less than or equal to another.
assert_command_stderr_le_x
Assert a command stderr string is less than or equal to an expression.
assert_command_stderr_lt
Assert a command stderr string is less than another.
assert_command_stderr_lt_x
Assert a command stderr string is less than an expression.
assert_command_stderr_ne
Assert a command stderr string is not equal to another.
assert_command_stderr_ne_x
Assert a command stderr string is not equal to an expression.
assert_command_stderr_string_contains
Assert a command stderr string contains a given containee.
assert_command_stderr_string_is_match
Assert a command stderr string is a match to a regex.
assert_command_stdout_contains
Assert a command stdout string contains a given containee.
assert_command_stdout_eq
Assert a command stdout string is equal to another.
assert_command_stdout_eq_x
Assert a command stdout string is equal to an expression.
assert_command_stdout_ge
Assert a command stdout string is greater than or equal to another.
assert_command_stdout_ge_x
Assert a command stdout string is greater than or equal to an expression.
assert_command_stdout_gt
Assert a command stdout string is greater than another.
assert_command_stdout_gt_x
Assert a command stdout string is greater than an expression.
assert_command_stdout_is_match
Assert a command stdout string is a match to a regex.
assert_command_stdout_le
Assert a command stdout string is less than or equal to another.
assert_command_stdout_le_x
Assert a command stdout string is less than or equal to an expression.
assert_command_stdout_lt
Assert a command stdout string is less than another.
assert_command_stdout_lt_x
Assert a command stdout string is less than an expression.
assert_command_stdout_ne
Assert a command stdout string is not equal to another.
assert_command_stdout_ne_x
Assert a command stdout string is not equal to an expression.
assert_command_stdout_string_contains
Assert a command stdout string contains a given containee.
assert_command_stdout_string_is_match
Assert a command stdout string is a match to a regex.