#![allow(
clippy::unicode_not_nfc,
clippy::cast_lossless,
clippy::cast_possible_truncation
)]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd")
))]
use nix::sys::signal::{Signal, kill};
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd")
))]
use nix::unistd::Pid;
use pretty_assertions::assert_eq;
use rand::distr::Alphanumeric;
use rstest::rstest;
use std::char::from_digit;
use std::fs::File;
use std::io::Write;
#[cfg(not(target_vendor = "apple"))]
use std::io::{Seek, SeekFrom};
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "android"),
not(target_os = "windows"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))]
use std::path::Path;
use std::process::Stdio;
use tail::chunks::BUFFER_SIZE as CHUNK_BUFFER_SIZE;
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "android"),
not(target_os = "windows"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))]
use tail::text;
use uutests::at_and_ucmd;
use uutests::new_ucmd;
use uutests::random::{AlphanumericNewline, RandomizedString};
#[cfg(unix)]
use uutests::unwrap_or_return;
use uutests::util::TestScenario;
#[cfg(unix)]
use uutests::util::expected_result;
#[cfg(unix)]
#[cfg(not(windows))]
use uutests::util::is_ci;
use uutests::util_name;
const FOOBAR_TXT: &str = "foobar.txt";
const FOOBAR_2_TXT: &str = "foobar2.txt";
const FOOBAR_WITH_NULL_TXT: &str = "foobar_with_null.txt";
#[allow(dead_code)]
const FOLLOW_NAME_TXT: &str = "follow_name.txt";
#[allow(dead_code)]
const FOLLOW_NAME_SHORT_EXP: &str = "follow_name_short.expected";
#[allow(dead_code)]
const FOLLOW_NAME_EXP: &str = "follow_name.expected";
const DEFAULT_SLEEP_INTERVAL_MILLIS: u64 = 1000;
#[cfg(unix)]
const INVALID_UTF8: u8 = 0x80;
#[cfg(windows)]
const INVALID_UTF16: u16 = 0xD800;
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
}
#[test]
fn test_stdin_default() {
new_ucmd!()
.pipe_in_fixture(FOOBAR_TXT)
.succeeds()
.stdout_is_fixture("foobar_stdin_default.expected")
.no_stderr();
}
#[test]
fn test_stdin_explicit() {
new_ucmd!()
.pipe_in_fixture(FOOBAR_TXT)
.arg("-")
.succeeds()
.stdout_is_fixture("foobar_stdin_default.expected")
.no_stderr();
}
#[test]
#[cfg(not(target_vendor = "apple"))] fn test_stdin_redirect_file() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.write("f", "foo");
ts.ucmd()
.set_stdin(File::open(at.plus("f")).unwrap())
.succeeds()
.stdout_is("foo");
ts.ucmd()
.set_stdin(File::open(at.plus("f")).unwrap())
.arg("-v")
.succeeds()
.stdout_only("==> standard input <==\nfoo");
}
#[test]
#[ignore = "disabled until fixed"]
#[cfg(not(target_vendor = "apple"))] fn test_stdin_redirect_file_follow() {
let (at, mut ucmd) = at_and_ucmd!();
at.write("f", "foo");
let mut p = ucmd
.arg("-f")
.set_stdin(File::open(at.plus("f")).unwrap())
.run_no_wait();
p.make_assertion_with_delay(500).is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stdout_only("foo");
}
#[test]
#[cfg(not(target_vendor = "apple"))] fn test_stdin_redirect_offset() {
let (at, mut ucmd) = at_and_ucmd!();
at.write("k", "1\n2\n");
let mut fh = File::open(at.plus("k")).unwrap();
fh.seek(SeekFrom::Start(2)).unwrap();
ucmd.set_stdin(fh).succeeds().stdout_only("2\n");
}
#[test]
#[cfg(not(target_vendor = "apple"))] fn test_stdin_redirect_offset2() {
let (at, mut ucmd) = at_and_ucmd!();
at.write("k", "1\n2\n");
at.write("l", "3\n4\n");
at.write("m", "5\n6\n");
let mut fh = File::open(at.plus("k")).unwrap();
fh.seek(SeekFrom::Start(2)).unwrap();
ucmd.set_stdin(fh)
.args(&["k", "-", "l", "m"])
.succeeds()
.stdout_only(
"==> k <==\n1\n2\n\n==> standard input <==\n2\n\n==> l <==\n3\n4\n\n==> m <==\n5\n6\n",
);
}
#[test]
fn test_nc_0_wo_follow() {
let ts = TestScenario::new(util_name!());
ts.ucmd().args(&["-n0", "missing"]).succeeds().no_output();
ts.ucmd().args(&["-c0", "missing"]).succeeds().no_output();
}
#[test]
#[cfg(all(unix, not(target_os = "freebsd")))]
fn test_nc_0_wo_follow2() {
use std::os::unix::fs::PermissionsExt;
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.make_file("unreadable")
.set_permissions(PermissionsExt::from_mode(0o000))
.unwrap();
ts.ucmd()
.args(&["-n0", "unreadable"])
.succeeds()
.no_output();
ts.ucmd()
.args(&["-c0", "unreadable"])
.succeeds()
.no_output();
}
#[test]
#[cfg(unix)]
fn test_permission_denied() {
use std::os::unix::fs::PermissionsExt;
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.make_file("unreadable")
.set_permissions(PermissionsExt::from_mode(0o000))
.unwrap();
ts.ucmd()
.arg("unreadable")
.fails_with_code(1)
.stderr_is("tail: cannot open 'unreadable' for reading: Permission denied\n")
.no_stdout();
}
#[test]
#[cfg(unix)]
fn test_permission_denied_multiple() {
use std::os::unix::fs::PermissionsExt;
let (at, mut ucmd) = at_and_ucmd!();
at.touch("file1");
at.touch("file2");
at.make_file("unreadable")
.set_permissions(PermissionsExt::from_mode(0o000))
.unwrap();
ucmd.args(&["file1", "unreadable", "file2"])
.fails_with_code(1)
.stderr_is("tail: cannot open 'unreadable' for reading: Permission denied\n")
.stdout_is("==> file1 <==\n\n==> file2 <==\n");
}
#[test]
fn test_follow_redirect_stdin_name_retry() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.touch("f");
let mut args = vec!["-F", "-"];
for _ in 0..2 {
ts.ucmd()
.set_stdin(File::open(at.plus("f")).unwrap())
.args(&args)
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: cannot follow '-' by name\n");
args.pop();
}
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_stdin_redirect_dir() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.mkdir("dir");
ts.ucmd()
.set_stdin(File::open(at.plus("dir")).unwrap())
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: error reading 'standard input': Is a directory\n");
ts.ucmd()
.set_stdin(File::open(at.plus("dir")).unwrap())
.arg("-")
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: error reading 'standard input': Is a directory\n");
}
#[test]
#[cfg(target_vendor = "apple")]
fn test_stdin_redirect_dir_when_target_os_is_macos() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.mkdir("dir");
ts.ucmd()
.set_stdin(File::open(at.plus("dir")).unwrap())
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: cannot open 'standard input' for reading: No such file or directory\n");
ts.ucmd()
.set_stdin(File::open(at.plus("dir")).unwrap())
.arg("-")
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: cannot open 'standard input' for reading: No such file or directory\n");
}
#[test]
#[cfg(unix)]
fn test_stdin_via_script_redirection_and_pipe() {
use std::os::unix::fs::PermissionsExt;
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let data = "line1\nline2\n";
at.write("file.txt", data);
let mut script = at.make_file("test.sh");
writeln!(script, "#!/usr/bin/env sh").unwrap();
writeln!(script, "tail").unwrap();
script
.set_permissions(PermissionsExt::from_mode(0o755))
.unwrap();
drop(script);
scene
.cmd("sh")
.current_dir(at.plus(""))
.arg("-c")
.arg("./test.sh < file.txt")
.succeeds()
.stdout_only(data);
scene
.cmd("sh")
.current_dir(at.plus(""))
.arg("-c")
.arg("cat file.txt | ./test.sh")
.succeeds()
.stdout_only(data);
}
#[test]
fn test_follow_stdin_descriptor() {
let ts = TestScenario::new(util_name!());
let mut args = vec!["-f", "-"];
for _ in 0..2 {
let mut p = ts
.ucmd()
.set_stdin(Stdio::piped())
.args(&args)
.run_no_wait();
p.make_assertion_with_delay(500).is_alive();
p.kill()
.make_assertion()
.with_all_output()
.no_stderr()
.no_stdout();
args.pop();
}
}
#[test]
fn test_follow_stdin_name_retry() {
let mut args = vec!["-F", "-"];
for _ in 0..2 {
new_ucmd!()
.args(&args)
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: cannot follow '-' by name\n");
args.pop();
}
}
#[test]
fn test_follow_bad_fd() {
}
#[test]
fn test_single_default() {
new_ucmd!()
.arg(FOOBAR_TXT)
.succeeds()
.stdout_is_fixture("foobar_single_default.expected");
}
#[test]
fn test_n_greater_than_number_of_lines() {
new_ucmd!()
.arg("-n")
.arg("99999999")
.arg(FOOBAR_TXT)
.succeeds()
.stdout_is_fixture(FOOBAR_TXT);
}
#[test]
fn test_null_default() {
new_ucmd!()
.arg("-z")
.arg(FOOBAR_WITH_NULL_TXT)
.succeeds()
.stdout_is_fixture("foobar_with_null_default.expected");
}
#[test]
#[cfg(not(target_os = "windows"))] fn test_follow_single() {
let (at, mut ucmd) = at_and_ucmd!();
let mut child = ucmd.arg("-f").arg(FOOBAR_TXT).run_no_wait();
let expected_fixture = "foobar_single_default.expected";
child
.make_assertion_with_delay(200)
.is_alive()
.with_current_output()
.stdout_only_fixture(expected_fixture);
let expected = "line1\nline2\n";
at.append(FOOBAR_TXT, expected);
child
.make_assertion_with_delay(DEFAULT_SLEEP_INTERVAL_MILLIS)
.is_alive();
child
.kill()
.make_assertion()
.with_current_output()
.stdout_only(expected);
}
#[test]
#[cfg(not(target_os = "windows"))] fn test_follow_non_utf8_bytes() {
let (at, mut ucmd) = at_and_ucmd!();
let mut child = ucmd.arg("-f").arg(FOOBAR_TXT).run_no_wait();
child
.make_assertion_with_delay(500)
.is_alive()
.with_current_output()
.stdout_only_fixture("foobar_single_default.expected");
let expected = [INVALID_UTF8, b'\n'];
at.append_bytes(FOOBAR_TXT, &expected);
child
.make_assertion_with_delay(DEFAULT_SLEEP_INTERVAL_MILLIS)
.with_current_output()
.stdout_only_bytes(expected);
child.make_assertion().is_alive();
child.kill();
}
#[test]
#[cfg(not(target_os = "windows"))] fn test_follow_multiple() {
let (at, mut ucmd) = at_and_ucmd!();
let mut child = ucmd
.arg("-f")
.arg(FOOBAR_TXT)
.arg(FOOBAR_2_TXT)
.run_no_wait();
child
.make_assertion_with_delay(500)
.is_alive()
.with_current_output()
.stdout_only_fixture("foobar_follow_multiple.expected");
let first_append = "trois\n";
at.append(FOOBAR_2_TXT, first_append);
child
.make_assertion_with_delay(DEFAULT_SLEEP_INTERVAL_MILLIS)
.with_current_output()
.stdout_only(first_append);
let second_append = "twenty\nthirty\n";
at.append(FOOBAR_TXT, second_append);
child
.make_assertion_with_delay(DEFAULT_SLEEP_INTERVAL_MILLIS)
.with_current_output()
.stdout_only_fixture("foobar_follow_multiple_appended.expected");
child.make_assertion().is_alive();
child.kill();
}
#[test]
#[cfg(not(target_os = "windows"))] fn test_follow_name_multiple() {
for argument in ["--follow=name", "--follo=nam", "--f=n"] {
let (at, mut ucmd) = at_and_ucmd!();
let mut child = ucmd
.arg(argument)
.arg(FOOBAR_TXT)
.arg(FOOBAR_2_TXT)
.run_no_wait();
child
.make_assertion_with_delay(500)
.is_alive()
.with_current_output()
.stdout_only_fixture("foobar_follow_multiple.expected");
let first_append = "trois\n";
at.append(FOOBAR_2_TXT, first_append);
child
.make_assertion_with_delay(DEFAULT_SLEEP_INTERVAL_MILLIS)
.with_current_output()
.stdout_only(first_append);
let second_append = "twenty\nthirty\n";
at.append(FOOBAR_TXT, second_append);
child
.make_assertion_with_delay(DEFAULT_SLEEP_INTERVAL_MILLIS)
.with_current_output()
.stdout_only_fixture("foobar_follow_multiple_appended.expected");
child.make_assertion().is_alive();
child.kill();
}
}
#[test]
fn test_follow_multiple_untailable() {
let expected_stdout = "==> DIR1 <==\n\n==> DIR2 <==\n";
let expected_stderr = "tail: error reading 'DIR1': Is a directory\n\
tail: DIR1: cannot follow end of this type of file; giving up on this name\n\
tail: error reading 'DIR2': Is a directory\n\
tail: DIR2: cannot follow end of this type of file; giving up on this name\n\
tail: no files remaining\n";
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("DIR1");
at.mkdir("DIR2");
ucmd.arg("-f")
.arg("DIR1")
.arg("DIR2")
.fails_with_code(1)
.stderr_is(expected_stderr)
.stdout_is(expected_stdout);
}
#[test]
fn test_follow_stdin_pipe() {
new_ucmd!()
.arg("-f")
.pipe_in_fixture(FOOBAR_TXT)
.succeeds()
.stdout_is_fixture("follow_stdin.expected")
.no_stderr();
}
#[test]
#[cfg(not(target_os = "windows"))] fn test_follow_invalid_pid() {
new_ucmd!()
.args(&["-f", "--pid=-1234"])
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: invalid PID: '-1234'\n");
new_ucmd!()
.args(&["-f", "--pid=abc"])
.fails()
.no_stdout()
.stderr_is("tail: invalid PID: 'abc': invalid digit found in string\n");
let max_pid = (i32::MAX as i64 + 1).to_string();
new_ucmd!()
.args(&["-f", "--pid", &max_pid])
.fails()
.no_stdout()
.stderr_is(format!(
"tail: invalid PID: '{max_pid}': number too large to fit in target type\n"
));
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd")
))] fn test_follow_with_pid() {
use std::process::Command;
let (at, mut ucmd) = at_and_ucmd!();
let dummy_cmd = "sh";
let mut dummy = Command::new(dummy_cmd).spawn().unwrap();
let pid = dummy.id();
let mut child = ucmd
.arg("-f")
.arg(format!("--pid={pid}"))
.arg(FOOBAR_TXT)
.arg(FOOBAR_2_TXT)
.run_no_wait();
child
.make_assertion_with_delay(500)
.is_alive()
.with_current_output()
.stdout_only_fixture("foobar_follow_multiple.expected");
let first_append = "trois\n";
at.append(FOOBAR_2_TXT, first_append);
child
.make_assertion_with_delay(DEFAULT_SLEEP_INTERVAL_MILLIS)
.with_current_output()
.stdout_only(first_append);
let second_append = "twenty\nthirty\n";
at.append(FOOBAR_TXT, second_append);
child
.make_assertion_with_delay(DEFAULT_SLEEP_INTERVAL_MILLIS)
.is_alive()
.with_current_output()
.stdout_only_fixture("foobar_follow_multiple_appended.expected");
kill(Pid::from_raw(i32::try_from(pid).unwrap()), Signal::SIGUSR1).unwrap();
let _ = dummy.wait();
child.delay(DEFAULT_SLEEP_INTERVAL_MILLIS);
let third_append = "should\nbe\nignored\n";
at.append(FOOBAR_TXT, third_append);
child
.make_assertion_with_delay(DEFAULT_SLEEP_INTERVAL_MILLIS)
.is_not_alive()
.with_current_output()
.no_stderr()
.no_stdout()
.success();
}
#[test]
fn test_single_big_args() {
const FILE: &str = "single_big_args.txt";
const EXPECTED_FILE: &str = "single_big_args_expected.txt";
const LINES: usize = 1_000_000;
const N_ARG: usize = 100_000;
let (at, mut ucmd) = at_and_ucmd!();
let mut big_input = at.make_file(FILE);
for i in 0..LINES {
writeln!(big_input, "Line {i}").expect("Could not write to FILE");
}
big_input.flush().expect("Could not flush FILE");
let mut big_expected = at.make_file(EXPECTED_FILE);
for i in (LINES - N_ARG)..LINES {
writeln!(big_expected, "Line {i}").expect("Could not write to EXPECTED_FILE");
}
big_expected.flush().expect("Could not flush EXPECTED_FILE");
ucmd.arg(FILE).arg("-n").arg(format!("{N_ARG}")).succeeds();
}
#[test]
fn test_bytes_single() {
new_ucmd!()
.arg("-c")
.arg("10")
.arg(FOOBAR_TXT)
.succeeds()
.stdout_is_fixture("foobar_bytes_single.expected");
}
#[test]
fn test_bytes_stdin() {
new_ucmd!()
.pipe_in_fixture(FOOBAR_TXT)
.arg("-c")
.arg("13")
.succeeds()
.stdout_is_fixture("foobar_bytes_stdin.expected")
.no_stderr();
}
#[test]
fn test_bytes_big() {
const FILE: &str = "test_bytes_big.txt";
const EXPECTED_FILE: &str = "test_bytes_big_expected.txt";
const BYTES: usize = 1_000_000;
const N_ARG: usize = 100_000;
let (at, mut ucmd) = at_and_ucmd!();
let mut big_input = at.make_file(FILE);
for i in 0..BYTES {
let digit = from_digit((i % 10) as u32, 10).unwrap();
write!(big_input, "{digit}").expect("Could not write to FILE");
}
big_input.flush().expect("Could not flush FILE");
let mut big_expected = at.make_file(EXPECTED_FILE);
for i in (BYTES - N_ARG)..BYTES {
let digit = from_digit((i % 10) as u32, 10).unwrap();
write!(big_expected, "{digit}").expect("Could not write to EXPECTED_FILE");
}
big_expected.flush().expect("Could not flush EXPECTED_FILE");
let result = ucmd
.arg(FILE)
.arg("-c")
.arg(format!("{N_ARG}"))
.succeeds()
.stdout_move_str();
let expected = at.read(EXPECTED_FILE);
assert_eq!(result.len(), expected.len());
for (actual_char, expected_char) in result.chars().zip(expected.chars()) {
assert_eq!(actual_char, expected_char);
}
}
#[test]
fn test_lines_with_size_suffix() {
const FILE: &str = "test_lines_with_size_suffix.txt";
const EXPECTED_FILE: &str = "test_lines_with_size_suffix_expected.txt";
const LINES: usize = 3_000;
const N_ARG: usize = 2 * 1024;
let (at, mut ucmd) = at_and_ucmd!();
let mut big_input = at.make_file(FILE);
for i in 0..LINES {
writeln!(big_input, "Line {i}").expect("Could not write to FILE");
}
big_input.flush().expect("Could not flush FILE");
let mut big_expected = at.make_file(EXPECTED_FILE);
for i in (LINES - N_ARG)..LINES {
writeln!(big_expected, "Line {i}").expect("Could not write to EXPECTED_FILE");
}
big_expected.flush().expect("Could not flush EXPECTED_FILE");
ucmd.arg(FILE)
.arg("-n")
.arg("2K")
.succeeds()
.stdout_is_fixture(EXPECTED_FILE);
}
#[test]
fn test_multiple_input_files() {
new_ucmd!()
.arg(FOOBAR_TXT)
.arg(FOOBAR_2_TXT)
.succeeds()
.no_stderr()
.stdout_is_fixture("foobar_follow_multiple.expected");
}
#[test]
fn test_multiple_input_files_missing() {
new_ucmd!()
.arg(FOOBAR_TXT)
.arg("missing1")
.arg(FOOBAR_2_TXT)
.arg("missing2")
.fails()
.stdout_is_fixture("foobar_follow_multiple.expected")
.stderr_is(
"tail: cannot open 'missing1' for reading: No such file or directory\n\
tail: cannot open 'missing2' for reading: No such file or directory\n",
);
}
#[test]
fn test_follow_missing() {
for follow_mode in &["--follow=descriptor", "--follow=name", "--fo=d", "--fo=n"] {
new_ucmd!()
.arg(follow_mode)
.arg("missing")
.fails_with_code(1)
.no_stdout()
.stderr_is(
"tail: cannot open 'missing' for reading: No such file or directory\n\
tail: no files remaining\n",
);
}
}
#[test]
fn test_follow_name_stdin() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.touch("FILE1");
at.touch("FILE2");
ts.ucmd()
.arg("--follow=name")
.arg("-")
.fails_with_code(1)
.stderr_is("tail: cannot follow '-' by name\n");
ts.ucmd()
.arg("--follow=name")
.arg("FILE1")
.arg("-")
.arg("FILE2")
.fails_with_code(1)
.stderr_is("tail: cannot follow '-' by name\n");
}
#[test]
fn test_multiple_input_files_with_suppressed_headers() {
new_ucmd!()
.arg(FOOBAR_TXT)
.arg(FOOBAR_2_TXT)
.arg("-q")
.succeeds()
.stdout_is_fixture("foobar_multiple_quiet.expected");
}
#[test]
fn test_multiple_input_quiet_flag_overrides_verbose_flag_for_suppressing_headers() {
new_ucmd!()
.arg(FOOBAR_TXT)
.arg(FOOBAR_2_TXT)
.arg("-v")
.arg("-q")
.succeeds()
.stdout_is_fixture("foobar_multiple_quiet.expected");
}
#[test]
fn test_dir() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("DIR");
ucmd.arg("DIR")
.fails_with_code(1)
.stderr_is("tail: error reading 'DIR': Is a directory\n");
}
#[test]
fn test_dir_follow() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.mkdir("DIR");
for mode in &["--follow=descriptor", "--follow=name"] {
ts.ucmd()
.arg(mode)
.arg("DIR")
.fails_with_code(1)
.no_stdout()
.stderr_is(
"tail: error reading 'DIR': Is a directory\n\
tail: DIR: cannot follow end of this type of file; giving up on this name\n\
tail: no files remaining\n",
);
}
}
#[test]
fn test_dir_follow_retry() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.mkdir("DIR");
ts.ucmd()
.arg("--follow=descriptor")
.arg("--retry")
.arg("DIR")
.fails_with_code(1)
.stderr_is(
"tail: warning: --retry only effective for the initial open\n\
tail: error reading 'DIR': Is a directory\n\
tail: DIR: cannot follow end of this type of file\n\
tail: no files remaining\n",
);
}
#[test]
fn test_negative_indexing() {
let positive_lines_index = new_ucmd!().arg("-n").arg("5").arg(FOOBAR_TXT).succeeds();
let negative_lines_index = new_ucmd!().arg("-n").arg("-5").arg(FOOBAR_TXT).succeeds();
let positive_bytes_index = new_ucmd!().arg("-c").arg("20").arg(FOOBAR_TXT).succeeds();
let negative_bytes_index = new_ucmd!().arg("-c").arg("-20").arg(FOOBAR_TXT).succeeds();
assert_eq!(positive_lines_index.stdout(), negative_lines_index.stdout());
assert_eq!(positive_bytes_index.stdout(), negative_bytes_index.stdout());
}
#[test]
fn test_sleep_interval() {
new_ucmd!().arg("-s").arg("10").arg(FOOBAR_TXT).succeeds();
new_ucmd!().arg("-s").arg(".1").arg(FOOBAR_TXT).succeeds();
new_ucmd!().arg("-s.1").arg(FOOBAR_TXT).succeeds();
new_ucmd!().arg("-s").arg("-1").arg(FOOBAR_TXT).fails();
new_ucmd!()
.arg("-s")
.arg("1..1")
.arg(FOOBAR_TXT)
.fails_with_code(1)
.stderr_contains("invalid number of seconds: '1..1'");
}
#[test]
fn test_positive_bytes() {
new_ucmd!()
.args(&["-c", "+3"])
.pipe_in("abcde")
.succeeds()
.stdout_is("cde");
}
#[test]
fn test_positive_zero_bytes() {
let ts = TestScenario::new(util_name!());
ts.ucmd()
.args(&["-c", "+0"])
.pipe_in("abcde")
.succeeds()
.stdout_is("abcde");
ts.ucmd()
.args(&["-c", "0"])
.pipe_in("abcde")
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
}
#[test]
fn test_positive_lines() {
new_ucmd!()
.args(&["-n", "+3"])
.pipe_in("a\nb\nc\nd\ne\n")
.succeeds()
.stdout_is("c\nd\ne\n");
}
#[test]
fn test_positive_lines_file() {
new_ucmd!()
.args(&["-n", "+7", "foobar.txt"])
.succeeds()
.stdout_is(
"siette
ocho
nueve
diez
once
",
);
}
#[test]
fn test_positive_bytes_file() {
new_ucmd!()
.args(&["-c", "+42", "foobar.txt"])
.succeeds()
.stdout_is(
"ho
nueve
diez
once
",
);
}
#[test]
fn test_obsolete_syntax_positive_lines() {
new_ucmd!()
.args(&["-3"])
.pipe_in("a\nb\nc\nd\ne\n")
.succeeds()
.stdout_is("c\nd\ne\n");
}
#[test]
fn test_small_file() {
new_ucmd!()
.args(&["-n -10"])
.pipe_in("a\nb\nc\nd\ne\n")
.succeeds()
.stdout_is("a\nb\nc\nd\ne\n");
}
#[test]
fn test_obsolete_syntax_small_file() {
new_ucmd!()
.args(&["-10"])
.pipe_in("a\nb\nc\nd\ne\n")
.succeeds()
.stdout_is("a\nb\nc\nd\ne\n");
}
#[test]
fn test_positive_zero_lines() {
let ts = TestScenario::new(util_name!());
ts.ucmd()
.args(&["-n", "+0"])
.pipe_in("a\nb\nc\nd\ne\n")
.succeeds()
.stdout_is("a\nb\nc\nd\ne\n");
ts.ucmd()
.args(&["-n", "0"])
.pipe_in("a\nb\nc\nd\ne\n")
.ignore_stdin_write_error()
.succeeds()
.no_stderr()
.no_stdout();
}
#[test]
fn test_invalid_num() {
new_ucmd!()
.args(&["-c", "1024R", "emptyfile.txt"])
.fails()
.stderr_str()
.starts_with("tail: invalid number of bytes: '1024R'");
new_ucmd!()
.args(&["-n", "1024R", "emptyfile.txt"])
.fails()
.stderr_str()
.starts_with("tail: invalid number of lines: '1024R'");
new_ucmd!()
.args(&["-c", "1Y", "emptyfile.txt"])
.fails()
.stderr_str()
.starts_with("tail: invalid number of bytes: '1Y': Value too large for defined data type");
new_ucmd!()
.args(&["-n", "1Y", "emptyfile.txt"])
.fails()
.stderr_str()
.starts_with("tail: invalid number of lines: '1Y': Value too large for defined data type");
new_ucmd!()
.args(&["-c", "-³"])
.fails()
.stderr_str()
.starts_with("tail: invalid number of bytes: '³'");
}
#[test]
fn test_num_with_undocumented_sign_bytes() {
const ALPHABET: &str = "abcdefghijklmnopqrstuvwxyz";
new_ucmd!()
.args(&["-c", "5"])
.pipe_in(ALPHABET)
.succeeds()
.stdout_is("vwxyz");
new_ucmd!()
.args(&["-c", "-5"])
.pipe_in(ALPHABET)
.succeeds()
.stdout_is("vwxyz");
new_ucmd!()
.args(&["-c", "+5"])
.pipe_in(ALPHABET)
.succeeds()
.stdout_is("efghijklmnopqrstuvwxyz");
}
#[test]
#[cfg(unix)]
fn test_bytes_for_funny_unix_files() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
for file in ["/proc/version", "/sys/kernel/profiling"] {
if !at.file_exists(file) {
continue;
}
let args = ["--bytes", "1", file];
let exp_result = unwrap_or_return!(expected_result(&ts, &args));
let result = ts.ucmd().args(&args).succeeds();
result
.stdout_is(exp_result.stdout_str())
.stderr_is(exp_result.stderr_str());
}
}
#[test]
fn test_retry1() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let file_name = "FILE";
at.touch(file_name);
ts.ucmd()
.arg(file_name)
.arg("--retry")
.succeeds()
.stderr_is("tail: warning: --retry ignored; --retry is useful only when following\n")
.code_is(0);
}
#[test]
fn test_retry2() {
let ts = TestScenario::new(util_name!());
let missing = "missing";
ts.ucmd()
.arg(missing)
.arg("--retry")
.fails_with_code(1)
.stderr_is(
"tail: warning: --retry ignored; --retry is useful only when following\n\
tail: cannot open 'missing' for reading: No such file or directory\n",
);
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_retry3() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let missing = "missing";
let expected_stderr = "tail: cannot open 'missing' for reading: No such file or directory\n\
tail: 'missing' has appeared; following new file\n";
let expected_stdout = "X\n";
let mut delay = 1500;
let mut args = vec!["--follow=name", "--retry", missing, "--use-polling"];
for _ in 0..2 {
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.touch(missing);
p.delay(delay);
at.truncate(missing, "X\n");
p.delay(delay);
p.kill()
.make_assertion()
.with_all_output()
.stderr_is(expected_stderr)
.stdout_is(expected_stdout);
at.remove(missing);
args.pop();
delay /= 3;
}
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_retry4() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let missing = "missing";
let expected_stderr = "tail: warning: --retry only effective for the initial open\n\
tail: cannot open 'missing' for reading: No such file or directory\n\
tail: 'missing' has appeared; following new file\n\
tail: missing: file truncated\n";
let expected_stdout = "X1\nX\n";
let mut args = vec![
"-s.1",
"--max-unchanged-stats=1",
"--follow=descriptor",
"--retry",
missing,
"---disable-inotify",
];
let mut delay = 1500;
for _ in 0..2 {
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.touch(missing);
p.delay(delay);
at.truncate(missing, "X1\n");
p.delay(delay);
at.truncate(missing, "X\n");
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stderr_is(expected_stderr)
.stdout_is(expected_stdout);
at.remove(missing);
args.pop();
delay /= 3;
}
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_retry5() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let missing = "missing";
let expected_stderr = "tail: warning: --retry only effective for the initial open\n\
tail: cannot open 'missing' for reading: No such file or directory\n\
tail: 'missing' has been replaced with an untailable file; giving up on this name\n\
tail: no files remaining\n";
let mut delay = 1500;
let mut args = vec!["--follow=descriptor", "--retry", missing, "--use-polling"];
for _ in 0..2 {
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.mkdir(missing);
p.delay(delay);
p.make_assertion()
.is_not_alive()
.with_all_output()
.stderr_only(expected_stderr)
.failure();
at.rmdir(missing);
args.pop();
delay /= 3;
}
}
#[test]
#[cfg(all(not(target_os = "windows"), not(target_os = "android")))] fn test_retry6() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let missing = "missing";
let existing = "existing";
at.touch(existing);
let expected_stderr = "tail: cannot open 'missing' for reading: No such file or directory\n";
let expected_stdout = "==> existing <==\nX\n";
let mut p = ts
.ucmd()
.arg("--follow=descriptor")
.arg("missing")
.arg("existing")
.run_no_wait();
let delay = 1000;
p.make_assertion_with_delay(delay).is_alive();
at.truncate(missing, "Y\n");
p.delay(delay);
at.truncate(existing, "X\n");
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stdout_is(expected_stdout)
.stderr_is(expected_stderr);
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_retry7() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let untailable = "untailable";
let expected_stderr = "tail: error reading 'untailable': Is a directory\n\
tail: untailable: cannot follow end of this type of file\n\
tail: 'untailable' has become accessible\n\
tail: 'untailable' has become inaccessible: No such file or directory\n\
tail: 'untailable' has been replaced with an untailable file\n\
tail: 'untailable' has become accessible\n";
let expected_stdout = "foo\nbar\n";
let mut args = vec![
"-s.1",
"--max-unchanged-stats=1",
"-F",
untailable,
"--use-polling",
];
let mut delay = 1500;
for _ in 0..2 {
at.mkdir(untailable);
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.rmdir(untailable);
at.truncate(untailable, "foo\n");
p.delay(delay);
at.remove(untailable);
p.delay(delay);
at.mkdir(untailable);
p.delay(delay);
at.rmdir(untailable);
at.truncate(untailable, "bar\n");
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stderr_is(expected_stderr)
.stdout_is(expected_stdout);
args.pop();
at.remove(untailable);
delay /= 3;
}
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_retry8() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let watched_file = Path::new("watched_file");
let parent_dir = Path::new("parent_dir");
let user_path = parent_dir.join(watched_file);
let parent_dir = parent_dir.to_str().unwrap();
let user_path = user_path.to_str().unwrap();
let expected_stderr = "\
tail: cannot open 'parent_dir/watched_file' for reading: No such file or directory\n\
tail: 'parent_dir/watched_file' has appeared; following new file\n\
tail: 'parent_dir/watched_file' has become inaccessible: No such file or directory\n\
tail: 'parent_dir/watched_file' has appeared; following new file\n";
let expected_stdout = "foo\nbar\n";
let delay = 1000;
let mut p = ts
.ucmd()
.arg("-F")
.arg("-s.1")
.arg("--max-unchanged-stats=1")
.arg(user_path)
.run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.mkdir(parent_dir); at.append(user_path, "foo\n");
p.delay(delay);
at.remove(user_path);
at.rmdir(parent_dir); p.delay(delay);
at.mkdir(parent_dir); at.append(user_path, "bar\n");
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stderr_is(expected_stderr)
.stdout_is(expected_stdout);
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "android"),
not(target_os = "windows"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_retry9() {
use text::BACKEND;
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let watched_file = Path::new("watched_file");
let parent_dir = Path::new("parent_dir");
let user_path = parent_dir.join(watched_file);
let parent_dir = parent_dir.to_str().unwrap();
let user_path = user_path.to_str().unwrap();
let expected_stderr = format!(
"\
tail: 'parent_dir/watched_file' has become inaccessible: No such file or directory\n\
tail: directory containing watched file was removed\n\
tail: {BACKEND} cannot be used, reverting to polling\n\
tail: 'parent_dir/watched_file' has appeared; following new file\n\
tail: 'parent_dir/watched_file' has become inaccessible: No such file or directory\n\
tail: 'parent_dir/watched_file' has appeared; following new file\n\
tail: 'parent_dir/watched_file' has become inaccessible: No such file or directory\n\
tail: 'parent_dir/watched_file' has appeared; following new file\n"
);
let expected_stdout = "foo\nbar\nfoo\nbar\n";
let delay = 1000;
at.mkdir(parent_dir);
at.truncate(user_path, "foo\n");
let mut p = ts
.ucmd()
.arg("-F")
.arg("-s.1")
.arg("--max-unchanged-stats=1")
.arg(user_path)
.run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.remove(user_path);
at.rmdir(parent_dir);
p.delay(delay);
at.mkdir(parent_dir);
at.truncate(user_path, "bar\n");
p.delay(delay);
at.remove(user_path);
at.rmdir(parent_dir);
p.delay(delay);
at.mkdir(parent_dir);
at.truncate(user_path, "foo\n");
p.delay(delay);
at.remove(user_path);
at.rmdir(parent_dir);
p.delay(delay);
at.mkdir(parent_dir);
at.truncate(user_path, "bar\n");
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stderr_is(expected_stderr)
.stdout_is(expected_stdout);
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "android"),
not(target_os = "windows"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_follow_descriptor_vs_rename1() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let file_a = "FILE_A";
let file_b = "FILE_B";
let file_c = "FILE_C";
let mut args = vec![
"--follow=descriptor",
"-s.1",
"--max-unchanged-stats=1",
file_a,
"---disable-inotify",
];
let mut delay = 1500;
for _ in 0..2 {
at.touch(file_a);
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.append(file_a, "A\n");
p.delay(delay);
at.rename(file_a, file_b);
p.delay(delay);
at.append(file_b, "B\n");
p.delay(delay);
at.rename(file_b, file_c);
p.delay(delay);
at.append(file_c, "C\n");
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stdout_only("A\nB\nC\n");
args.pop();
delay /= 3;
}
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "android"),
not(target_os = "windows"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_follow_descriptor_vs_rename2() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let file_a = "FILE_A";
let file_b = "FILE_B";
let file_c = "FILE_C";
let mut args = vec![
"--follow=descriptor",
"-s.1",
"--max-unchanged-stats=1",
file_a,
file_b,
"--verbose",
"---disable-inotify",
];
let mut delay = 1500;
for _ in 0..2 {
at.touch(file_a);
at.touch(file_b);
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.rename(file_a, file_c);
p.delay(delay);
at.append(file_c, "X\n");
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stdout_only("==> FILE_A <==\n\n==> FILE_B <==\n\n==> FILE_A <==\nX\n");
args.pop();
delay /= 3;
}
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_follow_name_retry_headers() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let file_a = "a";
let file_b = "b";
let mut args = vec![
"-F",
"-s.1",
"--max-unchanged-stats=1",
file_a,
file_b,
"---disable-inotify",
];
let mut delay = 1500;
for _ in 0..2 {
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.truncate(file_a, "x\n");
p.delay(delay);
at.truncate(file_b, "y\n");
p.delay(delay);
let expected_stderr = "tail: cannot open 'a' for reading: No such file or directory\n\
tail: cannot open 'b' for reading: No such file or directory\n\
tail: 'a' has appeared; following new file\n\
tail: 'b' has appeared; following new file\n";
let expected_stdout = "\n==> a <==\nx\n\n==> b <==\ny\n";
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stdout_is(expected_stdout)
.stderr_is(expected_stderr);
at.remove(file_a);
at.remove(file_b);
args.pop();
delay /= 3;
}
}
#[test]
#[cfg(all(not(target_os = "windows"), not(target_os = "android")))] fn test_follow_name_remove() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let source = FOLLOW_NAME_TXT;
let source_copy = "source_copy";
at.copy(source, source_copy);
let expected_stdout = at.read(FOLLOW_NAME_SHORT_EXP);
let expected_stderr = [
format!(
"{}: {source_copy}: No such file or directory\n{0}: no files remaining\n",
ts.util_name,
),
format!(
"{}: {source_copy}: No such file or directory\n",
ts.util_name,
),
];
let mut args = vec!["--follow=name", source_copy, "--use-polling"];
let mut delay = 1500;
#[allow(clippy::needless_range_loop)]
for i in 0..2 {
at.copy(source, source_copy);
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.remove(source_copy);
p.delay(delay);
if i == 0 {
p.make_assertion()
.is_not_alive()
.with_all_output()
.stdout_is(&expected_stdout)
.stderr_is(&expected_stderr[i])
.failure();
} else {
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stdout_is(&expected_stdout)
.stderr_is(&expected_stderr[i]);
}
args.pop();
delay /= 3;
}
}
#[test]
#[cfg(all(
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd")
))] fn test_follow_name_truncate1() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let source = FOLLOW_NAME_TXT;
let backup = "backup";
let expected_stdout = at.read(FOLLOW_NAME_EXP);
let expected_stderr = format!("{}: {source}: file truncated\n", ts.util_name);
let args = ["--follow=name", source];
let mut p = ts.ucmd().args(&args).run_no_wait();
let delay = 1000;
p.make_assertion().is_alive();
at.copy(source, backup);
p.delay(delay);
at.touch(source); p.delay(delay);
at.copy(backup, source);
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stderr_is(expected_stderr)
.stdout_is(expected_stdout);
}
#[test]
#[cfg(all(
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd")
))] fn test_follow_name_truncate2() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let source = "file";
at.touch(source);
let expected_stdout = "x\nx\nx\nx\n";
let expected_stderr = format!("{}: {source}: file truncated\n", ts.util_name);
let args = ["--follow=name", source];
let mut p = ts.ucmd().args(&args).run_no_wait();
let delay = 1000;
p.make_assertion().is_alive();
at.append(source, "x\n");
p.delay(delay);
at.append(source, "x\n");
p.delay(delay);
at.append(source, "x\n");
p.delay(delay);
at.truncate(source, "x\n");
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stderr_is(expected_stderr)
.stdout_is(expected_stdout);
}
#[test]
#[cfg(not(target_os = "windows"))] fn test_follow_name_truncate3() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let source = "file";
at.touch(source);
let expected_stdout = "x\n";
let args = ["--follow=name", source];
let mut p = ts.ucmd().args(&args).run_no_wait();
let delay = 1000;
p.make_assertion_with_delay(delay).is_alive();
at.truncate(source, "x\n");
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stdout_only(expected_stdout);
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(feature = "feat_selinux") // flaky
))] fn test_follow_name_truncate4() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let mut args = vec!["-s.1", "--max-unchanged-stats=1", "-F", "file"];
let mut delay = 500;
for i in 0..2 {
at.append("file", "foobar\n");
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.truncate("file", "foobar\n");
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stdout_only("foobar\n");
at.remove("file");
if i == 0 {
args.push("---disable-inotify");
}
delay *= 3;
}
}
#[test]
#[cfg(not(target_os = "windows"))] fn test_follow_truncate_fast() {
if is_ci() {
println!("TEST SKIPPED (too fast for CI)");
return;
}
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let mut args = vec!["-s.1", "--max-unchanged-stats=1", "f", "---disable-inotify"];
let follow = vec!["-f", "-F"];
let mut delay = 1000;
for _ in 0..2 {
for mode in &follow {
args.push(mode);
at.truncate("f", "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n");
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.truncate("f", "11\n12\n13\n14\n15\n");
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stderr_is("tail: f: file truncated\n")
.stdout_is("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n");
args.pop();
}
args.pop();
delay = 250;
}
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_follow_name_move_create1() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let source = FOLLOW_NAME_TXT;
let backup = "backup";
#[cfg(target_os = "linux")]
let expected_stdout = at.read(FOLLOW_NAME_EXP);
#[cfg(target_os = "linux")]
let expected_stderr = format!(
"{}: {source}: No such file or directory\n{0}: '{source}' has appeared; following new file\n",
ts.util_name,
);
#[cfg(not(target_os = "linux"))]
let expected_stdout = at.read(FOLLOW_NAME_SHORT_EXP);
#[cfg(not(target_os = "linux"))]
let expected_stderr = format!("{}: {source}: No such file or directory\n", ts.util_name);
let delay = 500;
let args = ["--follow=name", source];
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.rename(source, backup);
p.delay(delay);
at.copy(backup, source);
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stderr_is(expected_stderr)
.stdout_is(expected_stdout);
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "android"),
not(target_os = "windows"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_follow_name_move_create2() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
for n in ["1", "2", "3", "4", "5", "6", "7", "8", "9"] {
at.touch(n);
}
let mut args = vec![
"-s.1",
"--max-unchanged-stats=1",
"-q",
"-F",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
];
let mut delay = 500;
for i in 0..2 {
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.truncate("9", "x\n");
p.delay(delay);
at.rename("1", "f");
p.delay(delay);
at.truncate("1", "a\n");
p.delay(delay);
let expected_stdout = if args.len() == 14 {
"a\nx\na\n"
} else {
"x\na\n"
};
let expected_stderr = "tail: '1' has become inaccessible: No such file or directory\n\
tail: '1' has appeared; following new file\n";
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stderr_is(expected_stderr)
.stdout_is(expected_stdout);
at.remove("f");
if i == 0 {
args.push("---disable-inotify");
}
delay *= 3;
}
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_follow_name_move1() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let source = FOLLOW_NAME_TXT;
let backup = "backup";
let expected_stdout = at.read(FOLLOW_NAME_SHORT_EXP);
let expected_stderr = [
format!("{}: {source}: No such file or directory\n", ts.util_name),
format!(
"{}: {source}: No such file or directory\n{0}: no files remaining\n",
ts.util_name,
),
];
let mut args = vec!["--follow=name", source];
let mut delay = 500;
#[allow(clippy::needless_range_loop)]
for i in 0..2 {
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.rename(source, backup);
p.delay(delay);
if i == 0 {
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stderr_is(&expected_stderr[i])
.stdout_is(&expected_stdout);
} else {
p.make_assertion()
.is_not_alive()
.with_all_output()
.stderr_is(&expected_stderr[i])
.stdout_is(&expected_stdout)
.failure();
}
at.rename(backup, source);
args.push("--use-polling");
delay *= 3;
}
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_follow_name_move2() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let file1 = "file1";
let file2 = "file2";
let expected_stdout = format!(
"==> {file1} <==\n{file1}_content\n\n==> {file2} <==\n{file2}_content\n{file1}_content\n\
more_{file2}_content\n\n==> {file1} <==\nmore_{file1}_content\n"
);
let mut expected_stderr = format!(
"{0}: {1}: No such file or directory\n\
{0}: '{2}' has been replaced; following new file\n\
{0}: '{1}' has appeared; following new file\n",
ts.util_name, file1, file2
);
let mut args = vec!["--follow=name", file1, file2];
let mut delay = 500;
for i in 0..2 {
at.truncate(file1, "file1_content\n");
at.truncate(file2, "file2_content\n");
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.rename(file1, file2);
p.delay(delay);
at.append(file2, "more_file2_content\n");
p.delay(delay);
at.append(file1, "more_file1_content\n");
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stderr_is(&expected_stderr)
.stdout_is(&expected_stdout);
if i == 0 {
args.push("--use-polling");
}
delay *= 3;
expected_stderr = format!(
"{0}: '{2}' has been replaced; following new file\n\
{0}: {1}: No such file or directory\n\
{0}: '{1}' has appeared; following new file\n",
ts.util_name, file1, file2
);
}
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_follow_name_move_retry1() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let source = FOLLOW_NAME_TXT;
let backup = "backup";
let expected_stderr = format!(
"{0}: '{1}' has become inaccessible: No such file or directory\n\
{0}: '{1}' has appeared; following new file\n",
ts.util_name, source
);
let expected_stdout = "tailed\nnew content\n";
let mut args = vec!["--follow=name", "--retry", source, "--use-polling"];
let mut delay = 1500;
for _ in 0..2 {
at.touch(source);
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.append(source, "tailed\n");
p.delay(delay);
at.rename(source, backup);
p.delay(delay);
at.truncate(backup, "new content\n");
p.delay(delay);
at.rename(backup, source);
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stderr_is(&expected_stderr)
.stdout_is(expected_stdout);
at.remove(source);
args.pop();
delay /= 3;
}
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))] fn test_follow_name_move_retry2() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let file1 = "a";
let file2 = "b";
let expected_stdout = format!(
"==> {file1} <==\n\n==> {file2} <==\n\n==> {file1} <==\nx\n\n==> {file2} <==\
\nx\n\n==> {file1} <==\nx2\n\n==> {file2} <==\ny\n\n==> {file1} <==\nz\n"
);
let mut expected_stderr = format!(
"{0}: '{1}' has become inaccessible: No such file or directory\n\
{0}: '{2}' has been replaced; following new file\n\
{0}: '{1}' has appeared; following new file\n",
ts.util_name, file1, file2
);
let mut args = vec!["-s.1", "--max-unchanged-stats=1", "-F", file1, file2];
let mut delay = 500;
for i in 0..2 {
at.touch(file1);
at.touch(file2);
let mut p = ts.ucmd().args(&args).run_no_wait();
p.make_assertion_with_delay(delay).is_alive();
at.truncate(file1, "x\n");
p.delay(delay);
at.rename(file1, file2);
p.delay(delay);
at.truncate(file1, "x2\n");
p.delay(delay);
at.append(file2, "y\n");
p.delay(delay);
at.append(file1, "z\n");
p.delay(delay);
p.make_assertion().is_alive();
p.kill()
.make_assertion()
.with_all_output()
.stderr_is(&expected_stderr)
.stdout_is(&expected_stdout);
at.remove(file1);
at.remove(file2);
if i == 0 {
args.push("--use-polling");
}
delay *= 3;
expected_stderr = format!(
"{0}: '{2}' has been replaced; following new file\n\
{0}: '{1}' has become inaccessible: No such file or directory\n\
{0}: '{1}' has appeared; following new file\n",
ts.util_name, file1, file2
);
}
}
#[test]
#[cfg(not(target_os = "windows"))] fn test_follow_inotify_only_regular() {
let ts = TestScenario::new(util_name!());
let mut p = ts.ucmd().arg("-f").arg("/dev/null").run_no_wait();
p.make_assertion_with_delay(200).is_alive();
p.kill()
.make_assertion()
.with_all_output()
.no_stderr()
.no_stdout();
}
#[test]
fn test_no_such_file() {
new_ucmd!()
.arg("missing")
.fails_with_code(1)
.stderr_is("tail: cannot open 'missing' for reading: No such file or directory\n")
.no_stdout();
}
#[test]
fn test_no_trailing_newline() {
new_ucmd!().pipe_in("x").succeeds().stdout_only("x");
}
#[test]
fn test_lines_zero_terminated() {
new_ucmd!()
.args(&["-z", "-n", "2"])
.pipe_in("a\0b\0c\0d\0e\0")
.succeeds()
.stdout_only("d\0e\0");
new_ucmd!()
.args(&["-z", "-n", "+2"])
.pipe_in("a\0b\0c\0d\0e\0")
.succeeds()
.stdout_only("b\0c\0d\0e\0");
}
#[test]
fn test_presume_input_pipe_default() {
new_ucmd!()
.arg("---presume-input-pipe")
.pipe_in_fixture(FOOBAR_TXT)
.succeeds()
.stdout_is_fixture("foobar_stdin_default.expected")
.no_stderr();
}
#[test]
#[cfg(not(windows))]
fn test_fifo() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.mkfifo("FIFO");
let mut p = ts.ucmd().arg("FIFO").run_no_wait();
p.make_assertion_with_delay(500).is_alive();
p.kill()
.make_assertion()
.with_all_output()
.no_stderr()
.no_stdout();
for arg in ["-f", "-F"] {
let mut p = ts.ucmd().arg(arg).arg("FIFO").run_no_wait();
p.make_assertion_with_delay(500).is_alive();
p.kill()
.make_assertion()
.with_all_output()
.no_stderr()
.no_stdout();
}
}
#[test]
#[cfg(unix)]
#[ignore = "disabled until fixed"]
fn test_illegal_seek() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.append("FILE", "foo\n");
at.mkfifo("FIFO");
let mut p = ts.ucmd().arg("FILE").run_no_wait();
p.make_assertion_with_delay(500).is_alive();
at.rename("FILE", "FIFO");
p.delay(500);
p.make_assertion().is_alive();
let expected_stderr = "tail: 'FILE' has been replaced; following new file\n\
tail: FILE: cannot seek to offset 0: Illegal seek\n";
p.kill()
.make_assertion()
.with_all_output()
.stderr_is(expected_stderr)
.stdout_is("foo\n")
.code_is(1); }
#[test]
fn test_pipe_when_lines_option_value_is_higher_than_contained_lines() {
let test_string = "a\nb\n";
new_ucmd!()
.args(&["-n", "3"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(test_string);
new_ucmd!()
.args(&["-n", "4"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(test_string);
new_ucmd!()
.args(&["-n", "999"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(test_string);
new_ucmd!()
.args(&["-n", "+3"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
new_ucmd!()
.args(&["-n", "+4"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
new_ucmd!()
.args(&["-n", "+999"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
}
#[test]
fn test_pipe_when_negative_lines_option_given_no_newline_at_eof() {
let test_string = "a\nb";
new_ucmd!()
.args(&["-n", "0"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
new_ucmd!()
.args(&["-n", "1"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("b");
new_ucmd!()
.args(&["-n", "2"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("a\nb");
}
#[test]
fn test_pipe_when_positive_lines_option_given_no_newline_at_eof() {
let test_string = "a\nb";
new_ucmd!()
.args(&["-n", "+0"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("a\nb");
new_ucmd!()
.args(&["-n", "+1"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("a\nb");
new_ucmd!()
.args(&["-n", "+2"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("b");
}
#[test]
fn test_pipe_when_lines_option_given_multibyte_utf8_characters() {
let test_string = "𝅘𝅥𝅮\n⏻\nƒ\na";
new_ucmd!()
.args(&["-n", "+0"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(test_string);
new_ucmd!()
.args(&["-n", "+2"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("⏻\nƒ\na");
new_ucmd!()
.args(&["-n", "+3"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("ƒ\na");
new_ucmd!()
.args(&["-n", "+4"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("a");
new_ucmd!()
.args(&["-n", "+5"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
new_ucmd!()
.args(&["-n", "-4"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(test_string);
new_ucmd!()
.args(&["-n", "-3"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("⏻\nƒ\na");
new_ucmd!()
.args(&["-n", "-2"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("ƒ\na");
new_ucmd!()
.args(&["-n", "-1"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("a");
new_ucmd!()
.args(&["-n", "-0"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
}
#[test]
fn test_pipe_when_lines_option_given_input_size_is_equal_to_buffer_size_no_newline_at_eof() {
let total_lines = 1;
let random_string = RandomizedString::generate_with_delimiter(
Alphanumeric,
b'\n',
total_lines,
false,
CHUNK_BUFFER_SIZE,
);
let random_string = random_string.as_str();
let lines = random_string.split_inclusive('\n');
let expected = lines.clone().skip(1).collect::<String>();
new_ucmd!()
.args(&["-n", "+2"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(expected);
let expected = lines.clone().skip(1).collect::<String>();
new_ucmd!()
.args(&["-n", "-1"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(expected);
}
#[test]
fn test_pipe_when_lines_option_given_input_size_is_equal_to_buffer_size() {
let total_lines = 100;
let random_string = RandomizedString::generate_with_delimiter(
Alphanumeric,
b'\n',
total_lines,
true,
CHUNK_BUFFER_SIZE,
);
let random_string = random_string.as_str();
let lines = random_string.split_inclusive('\n');
new_ucmd!()
.args(&["-n", "+0"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(random_string);
let expected = lines.clone().skip(1).collect::<String>();
new_ucmd!()
.args(&["-n", "+2"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(expected);
new_ucmd!()
.args(&["-n", "-0"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
let expected = lines.clone().skip(total_lines - 1).collect::<String>();
new_ucmd!()
.args(&["-n", "-1"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(expected);
let expected = lines.clone().skip(1).collect::<String>();
new_ucmd!()
.args(&["-n", "-99"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(expected);
new_ucmd!()
.args(&["-n", "-100"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(random_string);
}
#[test]
fn test_pipe_when_lines_option_given_input_size_is_one_byte_greater_than_buffer_size() {
let total_lines = 100;
let random_string = RandomizedString::generate_with_delimiter(
Alphanumeric,
b'\n',
total_lines,
true,
CHUNK_BUFFER_SIZE + 1,
);
let random_string = random_string.as_str();
let lines = random_string.split_inclusive('\n');
new_ucmd!()
.args(&["-n", "+0"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(random_string);
let expected = lines.clone().skip(total_lines - 1).collect::<String>();
new_ucmd!()
.args(&["-n", "-1"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(expected);
let expected = lines.clone().skip(1).collect::<String>();
new_ucmd!()
.args(&["-n", "+2"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(expected);
let expected = lines.clone().skip(1).collect::<String>();
new_ucmd!()
.args(&["-n", "-99"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(expected);
}
#[test]
#[cfg(not(target_os = "windows"))]
fn test_pipe_when_lines_option_given_input_size_has_multiple_size_of_buffer_size() {
let total_lines = 100;
let random_string = RandomizedString::generate_with_delimiter(
Alphanumeric,
b'\n',
total_lines,
true,
CHUNK_BUFFER_SIZE * 3 + 1,
);
let random_string = random_string.as_str();
let lines = random_string.split_inclusive('\n');
new_ucmd!()
.args(&["-n", "+0"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(random_string);
let expected = lines.clone().skip(1).collect::<String>();
new_ucmd!()
.args(&["-n", "+2"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(expected);
new_ucmd!()
.args(&["-n", "-0"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
let expected = lines.clone().skip(total_lines - 1).collect::<String>();
new_ucmd!()
.args(&["-n", "-1"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(expected);
let expected = lines.clone().skip(1).collect::<String>();
new_ucmd!()
.args(&["-n", "-99"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(expected);
new_ucmd!()
.args(&["-n", "-100"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(random_string);
}
#[test]
fn test_pipe_when_bytes_option_value_is_higher_than_contained_bytes() {
let test_string = "a\nb";
new_ucmd!()
.args(&["-c", "4"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(test_string);
new_ucmd!()
.args(&["-c", "5"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(test_string);
new_ucmd!()
.args(&["-c", "999"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(test_string);
new_ucmd!()
.args(&["-c", "+4"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
new_ucmd!()
.args(&["-c", "+5"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
new_ucmd!()
.args(&["-c", "+999"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
}
#[test]
fn test_pipe_when_bytes_option_given_multibyte_utf8_characters() {
let test_string = "𝅘𝅥𝅮⏻ƒa";
new_ucmd!()
.args(&["-c", "+0"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(test_string);
new_ucmd!()
.args(&["-c", "+2"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(&test_string.as_bytes()[1..]);
new_ucmd!()
.args(&["-c", "+5"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("⏻ƒa");
new_ucmd!()
.args(&["-c", "+8"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("ƒa");
new_ucmd!()
.args(&["-c", "+10"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("a");
new_ucmd!()
.args(&["-c", "+11"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
new_ucmd!()
.args(&["-c", "-1"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("a");
new_ucmd!()
.args(&["-c", "-2"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(&"ƒa".as_bytes()[1..]);
new_ucmd!()
.args(&["-c", "-3"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("ƒa");
new_ucmd!()
.args(&["-c", "-6"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only("⏻ƒa");
new_ucmd!()
.args(&["-c", "-10"])
.pipe_in(test_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(test_string);
}
#[test]
fn test_pipe_when_bytes_option_given_input_size_is_equal_to_buffer_size() {
let random_string = RandomizedString::generate(AlphanumericNewline, CHUNK_BUFFER_SIZE);
let random_string = random_string.as_str();
new_ucmd!()
.args(&["-c", "+0"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(random_string);
let expected = &random_string.as_bytes()[1..];
new_ucmd!()
.args(&["-c", "+2"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(expected);
new_ucmd!()
.args(&["-c", "-0"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
let expected = &random_string.as_bytes()[1..];
new_ucmd!()
.args(&["-c", "-8191"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(expected);
new_ucmd!()
.args(&["-c", "-8192"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(random_string);
new_ucmd!()
.args(&["-c", "-8193"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(random_string);
let expected = &random_string.as_bytes()[CHUNK_BUFFER_SIZE - 1..];
new_ucmd!()
.args(&["-c", "-1"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(expected);
}
#[test]
fn test_pipe_when_bytes_option_given_input_size_is_one_byte_greater_than_buffer_size() {
let random_string = RandomizedString::generate(AlphanumericNewline, CHUNK_BUFFER_SIZE + 1);
let random_string = random_string.as_str();
new_ucmd!()
.args(&["-c", "+0"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(random_string);
let expected = &random_string.as_bytes()[1..];
new_ucmd!()
.args(&["-c", "+2"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(expected);
new_ucmd!()
.args(&["-c", "-0"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
let expected = &random_string.as_bytes()[CHUNK_BUFFER_SIZE..];
new_ucmd!()
.args(&["-c", "-1"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(expected);
let expected = &random_string.as_bytes()[1..];
new_ucmd!()
.args(&["-c", "-8192"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(expected);
new_ucmd!()
.args(&["-c", "-8193"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(random_string);
}
#[test]
#[cfg(not(target_os = "windows"))]
fn test_pipe_when_bytes_option_given_input_size_has_multiple_size_of_buffer_size() {
let random_string = RandomizedString::generate(AlphanumericNewline, CHUNK_BUFFER_SIZE * 3);
let random_string = random_string.as_str();
new_ucmd!()
.args(&["-c", "+0"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(random_string);
new_ucmd!()
.args(&["-c", "-0"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.no_stdout()
.no_stderr();
let expected = &random_string.as_bytes()[8192..];
new_ucmd!()
.args(&["-c", "+8193"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(expected);
let expected = &random_string.as_bytes()[8193..];
new_ucmd!()
.args(&["-c", "+8194"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(expected);
let expected = &random_string.as_bytes()[16384..];
new_ucmd!()
.args(&["-c", "+16385"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(expected);
let expected = &random_string.as_bytes()[16385..];
new_ucmd!()
.args(&["-c", "+16386"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(expected);
let expected = &random_string.as_bytes()[16384..];
new_ucmd!()
.args(&["-c", "-8192"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(expected);
let expected = &random_string.as_bytes()[16383..];
new_ucmd!()
.args(&["-c", "-8193"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(expected);
let expected = &random_string.as_bytes()[8192..];
new_ucmd!()
.args(&["-c", "-16384"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(expected);
let expected = &random_string.as_bytes()[8191..];
new_ucmd!()
.args(&["-c", "-16385"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only_bytes(expected);
new_ucmd!()
.args(&["-c", "-24576"])
.pipe_in(random_string)
.ignore_stdin_write_error()
.succeeds()
.stdout_only(random_string);
}
#[test]
fn test_seek_bytes_backward_outside_file() {
new_ucmd!()
.arg("-c")
.arg("100")
.arg(FOOBAR_TXT)
.succeeds()
.stdout_is_fixture(FOOBAR_TXT);
}
#[test]
fn test_seek_bytes_forward_outside_file() {
new_ucmd!()
.arg("-c")
.arg("+100")
.arg(FOOBAR_TXT)
.succeeds()
.stdout_is("");
}
#[cfg(all(not(target_os = "android"), not(target_os = "windows")))] #[test]
fn test_args_when_presume_input_pipe_given_input_is_pipe() {
let random_string = RandomizedString::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 = RandomizedString::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);
}
#[test]
#[ignore = "disabled until fixed"]
fn test_when_follow_retry_given_redirected_stdin_from_directory_then_correct_error_message() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.mkdir("dir");
let expected = "tail: warning: --retry only effective for the initial open\n\
tail: error reading 'standard input': Is a directory\n\
tail: 'standard input': cannot follow end of this type of file\n\
tail: no files remaining\n";
ts.ucmd()
.set_stdin(File::open(at.plus("dir")).unwrap())
.args(&["-f", "--retry"])
.fails_with_code(1)
.stderr_only(expected);
}
#[test]
fn test_when_argument_file_is_a_directory() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.mkdir("dir");
let expected = "tail: error reading 'dir': Is a directory\n";
ts.ucmd()
.arg("dir")
.fails_with_code(1)
.stderr_only(expected);
}
#[test]
#[cfg(unix)]
fn test_when_argument_file_is_a_symlink() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let mut file = at.make_file("target");
at.symlink_file("target", "link");
ts.ucmd()
.args(&["-c", "+0", "link"])
.succeeds()
.no_stdout()
.no_stderr();
let random_string = RandomizedString::generate(AlphanumericNewline, 100);
let result = file.write_all(random_string.as_bytes());
assert!(result.is_ok());
ts.ucmd()
.args(&["-c", "+0", "link"])
.succeeds()
.stdout_only(random_string);
at.mkdir("dir");
at.symlink_file("dir", "dir_link");
let expected = "tail: error reading 'dir_link': Is a directory\n";
ts.ucmd()
.arg("dir_link")
.fails_with_code(1)
.stderr_only(expected);
}
#[test]
#[cfg(unix)]
fn test_when_argument_file_is_a_symlink_to_directory_then_error() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.mkdir("dir");
at.symlink_file("dir", "dir_link");
let expected = "tail: error reading 'dir_link': Is a directory\n";
ts.ucmd()
.arg("dir_link")
.fails_with_code(1)
.stderr_only(expected);
}
#[test]
#[cfg(unix)]
#[ignore = "disabled until fixed"]
fn test_when_argument_file_is_a_faulty_symlink_then_error() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.symlink_file("self", "self");
#[cfg(all(not(target_env = "musl"), not(target_os = "android")))]
let expected = "tail: cannot open 'self' for reading: Too many levels of symbolic links";
#[cfg(all(not(target_env = "musl"), target_os = "android"))]
let expected = "tail: cannot open 'self' for reading: Too many symbolic links encountered";
#[cfg(all(target_env = "musl", not(target_os = "android")))]
let expected = "tail: cannot open 'self' for reading: Symbolic link loop";
ts.ucmd()
.arg("self")
.fails_with_code(1)
.stderr_only(expected);
at.symlink_file("missing", "broken");
let expected = "tail: cannot open 'broken' for reading: No such file or directory";
ts.ucmd()
.arg("broken")
.fails_with_code(1)
.stderr_only(expected);
}
#[test]
#[cfg(unix)]
#[ignore = "disabled until fixed"]
fn test_when_argument_file_is_non_existent_unix_socket_address_then_error() {
use std::os::unix::net;
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let socket = "socket";
let result = net::UnixListener::bind(at.plus(socket));
assert!(result.is_ok());
#[cfg(all(not(target_os = "freebsd"), not(target_os = "macos")))]
let expected_stderr =
format!("tail: cannot open '{socket}' for reading: No such device or address\n");
#[cfg(target_os = "freebsd")]
let expected_stderr =
format!("tail: cannot open '{socket}' for reading: Operation not supported\n",);
#[cfg(target_os = "macos")]
let expected_stderr =
format!("tail: cannot open '{socket}' for reading: Operation not supported on socket\n",);
ts.ucmd()
.arg(socket)
.fails_with_code(1)
.stderr_only(&expected_stderr);
let path = "file";
let mut file = at.make_file(path);
let random_string = RandomizedString::generate(AlphanumericNewline, 100);
let result = file.write_all(random_string.as_bytes());
assert!(result.is_ok());
let expected_stdout = [format!("==> {path} <=="), random_string].join("\n");
ts.ucmd()
.args(&["-c", "+0", path, socket])
.fails_with_code(1)
.stdout_is(&expected_stdout)
.stderr_is(&expected_stderr);
ts.ucmd()
.args(&["-c", "+0", socket, path])
.fails()
.stdout_is(&expected_stdout)
.stderr_is(&expected_stderr);
}
#[test]
#[ignore = "disabled until fixed"]
fn test_when_argument_files_are_simple_combinations_of_stdin_and_regular_file() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("empty");
at.write("data", "file data");
at.write("fifo", "fifo data");
let expected = "==> standard input <==\n\
fifo data\n\
==> empty <==\n";
scene
.ucmd()
.args(&["-c", "+0", "-", "empty"])
.set_stdin(File::open(at.plus("fifo")).unwrap())
.succeeds()
.stdout_only(expected);
let expected = "==> standard input <==\n\
\n\
==> empty <==\n";
scene
.ucmd()
.args(&["-c", "+0", "-", "empty"])
.pipe_in("")
.succeeds()
.stdout_only(expected);
let expected = "==> empty <==\n\
\n\
==> standard input <==\n";
scene
.ucmd()
.args(&["-c", "+0", "empty", "-"])
.pipe_in("")
.succeeds()
.stdout_only(expected);
let expected = "==> empty <==\n\
\n\
==> standard input <==\n\
fifo data";
scene
.ucmd()
.args(&["-c", "+0", "empty", "-"])
.set_stdin(File::open(at.plus("fifo")).unwrap())
.succeeds()
.stdout_only(expected);
let expected = "==> standard input <==\n\
pipe data\n\
==> data <==\n\
file data";
scene
.ucmd()
.args(&["-c", "+0", "-", "data"])
.pipe_in("pipe data")
.succeeds()
.stdout_only(expected);
let expected = "==> data <==\n\
file data\n\
==> standard input <==\n\
pipe data";
scene
.ucmd()
.args(&["-c", "+0", "data", "-"])
.pipe_in("pipe data")
.succeeds()
.stdout_only(expected);
let expected = "==> standard input <==\n\
pipe data\n\
==> standard input <==\n";
scene
.ucmd()
.args(&["-c", "+0", "-", "-"])
.pipe_in("pipe data")
.succeeds()
.stdout_only(expected);
let expected = "==> standard input <==\n\
fifo data\n\
==> standard input <==\n";
scene
.ucmd()
.args(&["-c", "+0", "-", "-"])
.set_stdin(File::open(at.plus("fifo")).unwrap())
.succeeds()
.stdout_only(expected);
}
#[test]
#[ignore = "disabled until fixed"]
fn test_when_argument_files_are_triple_combinations_of_fifo_pipe_and_regular_file() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("empty");
at.write("data", "file data");
at.write("fifo", "fifo data");
let expected = "==> standard input <==\n\
\n\
==> empty <==\n\
\n\
==> standard input <==\n";
scene
.ucmd()
.args(&["-c", "+0", "-", "empty", "-"])
.set_stdin(File::open(at.plus("empty")).unwrap())
.succeeds()
.stdout_only(expected);
let expected = "==> standard input <==\n\
\n\
==> empty <==\n\
\n\
==> standard input <==\n";
scene
.ucmd()
.args(&["-c", "+0", "-", "empty", "-"])
.pipe_in("")
.stderr_to_stdout()
.succeeds()
.stdout_only(expected);
let expected = "==> standard input <==\n\
pipe data\n\
==> data <==\n\
file data\n\
==> standard input <==\n";
scene
.ucmd()
.args(&["-c", "+0", "-", "data", "-"])
.pipe_in("pipe data")
.succeeds()
.stdout_only(expected);
#[cfg(windows)]
let expected = "==> standard input <==\n\
fifo data\n\
==> data <==\n\
file data\n\
==> standard input <==\n\
(The process tried to write to a nonexistent pipe.\r\n)?";
#[cfg(unix)]
let expected = "==> standard input <==\n\
fifo data\n\
==> data <==\n\
file data\n\
==> standard input <==\n";
#[cfg(windows)]
let cmd = ["cmd", "/C"];
#[cfg(unix)]
let cmd = ["sh", "-c"];
scene
.cmd(cmd[0])
.arg(cmd[1])
.arg(format!(
"echo pipe data | {} tail -c +0 - data - < fifo",
scene.bin_path.display(),
))
.succeeds()
.stdout_only(expected);
let expected = "==> standard input <==\n\
fifo data\n\
==> data <==\n\
file data\n\
==> standard input <==\n";
scene
.ucmd()
.args(&["-c", "+0", "-", "data", "-"])
.set_stdin(File::open(at.plus("fifo")).unwrap())
.succeeds()
.stdout_only(expected);
}
#[test]
#[ignore = "disabled until fixed"]
fn test_when_follow_retry_then_initial_print_of_file_is_written_to_stdout() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let expected_stdout = "file data";
at.write("data", expected_stdout);
let mut child = scene
.ucmd()
.args(&["--follow=name", "--retry", "data"])
.run_no_wait();
child
.delay(1500)
.kill()
.make_assertion()
.with_current_output()
.stdout_only(expected_stdout);
}
#[test]
fn test_args_when_settings_check_warnings_then_shows_warnings() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let file_data = "file data\n";
at.write("data", file_data);
let expected_stdout = format!(
"tail: warning: --retry ignored; --retry is useful only when following\n\
{file_data}"
);
scene
.ucmd()
.args(&["--retry", "data"])
.stderr_to_stdout()
.succeeds()
.stdout_only(expected_stdout);
let expected_stdout = format!(
"tail: warning: --retry only effective for the initial open\n\
{file_data}"
);
let mut child = scene
.ucmd()
.args(&["--follow=descriptor", "--retry", "data"])
.stderr_to_stdout()
.run_no_wait();
child
.delay(500)
.kill()
.make_assertion()
.with_current_output()
.stdout_only(expected_stdout);
let expected_stdout = format!(
"tail: warning: PID ignored; --pid=PID is useful only when following\n\
{file_data}"
);
scene
.ucmd()
.args(&["--pid=1000", "data"])
.stderr_to_stdout()
.succeeds()
.stdout_only(expected_stdout);
let expected_stdout = format!(
"tail: warning: --retry ignored; --retry is useful only when following\n\
tail: warning: PID ignored; --pid=PID is useful only when following\n\
{file_data}"
);
scene
.ucmd()
.args(&["--pid=1000", "--retry", "data"])
.stderr_to_stdout()
.succeeds()
.stdout_only(&expected_stdout);
scene
.ucmd()
.args(&["--pid=1000", "--pid=1000", "--retry", "data"])
.stderr_to_stdout()
.succeeds()
.stdout_only(expected_stdout);
}
#[test]
#[cfg(target_os = "linux")]
fn test_args_when_settings_check_warnings_follow_indefinitely_then_warning() {
let scene = TestScenario::new(util_name!());
let file_data = "file data\n";
scene.fixtures.write("data", file_data);
let expected_stdout = "==> standard input <==\n";
let expected_stderr = "tail: warning: following standard input indefinitely is ineffective\n";
let mut child = scene
.ucmd()
.args(&["--follow=descriptor", "-", "data"])
.set_stdin(File::open(text::DEV_PTMX).unwrap())
.run_no_wait();
child.make_assertion_with_delay(500).is_alive();
child
.kill()
.make_assertion()
.with_current_output()
.stderr_is(expected_stderr)
.stdout_is(expected_stdout);
let expected_stdout = "tail: warning: following standard input indefinitely is ineffective\n\
==> standard input <==\n";
let mut child = scene
.ucmd()
.args(&["--follow=descriptor", "-", "data"])
.set_stdin(File::open(text::DEV_PTMX).unwrap())
.stderr_to_stdout()
.run_no_wait();
child.make_assertion_with_delay(500).is_alive();
child
.kill()
.make_assertion()
.with_current_output()
.stdout_only(expected_stdout);
let expected_stdout = format!(
"tail: warning: following standard input indefinitely is ineffective\n\
==> data <==\n\
{file_data}\n\
==> standard input <==\n"
);
let mut child = scene
.ucmd()
.args(&["--follow=descriptor", "data", "-"])
.set_stdin(File::open(text::DEV_PTMX).unwrap())
.stderr_to_stdout()
.run_no_wait();
child.make_assertion_with_delay(500).is_alive();
child
.kill()
.make_assertion()
.with_current_output()
.stdout_only(expected_stdout);
let expected_stdout = "tail: warning: following standard input indefinitely is ineffective\n\
==> standard input <==\n";
let mut child = scene
.ucmd()
.args(&["--follow=descriptor", "-", "-"])
.set_stdin(File::open(text::DEV_PTMX).unwrap())
.stderr_to_stdout()
.run_no_wait();
child.make_assertion_with_delay(500).is_alive();
child
.kill()
.make_assertion()
.with_current_output()
.stdout_only(expected_stdout);
let expected_stdout = "tail: warning: following standard input indefinitely is ineffective\n\
==> standard input <==\n";
let mut child = scene
.ucmd()
.args(&["--follow=descriptor", "-", "-", "data"])
.set_stdin(File::open(text::DEV_PTMX).unwrap())
.stderr_to_stdout()
.run_no_wait();
child.make_assertion_with_delay(500).is_alive();
child
.kill()
.make_assertion()
.with_current_output()
.stdout_only(expected_stdout);
let expected_stdout = "tail: warning: following standard input indefinitely is ineffective\n\
==> standard input <==\n";
let mut child = scene
.ucmd()
.args(&["--follow=descriptor", "--pid=100000", "-", "data"])
.set_stdin(File::open(text::DEV_PTMX).unwrap())
.stderr_to_stdout()
.run_no_wait();
child.make_assertion_with_delay(500).is_alive();
child
.kill()
.make_assertion()
.with_current_output()
.stdout_only(expected_stdout);
}
#[test]
#[cfg(unix)]
fn test_args_when_settings_check_warnings_follow_indefinitely_then_no_warning() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
#[cfg(target_vendor = "apple")]
let delay = 1000;
#[cfg(not(target_vendor = "apple"))]
let delay = 500;
let file_data = "file data\n";
let fifo_data = "fifo data\n";
let fifo_name = "fifo";
let file_name = "data";
at.write(file_name, file_data);
at.write(fifo_name, fifo_data);
let pipe_data = "pipe data";
let expected_stdout = format!(
"==> standard input <==\n\
{pipe_data}\n\
==> {file_name} <==\n\
{file_data}"
);
let mut child = scene
.ucmd()
.args(&["--follow=descriptor", "-", file_name])
.pipe_in(pipe_data)
.stderr_to_stdout()
.run_no_wait();
child.make_assertion_with_delay(delay).is_alive();
child
.kill()
.make_assertion_with_delay(delay)
.with_current_output()
.stdout_only(expected_stdout);
#[cfg(not(target_vendor = "apple"))]
{
let expected_stdout = format!(
"==> standard input <==\n\
{fifo_data}\n\
==> {file_name} <==\n\
{file_data}"
);
let mut child = scene
.ucmd()
.args(&["--follow=descriptor", "-", file_name])
.set_stdin(File::open(at.plus(fifo_name)).unwrap())
.stderr_to_stdout()
.run_no_wait();
child.make_assertion_with_delay(delay).is_alive();
child
.kill()
.make_assertion_with_delay(delay)
.with_current_output()
.stdout_only(expected_stdout);
let expected_stdout = format!(
"==> standard input <==\n\
{fifo_data}\n\
==> {file_name} <==\n\
{file_data}"
);
let mut child = scene
.ucmd()
.args(&["--follow=descriptor", "--pid=0", "-", file_name])
.set_stdin(File::open(at.plus(fifo_name)).unwrap())
.stderr_to_stdout()
.run_no_wait();
child.make_assertion_with_delay(delay).is_alive();
child
.kill()
.make_assertion_with_delay(delay)
.with_current_output()
.stdout_only(expected_stdout);
}
}
#[test]
#[ignore = "disabled until fixed"]
fn test_follow_when_files_are_pointing_to_same_relative_file_and_data_is_appended() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let file_data = "file data";
let relative_path_name = "data";
at.write(relative_path_name, file_data);
let absolute_path = at.plus("data").canonicalize().unwrap();
let mut child = scene
.ucmd()
.args(&[
"--follow=name",
relative_path_name,
absolute_path.to_str().unwrap(),
])
.run_no_wait();
let more_data = "more data";
child.delay(500);
at.append(relative_path_name, more_data);
let expected_stdout = format!(
"==> {0} <==\n\
{1}\n\
==> {2} <==\n\
{1}\n\
==> {0} <==\n\
{3}",
relative_path_name,
file_data,
absolute_path.to_str().unwrap(),
more_data
);
child
.make_assertion_with_delay(DEFAULT_SLEEP_INTERVAL_MILLIS)
.is_alive();
child
.kill()
.make_assertion()
.with_current_output()
.stderr_only(expected_stdout);
at.write(relative_path_name, file_data);
let mut child = scene
.ucmd()
.args(&[
"--follow=name",
absolute_path.to_str().unwrap(),
relative_path_name,
])
.run_no_wait();
child.delay(500);
let more_data = "more data";
at.append(relative_path_name, more_data);
let expected_stdout = format!(
"==> {0} <==\n\
{1}\n\
==> {2} <==\n\
{1}\n\
==> {0} <==\n\
{3}",
absolute_path.to_str().unwrap(),
file_data,
relative_path_name,
more_data
);
child
.make_assertion_with_delay(DEFAULT_SLEEP_INTERVAL_MILLIS)
.is_alive();
child
.kill()
.make_assertion()
.with_current_output()
.stdout_only(expected_stdout);
}
#[test]
#[ignore = "disabled until fixed"]
fn test_follow_when_files_are_pointing_to_same_relative_file_and_file_is_truncated() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let file_data = "file data";
let relative_path_name = "data";
at.write(relative_path_name, file_data);
let absolute_path = at.plus("data").canonicalize().unwrap();
let mut child = scene
.ucmd()
.args(&[
"--follow=descriptor",
"--max-unchanged-stats=1",
"--sleep-interval=0.1",
relative_path_name,
absolute_path.to_str().unwrap(),
])
.stderr_to_stdout()
.run_no_wait();
child.delay(500);
let less_data = "less";
at.write(relative_path_name, "less");
let expected_stdout = format!(
"==> {0} <==\n\
{1}\n\
==> {2} <==\n\
{1}{4}: {0}: file truncated\n\
\n\
==> {0} <==\n\
{3}",
relative_path_name, file_data, absolute_path.to_str().unwrap(), less_data, scene.util_name );
child.make_assertion_with_delay(500).is_alive();
child
.kill()
.make_assertion()
.with_current_output()
.stdout_only(expected_stdout);
}
#[test]
#[cfg(unix)]
#[ignore = "disabled until fixed"]
fn test_follow_when_file_and_symlink_are_pointing_to_same_file_and_append_data() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let file_data = "file data";
let path_name = "data";
let link_name = "link";
at.write(path_name, file_data);
at.symlink_file(path_name, link_name);
let mut child = scene
.ucmd()
.args(&[
"--follow=descriptor",
"--max-unchanged-stats=1",
"--sleep-interval=0.1",
path_name,
link_name,
])
.run_no_wait();
child.delay(500);
let more_data = "more data";
at.append(path_name, more_data);
let expected_stdout = format!(
"==> {path_name} <==\n\
{file_data}\n\
==> {link_name} <==\n\
{file_data}\n\
==> {path_name} <==\n\
{more_data}\n\
==> {link_name} <==\n\
{more_data}"
);
child.make_assertion_with_delay(500).is_alive();
child
.kill()
.make_assertion()
.with_current_output()
.stdout_only(expected_stdout);
at.write(path_name, file_data);
let mut child = scene
.ucmd()
.args(&[
"--follow=descriptor",
"--max-unchanged-stats=1",
"--sleep-interval=0.1",
link_name,
path_name,
])
.run_no_wait();
child.delay(500);
let more_data = "more data";
at.append(path_name, more_data);
let expected_stdout = format!(
"==> {link_name} <==\n\
{file_data}\n\
==> {path_name} <==\n\
{file_data}\n\
==> {link_name} <==\n\
{more_data}\n\
==> {path_name} <==\n\
{more_data}"
);
child.make_assertion_with_delay(500).is_alive();
child
.kill()
.make_assertion()
.with_current_output()
.stdout_only(expected_stdout);
}
#[test]
fn test_args_when_directory_given_shorthand_big_f_together_with_retry() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let dirname = "dir";
at.mkdir(dirname);
let expected_stderr = format!(
"tail: error reading '{dirname}': Is a directory\n\
tail: {dirname}: cannot follow end of this type of file\n"
);
let mut child = scene.ucmd().args(&["-F", "--retry", "dir"]).run_no_wait();
child.make_assertion_with_delay(500).is_alive();
child
.kill()
.make_assertion()
.with_current_output()
.stderr_only(&expected_stderr);
let mut child = scene.ucmd().args(&["--retry", "-F", "dir"]).run_no_wait();
child.make_assertion_with_delay(500).is_alive();
child
.kill()
.make_assertion()
.with_current_output()
.stderr_only(expected_stderr);
}
#[test]
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "freebsd"),
not(target_os = "openbsd"),
not(feature = "feat_selinux") // flaky
))]
fn test_follow_when_files_are_pointing_to_same_relative_file_and_file_stays_same_size() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let file_data = "file data";
let relative_path_name = "data";
at.write(relative_path_name, file_data);
let absolute_path = scene.fixtures.plus("data").canonicalize().unwrap();
let mut child = scene
.ucmd()
.args(&[
"--follow=descriptor",
"--max-unchanged-stats=1",
"--sleep-interval=0.1",
relative_path_name,
absolute_path.to_str().unwrap(),
])
.run_no_wait();
child.delay(500);
let same_data = "same data"; at.write(relative_path_name, same_data);
let expected_stdout = format!(
"==> {0} <==\n\
{1}\n\
==> {2} <==\n\
{1}",
relative_path_name, file_data, absolute_path.to_str().unwrap(), );
child.make_assertion_with_delay(500).is_alive();
child
.kill()
.make_assertion()
.with_current_output()
.stdout_only(expected_stdout);
}
#[rstest]
#[case::underscore_delimiter("1_000")]
#[case::only_point(".")]
#[case::space_in_primes("' '")]
#[case::space(" ")]
#[case::empty("")]
#[case::comma_separator("0,0")]
#[case::words_nominator_fract("one.zero")]
#[case::words_fract(".zero")]
#[case::words_nominator("one.")]
#[case::two_points("0..0")]
#[case::seconds_unit("1.0s")]
#[case::circumflex_exponent("1.0e^1000")]
fn test_args_sleep_interval_when_illegal_argument_then_usage_error(#[case] sleep_interval: &str) {
new_ucmd!()
.args(&["--sleep-interval", sleep_interval])
.fails_with_code(1)
.usage_error(format!("invalid number of seconds: '{sleep_interval}'"));
}
#[test]
fn test_gnu_args_plus_c() {
let scene = TestScenario::new(util_name!());
scene
.ucmd()
.arg("+2c")
.pipe_in("abcd")
.succeeds()
.stdout_only("bcd");
scene
.ucmd()
.arg("+8c")
.pipe_in("abcd")
.succeeds()
.stdout_only("");
scene
.ucmd()
.arg("+c")
.pipe_in(format!("x{}z", "y".repeat(10)))
.succeeds()
.stdout_only("yyz");
}
#[test]
fn test_gnu_args_c() {
let scene = TestScenario::new(util_name!());
scene
.ucmd()
.arg("-1c")
.pipe_in("abcd")
.succeeds()
.stdout_only("d");
scene
.ucmd()
.arg("-9c")
.pipe_in("abcd")
.succeeds()
.stdout_only("abcd");
scene
.ucmd()
.arg("-12c")
.pipe_in(format!("x{}z", "y".repeat(12)))
.succeeds()
.stdout_only(format!("{}z", "y".repeat(11)));
}
#[test]
fn test_gnu_args_l() {
let scene = TestScenario::new(util_name!());
scene
.ucmd()
.arg("-1l")
.pipe_in("x")
.succeeds()
.stdout_only("x");
scene
.ucmd()
.arg("-1l")
.pipe_in("x\ny\n")
.succeeds()
.stdout_only("y\n");
scene
.ucmd()
.arg("-1l")
.pipe_in("x\ny")
.succeeds()
.stdout_only("y");
scene
.ucmd()
.arg("-l")
.pipe_in(format!("x{}z", "y\n".repeat(10)))
.succeeds()
.stdout_only(format!("{}z", "y\n".repeat(9)));
}
#[test]
fn test_gnu_args_plus_l() {
let scene = TestScenario::new(util_name!());
scene
.ucmd()
.arg("+1l")
.pipe_in("x\ny\n")
.succeeds()
.stdout_only("x\ny\n");
scene
.ucmd()
.arg("+2l")
.pipe_in("x\ny\n")
.succeeds()
.stdout_only("y\n");
scene
.ucmd()
.arg("+l")
.pipe_in(format!("x\n{}z", "y\n".repeat(10)))
.succeeds()
.stdout_only("y\ny\nz");
}
#[test]
fn test_gnu_args_number() {
let scene = TestScenario::new(util_name!());
scene
.ucmd()
.arg("-1")
.pipe_in("x")
.succeeds()
.stdout_only("x");
scene
.ucmd()
.arg("-1")
.pipe_in("x\ny\n")
.succeeds()
.stdout_only("y\n");
scene
.ucmd()
.arg("-1")
.pipe_in("x\ny")
.succeeds()
.stdout_only("y");
}
#[test]
fn test_gnu_args_plus_number() {
let scene = TestScenario::new(util_name!());
scene
.ucmd()
.arg("+1")
.pipe_in("x\ny\n")
.succeeds()
.stdout_only("x\ny\n");
scene
.ucmd()
.arg("+2")
.pipe_in("x\ny\n")
.succeeds()
.stdout_only("y\n");
}
#[test]
fn test_gnu_args_b() {
let scene = TestScenario::new(util_name!());
scene
.ucmd()
.arg("-b")
.pipe_in("x\n".repeat(512 * 10 / 2 + 1))
.succeeds()
.stdout_only("x\n".repeat(512 * 10 / 2));
}
#[test]
fn test_gnu_args_err() {
let scene = TestScenario::new(util_name!());
scene
.ucmd()
.arg("+cl")
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: cannot open '+cl' for reading: No such file or directory\n");
scene
.ucmd()
.arg("-cl")
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: invalid number of bytes: 'l'\n");
scene
.ucmd()
.arg("+2cz")
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: cannot open '+2cz' for reading: No such file or directory\n");
scene
.ucmd()
.arg("-2cX")
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: option used in invalid context -- 2\n");
scene
.ucmd()
.arg("-c99999999999999999999")
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: invalid number of bytes: '99999999999999999999'\n");
scene
.ucmd()
.arg("-c --")
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: invalid number of bytes: '-'\n");
scene
.ucmd()
.arg("-5cz")
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: option used in invalid context -- 5\n");
scene
.ucmd()
.arg("-9999999999999999999b")
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: invalid number: '-9999999999999999999b'\n");
scene
.ucmd()
.arg("-999999999999999999999b")
.fails_with_code(1)
.no_stdout()
.stderr_is(
"tail: invalid number: '-999999999999999999999b': Numerical result out of range\n",
);
}
#[test]
fn test_gnu_args_f() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let source = "file";
at.touch(source);
let mut p = scene.ucmd().args(&["+f", source]).run_no_wait();
p.make_assertion_with_delay(500).is_alive();
p.kill()
.make_assertion()
.with_all_output()
.no_stderr()
.no_stdout();
let mut p = scene
.ucmd()
.set_stdin(Stdio::piped())
.arg("+f")
.run_no_wait();
p.make_assertion_with_delay(500).is_alive();
p.kill()
.make_assertion()
.with_all_output()
.no_stderr()
.no_stdout();
}
#[test]
#[cfg(unix)]
fn test_obsolete_encoding_unix() {
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
let scene = TestScenario::new(util_name!());
let invalid_utf8_arg = OsStr::from_bytes(&[b'-', INVALID_UTF8, b'b']);
scene
.ucmd()
.arg(invalid_utf8_arg)
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: bad argument encoding: '-�b'\n");
}
#[test]
#[cfg(windows)]
fn test_obsolete_encoding_windows() {
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
let scene = TestScenario::new(util_name!());
let invalid_utf16_arg = OsString::from_wide(&['-' as u16, INVALID_UTF16, 'b' as u16]);
scene
.ucmd()
.arg(&invalid_utf16_arg)
.fails_with_code(1)
.no_stdout()
.stderr_is("tail: bad argument encoding: '-�b'\n");
}
#[test]
#[cfg(not(target_vendor = "apple"))] fn test_following_with_pid() {
use std::process::Command;
let ts = TestScenario::new(util_name!());
#[cfg(not(windows))]
let mut sleep_command = Command::new("sleep")
.arg("999d")
.spawn()
.expect("failed to start sleep command");
#[cfg(windows)]
let mut sleep_command = Command::new("powershell")
.arg("-Command")
.arg("Start-Sleep -Seconds 999")
.spawn()
.expect("failed to start sleep command");
let sleep_pid = sleep_command.id();
let at = &ts.fixtures;
at.touch("f");
let mut child = ts
.ucmd()
.args(&[
"--pid",
&sleep_pid.to_string(),
"-f",
at.plus("f").to_str().unwrap(),
])
.stderr_to_stdout()
.run_no_wait();
child.make_assertion_with_delay(2000).is_alive();
#[cfg(not(windows))]
Command::new("kill")
.arg("-9")
.arg(sleep_pid.to_string())
.output()
.expect("failed to kill sleep command");
#[cfg(windows)]
Command::new("taskkill")
.arg("/PID")
.arg(sleep_pid.to_string())
.arg("/F")
.output()
.expect("failed to kill sleep command");
let _ = sleep_command.wait();
child.make_assertion_with_delay(2000).is_not_alive();
child.kill();
}
#[test]
#[cfg_attr(not(feature = "expensive_tests"), ignore = "")]
fn test_when_piped_input_then_no_broken_pipe() {
let ts = TestScenario::new("tail");
for i in 0..10000 {
dbg!(i);
let test_string = "a\nb\n";
ts.ucmd()
.args(&["-n", "0"])
.pipe_in(test_string)
.succeeds()
.no_stdout()
.no_stderr();
}
}
#[test]
#[cfg(unix)]
fn test_when_output_closed_then_no_broken_pie() {
let mut cmd = new_ucmd!();
let mut child = cmd
.args(&["-c", "100000", "/dev/zero"])
.set_stdout(Stdio::piped())
.run_no_wait();
child.close_stdout();
child.wait().unwrap().fails_silently();
}
#[test]
fn test_child_when_run_with_stderr_to_stdout() {
let ts = TestScenario::new("tail");
let at = &ts.fixtures;
at.write("data", "file data\n");
let expected_stdout = "==> data <==\n\
file data\n\
tail: cannot open 'missing' for reading: No such file or directory\n";
ts.ucmd()
.args(&["data", "missing"])
.stderr_to_stdout()
.fails()
.stdout_only(expected_stdout);
}
#[cfg(target_os = "linux")]
#[test]
fn test_failed_write_is_reported() {
new_ucmd!()
.pipe_in("hello")
.set_stdout(std::fs::File::create("/dev/full").unwrap())
.fails()
.stderr_is("tail: No space left on device\n");
}
#[test]
#[cfg(target_os = "linux")]
fn test_dev_zero() {
new_ucmd!()
.args(&["-c", "1", "/dev/zero"])
.succeeds()
.stdout_only("\0");
}
#[test]
fn tail_n_lines_with_emoji() {
new_ucmd!()
.args(&["-n", "1"])
.pipe_in("a\n💐\n")
.succeeds()
.stdout_only("💐\n");
}