From e40a3e0621025ca2547b935c087c7b52a8d9761b Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Wed, 31 Jan 2024 17:29:18 +0100 Subject: [PATCH] pvapconfig/tests: use a temporary directory for tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the `TemporaryDirectory` type to create a temporary directory. This type has the advantage that the directory is automatically removed once it goes out of scope. The old implementation in test_sysfs_write_i32 leaks a directory if a previous subtest failed. Also, using a temporary directory for the LockFile test fixes the following error: $ RUST_BACKTRACE=1 cargo test -- helper::tests::test_lockfile ... running 1 test test helper::tests::test_lockfile ... FAILED failures: ---- helper::tests::test_lockfile stdout ---- thread 'helper::tests::test_lockfile' panicked at pvapconfig/src/helper.rs:265:9: assertion failed: r1.is_ok() stack backtrace: 0: rust_begin_unwind at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:597:5 1: core::panicking::panic_fmt at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/panicking.rs:72:14 2: core::panicking::panic at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/panicking.rs:127:5 3: pvapconfig::helper::tests::test_lockfile at ./src/helper.rs:265:9 4: pvapconfig::helper::tests::test_lockfile::{{closure}} at ./src/helper.rs:263:24 5: core::ops::function::FnOnce::call_once at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/ops/function.rs:250:5 6: core::ops::function::FnOnce::call_once at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/ops/function.rs:250:5 note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. Reviewed-by: Steffen Eiden Reviewed-By: Harald Freudenberger Signed-off-by: Marc Hartmayer Signed-off-by: Jan Höppner --- rust/pvapconfig/src/helper.rs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/rust/pvapconfig/src/helper.rs b/rust/pvapconfig/src/helper.rs index a2c0f17d..ae1b1b23 100644 --- a/rust/pvapconfig/src/helper.rs +++ b/rust/pvapconfig/src/helper.rs @@ -198,8 +198,8 @@ impl Drop for LockFile { #[cfg(test)] mod tests { - use super::*; + use pv_core::misc::TemporaryDirectory; // Only very simple tests @@ -245,28 +245,35 @@ mod tests { } #[test] fn test_sysfs_write_i32() { - const TEST_PATH: &str = "/tmp/test_sysfs_write_i32"; - let mut file = File::create(TEST_PATH).unwrap(); + let temp_dir = + TemporaryDirectory::new("pvtests").expect("creating a temporary directory should work"); + let test_path = temp_dir.path().join("test"); + let test_path = test_path.as_os_str().to_str().expect("should work"); + let mut file = File::create(test_path).unwrap(); let _ = file.write_all(b"XYZ"); drop(file); - let r = sysfs_read_i32(TEST_PATH); + let r = sysfs_read_i32(test_path); assert!(r.is_err()); - let r = sysfs_write_i32(TEST_PATH, 999); + let r = sysfs_write_i32(test_path, 999); assert!(r.is_ok()); - let r = sysfs_read_i32(TEST_PATH); + let r = sysfs_read_i32(test_path); assert!(r.is_ok()); let v = r.unwrap(); assert!(v == 999); - let _ = fs::remove_file(TEST_PATH); } #[test] fn test_lockfile() { - let r1 = LockFile::try_lock(PATH_PVAPCONFIG_LOCK); + let temp_dir = + TemporaryDirectory::new("pvtests").expect("creating a temporary directory should work"); + let file_path = temp_dir.path().join("my.lock"); + let file_path_str = file_path.to_str().expect("should work"); + + let r1 = LockFile::try_lock(file_path_str); assert!(r1.is_ok()); - let r2 = LockFile::try_lock(PATH_PVAPCONFIG_LOCK); + let r2 = LockFile::try_lock(file_path_str); assert!(r2.is_err()); drop(r1); - let r3 = LockFile::try_lock(PATH_PVAPCONFIG_LOCK); + let r3 = LockFile::try_lock(file_path_str); assert!(r3.is_ok()); } }