[pdm-devel] [PATCH proxmox-datacenter-manager 02/25] test support: add NamedTempDir helper

Lukas Wagner l.wagner at proxmox.com
Tue Feb 11 13:05:18 CET 2025


This one is useful when writing tests, it automatically removes the
temporary directory when dropped.

Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 server/src/test_support/temp.rs | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/server/src/test_support/temp.rs b/server/src/test_support/temp.rs
index a3a6d59..a93c914 100644
--- a/server/src/test_support/temp.rs
+++ b/server/src/test_support/temp.rs
@@ -31,3 +31,30 @@ impl Drop for NamedTempFile {
         let _ = std::fs::remove_file(&self.path);
     }
 }
+
+/// Temporary directory that is cleaned up when dropped.
+pub struct NamedTempDir {
+    path: PathBuf,
+}
+
+impl NamedTempDir {
+    /// Create a new temporary directory.
+    ///
+    /// The directory will be created with `0o700` permissions.
+    pub fn new() -> Result<Self, Error> {
+        let path = proxmox_sys::fs::make_tmp_dir("/tmp", None)?;
+
+        Ok(Self { path })
+    }
+
+    /// Return the [`Path`] to the temporary directory.
+    pub fn path(&self) -> &Path {
+        &self.path
+    }
+}
+
+impl Drop for NamedTempDir {
+    fn drop(&mut self) {
+        let _ = std::fs::remove_dir_all(&self.path);
+    }
+}
-- 
2.39.5





More information about the pdm-devel mailing list