@@ -15,13 +15,18 @@ fn main() {
15
15
test_rename ( ) ;
16
16
}
17
17
18
- fn test_file ( ) {
18
+ /// Prepare: compute filename and make sure the file does not exist.
19
+ fn prepare ( filename : & str ) -> PathBuf {
19
20
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) ;
23
22
// Clean the paths for robustness.
24
23
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 " ;
25
30
26
31
// Test creating, writing and closing a file (closing is tested when `file` is dropped).
27
32
let mut file = File :: create ( & path) . unwrap ( ) ;
@@ -45,12 +50,8 @@ fn test_file() {
45
50
}
46
51
47
52
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" ) ;
51
54
let bytes = b"Hello, World!\n " ;
52
- // Clean the paths for robustness.
53
- remove_file ( & path) . ok ( ) ;
54
55
55
56
let mut file = File :: create ( & path) . unwrap ( ) ;
56
57
file. write ( bytes) . unwrap ( ) ;
@@ -68,12 +69,8 @@ fn test_file_clone() {
68
69
}
69
70
70
71
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" ) ;
74
73
let bytes = b"Hello, World!\n " ;
75
- // Clean the paths for robustness.
76
- remove_file ( & path) . ok ( ) ;
77
74
78
75
let mut file = File :: create ( & path) . unwrap ( ) ;
79
76
file. write ( bytes) . unwrap ( ) ;
@@ -113,35 +110,26 @@ fn check_metadata(bytes: &[u8], path: &Path) -> Result<()> {
113
110
}
114
111
115
112
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" ) ;
119
114
let bytes = b"Hello, World!\n " ;
120
- // Clean the paths for robustness.
121
- remove_file ( & path) . ok ( ) ;
122
115
123
116
let mut file = File :: create ( & path) . unwrap ( ) ;
124
117
file. write ( bytes) . unwrap ( ) ;
125
118
126
119
// Test that metadata of an absolute path is correct.
127
120
check_metadata ( bytes, & path) . unwrap ( ) ;
128
121
// 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 ( ) ;
131
124
132
125
// Removing file should succeed.
133
126
remove_file ( & path) . unwrap ( ) ;
134
127
}
135
128
136
129
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" ) ;
141
132
let bytes = b"Hello, World!\n " ;
142
- // Clean the paths for robustness.
143
- remove_file ( & path) . ok ( ) ;
144
- remove_file ( & symlink_path) . ok ( ) ;
145
133
146
134
let mut file = File :: create ( & path) . unwrap ( ) ;
147
135
file. write ( bytes) . unwrap ( ) ;
@@ -165,12 +153,8 @@ fn test_symlink() {
165
153
}
166
154
167
155
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" ) ;
171
157
let bytes = b"Hello, World!\n " ;
172
- // Clean the paths for robustness.
173
- remove_file ( & path) . ok ( ) ;
174
158
175
159
// The following tests also check that the `__errno_location()` shim is working properly.
176
160
// Opening a non-existing file should fail with a "not found" error.
@@ -182,13 +166,10 @@ fn test_errors() {
182
166
}
183
167
184
168
fn test_rename ( ) {
185
- let tmp = std:: env:: temp_dir ( ) ;
186
169
// 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
+
192
173
let file = File :: create ( & path1) . unwrap ( ) ;
193
174
drop ( file) ;
194
175
rename ( & path1, & path2) . unwrap ( ) ;
0 commit comments