Skip to content

Commit 0933314

Browse files
committed
Rewrite file system tests
1 parent 8216f3c commit 0933314

File tree

1 file changed

+109
-25
lines changed

1 file changed

+109
-25
lines changed

tests/run-pass/fs.rs

Lines changed: 109 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,23 @@ use std::fs::{File, remove_file, rename};
55
use std::io::{Read, Write, ErrorKind, Result, Seek, SeekFrom};
66
use std::path::{PathBuf, Path};
77

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();
1616
}
1717

18-
fn main() {
18+
fn test_file() {
1919
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");
2121
let path = tmp.join(&filename);
22-
let symlink_path = tmp.join("miri_test_fs_symlink.txt");
2322
let bytes = b"Hello, World!\n";
2423
// Clean the paths for robustness.
2524
remove_file(&path).ok();
26-
remove_file(&symlink_path).ok();
2725

2826
// Test creating, writing and closing a file (closing is tested when `file` is dropped).
2927
let mut file = File::create(&path).unwrap();
@@ -42,6 +40,21 @@ fn main() {
4240
file.read_to_end(&mut contents).unwrap();
4341
assert_eq!(bytes, contents.as_slice());
4442

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+
4558
// Cloning a file should be successful.
4659
let file = File::open(&path).unwrap();
4760
let mut cloned = file.try_clone().unwrap();
@@ -50,7 +63,24 @@ fn main() {
5063
cloned.read_to_end(&mut contents).unwrap();
5164
assert_eq!(bytes, contents.as_slice());
5265

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+
5381
let mut file = File::open(&path).unwrap();
82+
let mut contents = Vec::new();
83+
file.read_to_end(&mut contents).unwrap();
5484
// Test that seeking to the beginning and reading until EOF gets the text again.
5585
file.seek(SeekFrom::Start(0)).unwrap();
5686
let mut contents = Vec::new();
@@ -68,11 +98,53 @@ fn main() {
6898
file.read_to_end(&mut contents).unwrap();
6999
assert_eq!(&bytes[2..], contents.as_slice());
70100

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+
71126
// Test that metadata of an absolute path is correct.
72-
test_metadata(bytes, &path).unwrap();
127+
check_metadata(bytes, &path).unwrap();
73128
// Test that metadata of a relative path is correct.
74129
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();
76148

77149
// Creating a symbolic link should succeed.
78150
std::os::unix::fs::symlink(&path, &symlink_path).unwrap();
@@ -82,18 +154,38 @@ fn main() {
82154
symlink_file.read_to_end(&mut contents).unwrap();
83155
assert_eq!(bytes, contents.as_slice());
84156
// Test that metadata of a symbolic link is correct.
85-
test_metadata(bytes, &symlink_path).unwrap();
157+
check_metadata(bytes, &symlink_path).unwrap();
86158
// Test that the metadata of a symbolic link is correct when not following it.
87159
assert!(symlink_path.symlink_metadata().unwrap().file_type().is_symlink());
88160
// Removing symbolic link should succeed.
89161
remove_file(&symlink_path).unwrap();
90162

91163
// Removing file should succeed.
92164
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+
}
93183

184+
fn test_rename() {
185+
let tmp = std::env::temp_dir();
94186
// 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");
97189
// Clean files for robustness.
98190
remove_file(&path1).ok();
99191
remove_file(&path2).ok();
@@ -103,12 +195,4 @@ fn main() {
103195
assert_eq!(ErrorKind::NotFound, path1.metadata().unwrap_err().kind());
104196
assert!(path2.metadata().unwrap().is_file());
105197
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());
114198
}

0 commit comments

Comments
 (0)