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

tests: fix whitespace

This commit is contained in:
Joseph Crail 2016-03-28 21:13:40 -04:00
parent 1fecba3226
commit 7d103a0a64

View file

@ -79,32 +79,40 @@ impl CmdResult {
assert!(self.success); assert!(self.success);
Box::new(self) Box::new(self)
} }
pub fn failure(&self) -> Box<&CmdResult> { pub fn failure(&self) -> Box<&CmdResult> {
assert!(!self.success); assert!(!self.success);
Box::new(self) Box::new(self)
} }
pub fn no_stderr(&self) -> Box<&CmdResult> { pub fn no_stderr(&self) -> Box<&CmdResult> {
assert!(self.stderr.len() == 0); assert!(self.stderr.len() == 0);
Box::new(self) Box::new(self)
} }
pub fn no_stdout(&self) -> Box<&CmdResult> { pub fn no_stdout(&self) -> Box<&CmdResult> {
assert!(self.stdout.len() == 0); assert!(self.stdout.len() == 0);
Box::new(self) Box::new(self)
} }
pub fn stdout_is<T: AsRef<str>>(&self, msg: T) -> Box<&CmdResult> { pub fn stdout_is<T: AsRef<str>>(&self, msg: T) -> Box<&CmdResult> {
assert!(self.stdout.trim_right() == String::from(msg.as_ref()).trim_right()); assert!(self.stdout.trim_right() == String::from(msg.as_ref()).trim_right());
Box::new(self) Box::new(self)
} }
pub fn stderr_is<T: AsRef<str>>(&self, msg: T) -> Box<&CmdResult> { pub fn stderr_is<T: AsRef<str>>(&self, msg: T) -> Box<&CmdResult> {
assert!(self.stderr.trim_right() == String::from(msg.as_ref()).trim_right()); assert!(self.stderr.trim_right() == String::from(msg.as_ref()).trim_right());
Box::new(self) Box::new(self)
} }
pub fn stdout_only<T: AsRef<str>>(&self, msg: T) -> Box<&CmdResult> { pub fn stdout_only<T: AsRef<str>>(&self, msg: T) -> Box<&CmdResult> {
self.stdout_is(msg).no_stderr() self.stdout_is(msg).no_stderr()
} }
pub fn stderr_only<T: AsRef<str>>(&self, msg: T) -> Box<&CmdResult> { pub fn stderr_only<T: AsRef<str>>(&self, msg: T) -> Box<&CmdResult> {
self.stderr_is(msg).no_stdout() self.stderr_is(msg).no_stdout()
} }
pub fn fails_silently(&self) -> Box<&CmdResult> { pub fn fails_silently(&self) -> Box<&CmdResult> {
assert!(!self.success); assert!(!self.success);
assert!(self.stderr.len() == 0); assert!(self.stderr.len() == 0);
@ -116,6 +124,14 @@ pub fn log_info<T: AsRef<str>, U: AsRef<str>>(msg: T, par: U) {
println!("{}: {}", msg.as_ref(), par.as_ref()); println!("{}: {}", msg.as_ref(), par.as_ref());
} }
pub fn repeat_component(s: &str, n: u32) -> String {
let mut path = PathBuf::from("");
for _ in 0..n {
path.push(s);
}
path.to_str().unwrap().to_owned()
}
pub fn recursive_copy(src: &Path, dest: &Path) -> Result<()> { pub fn recursive_copy(src: &Path, dest: &Path) -> Result<()> {
if try!(fs::metadata(src)).is_dir() { if try!(fs::metadata(src)).is_dir() {
for entry in try!(fs::read_dir(src)) { for entry in try!(fs::read_dir(src)) {
@ -178,21 +194,26 @@ impl Drop for ScopedFile {
pub struct AtPath { pub struct AtPath {
pub subdir: PathBuf, pub subdir: PathBuf,
} }
impl AtPath { impl AtPath {
pub fn new(subdir: &Path) -> AtPath { pub fn new(subdir: &Path) -> AtPath {
AtPath { subdir: PathBuf::from(subdir) } AtPath { subdir: PathBuf::from(subdir) }
} }
pub fn as_string(&self) -> String { pub fn as_string(&self) -> String {
self.subdir.to_str().unwrap().to_owned() self.subdir.to_str().unwrap().to_owned()
} }
pub fn plus(&self, name: &str) -> PathBuf { pub fn plus(&self, name: &str) -> PathBuf {
let mut pathbuf = self.subdir.clone(); let mut pathbuf = self.subdir.clone();
pathbuf.push(name); pathbuf.push(name);
pathbuf pathbuf
} }
pub fn plus_as_string(&self, name: &str) -> String { pub fn plus_as_string(&self, name: &str) -> String {
String::from(self.plus(name).to_str().unwrap()) String::from(self.plus(name).to_str().unwrap())
} }
fn minus(&self, name: &str) -> PathBuf { fn minus(&self, name: &str) -> PathBuf {
// relative_from is currently unstable // relative_from is currently unstable
let prefixed = PathBuf::from(name); let prefixed = PathBuf::from(name);
@ -207,23 +228,28 @@ impl AtPath {
prefixed prefixed
} }
} }
pub fn minus_as_string(&self, name: &str) -> String { pub fn minus_as_string(&self, name: &str) -> String {
String::from(self.minus(name).to_str().unwrap()) String::from(self.minus(name).to_str().unwrap())
} }
pub fn open(&self, name: &str) -> File { pub fn open(&self, name: &str) -> File {
log_info("open", self.plus_as_string(name)); log_info("open", self.plus_as_string(name));
File::open(self.plus(name)).unwrap() File::open(self.plus(name)).unwrap()
} }
pub fn read(&self, name: &str) -> String { pub fn read(&self, name: &str) -> String {
let mut f = self.open(name); let mut f = self.open(name);
let mut contents = String::new(); let mut contents = String::new();
let _ = f.read_to_string(&mut contents); let _ = f.read_to_string(&mut contents);
contents contents
} }
pub fn write(&self, name: &str, contents: &str) { pub fn write(&self, name: &str, contents: &str) {
let mut f = self.open(name); let mut f = self.open(name);
let _ = f.write(contents.as_bytes()); let _ = f.write(contents.as_bytes());
} }
pub fn mkdir(&self, dir: &str) { pub fn mkdir(&self, dir: &str) {
log_info("mkdir", self.plus_as_string(dir)); log_info("mkdir", self.plus_as_string(dir));
fs::create_dir(&self.plus(dir)).unwrap(); fs::create_dir(&self.plus(dir)).unwrap();
@ -232,24 +258,29 @@ impl AtPath {
log_info("mkdir_all", self.plus_as_string(dir)); log_info("mkdir_all", self.plus_as_string(dir));
fs::create_dir_all(self.plus(dir)).unwrap(); fs::create_dir_all(self.plus(dir)).unwrap();
} }
pub fn make_file(&self, name: &str) -> File { pub fn make_file(&self, name: &str) -> File {
match File::create(&self.plus(name)) { match File::create(&self.plus(name)) {
Ok(f) => f, Ok(f) => f,
Err(e) => panic!("{}", e), Err(e) => panic!("{}", e),
} }
} }
pub fn make_scoped_file(&self, name: &str) -> ScopedFile { pub fn make_scoped_file(&self, name: &str) -> ScopedFile {
ScopedFile::new(self.plus(name), self.make_file(name)) ScopedFile::new(self.plus(name), self.make_file(name))
} }
pub fn touch(&self, file: &str) { pub fn touch(&self, file: &str) {
log_info("touch", self.plus_as_string(file)); log_info("touch", self.plus_as_string(file));
File::create(&self.plus(file)).unwrap(); File::create(&self.plus(file)).unwrap();
} }
pub fn symlink(&self, src: &str, dst: &str) { pub fn symlink(&self, src: &str, dst: &str) {
log_info("symlink", log_info("symlink",
&format!("{},{}", self.plus_as_string(src), self.plus_as_string(dst))); &format!("{},{}", self.plus_as_string(src), self.plus_as_string(dst)));
symlink_file(&self.plus(src), &self.plus(dst)).unwrap(); symlink_file(&self.plus(src), &self.plus(dst)).unwrap();
} }
pub fn is_symlink(&self, path: &str) -> bool { pub fn is_symlink(&self, path: &str) -> bool {
log_info("is_symlink", self.plus_as_string(path)); log_info("is_symlink", self.plus_as_string(path));
match fs::symlink_metadata(&self.plus(path)) { match fs::symlink_metadata(&self.plus(path)) {
@ -332,6 +363,7 @@ pub struct TestSet {
pub fixtures: AtPath, pub fixtures: AtPath,
tmpd: Rc<TempDir>, tmpd: Rc<TempDir>,
} }
impl TestSet { impl TestSet {
pub fn new(util_name: &str) -> TestSet { pub fn new(util_name: &str) -> TestSet {
let tmpd = Rc::new(TempDir::new("uutils").unwrap()); let tmpd = Rc::new(TempDir::new("uutils").unwrap());
@ -356,14 +388,17 @@ impl TestSet {
} }
ts ts
} }
pub fn util_cmd(&self) -> UCommand { pub fn util_cmd(&self) -> UCommand {
let mut cmd = self.cmd(&self.bin_path); let mut cmd = self.cmd(&self.bin_path);
cmd.arg(&self.util_name); cmd.arg(&self.util_name);
cmd cmd
} }
pub fn cmd<S: AsRef<OsStr>>(&self, bin: S) -> UCommand { pub fn cmd<S: AsRef<OsStr>>(&self, bin: S) -> UCommand {
UCommand::new_from_tmp(bin, self.tmpd.clone(), true) UCommand::new_from_tmp(bin, self.tmpd.clone(), true)
} }
// different names are used rather than an argument // different names are used rather than an argument
// because the need to keep the environment is exceedingly rare. // because the need to keep the environment is exceedingly rare.
pub fn util_cmd_keepenv(&self) -> UCommand { pub fn util_cmd_keepenv(&self) -> UCommand {
@ -371,6 +406,7 @@ impl TestSet {
cmd.arg(&self.util_name); cmd.arg(&self.util_name);
cmd cmd
} }
pub fn cmd_keepenv<S: AsRef<OsStr>>(&self, bin: S) -> UCommand { pub fn cmd_keepenv<S: AsRef<OsStr>>(&self, bin: S) -> UCommand {
UCommand::new_from_tmp(bin, self.tmpd.clone(), false) UCommand::new_from_tmp(bin, self.tmpd.clone(), false)
} }
@ -383,6 +419,7 @@ pub struct UCommand {
has_run: bool, has_run: bool,
stdin: Option<Vec<u8>> stdin: Option<Vec<u8>>
} }
impl UCommand { impl UCommand {
pub fn new<T: AsRef<OsStr>, U: AsRef<OsStr>>(arg: T, curdir: U, env_clear: bool) -> UCommand { pub fn new<T: AsRef<OsStr>, U: AsRef<OsStr>>(arg: T, curdir: U, env_clear: bool) -> UCommand {
UCommand { UCommand {
@ -412,12 +449,14 @@ impl UCommand {
stdin: None stdin: None
} }
} }
pub fn new_from_tmp<T: AsRef<OsStr>>(arg: T, tmpd: Rc<TempDir>, env_clear: bool) -> UCommand { pub fn new_from_tmp<T: AsRef<OsStr>>(arg: T, tmpd: Rc<TempDir>, env_clear: bool) -> UCommand {
let tmpd_path_buf = String::from(&(*tmpd.as_ref().path().to_str().unwrap())); let tmpd_path_buf = String::from(&(*tmpd.as_ref().path().to_str().unwrap()));
let mut ucmd: UCommand = UCommand::new(arg.as_ref(), tmpd_path_buf, env_clear); let mut ucmd: UCommand = UCommand::new(arg.as_ref(), tmpd_path_buf, env_clear);
ucmd.tmpd = Some(tmpd); ucmd.tmpd = Some(tmpd);
ucmd ucmd
} }
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> Box<&mut UCommand> { pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> Box<&mut UCommand> {
if self.has_run { if self.has_run {
panic!(ALREADY_RUN); panic!(ALREADY_RUN);
@ -484,6 +523,7 @@ impl UCommand {
stderr: from_utf8(&prog.stderr).unwrap().to_string(), stderr: from_utf8(&prog.stderr).unwrap().to_string(),
} }
} }
pub fn pipe_in<T: Into<Vec<u8>>>(&mut self, input: T) -> Box<&mut UCommand> { pub fn pipe_in<T: Into<Vec<u8>>>(&mut self, input: T) -> Box<&mut UCommand> {
if self.stdin.is_some() { if self.stdin.is_some() {
panic!(MULTIPLE_STDIN_MEANINGLESS); panic!(MULTIPLE_STDIN_MEANINGLESS);
@ -491,14 +531,17 @@ impl UCommand {
self.stdin = Some(input.into()); self.stdin = Some(input.into());
Box::new(self) Box::new(self)
} }
pub fn run_piped_stdin<T: Into<Vec<u8>>>(&mut self, input: T) -> CmdResult { pub fn run_piped_stdin<T: Into<Vec<u8>>>(&mut self, input: T) -> CmdResult {
self.pipe_in(input).run() self.pipe_in(input).run()
} }
pub fn succeeds(&mut self) -> CmdResult { pub fn succeeds(&mut self) -> CmdResult {
let cmd_result = self.run(); let cmd_result = self.run();
cmd_result.success(); cmd_result.success();
cmd_result cmd_result
} }
pub fn fails(&mut self) -> CmdResult { pub fn fails(&mut self) -> CmdResult {
let cmd_result = self.run(); let cmd_result = self.run();
cmd_result.failure(); cmd_result.failure();
@ -513,6 +556,7 @@ pub fn testset_and_ucommand(utilname: &str) -> (TestSet, UCommand) {
let ucmd = ts.util_cmd(); let ucmd = ts.util_cmd();
(ts, ucmd) (ts, ucmd)
} }
pub fn testing(utilname: &str) -> (AtPath, UCommand) { pub fn testing(utilname: &str) -> (AtPath, UCommand) {
let ts = TestSet::new(utilname); let ts = TestSet::new(utilname);
let ucmd = ts.util_cmd(); let ucmd = ts.util_cmd();