Add helper function to get stderr of a command

But also get back the Assert object, which can then be used to check
whether the command succeeded or failed.

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-10-20 19:18:39 +02:00
parent 0c685fde73
commit 485ad3815f

View file

@ -22,6 +22,7 @@ use std::path::PathBuf;
use assert_fs::fixture::TempDir; use assert_fs::fixture::TempDir;
use assert_cmd::prelude::*; use assert_cmd::prelude::*;
use assert_cmd::assert::Assert;
pub fn make_temphome() -> TempDir { pub fn make_temphome() -> TempDir {
TempDir::new().unwrap().persist_if(std::env::var("IMAG_UI_TEST_PERSIST").is_ok()) TempDir::new().unwrap().persist_if(std::env::var("IMAG_UI_TEST_PERSIST").is_ok())
@ -53,6 +54,19 @@ pub fn stdout_of_command(mut command: Command) -> Vec<String> {
lines lines
} }
/// Run the passed command and get the stderr of it.
///
/// This function does _not_ ensure that stdin is inherited.
pub fn stderr_of_command(command: &mut Command) -> (Assert, Vec<String>) {
let assert = command.assert();
let lines = String::from_utf8(assert.get_output().stderr.clone())
.unwrap()
.lines()
.map(String::from)
.collect();
(assert, lines)
}
/// Create a PathBuf for a file in a TempDir /// Create a PathBuf for a file in a TempDir
pub fn file_path(tempdir: &TempDir, path_elements: &[&str]) -> PathBuf { pub fn file_path(tempdir: &TempDir, path_elements: &[&str]) -> PathBuf {
create_path_for(tempdir.path().to_path_buf(), path_elements) create_path_for(tempdir.path().to_path_buf(), path_elements)