Skip to content

Commit 82220c8

Browse files
committed
feat(fixture): Debug persistence support
Fixes #31
1 parent c6f8332 commit 82220c8

File tree

2 files changed

+81
-7
lines changed

2 files changed

+81
-7
lines changed

src/fixture/dir.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ use super::errors::*;
6060
/// [`std::fs`]: http://doc.rust-lang.org/std/fs/index.html
6161
/// [`std::process::exit()`]: http://doc.rust-lang.org/std/process/fn.exit.html
6262
pub struct TempDir {
63-
temp: tempfile::TempDir,
63+
temp: Inner,
64+
}
65+
66+
enum Inner {
67+
Temp(tempfile::TempDir),
68+
Persisted(path::PathBuf),
6469
}
6570

6671
impl TempDir {
@@ -86,9 +91,35 @@ impl TempDir {
8691
pub fn new() -> Result<Self, FixtureError> {
8792
let temp = tempfile::TempDir::new()
8893
.chain(FixtureError::new(FixtureKind::CreateDir))?;
94+
let temp = Inner::Temp(temp);
8995
Ok(Self { temp })
9096
}
9197

98+
/// Conditionally persist the temporary directory for debug purposes.
99+
///
100+
/// # Examples
101+
///
102+
/// ```no_run
103+
/// use assert_fs::fixture::TempDir;
104+
///
105+
/// let tmp_dir = TempDir::new().unwrap().persist_if(true);
106+
///
107+
/// // Ensure deletion happens.
108+
/// tmp_dir.close().unwrap();
109+
/// ```
110+
pub fn persist_if(self, yes: bool) -> Self {
111+
if ! yes {
112+
return self;
113+
}
114+
115+
let path = match self.temp {
116+
Inner::Temp(temp) => temp.into_path(),
117+
Inner::Persisted(path) => path,
118+
};
119+
let temp = Inner::Persisted(path);
120+
Self { temp }
121+
}
122+
92123
/// Accesses the [`Path`] to the temporary directory.
93124
///
94125
/// [`Path`]: http://doc.rust-lang.org/std/path/struct.Path.html
@@ -106,7 +137,10 @@ impl TempDir {
106137
/// tmp_dir.close().unwrap();
107138
/// ```
108139
pub fn path(&self) -> &path::Path {
109-
self.temp.path()
140+
match self.temp {
141+
Inner::Temp(ref temp) => temp.path(),
142+
Inner::Persisted(ref path) => path.as_path(),
143+
}
110144
}
111145

112146
/// Closes and removes the temporary directory, returing a `Result`.
@@ -135,8 +169,12 @@ impl TempDir {
135169
/// tmp_dir.close().unwrap();
136170
/// ```
137171
pub fn close(self) -> Result<(), FixtureError> {
138-
self.temp.close()
139-
.chain(FixtureError::new(FixtureKind::Cleanup))?;
172+
match self.temp {
173+
Inner::Temp(temp) =>
174+
temp.close()
175+
.chain(FixtureError::new(FixtureKind::Cleanup))?,
176+
Inner::Persisted(_) => (),
177+
}
140178
Ok(())
141179
}
142180
}

src/fixture/file.rs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ use super::errors::*;
6060
/// [`std::fs`]: http://doc.rust-lang.org/std/fs/index.html
6161
/// [`std::process::exit()`]: http://doc.rust-lang.org/std/process/fn.exit.html
6262
pub struct NamedTempFile {
63-
temp: tempfile::TempDir,
63+
temp: Inner,
6464
path: path::PathBuf,
6565
}
6666

67+
enum Inner {
68+
Temp(tempfile::TempDir),
69+
Persisted,
70+
}
71+
6772
impl NamedTempFile {
6873
/// Attempts to make a temporary file inside of `env::temp_dir()`.
6974
///
@@ -91,9 +96,36 @@ impl NamedTempFile {
9196
let temp = tempfile::TempDir::new()
9297
.chain(FixtureError::new(FixtureKind::CreateDir))?;
9398
let path = temp.path().join(name.as_ref());
99+
let temp = Inner::Temp(temp);
94100
Ok(Self { temp, path })
95101
}
96102

103+
/// Conditionally persist the temporary file for debug purposes.
104+
///
105+
/// # Examples
106+
///
107+
/// ```no_run
108+
/// use assert_fs::fixture::NamedTempFile;
109+
///
110+
/// let tmp_file = NamedTempFile::new("foo.rs").unwrap().persist_if(true);
111+
///
112+
/// // Ensure deletion happens.
113+
/// tmp_file.close().unwrap();
114+
/// ```
115+
pub fn persist_if(mut self, yes: bool) -> Self {
116+
if ! yes {
117+
return self;
118+
}
119+
120+
let mut temp = Inner::Persisted;
121+
::std::mem::swap(&mut self.temp, &mut temp);
122+
if let Inner::Temp(temp) = temp {
123+
temp.into_path();
124+
}
125+
126+
self
127+
}
128+
97129
/// Accesses the [`Path`] to the temporary file.
98130
///
99131
/// [`Path`]: http://doc.rust-lang.org/std/path/struct.Path.html
@@ -138,8 +170,12 @@ impl NamedTempFile {
138170
/// tmp_file.close().unwrap();
139171
/// ```
140172
pub fn close(self) -> Result<(), FixtureError> {
141-
self.temp.close()
142-
.chain(FixtureError::new(FixtureKind::Cleanup))?;
173+
match self.temp {
174+
Inner::Temp(temp) =>
175+
temp.close()
176+
.chain(FixtureError::new(FixtureKind::Cleanup))?,
177+
Inner::Persisted => (),
178+
}
143179
Ok(())
144180
}
145181
}

0 commit comments

Comments
 (0)