From e673a102b6b872663508e1e8745f59abf540584f Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 2 Apr 2016 23:09:20 +0200 Subject: [PATCH] tail: Add test for `tail -f`. --- tests/common/util.rs | 18 +++++++++++++++++- tests/tail.rs | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/tests/common/util.rs b/tests/common/util.rs index ef98e1906..0340329ae 100755 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -3,7 +3,7 @@ extern crate tempdir; use std::env; -use std::fs::{self, File}; +use std::fs::{self, File, OpenOptions}; use std::io::{Read, Write, Result}; use std::ops::{Deref, DerefMut}; #[cfg(unix)] @@ -16,6 +16,8 @@ use std::str::from_utf8; use std::ffi::OsStr; use self::tempdir::TempDir; use std::rc::Rc; +use std::thread::sleep; +use std::time::Duration; #[cfg(windows)] static PROGNAME: &'static str = "target\\debug\\uutils.exe"; @@ -242,6 +244,12 @@ impl AtPath { let _ = f.write(contents.as_bytes()); } + pub fn append(&self, name: &str, contents: &str) { + log_info("open(append)", self.plus_as_string(name)); + let mut f = OpenOptions::new().append(true).open(self.plus(name)).unwrap(); + let _ = f.write(contents.as_bytes()); + } + pub fn mkdir(&self, dir: &str) { log_info("mkdir", self.plus_as_string(dir)); fs::create_dir(&self.plus(dir)).unwrap(); @@ -544,6 +552,14 @@ impl UCommand { } } +pub fn read_size(child: &mut Child, size: usize) -> String { + let mut output = Vec::new(); + output.resize(size, 0); + sleep(Duration::from_millis(100)); + child.stdout.as_mut().unwrap().read(output.as_mut_slice()).unwrap(); + String::from_utf8(output).unwrap() +} + // returns a testSet and a ucommand initialized to the utility binary // operating in the fixtures directory with a cleared environment pub fn testset_and_ucommand(utilname: &str) -> (TestSet, UCommand) { diff --git a/tests/tail.rs b/tests/tail.rs index 6dd549918..591c69769 100644 --- a/tests/tail.rs +++ b/tests/tail.rs @@ -1,6 +1,7 @@ extern crate uu_tail; use uu_tail::parse_size; +use std::io::Read; use std::io::Write; #[macro_use] @@ -41,6 +42,24 @@ fn test_null_default() { assert_eq!(result.stdout, at.read("foobar_with_null_default.expected")); } +#[test] +fn test_follow() { + let (at, mut ucmd) = testing(UTIL_NAME); + + let mut child = ucmd.arg("-f").arg(FOOBAR_TXT).run_no_wait(); + + let expected = at.read("foobar_single_default.expected"); + assert_eq!(read_size(&mut child, expected.len()), expected); + + // We write in a temporary copy of foobar.txt + let expected = "line1\nline2\n"; + at.append(FOOBAR_TXT, expected); + + assert_eq!(read_size(&mut child, expected.len()), expected); + + child.kill().unwrap(); +} + #[test] fn test_single_big_args() { const FILE: &'static str = "single_big_args.txt";