Skip to content

Commit f1e051b

Browse files
authored
nl: continue if a file can't be opened (#11983)
1 parent dd80f63 commit f1e051b

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/uu/nl/src/nl.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
254254
);
255255
set_exit_code(1);
256256
} else {
257-
let reader = File::open(path).map_err_context(|| file.maybe_quote().to_string())?;
258-
let mut buffer = BufReader::new(reader);
259-
nl(&mut buffer, &mut stats, &settings)?;
257+
match File::open(path) {
258+
Ok(reader) => {
259+
let mut buffer = BufReader::new(reader);
260+
nl(&mut buffer, &mut stats, &settings)?;
261+
}
262+
Err(e) => {
263+
show_error!("{}", e.map_err_context(|| file.maybe_quote().to_string()));
264+
set_exit_code(1);
265+
}
266+
}
260267
}
261268
}
262269
}

tests/by-util/test_nl.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// file that was distributed with this source code.
55
//
66
// spell-checker:ignore binvalid finvalid hinvalid iinvalid linvalid nabcabc nabcabcabc ninvalid vinvalid winvalid dabc näää févr
7+
78
use uutests::{at_and_ucmd, new_ucmd, util::TestScenario, util_name};
89

910
#[test]
@@ -414,6 +415,19 @@ fn test_default_body_numbering_multiple_files_and_stdin() {
414415
.stdout_is(" 1\ta\n 2\tb\n 3\tc\n");
415416
}
416417

418+
#[test]
419+
fn test_default_body_numbering_multiple_files_with_non_existing_file() {
420+
let (at, mut ucmd) = at_and_ucmd!();
421+
422+
at.write("a.txt", "a");
423+
at.write("b.txt", "b");
424+
425+
ucmd.args(&["a.txt", "non_existing", "b.txt"])
426+
.fails_with_code(1)
427+
.stdout_is(" 1\ta\n 2\tb\n")
428+
.stderr_is("nl: non_existing: No such file or directory\n");
429+
}
430+
417431
#[test]
418432
fn test_body_numbering_all_lines_without_delimiter() {
419433
for arg in ["-ba", "--body-numbering=a"] {

0 commit comments

Comments
 (0)