test_rename();
}
-fn test_file() {
+/// Prepare: compute filename and make sure the file does not exist.
+fn prepare(filename: &str) -> PathBuf {
let tmp = std::env::temp_dir();
- let filename = PathBuf::from("miri_test_fs_file.txt");
- let path = tmp.join(&filename);
- let bytes = b"Hello, World!\n";
+ let path = tmp.join(filename);
// Clean the paths for robustness.
remove_file(&path).ok();
+ path
+}
+
+fn test_file() {
+ let path = prepare("miri_test_fs_file.txt");
+ let bytes = b"Hello, World!\n";
// Test creating, writing and closing a file (closing is tested when `file` is dropped).
let mut file = File::create(&path).unwrap();
}
fn test_file_clone() {
- let tmp = std::env::temp_dir();
- let filename = PathBuf::from("miri_test_fs_file_clone.txt");
- let path = tmp.join(&filename);
+ let path = prepare("miri_test_fs_file_clone.txt");
let bytes = b"Hello, World!\n";
- // Clean the paths for robustness.
- remove_file(&path).ok();
let mut file = File::create(&path).unwrap();
file.write(bytes).unwrap();
}
fn test_seek() {
- let tmp = std::env::temp_dir();
- let filename = PathBuf::from("miri_test_fs_seek.txt");
- let path = tmp.join(&filename);
+ let path = prepare("miri_test_fs_seek.txt");
let bytes = b"Hello, World!\n";
- // Clean the paths for robustness.
- remove_file(&path).ok();
let mut file = File::create(&path).unwrap();
file.write(bytes).unwrap();
}
fn test_metadata() {
- let tmp = std::env::temp_dir();
- let filename = PathBuf::from("miri_test_fs_metadata.txt");
- let path = tmp.join(&filename);
+ let path = prepare("miri_test_fs_metadata.txt");
let bytes = b"Hello, World!\n";
- // Clean the paths for robustness.
- remove_file(&path).ok();
let mut file = File::create(&path).unwrap();
file.write(bytes).unwrap();
// Test that metadata of an absolute path is correct.
check_metadata(bytes, &path).unwrap();
// Test that metadata of a relative path is correct.
- std::env::set_current_dir(&tmp).unwrap();
- check_metadata(bytes, &filename).unwrap();
+ std::env::set_current_dir(path.parent().unwrap()).unwrap();
+ check_metadata(bytes, Path::new(path.file_name().unwrap())).unwrap();
// Removing file should succeed.
remove_file(&path).unwrap();
}
fn test_symlink() {
- let tmp = std::env::temp_dir();
- let filename = PathBuf::from("miri_test_fs_link_target.txt");
- let path = tmp.join(&filename);
- let symlink_path = tmp.join("miri_test_fs_symlink.txt");
+ let path = prepare("miri_test_fs_link_target.txt");
+ let symlink_path = prepare("miri_test_fs_symlink.txt");
let bytes = b"Hello, World!\n";
- // Clean the paths for robustness.
- remove_file(&path).ok();
- remove_file(&symlink_path).ok();
let mut file = File::create(&path).unwrap();
file.write(bytes).unwrap();
}
fn test_errors() {
- let tmp = std::env::temp_dir();
- let filename = PathBuf::from("miri_test_fs_errors.txt");
- let path = tmp.join(&filename);
+ let path = prepare("miri_test_fs_errors.txt");
let bytes = b"Hello, World!\n";
- // Clean the paths for robustness.
- remove_file(&path).ok();
// The following tests also check that the `__errno_location()` shim is working properly.
// Opening a non-existing file should fail with a "not found" error.
}
fn test_rename() {
- let tmp = std::env::temp_dir();
// Renaming a file should succeed.
- let path1 = tmp.join("miri_test_fs_rename_source.txt");
- let path2 = tmp.join("miri_test_fs_rename_destination.txt");
- // Clean files for robustness.
- remove_file(&path1).ok();
- remove_file(&path2).ok();
+ let path1 = prepare("miri_test_fs_rename_source.txt");
+ let path2 = prepare("miri_test_fs_rename_destination.txt");
+
let file = File::create(&path1).unwrap();
drop(file);
rename(&path1, &path2).unwrap();