From 659bf58a4c80201db406e4d7c2b3e8cef56931a3 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Sun, 16 May 2021 11:30:10 -0400 Subject: [PATCH] head: print headings when reading multiple files Fix a bug in which `head` failed to print headings for `stdin` inputs when reading from multiple files, and fix another bug in which `head` failed to print a blank line between the contents of a file and the heading for the next file when reading multiple files. The output now matches that of GNU `head`. --- src/uu/head/src/head.rs | 5 ++++- tests/by-util/test_head.rs | 25 +++++++++++++++++++++++++ tests/fixtures/head/emptyfile.txt | 0 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/head/emptyfile.txt diff --git a/src/uu/head/src/head.rs b/src/uu/head/src/head.rs index e050d26f6..faaeedd3f 100644 --- a/src/uu/head/src/head.rs +++ b/src/uu/head/src/head.rs @@ -405,7 +405,7 @@ fn uu_head(options: &HeadOptions) { for fname in &options.files { let res = match fname.as_str() { "-" => { - if options.verbose { + if (options.files.len() > 1 && !options.quiet) || options.verbose { if !first { println!(); } @@ -459,6 +459,9 @@ fn uu_head(options: &HeadOptions) { }, }; if (options.files.len() > 1 && !options.quiet) || options.verbose { + if !first { + println!(); + } println!("==> {} <==", name) } head_file(&mut file, options) diff --git a/tests/by-util/test_head.rs b/tests/by-util/test_head.rs index 4f009c800..2aedbdcbe 100755 --- a/tests/by-util/test_head.rs +++ b/tests/by-util/test_head.rs @@ -196,3 +196,28 @@ fn test_obsolete_extras() { .succeeds() .stdout_is("==> standard input <==\n1\02\03\04\05\0"); } + +#[test] +fn test_multiple_files() { + new_ucmd!() + .args(&["emptyfile.txt", "emptyfile.txt"]) + .succeeds() + .stdout_is("==> emptyfile.txt <==\n\n==> emptyfile.txt <==\n"); +} + +#[test] +fn test_multiple_files_with_stdin() { + new_ucmd!() + .args(&["emptyfile.txt", "-", "emptyfile.txt"]) + .pipe_in("hello\n") + .succeeds() + .stdout_is( + "==> emptyfile.txt <== + +==> standard input <== +hello + +==> emptyfile.txt <== +", + ); +} diff --git a/tests/fixtures/head/emptyfile.txt b/tests/fixtures/head/emptyfile.txt new file mode 100644 index 000000000..e69de29bb