Skip to content

Commit b9c7f77

Browse files
authored
Unrolled build for rust-lang#126223
Rollup merge of rust-lang#126223 - jieyouxu:rmake-run-in-tmpdir-self-test, r=Kobzol run-make: add `run_in_tmpdir` self-test Add a basic sanity test for `run_in_tmpdir` to make sure that files (including read-only files) and directories created inside the "scratch" tmpdir are removed after the closure returns. r? ghost (while i run a try job) try-job: x86_64-msvc
2 parents d402830 + 256387b commit b9c7f77

File tree

1 file changed

+38
-0
lines changed
  • tests/run-make/run-in-tmpdir-self-test

1 file changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//! This is a self-test for the `run_in_tmpdir` helper in the support library. This test tries to
2+
//! check that files and directories created within the temporary directory gets properly cleared
3+
//! when returning from the closure.
4+
5+
use std::fs;
6+
use std::path::{Path, PathBuf};
7+
8+
use run_make_support::{cwd, run_in_tmpdir};
9+
10+
fn main() {
11+
let mut file_path = PathBuf::new();
12+
let mut dir_path = PathBuf::new();
13+
let mut readonly_file_path = PathBuf::new();
14+
let test_cwd = cwd();
15+
run_in_tmpdir(|| {
16+
assert_ne!(test_cwd, cwd(), "test cwd should not be the same as tmpdir cwd");
17+
18+
file_path = cwd().join("foo.txt");
19+
fs::write(&file_path, "hi").unwrap();
20+
21+
dir_path = cwd().join("bar");
22+
fs::create_dir_all(&dir_path).unwrap();
23+
24+
readonly_file_path = cwd().join("readonly-file.txt");
25+
fs::write(&readonly_file_path, "owo").unwrap();
26+
let mut perms = fs::metadata(&readonly_file_path).unwrap().permissions();
27+
perms.set_readonly(true);
28+
fs::set_permissions(&mut readonly_file_path, perms).unwrap();
29+
30+
assert!(file_path.exists());
31+
assert!(dir_path.exists());
32+
assert!(readonly_file_path.exists());
33+
});
34+
assert!(!file_path.exists());
35+
assert!(!dir_path.exists());
36+
assert!(!readonly_file_path.exists());
37+
assert_eq!(test_cwd, cwd(), "test cwd is not correctly restored");
38+
}

0 commit comments

Comments
 (0)