Use `testdir` instead of own tmpdir implementation

This crate seems well suited for our task. The code is also simpler as
we deal with a PathBuf directly.
This commit is contained in:
Jean-Claude 2023-09-13 12:14:27 +02:00
parent e0c16c2cd4
commit 3325a28bd8
Signed by: jeanclaude
GPG Key ID: 8A300F57CBB9F63E
4 changed files with 221 additions and 74 deletions

195
Cargo.lock generated
View File

@ -2,6 +2,21 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "addr2line"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a76fd60b23679b7d19bd066031410fb7e458ccc5e958eb5c325888ce4baedc97"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "1.0.1"
@ -81,6 +96,21 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "backtrace"
version = "0.3.67"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "233d376d6d185f2a3093e58f283f60f880315b6c60075b01f36b3b85154564ca"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "bitflags"
version = "1.3.2"
@ -93,6 +123,37 @@ version = "3.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c6ed94e98ecff0c12dd1b04c15ec0d7d9458ca8fe806cea6f12954efe74c63b"
[[package]]
name = "camino"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c59e92b5a388f549b863a7bea62612c09f24c8393560709a54558a9abdfb3b9c"
dependencies = [
"serde",
]
[[package]]
name = "cargo-platform"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2cfa25e60aea747ec7e1124f238816749faa93759c6ff5b31f1ccdda137f4479"
dependencies = [
"serde",
]
[[package]]
name = "cargo_metadata"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4acbb09d9ee8e23699b9634375c72795d095bf268439da88562cf9b501f181fa"
dependencies = [
"camino",
"cargo-platform",
"semver",
"serde",
"serde_json",
]
[[package]]
name = "cc"
version = "1.0.79"
@ -356,6 +417,12 @@ dependencies = [
"wasi 0.11.0+wasi-snapshot-preview1",
]
[[package]]
name = "gimli"
version = "0.27.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e"
[[package]]
name = "git2"
version = "0.17.1"
@ -451,6 +518,12 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "itoa"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]]
name = "jobserver"
version = "0.1.26"
@ -559,15 +632,34 @@ dependencies = [
"regex",
"rstest",
"rust-ini",
"testdir",
"thiserror",
]
[[package]]
name = "miniz_oxide"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b275950c28b37e794e8c55d88aeb5e139d0ce23fdbbeda68f8d7174abdf9e8fa"
dependencies = [
"adler",
]
[[package]]
name = "mutate_once"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16cf681a23b4d0a43fc35024c176437f9dcd818db34e0f42ab456a0ee5ad497b"
[[package]]
name = "ntapi"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4"
dependencies = [
"winapi",
]
[[package]]
name = "num-integer"
version = "0.1.45"
@ -587,6 +679,15 @@ dependencies = [
"autocfg",
]
[[package]]
name = "object"
version = "0.30.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03b4680b86d9cfafba8fc491dc9b6df26b68cf40e9e6cd73909194759a63c385"
dependencies = [
"memchr",
]
[[package]]
name = "once_cell"
version = "1.17.1"
@ -777,6 +878,12 @@ dependencies = [
"ordered-multimap",
]
[[package]]
name = "rustc-demangle"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "rustc_version"
version = "0.4.0"
@ -800,11 +907,51 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "ryu"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
[[package]]
name = "semver"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918"
dependencies = [
"serde",
]
[[package]]
name = "serde"
version = "1.0.164"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e8c8cf938e98f769bc164923b06dce91cea1751522f46f8466461af04c9027d"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.164"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9735b638ccc51c28bf6914d90a2e9725b377144fc612c49a611fddd1b631d68"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46266871c240a00b8f503b877622fe33430b3c7d963bdc0f2adc511e54a1eae3"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "slab"
@ -832,6 +979,34 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "sysinfo"
version = "0.26.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c18a6156d1f27a9592ee18c1a846ca8dd5c258b7179fc193ae87c74ebb666f5"
dependencies = [
"cfg-if",
"core-foundation-sys",
"libc",
"ntapi",
"once_cell",
"winapi",
]
[[package]]
name = "testdir"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48b7965698cfb3d1ac1e6e54b4b45f5caa9e89bda223c8cf723d9cf53d7cefa7"
dependencies = [
"anyhow",
"backtrace",
"cargo_metadata",
"once_cell",
"sysinfo",
"whoami",
]
[[package]]
name = "thiserror"
version = "1.0.40"
@ -997,6 +1172,26 @@ version = "0.2.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed9d5b4305409d1fc9482fee2d7f9bcbf24b3972bf59817ef757e23982242a93"
[[package]]
name = "web-sys"
version = "0.3.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bdd9ef4e984da1187bf8110c5cf5b845fbc87a23602cdf912386a76fcd3a7c2"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "whoami"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22fc3756b8a9133049b26c7f61ab35416c130e8c09b660f5b3958b446f52cc50"
dependencies = [
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "winapi"
version = "0.3.9"

View File

@ -21,3 +21,4 @@ thiserror = "1.0.40"
pretty_assertions = "1.4.0"
rand = "0.8.5"
rstest = "0.18.2"
testdir = "0.8.0"

View File

@ -260,13 +260,13 @@ Event=%Y/*Sy*Sm*Sd-*Ey*Em*Ed*i/%y%m%d/%y%m%d_%H%M%S*c.*x",
}
#[rstest]
fn config_from_file(tmp_dir: TmpPath, default_config_string: String, default_config: Config) {
fn config_from_file(tmp_dir: PathBuf, default_config_string: String, default_config: Config) {
// TODO: from_file also calls from_ini. Patch it?
let mut f = tmp_dir;
f.path.push("config");
fs::write(&f.path, default_config_string).expect("Failed to write to file");
let mut path = tmp_dir;
path.push("config");
fs::write(&path, default_config_string).expect("Failed to write to file");
let config = Config::from_file(&f.path).unwrap();
let config = Config::from_file(&path).unwrap();
assert_eq!(config, default_config);
}
@ -323,11 +323,11 @@ Default=MORE_MISSING_FIELDS").unwrap())]
}
#[rstest]
fn config_dump_default(tmp_dir: TmpPath, default_config_string: String) {
let mut f = tmp_dir;
f.path.push("dump");
assert!(Config::dump_default(&f.path).is_ok());
let content = fs::read_to_string(&f.path).expect("Failed to read from file");
fn config_dump_default(tmp_dir: PathBuf, default_config_string: String) {
let mut path = tmp_dir;
path.push("dump");
assert!(Config::dump_default(&path).is_ok());
let content = fs::read_to_string(&path).expect("Failed to read from file");
let content = content.trim();
assert_eq!(content, default_config_string);
}

View File

@ -5,72 +5,23 @@ use rand::thread_rng;
use rstest::fixture;
use std::fs;
use std::path::PathBuf;
use testdir::testdir;
#[derive(Debug)]
/// Represents a temporary path that is deleted when no longer used.
pub struct TmpPath {
/// PathBuf to the temporary file.
pub path: PathBuf,
/// Equivalend to path, but is immutable. Used for deleting the path on drop.
init_path: PathBuf,
}
impl TmpPath {
/// Create a temporary directory and provide corresponding `TmpPath`.
fn new_dir() -> Self {
// TODO check for CARGO_TARGET_TMPDIR
let mut path = PathBuf::new();
path.push(r"/tmp");
path.push(r"mianex");
path.push(format!(
"dir_{}",
Alphanumeric.sample_string(&mut thread_rng(), 8)
));
fs::create_dir_all(&path)
.unwrap_or_else(|_| panic!("Failed to create tmpdir {}", path.display()));
Self {
path: path.clone(),
init_path: path,
}
}
/// Create a temporary file and provide corresponding `TmpPath`.
fn new_file() -> Self {
// TODO check for CARGO_TARGET_TMPDIR
let mut path = PathBuf::new();
path.push(r"/tmp");
path.push(r"mianex");
fs::create_dir_all(&path)
.unwrap_or_else(|_| panic!("Failed to create tmpdir {}", path.display()));
path.push(format!(
"file_{}",
Alphanumeric.sample_string(&mut thread_rng(), 8)
));
fs::File::create(&path)
.unwrap_or_else(|_| panic!("Failed to create tmpfile {}", path.display()));
Self {
path: path.clone(),
init_path: path,
}
}
}
impl Drop for TmpPath {
/// Remove created directories when `TmpPath` goes out of scope.
fn drop(&mut self) {
fs::remove_dir_all(&self.init_path)
.unwrap_or_else(|_| panic!("Failed to remove tmpfile {}", self.init_path.display()));
}
#[fixture]
/// Return path to a temporary test directory
pub fn tmp_dir() -> PathBuf {
testdir!()
}
#[fixture]
/// Obtain `TmpPath` for a temporary directory.
pub fn tmp_dir() -> TmpPath {
TmpPath::new_dir()
}
#[fixture]
/// Obtain `TmpPath` for a temporary file.
pub fn tmp_file() -> TmpPath {
TmpPath::new_file()
/// Return path to a temporary test file
pub fn tmp_file() -> PathBuf {
let mut path = tmp_dir();
path.push(format!(
"file_{}",
Alphanumeric.sample_string(&mut thread_rng(), 8)
));
fs::File::create(&path)
.unwrap_or_else(|_| panic!("Failed to create tmpfile {}", path.display()));
path
}