From 317e38f6d266755e31a0411501814de832f09bbe Mon Sep 17 00:00:00 2001 From: Jesse Rosenstock Date: Sat, 18 Apr 2026 16:44:31 +0200 Subject: [PATCH] tests: lsmem: Use tempfile::TempDir for test sysroot Refactor `TestSysMemory` in `test_lsmem.rs` to use the `tempfile` crate for managing the temporary sysroot directory. --- tests/by-util/test_lsmem.rs | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/tests/by-util/test_lsmem.rs b/tests/by-util/test_lsmem.rs index 02a5f9b6..5aefa65d 100644 --- a/tests/by-util/test_lsmem.rs +++ b/tests/by-util/test_lsmem.rs @@ -21,7 +21,7 @@ const MEMORY_BLOCK_IDS: [usize; 125] = [ ]; struct TestSysMemory { - sysroot: String, + sysroot: tempfile::TempDir, } /// Builds up a fake /sys/devices/system/memory filesystem. @@ -35,11 +35,9 @@ struct TestSysMemory { /// And removes it automatically after the reference is dropped. impl TestSysMemory { fn new() -> Self { - let random = rand::random::(); - let sysroot = Path::new(&env!("CARGO_MANIFEST_DIR")) - .join("target") - .join(format!("testsysmem-{random}")); + let sysroot = tempfile::tempdir().unwrap(); let sysmem = sysroot + .path() .join("sys") .join("devices") .join("system") @@ -60,21 +58,13 @@ impl TestSysMemory { write_file_content(&node_dir, ".gitkeep", ""); } - TestSysMemory { - sysroot: sysroot.display().to_string(), - } - } -} - -impl Drop for TestSysMemory { - fn drop(&mut self) { - std::fs::remove_dir_all(&self.sysroot).unwrap(); + TestSysMemory { sysroot } } } fn sysroot_test_with_args(test_root: &TestSysMemory, expected_output: &str, args: &[&str]) { let mut cmd = new_ucmd!(); - cmd.arg("-s").arg(&test_root.sysroot); + cmd.arg("-s").arg(test_root.sysroot.path()); for arg in args { cmd.arg(arg); }