Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/commands/tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn cmd_tail(os: &dyn Kernel, args: &[String]) -> CommandResult {
}
} else {
// Keep last N lines in a ring buffer
let mut ring: VecDeque<String> = VecDeque::with_capacity(count + 1);
let mut ring: VecDeque<String> = VecDeque::new();
let mut line = String::new();
loop {
line.clear();
Expand Down
40 changes: 40 additions & 0 deletions tests/shell_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4481,6 +4481,46 @@ expect!(

// ── tail coverage ───────────────────────────────────────────────────

#[test]
fn tail_large_line_counts_preserve_small_input() {
let (rt, local) = rt();
rt.block_on(local.run_until(async {
let mut shell = Shell::builder().build().unwrap();
for count in [usize::MAX, usize::MAX / 2] {
for (cmd, expected) in [
(format!("printf 'a\\nb\\nc' | tail -n {count}"), "a\nb\nc"),
(
format!(
"printf 'a\\nb\\nc' > /tmp/tail-large; tail -n {count} /tmp/tail-large"
),
"a\nb\nc",
),
(format!("printf '' | tail -n {count}"), ""),
] {
let out = shell.run(&cmd).await;
assert_eq!(out.status, 0, "{cmd}: {}", out.stderr);
assert_eq!(out.stdout, expected, "{cmd}");
assert!(out.stderr.is_empty(), "{cmd}: {}", out.stderr);
}
}
}));
}

#[test]
fn tail_line_count_boundaries() {
let (rt, local) = rt();
rt.block_on(local.run_until(async {
let mut shell = Shell::builder().build().unwrap();
for (count, expected) in [(0, ""), (1, "c"), (3, "a\nb\nc"), (4, "a\nb\nc")] {
let cmd = format!("printf 'a\\nb\\nc' | tail -n {count}");
let out = shell.run(&cmd).await;
assert_eq!(out.status, 0, "{cmd}: {}", out.stderr);
assert_eq!(out.stdout, expected, "{cmd}");
assert!(out.stderr.is_empty(), "{cmd}: {}", out.stderr);
}
}));
}

expect!(
tail_from_file,
"printf 'a\\nb\\nc\\nd\\ne\\n' > /tmp/tf; tail -n 2 /tmp/tf",
Expand Down
Loading