@@ -15,13 +15,26 @@ 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
+ /// 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" ) ;
25
38
26
39
// Test creating, writing and closing a file (closing is tested when `file` is dropped).
27
40
let mut file = File :: create ( & path) . unwrap ( ) ;
@@ -45,15 +58,8 @@ fn test_file() {
45
58
}
46
59
47
60
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) ;
51
61
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) ;
57
63
58
64
// Cloning a file should be successful.
59
65
let file = File :: open ( & path) . unwrap ( ) ;
@@ -68,19 +74,13 @@ fn test_file_clone() {
68
74
}
69
75
70
76
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) ;
80
79
81
80
let mut file = File :: open ( & path) . unwrap ( ) ;
82
81
let mut contents = Vec :: new ( ) ;
83
82
file. read_to_end ( & mut contents) . unwrap ( ) ;
83
+ assert_eq ! ( bytes, contents. as_slice( ) ) ;
84
84
// Test that seeking to the beginning and reading until EOF gets the text again.
85
85
file. seek ( SeekFrom :: Start ( 0 ) ) . unwrap ( ) ;
86
86
let mut contents = Vec :: new ( ) ;
@@ -113,38 +113,23 @@ fn check_metadata(bytes: &[u8], path: &Path) -> Result<()> {
113
113
}
114
114
115
115
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) ;
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" ) ;
141
130
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" ) ;
148
133
149
134
// Creating a symbolic link should succeed.
150
135
std:: os:: unix:: fs:: symlink ( & path, & symlink_path) . unwrap ( ) ;
@@ -165,12 +150,8 @@ fn test_symlink() {
165
150
}
166
151
167
152
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) ;
171
153
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" ) ;
174
155
175
156
// The following tests also check that the `__errno_location()` shim is working properly.
176
157
// Opening a non-existing file should fail with a "not found" error.
@@ -182,13 +163,10 @@ fn test_errors() {
182
163
}
183
164
184
165
fn test_rename ( ) {
185
- let tmp = std:: env:: temp_dir ( ) ;
186
166
// 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
+
192
170
let file = File :: create ( & path1) . unwrap ( ) ;
193
171
drop ( file) ;
194
172
rename ( & path1, & path2) . unwrap ( ) ;
0 commit comments