1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

Merge pull request #2283 from Mikadore/master

Closing #1916 - Concluding the test refactor
This commit is contained in:
Sylvestre Ledru 2021-05-28 22:36:47 +02:00 committed by GitHub
commit 9442f26fdb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,7 +16,6 @@ use std::os::windows::fs::{symlink_dir, symlink_file};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio}; use std::process::{Child, Command, Stdio};
use std::rc::Rc; use std::rc::Rc;
use std::str::from_utf8;
use std::thread::sleep; use std::thread::sleep;
use std::time::Duration; use std::time::Duration;
use tempfile::TempDir; use tempfile::TempDir;
@ -62,54 +61,54 @@ pub struct CmdResult {
/// see [`success`] /// see [`success`]
success: bool, success: bool,
/// captured standard output after running the Command /// captured standard output after running the Command
stdout: String, stdout: Vec<u8>,
/// captured standard error after running the Command /// captured standard error after running the Command
stderr: String, stderr: Vec<u8>,
} }
impl CmdResult { impl CmdResult {
/// Returns a reference to the program's standard output as a slice of bytes /// Returns a reference to the program's standard output as a slice of bytes
pub fn stdout(&self) -> &[u8] { pub fn stdout(&self) -> &[u8] {
&self.stdout.as_bytes() &self.stdout
} }
/// Returns the program's standard output as a string slice /// Returns the program's standard output as a string slice
pub fn stdout_str(&self) -> &str { pub fn stdout_str(&self) -> &str {
&self.stdout std::str::from_utf8(&self.stdout).unwrap()
} }
/// Returns the program's standard output as a string /// Returns the program's standard output as a string
/// consumes self /// consumes self
pub fn stdout_move_str(self) -> String { pub fn stdout_move_str(self) -> String {
self.stdout String::from_utf8(self.stdout).unwrap()
} }
/// Returns the program's standard output as a vec of bytes /// Returns the program's standard output as a vec of bytes
/// consumes self /// consumes self
pub fn stdout_move_bytes(self) -> Vec<u8> { pub fn stdout_move_bytes(self) -> Vec<u8> {
Vec::from(self.stdout) self.stdout
} }
/// Returns a reference to the program's standard error as a slice of bytes /// Returns a reference to the program's standard error as a slice of bytes
pub fn stderr(&self) -> &[u8] { pub fn stderr(&self) -> &[u8] {
&self.stderr.as_bytes() &self.stderr
} }
/// Returns the program's standard error as a string slice /// Returns the program's standard error as a string slice
pub fn stderr_str(&self) -> &str { pub fn stderr_str(&self) -> &str {
&self.stderr std::str::from_utf8(&self.stderr).unwrap()
} }
/// Returns the program's standard error as a string /// Returns the program's standard error as a string
/// consumes self /// consumes self
pub fn stderr_move_str(self) -> String { pub fn stderr_move_str(self) -> String {
self.stderr String::from_utf8(self.stderr).unwrap()
} }
/// Returns the program's standard error as a vec of bytes /// Returns the program's standard error as a vec of bytes
/// consumes self /// consumes self
pub fn stderr_move_bytes(self) -> Vec<u8> { pub fn stderr_move_bytes(self) -> Vec<u8> {
Vec::from(self.stderr) self.stderr
} }
/// Returns the program's exit code /// Returns the program's exit code
@ -202,21 +201,21 @@ impl CmdResult {
/// passed in value, trailing whitespace are kept to force strict comparison (#1235) /// passed in value, trailing whitespace are kept to force strict comparison (#1235)
/// stdout_only is a better choice unless stderr may or will be non-empty /// stdout_only is a better choice unless stderr may or will be non-empty
pub fn stdout_is<T: AsRef<str>>(&self, msg: T) -> &CmdResult { pub fn stdout_is<T: AsRef<str>>(&self, msg: T) -> &CmdResult {
assert_eq!(self.stdout, String::from(msg.as_ref())); assert_eq!(self.stdout_str(), String::from(msg.as_ref()));
self self
} }
/// Like `stdout_is` but newlines are normalized to `\n`. /// Like `stdout_is` but newlines are normalized to `\n`.
pub fn normalized_newlines_stdout_is<T: AsRef<str>>(&self, msg: T) -> &CmdResult { pub fn normalized_newlines_stdout_is<T: AsRef<str>>(&self, msg: T) -> &CmdResult {
let msg = msg.as_ref().replace("\r\n", "\n"); let msg = msg.as_ref().replace("\r\n", "\n");
assert_eq!(self.stdout.replace("\r\n", "\n"), msg); assert_eq!(self.stdout_str().replace("\r\n", "\n"), msg);
self self
} }
/// asserts that the command resulted in stdout stream output, /// asserts that the command resulted in stdout stream output,
/// whose bytes equal those of the passed in slice /// whose bytes equal those of the passed in slice
pub fn stdout_is_bytes<T: AsRef<[u8]>>(&self, msg: T) -> &CmdResult { pub fn stdout_is_bytes<T: AsRef<[u8]>>(&self, msg: T) -> &CmdResult {
assert_eq!(self.stdout.as_bytes(), msg.as_ref()); assert_eq!(self.stdout, msg.as_ref());
self self
} }
@ -231,7 +230,7 @@ impl CmdResult {
/// stderr_only is a better choice unless stdout may or will be non-empty /// stderr_only is a better choice unless stdout may or will be non-empty
pub fn stderr_is<T: AsRef<str>>(&self, msg: T) -> &CmdResult { pub fn stderr_is<T: AsRef<str>>(&self, msg: T) -> &CmdResult {
assert_eq!( assert_eq!(
self.stderr.trim_end(), self.stderr_str().trim_end(),
String::from(msg.as_ref()).trim_end() String::from(msg.as_ref()).trim_end()
); );
self self
@ -240,7 +239,7 @@ impl CmdResult {
/// asserts that the command resulted in stderr stream output, /// asserts that the command resulted in stderr stream output,
/// whose bytes equal those of the passed in slice /// whose bytes equal those of the passed in slice
pub fn stderr_is_bytes<T: AsRef<[u8]>>(&self, msg: T) -> &CmdResult { pub fn stderr_is_bytes<T: AsRef<[u8]>>(&self, msg: T) -> &CmdResult {
assert_eq!(self.stderr.as_bytes(), msg.as_ref()); assert_eq!(self.stderr, msg.as_ref());
self self
} }
@ -886,8 +885,8 @@ impl UCommand {
tmpd: self.tmpd.clone(), tmpd: self.tmpd.clone(),
code: prog.status.code(), code: prog.status.code(),
success: prog.status.success(), success: prog.status.success(),
stdout: from_utf8(&prog.stdout).unwrap().to_string(), stdout: prog.stdout,
stderr: from_utf8(&prog.stderr).unwrap().to_string(), stderr: prog.stderr,
} }
} }