From 115b203b22377e8ae4dcc24fb6e32503fbb6d86c Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Tue, 14 May 2024 23:02:06 +0200 Subject: [PATCH] cksum: also show the error on stdout --- src/uu/cksum/src/cksum.rs | 7 +++---- tests/by-util/test_cksum.rs | 21 ++++++++++++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index 127e932c1..8645f2d4e 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -510,10 +510,9 @@ where match File::open(filename_to_check) { Ok(f) => Box::new(f), Err(err) => { - show!(err.map_err_context(|| format!( - "Failed to open file: {}", - filename_to_check - ))); + // yes, we have both stderr and stdout here + show!(err.map_err_context(|| filename_to_check.to_string())); + println!("{}: FAILED open or read", filename_to_check); failed_open_file += 1; // we could not open the file but we want to continue continue; diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index e60ed7e94..0e5d7f7f7 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -80,6 +80,21 @@ fn test_nonexisting_file() { .stderr_contains(format!("cksum: {file_name}: No such file or directory")); } +#[test] +fn test_nonexisting_file_out() { + let (at, mut ucmd) = at_and_ucmd!(); + at.write( + "f", + "MD5 (nonexistent) = e5773576fc75ff0f8eba14f61587ae28\n", + ); + + ucmd.arg("-c") + .arg("f") + .fails() + .stdout_contains("nonexistent: FAILED open or read") + .stderr_contains("cksum: nonexistent: No such file or directory"); +} + #[test] fn test_one_nonexisting_file() { let (at, mut ucmd) = at_and_ucmd!(); @@ -905,7 +920,7 @@ fn test_cksum_check_failed() { .arg("CHECKSUM") .fails(); - assert!(result.stderr_str().contains("Failed to open file: input")); + assert!(result.stderr_str().contains("input: No such file or directory")); assert!(result .stderr_str() .contains("2 lines are improperly formatted\n")); @@ -917,7 +932,7 @@ fn test_cksum_check_failed() { // without strict let result = scene.ucmd().arg("--check").arg("CHECKSUM").fails(); - assert!(result.stderr_str().contains("Failed to open file: input")); + assert!(result.stderr_str().contains("input: No such file or directory")); assert!(result .stderr_str() .contains("2 lines are improperly formatted\n")); @@ -945,7 +960,7 @@ fn test_cksum_check_failed() { .fails(); println!("result.stderr_str() {}", result.stderr_str()); println!("result.stdout_str() {}", result.stdout_str()); - assert!(result.stderr_str().contains("Failed to open file: input2")); + assert!(result.stderr_str().contains("input2: No such file or directory")); assert!(result .stderr_str() .contains("4 lines are improperly formatted\n"));