Skip to content

Commit 62bc08a

Browse files
committed
Auto merge of #1180 - RalfJung:fs-refact, r=RalfJung
Slight refactoring of FS test
2 parents 12170f1 + 3cd13cb commit 62bc08a

File tree

1 file changed

+31
-53
lines changed

1 file changed

+31
-53
lines changed

tests/run-pass/fs.rs

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,26 @@ 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+
/// Prepare like above, and also write some initial content to the file.
28+
fn prepare_with_content(filename: &str, content: &[u8]) -> PathBuf {
29+
let path = prepare(filename);
30+
let mut file = File::create(&path).unwrap();
31+
file.write(content).unwrap();
32+
path
33+
}
34+
35+
fn test_file() {
36+
let bytes = b"Hello, World!\n";
37+
let path = prepare("miri_test_fs_file.txt");
2538

2639
// Test creating, writing and closing a file (closing is tested when `file` is dropped).
2740
let mut file = File::create(&path).unwrap();
@@ -45,15 +58,8 @@ fn test_file() {
4558
}
4659

4760
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);
5161
let bytes = b"Hello, World!\n";
52-
// Clean the paths for robustness.
53-
remove_file(&path).ok();
54-
55-
let mut file = File::create(&path).unwrap();
56-
file.write(bytes).unwrap();
62+
let path = prepare_with_content("miri_test_fs_file_clone.txt", bytes);
5763

5864
// Cloning a file should be successful.
5965
let file = File::open(&path).unwrap();
@@ -68,19 +74,13 @@ fn test_file_clone() {
6874
}
6975

7076
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);
74-
let bytes = b"Hello, World!\n";
75-
// Clean the paths for robustness.
76-
remove_file(&path).ok();
77-
78-
let mut file = File::create(&path).unwrap();
79-
file.write(bytes).unwrap();
77+
let bytes = b"Hello, entire World!\n";
78+
let path = prepare_with_content("miri_test_fs_seek.txt", bytes);
8079

8180
let mut file = File::open(&path).unwrap();
8281
let mut contents = Vec::new();
8382
file.read_to_end(&mut contents).unwrap();
83+
assert_eq!(bytes, contents.as_slice());
8484
// Test that seeking to the beginning and reading until EOF gets the text again.
8585
file.seek(SeekFrom::Start(0)).unwrap();
8686
let mut contents = Vec::new();
@@ -113,38 +113,23 @@ fn check_metadata(bytes: &[u8], path: &Path) -> Result<()> {
113113
}
114114

115115
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);
119-
let bytes = b"Hello, World!\n";
120-
// Clean the paths for robustness.
121-
remove_file(&path).ok();
122-
123-
let mut file = File::create(&path).unwrap();
124-
file.write(bytes).unwrap();
116+
let bytes = b"Hello, meta-World!\n";
117+
let path = prepare_with_content("miri_test_fs_metadata.txt", bytes);
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");
141130
let bytes = b"Hello, World!\n";
142-
// Clean the paths for robustness.
143-
remove_file(&path).ok();
144-
remove_file(&symlink_path).ok();
145-
146-
let mut file = File::create(&path).unwrap();
147-
file.write(bytes).unwrap();
131+
let path = prepare_with_content("miri_test_fs_link_target.txt", bytes);
132+
let symlink_path = prepare("miri_test_fs_symlink.txt");
148133

149134
// Creating a symbolic link should succeed.
150135
std::os::unix::fs::symlink(&path, &symlink_path).unwrap();
@@ -165,12 +150,8 @@ fn test_symlink() {
165150
}
166151

167152
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);
171153
let bytes = b"Hello, World!\n";
172-
// Clean the paths for robustness.
173-
remove_file(&path).ok();
154+
let path = prepare("miri_test_fs_errors.txt");
174155

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

184165
fn test_rename() {
185-
let tmp = std::env::temp_dir();
186166
// 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();
167+
let path1 = prepare("miri_test_fs_rename_source.txt");
168+
let path2 = prepare("miri_test_fs_rename_destination.txt");
169+
192170
let file = File::create(&path1).unwrap();
193171
drop(file);
194172
rename(&path1, &path2).unwrap();

0 commit comments

Comments
 (0)