1 // ignore-windows: File handling is not implemented yet
2 // compile-flags: -Zmiri-disable-isolation
4 use std::fs::{File, remove_file};
5 use std::io::{Read, Write, ErrorKind};
8 let path = std::env::temp_dir().join("miri_test_fs.txt");
9 let bytes = b"Hello, World!\n";
10 // Test creating, writing and closing a file (closing is tested when `file` is dropped).
11 let mut file = File::create(&path).unwrap();
12 // Writing 0 bytes should not change the file contents.
13 file.write(&mut []).unwrap();
15 file.write(bytes).unwrap();
16 // Test opening, reading and closing a file.
17 let mut file = File::open(&path).unwrap();
18 let mut contents = Vec::new();
19 // Reading 0 bytes should not move the file pointer.
20 file.read(&mut []).unwrap();
21 // Reading until EOF should get the whole text.
22 file.read_to_end(&mut contents).unwrap();
23 assert_eq!(bytes, contents.as_slice());
24 // Removing file should succeed
25 remove_file(&path).unwrap();
27 // The two following tests also check that the `__errno_location()` shim is working properly.
28 // Opening a non-existing file should fail with a "not found" error.
29 assert_eq!(ErrorKind::NotFound, File::open(&path).unwrap_err().kind());
30 // Removing a non-existing file should fail with a "not found" error.
31 assert_eq!(ErrorKind::NotFound, remove_file(&path).unwrap_err().kind());