Skip to content

Commit 48ac935

Browse files
authored
Merge pull request #42 from mgautierfr/tempdir_fixture
Introduce TempDir as std fixture.
2 parents 5eaf1ee + be5a9f7 commit 48ac935

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

rustest-fixtures/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
//! If you have a need for new standard fixture, issue or PR are welcomed.
55
66
mod global;
7+
mod tempdir;
78
mod tempfile;
89

910
pub use global::Global;
11+
pub use tempdir::TempDir;
1012
pub use tempfile::TempFile;

rustest-fixtures/src/tempdir.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use rustest::{
2+
Duplicate, Fixture, FixtureCreationError, FixtureCreationResult, FixtureProxy, FixtureScope,
3+
TestContext, TestName,
4+
};
5+
6+
/// A temporary directory.
7+
///
8+
/// A temporary directory, generated with `tempfile` crate.
9+
pub struct TempDir(tempfile::TempDir);
10+
11+
impl std::ops::Deref for TempDir {
12+
type Target = tempfile::TempDir;
13+
fn deref(&self) -> &Self::Target {
14+
&self.0
15+
}
16+
}
17+
18+
impl Fixture for TempDir {
19+
type Type = tempfile::TempDir;
20+
type Proxy = Proxy;
21+
}
22+
23+
pub struct Proxy;
24+
25+
impl Duplicate for Proxy {
26+
fn duplicate(&self) -> Self {
27+
Self
28+
}
29+
}
30+
31+
impl TestName for Proxy {
32+
fn name(&self) -> Option<String> {
33+
None
34+
}
35+
}
36+
37+
impl FixtureProxy for Proxy {
38+
type Fixt = TempDir;
39+
const SCOPE: FixtureScope = FixtureScope::Once;
40+
41+
fn setup(_ctx: &mut TestContext) -> Vec<Self>
42+
where
43+
Self: Sized,
44+
{
45+
vec![Self]
46+
}
47+
48+
fn build(self) -> FixtureCreationResult<Self::Fixt> {
49+
tempfile::tempdir_in(std::env::temp_dir())
50+
.map(TempDir)
51+
.map_err(|e| FixtureCreationError::new("TempDir", e))
52+
}
53+
}

0 commit comments

Comments
 (0)