about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPaul Mabileau <paul.mabileau@harfanglab.fr>2025-05-16 22:49:00 +0200
committerPaul Mabileau <paul.mabileau@harfanglab.fr>2025-05-28 12:06:25 +0200
commit0dd5722d6790be37b310c2e578be4f162d2be6ef (patch)
tree0441538ddbb1154cf6388b7e87143c0981af16f4
parentcb678b94c332548e3c3cefc22637929004c975d6 (diff)
downloadrust-0dd5722d6790be37b310c2e578be4f162d2be6ef.tar.gz
rust-0dd5722d6790be37b310c2e578be4f162d2be6ef.zip
Test(fs): Fix test_eq_windows_file_type for Windows 7
Would otherwise fail on:

```
thread 'fs::tests::test_eq_windows_file_type' panicked at library/std/src/test_helpers.rs:53:20:
called `Result::unwrap()` on an `Err` value: Os { code: 5, kind: PermissionDenied, message: "Access is denied." }
```

This came from the read-only attribute set on the test file. In order to
fix this, instead of simply disabling the test, the attribute is reset
before the test's end so it may still run successfully.

Signed-off-by: Paul Mabileau <paul.mabileau@harfanglab.fr>
-rw-r--r--library/std/src/fs/tests.rs24
1 files changed, 23 insertions, 1 deletions
diff --git a/library/std/src/fs/tests.rs b/library/std/src/fs/tests.rs
index 8da6d75b73e..c81e3af2f0d 100644
--- a/library/std/src/fs/tests.rs
+++ b/library/std/src/fs/tests.rs
@@ -1782,8 +1782,30 @@ fn test_eq_windows_file_type() {
     // Change the readonly attribute of one file.
     let mut perms = file1.metadata().unwrap().permissions();
     perms.set_readonly(true);
-    file1.set_permissions(perms).unwrap();
+    file1.set_permissions(perms.clone()).unwrap();
+    #[cfg(target_vendor = "win7")]
+    let _g = ReadonlyGuard { file: &file1, perms };
     assert_eq!(file1.metadata().unwrap().file_type(), file2.metadata().unwrap().file_type());
+
+    // Reset the attribute before the `TmpDir`'s drop that removes the
+    // associated directory, which fails with a `PermissionDenied` error when
+    // running under Windows 7.
+    #[cfg(target_vendor = "win7")]
+    struct ReadonlyGuard<'f> {
+        file: &'f File,
+        perms: fs::Permissions,
+    }
+    #[cfg(target_vendor = "win7")]
+    impl<'f> Drop for ReadonlyGuard<'f> {
+        fn drop(&mut self) {
+            self.perms.set_readonly(false);
+            let res = self.file.set_permissions(self.perms.clone());
+
+            if !thread::panicking() {
+                res.unwrap();
+            }
+        }
+    }
 }
 
 /// Regression test for https://github.com/rust-lang/rust/issues/50619.