Skip to content

Commit 61c730d

Browse files
authored
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
1 parent 1693640 commit 61c730d

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/uu/expand/src/expand.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -471,15 +471,21 @@ fn expand(options: &Options) -> UResult<()> {
471471
set_exit_code(1);
472472
continue;
473473
}
474-
475-
let mut fh = open(file)?;
476-
477-
while match fh.read_until(b'\n', &mut buf) {
478-
Ok(s) => s > 0,
479-
Err(_) => buf.is_empty(),
480-
} {
481-
expand_line(&mut buf, &mut output, ts, options)
482-
.map_err_context(|| "failed to write output".to_string())?;
474+
match open(file) {
475+
Ok(mut fh) => {
476+
while match fh.read_until(b'\n', &mut buf) {
477+
Ok(s) => s > 0,
478+
Err(_) => buf.is_empty(),
479+
} {
480+
expand_line(&mut buf, &mut output, ts, options)
481+
.map_err_context(|| "failed to write output".to_string())?;
482+
}
483+
}
484+
Err(e) => {
485+
show_error!("{}", e);
486+
set_exit_code(1);
487+
continue;
488+
}
483489
}
484490
}
485491
Ok(())

tests/by-util/test_expand.rs

+9
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,12 @@ fn test_expand_directory() {
417417
.fails()
418418
.stderr_contains("expand: .: Is a directory");
419419
}
420+
421+
#[test]
422+
fn test_nonexisting_file() {
423+
new_ucmd!()
424+
.args(&["nonexistent", "with-spaces.txt"])
425+
.fails()
426+
.stderr_contains("expand: nonexistent: No such file or directory")
427+
.stdout_contains_line("// !note: file contains significant whitespace");
428+
}

0 commit comments

Comments
 (0)