about summary refs log tree commit diff
diff options
context:
space:
mode:
author许杰友 Jieyou Xu (Joe) <jieyouxu@outlook.com>2024-06-10 10:09:48 +0000
committer许杰友 Jieyou Xu (Joe) <jieyouxu@outlook.com>2024-06-10 10:46:36 +0000
commit256387b63e33edf3517157fa737ee4369d9d3e3f (patch)
tree641d77a30f7ec55105fa32f497485c1956550bf4
parent06194cadcd0948a7100b46737f61e82b8c00d632 (diff)
downloadrust-256387b63e33edf3517157fa737ee4369d9d3e3f.tar.gz
rust-256387b63e33edf3517157fa737ee4369d9d3e3f.zip
run-make: add run_in_tmpdir self-test
-rw-r--r--tests/run-make/run-in-tmpdir-self-test/rmake.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/run-make/run-in-tmpdir-self-test/rmake.rs b/tests/run-make/run-in-tmpdir-self-test/rmake.rs
new file mode 100644
index 00000000000..83b99bfe863
--- /dev/null
+++ b/tests/run-make/run-in-tmpdir-self-test/rmake.rs
@@ -0,0 +1,38 @@
+//! This is a self-test for the `run_in_tmpdir` helper in the support library. This test tries to
+//! check that files and directories created within the temporary directory gets properly cleared
+//! when returning from the closure.
+
+use std::fs;
+use std::path::{Path, PathBuf};
+
+use run_make_support::{cwd, run_in_tmpdir};
+
+fn main() {
+    let mut file_path = PathBuf::new();
+    let mut dir_path = PathBuf::new();
+    let mut readonly_file_path = PathBuf::new();
+    let test_cwd = cwd();
+    run_in_tmpdir(|| {
+        assert_ne!(test_cwd, cwd(), "test cwd should not be the same as tmpdir cwd");
+
+        file_path = cwd().join("foo.txt");
+        fs::write(&file_path, "hi").unwrap();
+
+        dir_path = cwd().join("bar");
+        fs::create_dir_all(&dir_path).unwrap();
+
+        readonly_file_path = cwd().join("readonly-file.txt");
+        fs::write(&readonly_file_path, "owo").unwrap();
+        let mut perms = fs::metadata(&readonly_file_path).unwrap().permissions();
+        perms.set_readonly(true);
+        fs::set_permissions(&mut readonly_file_path, perms).unwrap();
+
+        assert!(file_path.exists());
+        assert!(dir_path.exists());
+        assert!(readonly_file_path.exists());
+    });
+    assert!(!file_path.exists());
+    assert!(!dir_path.exists());
+    assert!(!readonly_file_path.exists());
+    assert_eq!(test_cwd, cwd(), "test cwd is not correctly restored");
+}