Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run-make: add run_in_tmpdir self-test #126223

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions tests/run-make/run-in-tmpdir-self-test/rmake.rs
Original file line number Diff line number Diff line change
@@ -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());
jieyouxu marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(test_cwd, cwd(), "test cwd is not correctly restored");
}
Loading