Skip to content

Commit 0dd5722

Browse files
committed
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>
1 parent cb678b9 commit 0dd5722

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

library/std/src/fs/tests.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1782,8 +1782,30 @@ fn test_eq_windows_file_type() {
17821782
// Change the readonly attribute of one file.
17831783
let mut perms = file1.metadata().unwrap().permissions();
17841784
perms.set_readonly(true);
1785-
file1.set_permissions(perms).unwrap();
1785+
file1.set_permissions(perms.clone()).unwrap();
1786+
#[cfg(target_vendor = "win7")]
1787+
let _g = ReadonlyGuard { file: &file1, perms };
17861788
assert_eq!(file1.metadata().unwrap().file_type(), file2.metadata().unwrap().file_type());
1789+
1790+
// Reset the attribute before the `TmpDir`'s drop that removes the
1791+
// associated directory, which fails with a `PermissionDenied` error when
1792+
// running under Windows 7.
1793+
#[cfg(target_vendor = "win7")]
1794+
struct ReadonlyGuard<'f> {
1795+
file: &'f File,
1796+
perms: fs::Permissions,
1797+
}
1798+
#[cfg(target_vendor = "win7")]
1799+
impl<'f> Drop for ReadonlyGuard<'f> {
1800+
fn drop(&mut self) {
1801+
self.perms.set_readonly(false);
1802+
let res = self.file.set_permissions(self.perms.clone());
1803+
1804+
if !thread::panicking() {
1805+
res.unwrap();
1806+
}
1807+
}
1808+
}
17871809
}
17881810

17891811
/// Regression test for https://github.com/rust-lang/rust/issues/50619.

0 commit comments

Comments
 (0)