nl: continue if a file can't be opened (#11983)

This commit is contained in:
Daniel Hofstetter
2026-05-16 22:20:02 +02:00
committed by GitHub
parent dd80f6368d
commit f1e051b8d8
2 changed files with 24 additions and 3 deletions
+10 -3
View File
@@ -254,9 +254,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
);
set_exit_code(1);
} else {
let reader = File::open(path).map_err_context(|| file.maybe_quote().to_string())?;
let mut buffer = BufReader::new(reader);
nl(&mut buffer, &mut stats, &settings)?;
match File::open(path) {
Ok(reader) => {
let mut buffer = BufReader::new(reader);
nl(&mut buffer, &mut stats, &settings)?;
}
Err(e) => {
show_error!("{}", e.map_err_context(|| file.maybe_quote().to_string()));
set_exit_code(1);
}
}
}
}
}
+14
View File
@@ -4,6 +4,7 @@
// file that was distributed with this source code.
//
// spell-checker:ignore binvalid finvalid hinvalid iinvalid linvalid nabcabc nabcabcabc ninvalid vinvalid winvalid dabc näää févr
use uutests::{at_and_ucmd, new_ucmd, util::TestScenario, util_name};
#[test]
@@ -414,6 +415,19 @@ fn test_default_body_numbering_multiple_files_and_stdin() {
.stdout_is(" 1\ta\n 2\tb\n 3\tc\n");
}
#[test]
fn test_default_body_numbering_multiple_files_with_non_existing_file() {
let (at, mut ucmd) = at_and_ucmd!();
at.write("a.txt", "a");
at.write("b.txt", "b");
ucmd.args(&["a.txt", "non_existing", "b.txt"])
.fails_with_code(1)
.stdout_is(" 1\ta\n 2\tb\n")
.stderr_is("nl: non_existing: No such file or directory\n");
}
#[test]
fn test_body_numbering_all_lines_without_delimiter() {
for arg in ["-ba", "--body-numbering=a"] {