From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <pdm-devel-bounces@lists.proxmox.com> Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 4EF7F1FF15E for <inbox@lore.proxmox.com>; Tue, 11 Feb 2025 13:05:57 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 1B1DB2A5D5; Tue, 11 Feb 2025 13:05:52 +0100 (CET) From: Lukas Wagner <l.wagner@proxmox.com> To: pdm-devel@lists.proxmox.com Date: Tue, 11 Feb 2025 13:05:17 +0100 Message-Id: <20250211120541.163621-2-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250211120541.163621-1-l.wagner@proxmox.com> References: <20250211120541.163621-1-l.wagner@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.010 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 Subject: [pdm-devel] [PATCH proxmox-datacenter-manager 01/25] 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 <pdm-devel.lists.proxmox.com> List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pdm-devel>, <mailto:pdm-devel-request@lists.proxmox.com?subject=unsubscribe> List-Archive: <http://lists.proxmox.com/pipermail/pdm-devel/> List-Post: <mailto:pdm-devel@lists.proxmox.com> List-Help: <mailto:pdm-devel-request@lists.proxmox.com?subject=help> List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel>, <mailto:pdm-devel-request@lists.proxmox.com?subject=subscribe> Reply-To: Proxmox Datacenter Manager development discussion <pdm-devel@lists.proxmox.com> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pdm-devel-bounces@lists.proxmox.com Sender: "pdm-devel" <pdm-devel-bounces@lists.proxmox.com> 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 <l.wagner@proxmox.com> --- Notes: Cherry-picked from https://lore.proxmox.com/pdm-devel/20250128122520.167796-3-l.wagner@proxmox.com/T/#u 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<Self, Error> { + 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