From 8b3176381690d01eb1cb50926ebb784bdac3ab9f Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 19 Feb 2020 11:04:59 +0100 Subject: [PATCH 1/3] fs test: factor some common code --- tests/run-pass/fs.rs | 59 +++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 39 deletions(-) diff --git a/tests/run-pass/fs.rs b/tests/run-pass/fs.rs index 3851a4d45e92..a69f9f3b23d8 100644 --- a/tests/run-pass/fs.rs +++ b/tests/run-pass/fs.rs @@ -15,13 +15,18 @@ fn main() { test_rename(); } -fn test_file() { +/// Prepare: compute filename and make sure the file does not exist. +fn prepare(filename: &str) -> PathBuf { let tmp = std::env::temp_dir(); - let filename = PathBuf::from("miri_test_fs_file.txt"); - let path = tmp.join(&filename); - let bytes = b"Hello, World!\n"; + let path = tmp.join(filename); // Clean the paths for robustness. remove_file(&path).ok(); + path +} + +fn test_file() { + let path = prepare("miri_test_fs_file.txt"); + let bytes = b"Hello, World!\n"; // Test creating, writing and closing a file (closing is tested when `file` is dropped). let mut file = File::create(&path).unwrap(); @@ -45,12 +50,8 @@ fn test_file() { } fn test_file_clone() { - let tmp = std::env::temp_dir(); - let filename = PathBuf::from("miri_test_fs_file_clone.txt"); - let path = tmp.join(&filename); + let path = prepare("miri_test_fs_file_clone.txt"); let bytes = b"Hello, World!\n"; - // Clean the paths for robustness. - remove_file(&path).ok(); let mut file = File::create(&path).unwrap(); file.write(bytes).unwrap(); @@ -68,12 +69,8 @@ fn test_file_clone() { } fn test_seek() { - let tmp = std::env::temp_dir(); - let filename = PathBuf::from("miri_test_fs_seek.txt"); - let path = tmp.join(&filename); + let path = prepare("miri_test_fs_seek.txt"); let bytes = b"Hello, World!\n"; - // Clean the paths for robustness. - remove_file(&path).ok(); let mut file = File::create(&path).unwrap(); file.write(bytes).unwrap(); @@ -113,12 +110,8 @@ fn check_metadata(bytes: &[u8], path: &Path) -> Result<()> { } fn test_metadata() { - let tmp = std::env::temp_dir(); - let filename = PathBuf::from("miri_test_fs_metadata.txt"); - let path = tmp.join(&filename); + let path = prepare("miri_test_fs_metadata.txt"); let bytes = b"Hello, World!\n"; - // Clean the paths for robustness. - remove_file(&path).ok(); let mut file = File::create(&path).unwrap(); file.write(bytes).unwrap(); @@ -126,22 +119,17 @@ fn test_metadata() { // Test that metadata of an absolute path is correct. check_metadata(bytes, &path).unwrap(); // Test that metadata of a relative path is correct. - std::env::set_current_dir(&tmp).unwrap(); - check_metadata(bytes, &filename).unwrap(); + std::env::set_current_dir(path.parent().unwrap()).unwrap(); + check_metadata(bytes, Path::new(path.file_name().unwrap())).unwrap(); // Removing file should succeed. remove_file(&path).unwrap(); } fn test_symlink() { - let tmp = std::env::temp_dir(); - let filename = PathBuf::from("miri_test_fs_link_target.txt"); - let path = tmp.join(&filename); - let symlink_path = tmp.join("miri_test_fs_symlink.txt"); + let path = prepare("miri_test_fs_link_target.txt"); + let symlink_path = prepare("miri_test_fs_symlink.txt"); let bytes = b"Hello, World!\n"; - // Clean the paths for robustness. - remove_file(&path).ok(); - remove_file(&symlink_path).ok(); let mut file = File::create(&path).unwrap(); file.write(bytes).unwrap(); @@ -165,12 +153,8 @@ fn test_symlink() { } fn test_errors() { - let tmp = std::env::temp_dir(); - let filename = PathBuf::from("miri_test_fs_errors.txt"); - let path = tmp.join(&filename); + let path = prepare("miri_test_fs_errors.txt"); let bytes = b"Hello, World!\n"; - // Clean the paths for robustness. - remove_file(&path).ok(); // The following tests also check that the `__errno_location()` shim is working properly. // Opening a non-existing file should fail with a "not found" error. @@ -182,13 +166,10 @@ fn test_errors() { } fn test_rename() { - let tmp = std::env::temp_dir(); // Renaming a file should succeed. - let path1 = tmp.join("miri_test_fs_rename_source.txt"); - let path2 = tmp.join("miri_test_fs_rename_destination.txt"); - // Clean files for robustness. - remove_file(&path1).ok(); - remove_file(&path2).ok(); + let path1 = prepare("miri_test_fs_rename_source.txt"); + let path2 = prepare("miri_test_fs_rename_destination.txt"); + let file = File::create(&path1).unwrap(); drop(file); rename(&path1, &path2).unwrap(); From f79c453860ce113374029685b2b78b966a89559e Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 19 Feb 2020 11:08:24 +0100 Subject: [PATCH 2/3] factor more common code --- tests/run-pass/fs.rs | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/tests/run-pass/fs.rs b/tests/run-pass/fs.rs index a69f9f3b23d8..ebbd0b0c57ca 100644 --- a/tests/run-pass/fs.rs +++ b/tests/run-pass/fs.rs @@ -24,9 +24,17 @@ fn prepare(filename: &str) -> PathBuf { path } +/// Prepare like above, and also write some initial content to the file. +fn prepare_with_content(filename: &str, content: &[u8]) -> PathBuf { + let path = prepare(filename); + let mut file = File::create(&path).unwrap(); + file.write(content).unwrap(); + path +} + fn test_file() { - let path = prepare("miri_test_fs_file.txt"); let bytes = b"Hello, World!\n"; + let path = prepare("miri_test_fs_file.txt"); // Test creating, writing and closing a file (closing is tested when `file` is dropped). let mut file = File::create(&path).unwrap(); @@ -50,11 +58,8 @@ fn test_file() { } fn test_file_clone() { - let path = prepare("miri_test_fs_file_clone.txt"); let bytes = b"Hello, World!\n"; - - let mut file = File::create(&path).unwrap(); - file.write(bytes).unwrap(); + let path = prepare_with_content("miri_test_fs_file_clone.txt", bytes); // Cloning a file should be successful. let file = File::open(&path).unwrap(); @@ -69,11 +74,8 @@ fn test_file_clone() { } fn test_seek() { - let path = prepare("miri_test_fs_seek.txt"); - let bytes = b"Hello, World!\n"; - - let mut file = File::create(&path).unwrap(); - file.write(bytes).unwrap(); + let bytes = b"Hello, entire World!\n"; + let path = prepare_with_content("miri_test_fs_seek.txt", bytes); let mut file = File::open(&path).unwrap(); let mut contents = Vec::new(); @@ -110,11 +112,8 @@ fn check_metadata(bytes: &[u8], path: &Path) -> Result<()> { } fn test_metadata() { - let path = prepare("miri_test_fs_metadata.txt"); - let bytes = b"Hello, World!\n"; - - let mut file = File::create(&path).unwrap(); - file.write(bytes).unwrap(); + let bytes = b"Hello, meta-World!\n"; + let path = prepare_with_content("miri_test_fs_metadata.txt", bytes); // Test that metadata of an absolute path is correct. check_metadata(bytes, &path).unwrap(); @@ -127,12 +126,9 @@ fn test_metadata() { } fn test_symlink() { - let path = prepare("miri_test_fs_link_target.txt"); - let symlink_path = prepare("miri_test_fs_symlink.txt"); let bytes = b"Hello, World!\n"; - - let mut file = File::create(&path).unwrap(); - file.write(bytes).unwrap(); + let path = prepare_with_content("miri_test_fs_link_target.txt", bytes); + let symlink_path = prepare("miri_test_fs_symlink.txt"); // Creating a symbolic link should succeed. std::os::unix::fs::symlink(&path, &symlink_path).unwrap(); @@ -153,8 +149,8 @@ fn test_symlink() { } fn test_errors() { - let path = prepare("miri_test_fs_errors.txt"); let bytes = b"Hello, World!\n"; + let path = prepare("miri_test_fs_errors.txt"); // The following tests also check that the `__errno_location()` shim is working properly. // Opening a non-existing file should fail with a "not found" error. From 3cd13cb1746193f077766985273b144beba01480 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 19 Feb 2020 11:14:30 +0100 Subject: [PATCH 3/3] test a bit more --- tests/run-pass/fs.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/run-pass/fs.rs b/tests/run-pass/fs.rs index ebbd0b0c57ca..453432f64f10 100644 --- a/tests/run-pass/fs.rs +++ b/tests/run-pass/fs.rs @@ -80,6 +80,7 @@ fn test_seek() { let mut file = File::open(&path).unwrap(); let mut contents = Vec::new(); file.read_to_end(&mut contents).unwrap(); + assert_eq!(bytes, contents.as_slice()); // Test that seeking to the beginning and reading until EOF gets the text again. file.seek(SeekFrom::Start(0)).unwrap(); let mut contents = Vec::new();