Skip to content

Commit f79c453

Browse files
committed
factor more common code
1 parent 8b31763 commit f79c453

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

tests/run-pass/fs.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@ fn prepare(filename: &str) -> PathBuf {
2424
path
2525
}
2626

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+
2735
fn test_file() {
28-
let path = prepare("miri_test_fs_file.txt");
2936
let bytes = b"Hello, World!\n";
37+
let path = prepare("miri_test_fs_file.txt");
3038

3139
// Test creating, writing and closing a file (closing is tested when `file` is dropped).
3240
let mut file = File::create(&path).unwrap();
@@ -50,11 +58,8 @@ fn test_file() {
5058
}
5159

5260
fn test_file_clone() {
53-
let path = prepare("miri_test_fs_file_clone.txt");
5461
let bytes = b"Hello, World!\n";
55-
56-
let mut file = File::create(&path).unwrap();
57-
file.write(bytes).unwrap();
62+
let path = prepare_with_content("miri_test_fs_file_clone.txt", bytes);
5863

5964
// Cloning a file should be successful.
6065
let file = File::open(&path).unwrap();
@@ -69,11 +74,8 @@ fn test_file_clone() {
6974
}
7075

7176
fn test_seek() {
72-
let path = prepare("miri_test_fs_seek.txt");
73-
let bytes = b"Hello, World!\n";
74-
75-
let mut file = File::create(&path).unwrap();
76-
file.write(bytes).unwrap();
77+
let bytes = b"Hello, entire World!\n";
78+
let path = prepare_with_content("miri_test_fs_seek.txt", bytes);
7779

7880
let mut file = File::open(&path).unwrap();
7981
let mut contents = Vec::new();
@@ -110,11 +112,8 @@ fn check_metadata(bytes: &[u8], path: &Path) -> Result<()> {
110112
}
111113

112114
fn test_metadata() {
113-
let path = prepare("miri_test_fs_metadata.txt");
114-
let bytes = b"Hello, World!\n";
115-
116-
let mut file = File::create(&path).unwrap();
117-
file.write(bytes).unwrap();
115+
let bytes = b"Hello, meta-World!\n";
116+
let path = prepare_with_content("miri_test_fs_metadata.txt", bytes);
118117

119118
// Test that metadata of an absolute path is correct.
120119
check_metadata(bytes, &path).unwrap();
@@ -127,12 +126,9 @@ fn test_metadata() {
127126
}
128127

129128
fn test_symlink() {
130-
let path = prepare("miri_test_fs_link_target.txt");
131-
let symlink_path = prepare("miri_test_fs_symlink.txt");
132129
let bytes = b"Hello, World!\n";
133-
134-
let mut file = File::create(&path).unwrap();
135-
file.write(bytes).unwrap();
130+
let path = prepare_with_content("miri_test_fs_link_target.txt", bytes);
131+
let symlink_path = prepare("miri_test_fs_symlink.txt");
136132

137133
// Creating a symbolic link should succeed.
138134
std::os::unix::fs::symlink(&path, &symlink_path).unwrap();
@@ -153,8 +149,8 @@ fn test_symlink() {
153149
}
154150

155151
fn test_errors() {
156-
let path = prepare("miri_test_fs_errors.txt");
157152
let bytes = b"Hello, World!\n";
153+
let path = prepare("miri_test_fs_errors.txt");
158154

159155
// The following tests also check that the `__errno_location()` shim is working properly.
160156
// Opening a non-existing file should fail with a "not found" error.

0 commit comments

Comments
 (0)