1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

Fix various 'if_then_panic' clippy warnings

This commit is contained in:
Sylvestre Ledru 2021-10-10 09:57:39 +02:00
parent 32ec3f75ee
commit 00de952592
3 changed files with 42 additions and 59 deletions

View file

@ -28,9 +28,7 @@ macro_rules! fixture_path {
macro_rules! assert_fixture_exists { macro_rules! assert_fixture_exists {
($fname:expr) => {{ ($fname:expr) => {{
let fpath = fixture_path!($fname); let fpath = fixture_path!($fname);
if !fpath.exists() { assert!(fpath.exists(), "Fixture missing: {:?}", fpath);
panic!("Fixture missing: {:?}", fpath);
}
}}; }};
} }
@ -38,9 +36,7 @@ macro_rules! assert_fixture_exists {
macro_rules! assert_fixture_not_exists { macro_rules! assert_fixture_not_exists {
($fname:expr) => {{ ($fname:expr) => {{
let fpath = PathBuf::from(format!("./fixtures/dd/{}", $fname)); let fpath = PathBuf::from(format!("./fixtures/dd/{}", $fname));
if fpath.exists() { assert!(!fpath.exists(), "Fixture present: {:?}", fpath);
panic!("Fixture present: {:?}", fpath);
}
}}; }};
} }

View file

@ -35,9 +35,10 @@ fn test_file() {
{ {
let mut f = File::create(&file).unwrap(); let mut f = File::create(&file).unwrap();
// spell-checker:disable-next-line // spell-checker:disable-next-line
if f.write_all(b"abcdefghijklmnopqrstuvwxyz\n").is_err() { assert!(
panic!("Test setup failed - could not write file"); !f.write_all(b"abcdefghijklmnopqrstuvwxyz\n").is_err(),
} "Test setup failed - could not write file"
);
} }
new_ucmd!() new_ucmd!()
@ -75,9 +76,10 @@ fn test_2files() {
// spell-checker:disable-next-line // spell-checker:disable-next-line
for &(path, data) in &[(&file1, "abcdefghijklmnop"), (&file2, "qrstuvwxyz\n")] { for &(path, data) in &[(&file1, "abcdefghijklmnop"), (&file2, "qrstuvwxyz\n")] {
let mut f = File::create(&path).unwrap(); let mut f = File::create(&path).unwrap();
if f.write_all(data.as_bytes()).is_err() { assert!(
panic!("Test setup failed - could not write file"); !f.write_all(data.as_bytes()).is_err(),
} "Test setup failed - could not write file"
);
} }
new_ucmd!() new_ucmd!()
@ -126,9 +128,10 @@ fn test_from_mixed() {
let (data1, data2, data3) = ("abcdefg", "hijklmnop", "qrstuvwxyz\n"); let (data1, data2, data3) = ("abcdefg", "hijklmnop", "qrstuvwxyz\n");
for &(path, data) in &[(&file1, data1), (&file3, data3)] { for &(path, data) in &[(&file1, data1), (&file3, data3)] {
let mut f = File::create(&path).unwrap(); let mut f = File::create(&path).unwrap();
if f.write_all(data.as_bytes()).is_err() { assert!(
panic!("Test setup failed - could not write file"); !f.write_all(data.as_bytes()).is_err(),
} "Test setup failed - could not write file"
);
} }
new_ucmd!() new_ucmd!()

View file

@ -163,25 +163,23 @@ impl CmdResult {
/// asserts that the command resulted in a success (zero) status code /// asserts that the command resulted in a success (zero) status code
pub fn success(&self) -> &CmdResult { pub fn success(&self) -> &CmdResult {
if !self.success { assert!(
panic!( self.success,
"Command was expected to succeed.\nstdout = {}\n stderr = {}", "Command was expected to succeed.\nstdout = {}\n stderr = {}",
self.stdout_str(), self.stdout_str(),
self.stderr_str() self.stderr_str()
); );
}
self self
} }
/// asserts that the command resulted in a failure (non-zero) status code /// asserts that the command resulted in a failure (non-zero) status code
pub fn failure(&self) -> &CmdResult { pub fn failure(&self) -> &CmdResult {
if self.success { assert!(
panic!( !self.success,
"Command was expected to fail.\nstdout = {}\n stderr = {}", "Command was expected to fail.\nstdout = {}\n stderr = {}",
self.stdout_str(), self.stdout_str(),
self.stderr_str() self.stderr_str()
); );
}
self self
} }
@ -197,12 +195,11 @@ impl CmdResult {
/// 1. you can not know exactly what stdout will be or /// 1. you can not know exactly what stdout will be or
/// 2. you know that stdout will also be empty /// 2. you know that stdout will also be empty
pub fn no_stderr(&self) -> &CmdResult { pub fn no_stderr(&self) -> &CmdResult {
if !self.stderr.is_empty() { assert!(
panic!( self.stderr.is_empty(),
"Expected stderr to be empty, but it's:\n{}", "Expected stderr to be empty, but it's:\n{}",
self.stderr_str() self.stderr_str()
); );
}
self self
} }
@ -213,12 +210,11 @@ impl CmdResult {
/// 1. you can not know exactly what stderr will be or /// 1. you can not know exactly what stderr will be or
/// 2. you know that stderr will also be empty /// 2. you know that stderr will also be empty
pub fn no_stdout(&self) -> &CmdResult { pub fn no_stdout(&self) -> &CmdResult {
if !self.stdout.is_empty() { assert!(
panic!( self.stdout.is_empty(),
"Expected stdout to be empty, but it's:\n{}", "Expected stdout to be empty, but it's:\n{}",
self.stderr_str() self.stderr_str()
); );
}
self self
} }
@ -868,9 +864,7 @@ impl UCommand {
/// Add a parameter to the invocation. Path arguments are treated relative /// Add a parameter to the invocation. Path arguments are treated relative
/// to the test environment directory. /// to the test environment directory.
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut UCommand { pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut UCommand {
if self.has_run { assert!(!self.has_run, ALREADY_RUN);
panic!("{}", ALREADY_RUN);
}
self.comm_string.push(' '); self.comm_string.push(' ');
self.comm_string self.comm_string
.push_str(arg.as_ref().to_str().unwrap_or_default()); .push_str(arg.as_ref().to_str().unwrap_or_default());
@ -881,9 +875,7 @@ impl UCommand {
/// Add multiple parameters to the invocation. Path arguments are treated relative /// Add multiple parameters to the invocation. Path arguments are treated relative
/// to the test environment directory. /// to the test environment directory.
pub fn args<S: AsRef<OsStr>>(&mut self, args: &[S]) -> &mut UCommand { pub fn args<S: AsRef<OsStr>>(&mut self, args: &[S]) -> &mut UCommand {
if self.has_run { assert!(!self.has_run, MULTIPLE_STDIN_MEANINGLESS);
panic!("{}", MULTIPLE_STDIN_MEANINGLESS);
}
let strings = args let strings = args
.iter() .iter()
.map(|s| s.as_ref().to_os_string()) .map(|s| s.as_ref().to_os_string())
@ -901,9 +893,7 @@ impl UCommand {
/// provides standard input to feed in to the command when spawned /// provides standard input to feed in to the command when spawned
pub fn pipe_in<T: Into<Vec<u8>>>(&mut self, input: T) -> &mut UCommand { pub fn pipe_in<T: Into<Vec<u8>>>(&mut self, input: T) -> &mut UCommand {
if self.bytes_into_stdin.is_some() { assert!(!self.bytes_into_stdin.is_some(), MULTIPLE_STDIN_MEANINGLESS);
panic!("{}", MULTIPLE_STDIN_MEANINGLESS);
}
self.bytes_into_stdin = Some(input.into()); self.bytes_into_stdin = Some(input.into());
self self
} }
@ -918,9 +908,7 @@ impl UCommand {
/// This is typically useful to test non-standard workflows /// This is typically useful to test non-standard workflows
/// like feeding something to a command that does not read it /// like feeding something to a command that does not read it
pub fn ignore_stdin_write_error(&mut self) -> &mut UCommand { pub fn ignore_stdin_write_error(&mut self) -> &mut UCommand {
if self.bytes_into_stdin.is_none() { assert!(!self.bytes_into_stdin.is_none(), NO_STDIN_MEANINGLESS);
panic!("{}", NO_STDIN_MEANINGLESS);
}
self.ignore_stdin_write_error = true; self.ignore_stdin_write_error = true;
self self
} }
@ -930,9 +918,7 @@ impl UCommand {
K: AsRef<OsStr>, K: AsRef<OsStr>,
V: AsRef<OsStr>, V: AsRef<OsStr>,
{ {
if self.has_run { assert!(!self.has_run, ALREADY_RUN);
panic!("{}", ALREADY_RUN);
}
self.raw.env(key, val); self.raw.env(key, val);
self self
} }
@ -951,9 +937,7 @@ impl UCommand {
/// Spawns the command, feeds the stdin if any, and returns the /// Spawns the command, feeds the stdin if any, and returns the
/// child process immediately. /// child process immediately.
pub fn run_no_wait(&mut self) -> Child { pub fn run_no_wait(&mut self) -> Child {
if self.has_run { assert!(!self.has_run, ALREADY_RUN);
panic!("{}", ALREADY_RUN);
}
self.has_run = true; self.has_run = true;
log_info("run", &self.comm_string); log_info("run", &self.comm_string);
let mut child = self let mut child = self