Skip to content

Commit

Permalink
tempdir: Add a persist method
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 <walters@verbum.org>
  • Loading branch information
cgwalters committed Jul 17, 2024
1 parent 076ab49 commit 2cd850b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cap-tempfile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ impl TempDir {
Err(Self::already_exists())
}

/// Make this directory persistent.
///
/// This corresponds to [`tempfile::TempDir::into_path`].
///
/// [`tempfile::TempDir::into_path`]: https://docs.rs/tempfile/latest/tempfile/struct.TempDir.html#method.into_path
pub fn persist(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 +231,15 @@ fn close_tempdir() {
t.close().unwrap();
}

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

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

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

0 comments on commit 2cd850b

Please sign in to comment.