@@ -5,25 +5,23 @@ use std::fs::{File, remove_file, rename};
5
5
use std:: io:: { Read , Write , ErrorKind , Result , Seek , SeekFrom } ;
6
6
use std:: path:: { PathBuf , Path } ;
7
7
8
- fn test_metadata ( bytes : & [ u8 ] , path : & Path ) -> Result < ( ) > {
9
- // Test that the file metadata is correct.
10
- let metadata = path . metadata ( ) ? ;
11
- // `path` should point to a file.
12
- assert ! ( metadata . is_file ( ) ) ;
13
- // The size of the file must be equal to the number of written bytes.
14
- assert_eq ! ( bytes . len ( ) as u64 , metadata . len ( ) ) ;
15
- Ok ( ( ) )
8
+ fn main ( ) {
9
+ test_file ( ) ;
10
+ test_file_clone ( ) ;
11
+ test_seek ( ) ;
12
+ test_metadata ( ) ;
13
+ test_symlink ( ) ;
14
+ test_errors ( ) ;
15
+ test_rename ( ) ;
16
16
}
17
17
18
- fn main ( ) {
18
+ fn test_file ( ) {
19
19
let tmp = std:: env:: temp_dir ( ) ;
20
- let filename = PathBuf :: from ( "miri_test_fs .txt" ) ;
20
+ let filename = PathBuf :: from ( "miri_test_fs_file .txt" ) ;
21
21
let path = tmp. join ( & filename) ;
22
- let symlink_path = tmp. join ( "miri_test_fs_symlink.txt" ) ;
23
22
let bytes = b"Hello, World!\n " ;
24
23
// Clean the paths for robustness.
25
24
remove_file ( & path) . ok ( ) ;
26
- remove_file ( & symlink_path) . ok ( ) ;
27
25
28
26
// Test creating, writing and closing a file (closing is tested when `file` is dropped).
29
27
let mut file = File :: create ( & path) . unwrap ( ) ;
@@ -42,6 +40,21 @@ fn main() {
42
40
file. read_to_end ( & mut contents) . unwrap ( ) ;
43
41
assert_eq ! ( bytes, contents. as_slice( ) ) ;
44
42
43
+ // Removing file should succeed.
44
+ remove_file ( & path) . unwrap ( ) ;
45
+ }
46
+
47
+ 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
+ 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 ( ) ;
57
+
45
58
// Cloning a file should be successful.
46
59
let file = File :: open ( & path) . unwrap ( ) ;
47
60
let mut cloned = file. try_clone ( ) . unwrap ( ) ;
@@ -50,7 +63,24 @@ fn main() {
50
63
cloned. read_to_end ( & mut contents) . unwrap ( ) ;
51
64
assert_eq ! ( bytes, contents. as_slice( ) ) ;
52
65
66
+ // Removing file should succeed.
67
+ remove_file ( & path) . unwrap ( ) ;
68
+ }
69
+
70
+ 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 ( ) ;
80
+
53
81
let mut file = File :: open ( & path) . unwrap ( ) ;
82
+ let mut contents = Vec :: new ( ) ;
83
+ file. read_to_end ( & mut contents) . unwrap ( ) ;
54
84
// Test that seeking to the beginning and reading until EOF gets the text again.
55
85
file. seek ( SeekFrom :: Start ( 0 ) ) . unwrap ( ) ;
56
86
let mut contents = Vec :: new ( ) ;
@@ -68,11 +98,53 @@ fn main() {
68
98
file. read_to_end ( & mut contents) . unwrap ( ) ;
69
99
assert_eq ! ( & bytes[ 2 ..] , contents. as_slice( ) ) ;
70
100
101
+ // Removing file should succeed.
102
+ remove_file ( & path) . unwrap ( ) ;
103
+ }
104
+
105
+ fn check_metadata ( bytes : & [ u8 ] , path : & Path ) -> Result < ( ) > {
106
+ // Test that the file metadata is correct.
107
+ let metadata = path. metadata ( ) ?;
108
+ // `path` should point to a file.
109
+ assert ! ( metadata. is_file( ) ) ;
110
+ // The size of the file must be equal to the number of written bytes.
111
+ assert_eq ! ( bytes. len( ) as u64 , metadata. len( ) ) ;
112
+ Ok ( ( ) )
113
+ }
114
+
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 ( ) ;
125
+
71
126
// Test that metadata of an absolute path is correct.
72
- test_metadata ( bytes, & path) . unwrap ( ) ;
127
+ check_metadata ( bytes, & path) . unwrap ( ) ;
73
128
// Test that metadata of a relative path is correct.
74
129
std:: env:: set_current_dir ( & tmp) . unwrap ( ) ;
75
- test_metadata ( bytes, & filename) . unwrap ( ) ;
130
+ check_metadata ( bytes, & filename) . unwrap ( ) ;
131
+
132
+ // Removing file should succeed.
133
+ remove_file ( & path) . unwrap ( ) ;
134
+ }
135
+
136
+ 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
+ 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 ( ) ;
76
148
77
149
// Creating a symbolic link should succeed.
78
150
std:: os:: unix:: fs:: symlink ( & path, & symlink_path) . unwrap ( ) ;
@@ -82,18 +154,38 @@ fn main() {
82
154
symlink_file. read_to_end ( & mut contents) . unwrap ( ) ;
83
155
assert_eq ! ( bytes, contents. as_slice( ) ) ;
84
156
// Test that metadata of a symbolic link is correct.
85
- test_metadata ( bytes, & symlink_path) . unwrap ( ) ;
157
+ check_metadata ( bytes, & symlink_path) . unwrap ( ) ;
86
158
// Test that the metadata of a symbolic link is correct when not following it.
87
159
assert ! ( symlink_path. symlink_metadata( ) . unwrap( ) . file_type( ) . is_symlink( ) ) ;
88
160
// Removing symbolic link should succeed.
89
161
remove_file ( & symlink_path) . unwrap ( ) ;
90
162
91
163
// Removing file should succeed.
92
164
remove_file ( & path) . unwrap ( ) ;
165
+ }
166
+
167
+ 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
+ let bytes = b"Hello, World!\n " ;
172
+ // Clean the paths for robustness.
173
+ remove_file ( & path) . ok ( ) ;
174
+
175
+ // The following tests also check that the `__errno_location()` shim is working properly.
176
+ // Opening a non-existing file should fail with a "not found" error.
177
+ assert_eq ! ( ErrorKind :: NotFound , File :: open( & path) . unwrap_err( ) . kind( ) ) ;
178
+ // Removing a non-existing file should fail with a "not found" error.
179
+ assert_eq ! ( ErrorKind :: NotFound , remove_file( & path) . unwrap_err( ) . kind( ) ) ;
180
+ // Reading the metadata of a non-existing file should fail with a "not found" error.
181
+ assert_eq ! ( ErrorKind :: NotFound , check_metadata( bytes, & path) . unwrap_err( ) . kind( ) ) ;
182
+ }
93
183
184
+ fn test_rename ( ) {
185
+ let tmp = std:: env:: temp_dir ( ) ;
94
186
// Renaming a file should succeed.
95
- let path1 = tmp. join ( "rename_source .txt" ) ;
96
- let path2 = tmp. join ( "rename_destination .txt" ) ;
187
+ let path1 = tmp. join ( "miri_test_fs_rename_source .txt" ) ;
188
+ let path2 = tmp. join ( "miri_test_fs_rename_destination .txt" ) ;
97
189
// Clean files for robustness.
98
190
remove_file ( & path1) . ok ( ) ;
99
191
remove_file ( & path2) . ok ( ) ;
@@ -103,12 +195,4 @@ fn main() {
103
195
assert_eq ! ( ErrorKind :: NotFound , path1. metadata( ) . unwrap_err( ) . kind( ) ) ;
104
196
assert ! ( path2. metadata( ) . unwrap( ) . is_file( ) ) ;
105
197
remove_file ( & path2) . unwrap ( ) ;
106
-
107
- // The two following tests also check that the `__errno_location()` shim is working properly.
108
- // Opening a non-existing file should fail with a "not found" error.
109
- assert_eq ! ( ErrorKind :: NotFound , File :: open( & path) . unwrap_err( ) . kind( ) ) ;
110
- // Removing a non-existing file should fail with a "not found" error.
111
- assert_eq ! ( ErrorKind :: NotFound , remove_file( & path) . unwrap_err( ) . kind( ) ) ;
112
- // Reading the metadata of a non-existing file should fail with a "not found" error.
113
- assert_eq ! ( ErrorKind :: NotFound , test_metadata( bytes, & path) . unwrap_err( ) . kind( ) ) ;
114
198
}
0 commit comments