umanux/tests/testfiles.rs

47 lines
1.1 KiB
Rust
Raw Normal View History

2020-11-05 09:01:51 +01:00
use std::{env, path::Path};
use std::{fs, path::PathBuf};
use tempfile::TempDir;
use std::ops::Deref;
struct Fixture {
path: PathBuf,
source: PathBuf,
_tempdir: TempDir,
}
impl Fixture {
fn blank(fixture_filename: &str) -> Self {
// First, figure out the right file in `tests/fixtures/`:
let root_dir = &env::var("CARGO_MANIFEST_DIR").expect("$CARGO_MANIFEST_DIR");
let mut source = PathBuf::from(root_dir);
source.push("tests/fixtures");
source.push(&fixture_filename);
// The "real" path of the file is going to be under a temporary directory:
let tempdir = tempfile::tempdir().unwrap();
let mut path = PathBuf::from(&tempdir.path());
path.push(&fixture_filename);
Fixture {
_tempdir: tempdir,
source,
path,
}
}
fn copy(fixture_filename: &str) -> Self {
let fixture = Fixture::blank(fixture_filename);
fs::copy(&fixture.source, &fixture.path).unwrap();
fixture
}
}
impl Deref for Fixture {
type Target = Path;
fn deref(&self) -> &Self::Target {
self.path.deref()
}
}