1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

head: fix bug with non-terminated files.

Fixes issue #7472.
Update to head app when printing all-but-last-n-lines of a file.
Code now checks if the last line of the input file is missing a terminating
newline character, and if so prints an extra line.
This aligns with GNU-head behavior.
Also improved performance of this usecase by using an optimized iterator
(memchr-iter) for searching through the input file.
This commit is contained in:
Karl McDowall 2025-03-18 09:05:34 -06:00
parent 5a0988c28d
commit 9957d0a64e
2 changed files with 88 additions and 13 deletions

View file

@ -148,6 +148,15 @@ fn test_zero_terminated_syntax_2() {
.stdout_is("x\0y");
}
#[test]
fn test_non_terminated_input() {
new_ucmd!()
.args(&["-n", "-1"])
.pipe_in("x\ny")
.succeeds()
.stdout_is("x\n");
}
#[test]
fn test_zero_terminated_negative_lines() {
new_ucmd!()
@ -448,12 +457,19 @@ fn test_all_but_last_lines_large_file() {
let scene = TestScenario::new(util_name!());
let fixtures = &scene.fixtures;
let seq_20000_file_name = "seq_20000";
let seq_20000_truncated_file_name = "seq_20000_truncated";
let seq_1000_file_name = "seq_1000";
scene
.cmd("seq")
.arg("20000")
.set_stdout(fixtures.make_file(seq_20000_file_name))
.succeeds();
// Create a file the same as seq_20000 except for the final terminating endline.
scene
.ucmd()
.args(&["-c", "-1", seq_20000_file_name])
.set_stdout(fixtures.make_file(seq_20000_truncated_file_name))
.succeeds();
scene
.cmd("seq")
.arg("1000")
@ -465,7 +481,7 @@ fn test_all_but_last_lines_large_file() {
.ucmd()
.args(&["-n", "-19000", seq_20000_file_name])
.succeeds()
.stdout_only_fixture("seq_1000");
.stdout_only_fixture(seq_1000_file_name);
scene
.ucmd()
@ -478,6 +494,25 @@ fn test_all_but_last_lines_large_file() {
.args(&["-n", "-20001", seq_20000_file_name])
.succeeds()
.stdout_only_fixture("emptyfile.txt");
// Confirm correct behavior when the input file doesn't end with a newline.
scene
.ucmd()
.args(&["-n", "-19000", seq_20000_truncated_file_name])
.succeeds()
.stdout_only_fixture(seq_1000_file_name);
scene
.ucmd()
.args(&["-n", "-20000", seq_20000_truncated_file_name])
.succeeds()
.stdout_only_fixture("emptyfile.txt");
scene
.ucmd()
.args(&["-n", "-20001", seq_20000_truncated_file_name])
.succeeds()
.stdout_only_fixture("emptyfile.txt");
}
#[cfg(all(