Skip to content

Commit e080102

Browse files
authored
tempdir: Add a into_dir method (#363)
I have a use case where I want the equivalent of `into_path()`. Signed-off-by: Colin Walters <walters@verbum.org>
1 parent 0c0c286 commit e080102

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

cap-tempfile/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ impl TempDir {
101101
Err(Self::already_exists())
102102
}
103103

104+
/// Make this directory persistent.
105+
///
106+
/// This corresponds to [`tempfile::TempDir::into_path`], but returns a [`Dir`].
107+
///
108+
/// [`tempfile::TempDir::into_path`]: https://docs.rs/tempfile/latest/tempfile/struct.TempDir.html#method.into_path
109+
/// [`Dir`]: https://docs.rs/cap-std/latest/cap_std/fs/struct.Dir.html
110+
pub fn into_dir(mut self) -> io::Result<Dir> {
111+
Ok(self.dir.take().unwrap())
112+
}
113+
104114
/// Closes and removes the temporary directory, returning a `Result`.
105115
///
106116
/// This corresponds to [`tempfile::TempDir::close`].
@@ -222,6 +232,15 @@ fn close_tempdir() {
222232
t.close().unwrap();
223233
}
224234

235+
#[test]
236+
fn persist_tempdir() {
237+
use crate::ambient_authority;
238+
239+
let t = tempdir(ambient_authority()).unwrap();
240+
let d = t.into_dir().unwrap();
241+
assert!(d.exists("."));
242+
}
243+
225244
#[test]
226245
fn drop_tempdir_in() {
227246
use crate::ambient_authority;

0 commit comments

Comments
 (0)