mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
tests: Create the ScopedFile
type for temporary files in tests
This commit adds the `ScopedFile` type, which wraps and derefs to a `File`. When a `ScopedFile` is dropped, it removes the underlying file from the filesystem. This is useful for temporary, generated files in tests.
This commit is contained in:
parent
2b2c2b64c2
commit
a629bb3076
1 changed files with 42 additions and 4 deletions
46
tests/common/util.rs
Normal file → Executable file
46
tests/common/util.rs
Normal file → Executable file
|
@ -5,6 +5,7 @@ extern crate tempdir;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::io::{Read, Write, Result};
|
use std::io::{Read, Write, Result};
|
||||||
|
use std::ops::{Deref, DerefMut};
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use std::os::unix::fs::symlink as symlink_file;
|
use std::os::unix::fs::symlink as symlink_file;
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
|
@ -28,7 +29,7 @@ static ALREADY_RUN: &'static str = " you have already run this UCommand, if you
|
||||||
another command in the same test, use TestSet::new instead of \
|
another command in the same test, use TestSet::new instead of \
|
||||||
testing();";
|
testing();";
|
||||||
static MULTIPLE_STDIN_MEANINGLESS: &'static str = "Ucommand is designed around a typical use case of: provide args and input stream -> spawn process -> block until completion -> return output streams. For verifying that a particular section of the input stream is what causes a particular behavior, use the Command type directly.";
|
static MULTIPLE_STDIN_MEANINGLESS: &'static str = "Ucommand is designed around a typical use case of: provide args and input stream -> spawn process -> block until completion -> return output streams. For verifying that a particular section of the input stream is what causes a particular behavior, use the Command type directly.";
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! assert_empty_stderr(
|
macro_rules! assert_empty_stderr(
|
||||||
($cond:expr) => (
|
($cond:expr) => (
|
||||||
|
@ -122,6 +123,40 @@ pub fn recursive_copy(src: &Path, dest: &Path) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A scoped, temporary file that is removed upon drop.
|
||||||
|
pub struct ScopedFile {
|
||||||
|
path: PathBuf,
|
||||||
|
file: File,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ScopedFile {
|
||||||
|
fn new(path: PathBuf, file: File) -> ScopedFile {
|
||||||
|
ScopedFile {
|
||||||
|
path: path,
|
||||||
|
file: file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for ScopedFile {
|
||||||
|
type Target = File;
|
||||||
|
fn deref(&self) -> &File {
|
||||||
|
&self.file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DerefMut for ScopedFile {
|
||||||
|
fn deref_mut(&mut self) -> &mut File {
|
||||||
|
&mut self.file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for ScopedFile {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
fs::remove_file(&self.path).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct AtPath {
|
pub struct AtPath {
|
||||||
pub subdir: PathBuf,
|
pub subdir: PathBuf,
|
||||||
}
|
}
|
||||||
|
@ -185,6 +220,9 @@ impl AtPath {
|
||||||
Err(e) => panic!("{}", e),
|
Err(e) => panic!("{}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn make_scoped_file(&self, name: &str) -> ScopedFile {
|
||||||
|
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();
|
||||||
|
@ -393,7 +431,7 @@ impl UCommand {
|
||||||
.stderr(Stdio::piped())
|
.stderr(Stdio::piped())
|
||||||
.spawn()
|
.spawn()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
result.stdin
|
result.stdin
|
||||||
.take()
|
.take()
|
||||||
.unwrap_or_else(
|
.unwrap_or_else(
|
||||||
|
@ -401,8 +439,8 @@ impl UCommand {
|
||||||
"Could not take child process stdin"))
|
"Could not take child process stdin"))
|
||||||
.write_all(&input)
|
.write_all(&input)
|
||||||
.unwrap_or_else(|e| panic!("{}", e));
|
.unwrap_or_else(|e| panic!("{}", e));
|
||||||
|
|
||||||
result.wait_with_output().unwrap()
|
result.wait_with_output().unwrap()
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
self.raw.output().unwrap()
|
self.raw.output().unwrap()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue