From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 708B21FF15E for ; Tue, 28 Jan 2025 13:26:24 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 8CAAFCD49; Tue, 28 Jan 2025 13:26:23 +0100 (CET) From: Lukas Wagner To: pdm-devel@lists.proxmox.com Date: Tue, 28 Jan 2025 13:25:07 +0100 Message-Id: <20250128122520.167796-3-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250128122520.167796-1-l.wagner@proxmox.com> References: <20250128122520.167796-1-l.wagner@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.015 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pdm-devel] [PATCH proxmox-datacenter-manager 02/15] test support: add NamedTempFile helper X-BeenThere: pdm-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox Datacenter Manager development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pdm-devel-bounces@lists.proxmox.com Sender: "pdm-devel" This one is useful when writing tests, it automatically removes the temporary file when dropped. The name was chosen because of the similar NamedTempFile struct in the popular tempfile crate. Signed-off-by: Lukas Wagner --- server/src/lib.rs | 2 +- server/src/test_support/mod.rs | 4 ++++ server/src/test_support/temp.rs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 server/src/test_support/temp.rs diff --git a/server/src/lib.rs b/server/src/lib.rs index 12dc912..143ee32 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -13,7 +13,7 @@ pub mod task_utils; pub mod connection; pub mod pbs_client; -#[cfg(remote_config = "faked")] +#[cfg(any(remote_config = "faked", test))] pub mod test_support; use anyhow::Error; diff --git a/server/src/test_support/mod.rs b/server/src/test_support/mod.rs index e54cd72..f026011 100644 --- a/server/src/test_support/mod.rs +++ b/server/src/test_support/mod.rs @@ -1 +1,5 @@ +#[cfg(remote_config = "faked")] pub mod fake_remote; + +#[cfg(test)] +pub mod temp; diff --git a/server/src/test_support/temp.rs b/server/src/test_support/temp.rs new file mode 100644 index 0000000..a3a6d59 --- /dev/null +++ b/server/src/test_support/temp.rs @@ -0,0 +1,33 @@ +use std::path::{Path, PathBuf}; + +use anyhow::Error; + +use proxmox_sys::fs::CreateOptions; + +/// Temporary file that be cleaned up when dropped. +pub struct NamedTempFile { + path: PathBuf, +} + +impl NamedTempFile { + /// Create a new temporary file. + /// + /// The file will be created with the passed [`CreateOptions`]. + pub fn new(options: CreateOptions) -> Result { + let base = std::env::temp_dir().join("test"); + let (_, path) = proxmox_sys::fs::make_tmp_file(base, options)?; + + Ok(Self { path }) + } + + /// Return the [`Path`] to the temporary file. + pub fn path(&self) -> &Path { + &self.path + } +} + +impl Drop for NamedTempFile { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.path); + } +} -- 2.39.5 _______________________________________________ pdm-devel mailing list pdm-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel