Skip to main content

rustest_fixtures/
tempfile.rs

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