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

tail: implement --pid. Closes #846.

Kudos to zHz for helping out with Windows API part.
This commit is contained in:
Alexander Batischev 2016-12-20 04:47:40 +03:00
parent 2660bb4fc3
commit f2166fed0a
8 changed files with 205 additions and 2 deletions

View file

@ -2,8 +2,11 @@ extern crate uu_tail;
use common::util::*;
use std::char::from_digit;
use std::io::Write;
use self::uu_tail::parse_size;
use std::io::Write;
use std::process::{Command, Stdio};
use std::thread::sleep;
use std::time::Duration;
static FOOBAR_TXT: &'static str = "foobar.txt";
@ -74,6 +77,48 @@ fn test_follow_stdin() {
new_ucmd!().arg("-f").pipe_in_fixture(FOOBAR_TXT).run().stdout_is_fixture("follow_stdin.expected");
}
#[test]
fn test_follow_with_pid() {
let (at, mut ucmd) = at_and_ucmd!();
#[cfg(unix)]
let dummy_cmd = "sh";
#[cfg(windows)]
let dummy_cmd = "cmd";
let mut dummy = Command::new(dummy_cmd).stdout(Stdio::null()).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();
let expected = at.read("foobar_follow_multiple.expected");
assert_eq!(read_size(&mut child, expected.len()), expected);
let first_append = "trois\n";
at.append(FOOBAR_2_TXT, first_append);
assert_eq!(read_size(&mut child, first_append.len()), first_append);
let second_append = "doce\ntrece\n";
let expected = at.read("foobar_follow_multiple_appended.expected");
at.append(FOOBAR_TXT, second_append);
assert_eq!(read_size(&mut child, expected.len()), expected);
// kill the dummy process and give tail time to notice this
dummy.kill().unwrap();
let _ = dummy.wait();
sleep(Duration::from_secs(1));
let third_append = "should\nbe\nignored\n";
at.append(FOOBAR_TXT, third_append);
assert_eq!(read_size(&mut child, 1), "\u{0}");
// On Unix, trying to kill a process that's already dead is fine; on Windows it's an error.
#[cfg(unix)]
child.kill().unwrap();
#[cfg(windows)]
assert_eq!(child.kill().is_err(), true);
}
#[test]
fn test_single_big_args() {
const FILE: &'static str = "single_big_args.txt";