From 82ff02156ecf2982d61654a0bd7b17ebc4b9fc1a Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Wed, 10 Jun 2026 07:50:38 +0200 Subject: [PATCH] fold: continue processing files after an open error (#12668) Should make test fold/multiple-files.sh pass --- src/uu/fold/src/fold.rs | 10 +++++++++- tests/by-util/test_fold.rs | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/uu/fold/src/fold.rs b/src/uu/fold/src/fold.rs index 97feeeea5..3412dde18 100644 --- a/src/uu/fold/src/fold.rs +++ b/src/uu/fold/src/fold.rs @@ -13,6 +13,7 @@ use unicode_width::UnicodeWidthChar; use uucore::display::Quotable; use uucore::error::{FromIo, UResult, USimpleError}; use uucore::format_usage; +use uucore::show; use uucore::translate; const TAB_WIDTH: usize = 8; @@ -164,7 +165,14 @@ fn fold( stdin_buf = stdin(); &mut stdin_buf as &mut dyn Read } else { - file_buf = File::open(Path::new(filename)).map_err_context(|| filename.to_string())?; + // Like GNU, report the error but keep processing the remaining files. + match File::open(Path::new(filename)) { + Ok(f) => file_buf = f, + Err(e) => { + show!(e.map_err_context(|| filename.to_string())); + continue; + } + } &mut file_buf as &mut dyn Read }); diff --git a/tests/by-util/test_fold.rs b/tests/by-util/test_fold.rs index 6cd854016..1d8f3eb59 100644 --- a/tests/by-util/test_fold.rs +++ b/tests/by-util/test_fold.rs @@ -930,6 +930,21 @@ fn test_bytewise_fold_at_read_buffer_boundary() { .stdout_is_bytes(expected); } +#[test] +fn test_continue_after_missing_file() { + // A nonexistent operand must not abort the run: the surrounding files are + // still folded, the error is reported on stderr, and the exit status is 1. + let ts = TestScenario::new(util_name!()); + ts.fixtures.write("first.txt", "hello\n"); + ts.fixtures.write("third.txt", "world\n"); + + ts.ucmd() + .args(&["first.txt", "absent.txt", "third.txt"]) + .fails_with_code(1) + .stdout_is("hello\nworld\n") + .stderr_is("fold: absent.txt: No such file or directory\n"); +} + #[test] fn test_obsolete_syntax() { new_ucmd!()