Skip to content

Commit

Permalink
tempdir: Add a into_dir method (#363)
Browse files Browse the repository at this point in the history
I have a use case where I want the equivalent of `into_path()`.

Signed-off-by: Colin Walters <[email protected]>
  • Loading branch information
cgwalters authored Sep 24, 2024
1 parent 0c0c286 commit e080102
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cap-tempfile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ impl TempDir {
Err(Self::already_exists())
}

/// Make this directory persistent.
///
/// This corresponds to [`tempfile::TempDir::into_path`], but returns a [`Dir`].
///
/// [`tempfile::TempDir::into_path`]: https://docs.rs/tempfile/latest/tempfile/struct.TempDir.html#method.into_path
/// [`Dir`]: https://docs.rs/cap-std/latest/cap_std/fs/struct.Dir.html
pub fn into_dir(mut self) -> io::Result<Dir> {
Ok(self.dir.take().unwrap())
}

/// Closes and removes the temporary directory, returning a `Result`.
///
/// This corresponds to [`tempfile::TempDir::close`].
Expand Down Expand Up @@ -222,6 +232,15 @@ fn close_tempdir() {
t.close().unwrap();
}

#[test]
fn persist_tempdir() {
use crate::ambient_authority;

let t = tempdir(ambient_authority()).unwrap();
let d = t.into_dir().unwrap();
assert!(d.exists("."));
}

#[test]
fn drop_tempdir_in() {
use crate::ambient_authority;
Expand Down

0 comments on commit e080102

Please sign in to comment.