Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tail: Reactivate ---presume-input-pipe option #3959

Merged
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
9 changes: 4 additions & 5 deletions src/uu/tail/src/tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ fn tail_file(
Ok(mut file) => {
input_service.print_header(input);
let mut reader;
if file.is_seekable(if input.is_stdin() { offset } else { 0 })
if !settings.presume_input_pipe
&& file.is_seekable(if input.is_stdin() { offset } else { 0 })
&& metadata.as_ref().unwrap().get_block_size() > 0
{
bounded_tail(&mut file, settings);
Expand Down Expand Up @@ -379,9 +380,9 @@ fn backwards_thru_file(file: &mut File, num_delimiters: u64, delimiter: u8) {
/// `BLOCK_SIZE` until we find the location of the first line/byte. This ends up
/// being a nice performance win for very large files.
fn bounded_tail(file: &mut File, settings: &Settings) {
debug_assert!(!settings.presume_input_pipe);

// Find the position in the file to start printing from.
// dbg!("bounded");
// dbg!(&settings.mode);
match &settings.mode {
FilterMode::Lines(Signum::Negative(count), delimiter) => {
backwards_thru_file(file, *count, *delimiter);
Expand Down Expand Up @@ -418,8 +419,6 @@ fn bounded_tail(file: &mut File, settings: &Settings) {
fn unbounded_tail<T: Read>(reader: &mut BufReader<T>, settings: &Settings) -> UResult<()> {
let stdout = stdout();
let mut writer = BufWriter::new(stdout.lock());
// dbg!("unbounded");
// dbg!(&settings.mode);
match &settings.mode {
FilterMode::Lines(Signum::Negative(count), sep) => {
let mut chunks = chunks::LinesChunkBuffer::new(*sep, *count);
Expand Down
76 changes: 73 additions & 3 deletions tests/by-util/test_tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
// spell-checker:ignore (libs) kqueue
// spell-checker:ignore (jargon) tailable untailable

// TODO: add tests for presume_input_pipe

extern crate tail;

use crate::common::random::*;
use crate::common::util::*;
use std::char::from_digit;
#[cfg(unix)]
Expand Down Expand Up @@ -2516,7 +2515,6 @@ fn test_illegal_seek() {
#[cfg(all(not(target_os = "android"), not(target_os = "windows")))] // FIXME: See https://github.com/uutils/coreutils/issues/3881
mod pipe_tests {
use super::*;
use crate::common::random::*;
use rand::distributions::Alphanumeric;
use tail::chunks::BUFFER_SIZE as CHUNK_BUFFER_SIZE;

Expand Down Expand Up @@ -3251,3 +3249,75 @@ fn test_seek_bytes_forward_outside_file() {
.run()
.stdout_is("");
}

// Some basic tests for ---presume-input-pipe. These tests build upon the
// debug_assert in bounded tail to detect that we're using the bounded_tail in
// case the option is given on command line.
#[cfg(all(not(target_os = "android"), not(target_os = "windows")))] // FIXME:
#[test]
fn test_args_when_presume_input_pipe_given_input_is_pipe() {
let random_string = RandomString::generate(AlphanumericNewline, 1000);
let random_string = random_string.as_str();

new_ucmd!()
.args(&["---presume-input-pipe", "-c", "-0"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();

new_ucmd!()
.args(&["---presume-input-pipe", "-c", "+0"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(random_string);

new_ucmd!()
.args(&["---presume-input-pipe", "-n", "-0"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();

new_ucmd!()
.args(&["---presume-input-pipe", "-n", "+0"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(random_string);
}

#[test]
fn test_args_when_presume_input_pipe_given_input_is_file() {
let random_string = RandomString::generate(AlphanumericNewline, 1000);
let random_string = random_string.as_str();

let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.write("data", random_string);

ts.ucmd()
.args(&["---presume-input-pipe", "-c", "-0", "data"])
.succeeds()
.no_stdout()
.no_stderr();

ts.ucmd()
.args(&["---presume-input-pipe", "-c", "+0", "data"])
.succeeds()
.stdout_only(random_string);

ts.ucmd()
.args(&["---presume-input-pipe", "-n", "-0", "data"])
.succeeds()
.no_stdout()
.no_stderr();

ts.ucmd()
.args(&["---presume-input-pipe", "-n", "+0", "data"])
.succeeds()
.stdout_only(random_string);
}