Skip to content

Commit 55b2456

Browse files
committed
fs: add test for Windows ready-only file deletion.
Deleting a read-only file should result in `AccessDenied` (`CANNOT_DELETE`). Note: This test was observed to fail when the file is closed then reopened before the change in permission due to the absence of `FILE_WRITE_ATTRIBUTES` when re-opened. (see #15316).
1 parent 89334fa commit 55b2456

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

lib/std/fs/test.zig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,3 +1415,22 @@ test "File.PermissionsUnix" {
14151415
try testing.expect(permissions_unix.unixHas(.user, .execute));
14161416
try testing.expect(!permissions_unix.unixHas(.other, .execute));
14171417
}
1418+
1419+
test "delete a read-only file on windows" {
1420+
if (builtin.os.tag != .windows) return error.SkipZigTest;
1421+
1422+
var tmp = tmpDir(.{});
1423+
defer tmp.cleanup();
1424+
const file = try tmp.dir.createFile("test_file", .{ .read = true });
1425+
// Create a file and make it read-only
1426+
const metadata = try file.metadata();
1427+
var permissions = metadata.permissions();
1428+
permissions.setReadOnly(true);
1429+
try file.setPermissions(permissions);
1430+
try testing.expectError(error.AccessDenied, tmp.dir.deleteFile("test_file"));
1431+
// Now make the file not read-only
1432+
permissions.setReadOnly(false);
1433+
try file.setPermissions(permissions);
1434+
file.close();
1435+
try tmp.dir.deleteFile("test_file");
1436+
}

0 commit comments

Comments
 (0)