Skip to content

tempdir: Add a persist method #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cap-tempfile/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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`].
@@ -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;
Loading