|
| 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