Skip to content

Commit 8b31763

Browse files
committed
fs test: factor some common code
1 parent 12170f1 commit 8b31763

File tree

1 file changed

+20
-39
lines changed

1 file changed

+20
-39
lines changed

tests/run-pass/fs.rs

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ fn main() {
1515
test_rename();
1616
}
1717

18-
fn test_file() {
18+
/// Prepare: compute filename and make sure the file does not exist.
19+
fn prepare(filename: &str) -> PathBuf {
1920
let tmp = std::env::temp_dir();
20-
let filename = PathBuf::from("miri_test_fs_file.txt");
21-
let path = tmp.join(&filename);
22-
let bytes = b"Hello, World!\n";
21+
let path = tmp.join(filename);
2322
// Clean the paths for robustness.
2423
remove_file(&path).ok();
24+
path
25+
}
26+
27+
fn test_file() {
28+
let path = prepare("miri_test_fs_file.txt");
29+
let bytes = b"Hello, World!\n";
2530

2631
// Test creating, writing and closing a file (closing is tested when `file` is dropped).
2732
let mut file = File::create(&path).unwrap();
@@ -45,12 +50,8 @@ fn test_file() {
4550
}
4651

4752
fn test_file_clone() {
48-
let tmp = std::env::temp_dir();
49-
let filename = PathBuf::from("miri_test_fs_file_clone.txt");
50-
let path = tmp.join(&filename);
53+
let path = prepare("miri_test_fs_file_clone.txt");
5154
let bytes = b"Hello, World!\n";
52-
// Clean the paths for robustness.
53-
remove_file(&path).ok();
5455

5556
let mut file = File::create(&path).unwrap();
5657
file.write(bytes).unwrap();
@@ -68,12 +69,8 @@ fn test_file_clone() {
6869
}
6970

7071
fn test_seek() {
71-
let tmp = std::env::temp_dir();
72-
let filename = PathBuf::from("miri_test_fs_seek.txt");
73-
let path = tmp.join(&filename);
72+
let path = prepare("miri_test_fs_seek.txt");
7473
let bytes = b"Hello, World!\n";
75-
// Clean the paths for robustness.
76-
remove_file(&path).ok();
7774

7875
let mut file = File::create(&path).unwrap();
7976
file.write(bytes).unwrap();
@@ -113,35 +110,26 @@ fn check_metadata(bytes: &[u8], path: &Path) -> Result<()> {
113110
}
114111

115112
fn test_metadata() {
116-
let tmp = std::env::temp_dir();
117-
let filename = PathBuf::from("miri_test_fs_metadata.txt");
118-
let path = tmp.join(&filename);
113+
let path = prepare("miri_test_fs_metadata.txt");
119114
let bytes = b"Hello, World!\n";
120-
// Clean the paths for robustness.
121-
remove_file(&path).ok();
122115

123116
let mut file = File::create(&path).unwrap();
124117
file.write(bytes).unwrap();
125118

126119
// Test that metadata of an absolute path is correct.
127120
check_metadata(bytes, &path).unwrap();
128121
// Test that metadata of a relative path is correct.
129-
std::env::set_current_dir(&tmp).unwrap();
130-
check_metadata(bytes, &filename).unwrap();
122+
std::env::set_current_dir(path.parent().unwrap()).unwrap();
123+
check_metadata(bytes, Path::new(path.file_name().unwrap())).unwrap();
131124

132125
// Removing file should succeed.
133126
remove_file(&path).unwrap();
134127
}
135128

136129
fn test_symlink() {
137-
let tmp = std::env::temp_dir();
138-
let filename = PathBuf::from("miri_test_fs_link_target.txt");
139-
let path = tmp.join(&filename);
140-
let symlink_path = tmp.join("miri_test_fs_symlink.txt");
130+
let path = prepare("miri_test_fs_link_target.txt");
131+
let symlink_path = prepare("miri_test_fs_symlink.txt");
141132
let bytes = b"Hello, World!\n";
142-
// Clean the paths for robustness.
143-
remove_file(&path).ok();
144-
remove_file(&symlink_path).ok();
145133

146134
let mut file = File::create(&path).unwrap();
147135
file.write(bytes).unwrap();
@@ -165,12 +153,8 @@ fn test_symlink() {
165153
}
166154

167155
fn test_errors() {
168-
let tmp = std::env::temp_dir();
169-
let filename = PathBuf::from("miri_test_fs_errors.txt");
170-
let path = tmp.join(&filename);
156+
let path = prepare("miri_test_fs_errors.txt");
171157
let bytes = b"Hello, World!\n";
172-
// Clean the paths for robustness.
173-
remove_file(&path).ok();
174158

175159
// The following tests also check that the `__errno_location()` shim is working properly.
176160
// Opening a non-existing file should fail with a "not found" error.
@@ -182,13 +166,10 @@ fn test_errors() {
182166
}
183167

184168
fn test_rename() {
185-
let tmp = std::env::temp_dir();
186169
// Renaming a file should succeed.
187-
let path1 = tmp.join("miri_test_fs_rename_source.txt");
188-
let path2 = tmp.join("miri_test_fs_rename_destination.txt");
189-
// Clean files for robustness.
190-
remove_file(&path1).ok();
191-
remove_file(&path2).ok();
170+
let path1 = prepare("miri_test_fs_rename_source.txt");
171+
let path2 = prepare("miri_test_fs_rename_destination.txt");
172+
192173
let file = File::create(&path1).unwrap();
193174
drop(file);
194175
rename(&path1, &path2).unwrap();

0 commit comments

Comments
 (0)