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`.
This commit is contained in:
Jeffrey Finkelstein 2021-05-16 11:30:10 -04:00
parent 7d2b6409e2
commit 659bf58a4c
3 changed files with 29 additions and 1 deletions

View file

@ -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)

View file

@ -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 <==
",
);
}

0
tests/fixtures/head/emptyfile.txt vendored Normal file
View file