Skip to main content

rustest_fixtures/
tempdir.rs

1use 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.
9pub struct TempDir(tempfile::TempDir);
10
11impl std::ops::Deref for TempDir {
12    type Target = tempfile::TempDir;
13    fn deref(&self) -> &Self::Target {
14        &self.0
15    }
16}
17
18impl Fixture for TempDir {
19    type Type = tempfile::TempDir;
20    type Proxy = Proxy;
21}
22
23pub struct Proxy;
24
25impl Duplicate for Proxy {
26    fn duplicate(&self) -> Self {
27        Self
28    }
29}
30
31impl TestName for Proxy {
32    fn name(&self) -> Option<String> {
33        None
34    }
35}
36
37impl 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}