umanux/tests/testfiles.rs

50 lines
1.2 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;
2020-11-11 08:20:09 +01:00
#[derive(Debug)]
pub struct Fixture {
pub path: PathBuf,
pub source: PathBuf,
2020-11-05 09:01:51 +01:00
_tempdir: TempDir,
}
impl Fixture {
2020-11-09 09:56:24 +01:00
#[must_use]
pub fn blank(fixture_filename: &str) -> Self {
2020-11-05 09:01:51 +01:00
// 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);
2020-11-09 09:56:24 +01:00
Self {
2020-11-05 09:01:51 +01:00
_tempdir: tempdir,
source,
path,
}
}
2020-11-09 09:56:24 +01:00
#[must_use]
pub fn copy(fixture_filename: &str) -> Self {
2020-11-09 09:56:24 +01:00
let fixture = Self::blank(fixture_filename);
2020-11-05 09:01:51 +01:00
fs::copy(&fixture.source, &fixture.path).unwrap();
fixture
}
}
impl Deref for Fixture {
type Target = Path;
fn deref(&self) -> &Self::Target {
2020-11-09 09:56:24 +01:00
&self.path
2020-11-05 09:01:51 +01:00
}
}