From 61c730d1b313217bd1e0218e04bf9e9a582c639b Mon Sep 17 00:00:00 2001 From: Daniel Lungu Date: Thu, 25 Jan 2024 10:10:59 +0200 Subject: [PATCH] expand: Continue work when one of given files doesn't exist (#5873) * expand: continues work when one of given files doesn't exist * fixed test for nonexisting file --- src/uu/expand/src/expand.rs | 24 +++++++++++++++--------- tests/by-util/test_expand.rs | 9 +++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/uu/expand/src/expand.rs b/src/uu/expand/src/expand.rs index 1efb36c654c..94e12f451be 100644 --- a/src/uu/expand/src/expand.rs +++ b/src/uu/expand/src/expand.rs @@ -471,15 +471,21 @@ fn expand(options: &Options) -> UResult<()> { set_exit_code(1); continue; } - - let mut fh = open(file)?; - - while match fh.read_until(b'\n', &mut buf) { - Ok(s) => s > 0, - Err(_) => buf.is_empty(), - } { - expand_line(&mut buf, &mut output, ts, options) - .map_err_context(|| "failed to write output".to_string())?; + match open(file) { + Ok(mut fh) => { + while match fh.read_until(b'\n', &mut buf) { + Ok(s) => s > 0, + Err(_) => buf.is_empty(), + } { + expand_line(&mut buf, &mut output, ts, options) + .map_err_context(|| "failed to write output".to_string())?; + } + } + Err(e) => { + show_error!("{}", e); + set_exit_code(1); + continue; + } } } Ok(()) diff --git a/tests/by-util/test_expand.rs b/tests/by-util/test_expand.rs index c420f5ad5b9..a13f7b06d41 100644 --- a/tests/by-util/test_expand.rs +++ b/tests/by-util/test_expand.rs @@ -417,3 +417,12 @@ fn test_expand_directory() { .fails() .stderr_contains("expand: .: Is a directory"); } + +#[test] +fn test_nonexisting_file() { + new_ucmd!() + .args(&["nonexistent", "with-spaces.txt"]) + .fails() + .stderr_contains("expand: nonexistent: No such file or directory") + .stdout_contains_line("// !note: file contains significant whitespace"); +}