From fe5bc47971bf43e25b46d3ea179778a587e9d5f0 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 2 Apr 2016 16:23:26 +0200 Subject: [PATCH 1/4] tests: Simplify logic of UCommand::run. --- tests/common/util.rs | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/tests/common/util.rs b/tests/common/util.rs index 84459153c..9448c8b70 100755 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -486,29 +486,25 @@ impl UCommand { } self.has_run = true; log_info("run", &self.comm_string); - let prog = match self.stdin { - Some(ref input) => { - let mut result = self.raw - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn() - .unwrap(); + let mut result = self.raw + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); - result.stdin - .take() - .unwrap_or_else( - || panic!( - "Could not take child process stdin")) - .write_all(&input) - .unwrap_or_else(|e| panic!("{}", e)); + if let Some(ref input) = self.stdin { + result.stdin + .take() + .unwrap_or_else( + || panic!( + "Could not take child process stdin")) + .write_all(&input) + .unwrap_or_else(|e| panic!("{}", e)); + } + + let prog = result.wait_with_output().unwrap(); - result.wait_with_output().unwrap() - } - None => { - self.raw.output().unwrap() - } - }; CmdResult { success: prog.status.success(), stdout: from_utf8(&prog.stdout).unwrap().to_string(), From 4ba6ea7aad9dc5c22ace858df7254269de4a4e62 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 2 Apr 2016 16:41:59 +0200 Subject: [PATCH 2/4] tests: Move part of UCommand::run to a new function --- tests/common/util.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/common/util.rs b/tests/common/util.rs index 9448c8b70..ef98e1906 100755 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -11,7 +11,7 @@ use std::os::unix::fs::symlink as symlink_file; #[cfg(windows)] use std::os::windows::fs::symlink_file; use std::path::{Path, PathBuf}; -use std::process::{Command, Stdio}; +use std::process::{Command, Stdio, Child}; use std::str::from_utf8; use std::ffi::OsStr; use self::tempdir::TempDir; @@ -480,7 +480,8 @@ impl UCommand { Box::new(self) } - pub fn run(&mut self) -> CmdResult { + /// Spawns the command, feeds the stdin if any, and returns immediately. + pub fn run_no_wait(&mut self) -> Child { if self.has_run { panic!(ALREADY_RUN); } @@ -503,7 +504,13 @@ impl UCommand { .unwrap_or_else(|e| panic!("{}", e)); } - let prog = result.wait_with_output().unwrap(); + result + } + + /// Spawns the command, feeds the stdin if any, waits for the result + /// and returns it. + pub fn run(&mut self) -> CmdResult { + let prog = self.run_no_wait().wait_with_output().unwrap(); CmdResult { success: prog.status.success(), From e673a102b6b872663508e1e8745f59abf540584f Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 2 Apr 2016 23:09:20 +0200 Subject: [PATCH 3/4] 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"; From 8ff308740c33479019e137fe2ece5bb5372c9ecb Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Thu, 7 Apr 2016 20:54:22 +0200 Subject: [PATCH 4/4] Fix test on Rust Stable. https://github.com/rust-lang/rust/issues/32801 --- Makefile | 2 +- tests/common/util.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 19bc50449..63467fe4b 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ BUILDDIR := $(BASEDIR)/target/${PROFILE}/ PKG_BUILDDIR := $(BUILDDIR)/deps/ BUSYBOX_ROOT := $(BASEDIR)/tmp/ -BUSYBOX_VER := 1.24.1 +BUSYBOX_VER := 1.22.0 BUSYBOX_SRC:=$(BUSYBOX_ROOT)/busybox-$(BUSYBOX_VER)/ # Possible programs diff --git a/tests/common/util.rs b/tests/common/util.rs index 0340329ae..540cf5b5f 100755 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -246,7 +246,7 @@ impl AtPath { 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 mut f = OpenOptions::new().write(true).append(true).open(self.plus(name)).unwrap(); let _ = f.write(contents.as_bytes()); }