Skip to content

Commit 6b41bc2

Browse files
committed
Fixed atomicWriteReplacesReadOnlyDestination test
1 parent 35075e0 commit 6b41bc2

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

Tests/FoundationEssentialsTests/DataIOTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ private final class DataIOTests {
212212
#expect(readData == data)
213213
}
214214

215-
// Atomic writes must succeed when the destination is read-only, and the resulting file must not retain the read-only state.
215+
// Atomic writes must succeed when the destination is read-only.
216216
// On POSIX this is the behavior of rename(2). On Windows, SetFileInformationByHandle with FileRenameInfoEx returns ERROR_ACCESS_DENIED for a FILE_ATTRIBUTE_READONLY destination even with FILE_RENAME_FLAG_POSIX_SEMANTICS | FILE_RENAME_FLAG_REPLACE_IF_EXISTS. So the read-only attribute is cleared and the rename is retried.
217+
// The resulting file retains the read-only attribute. (Not yet on Windows).
217218
@Test
218219
func atomicWriteReplacesReadOnlyDestination() throws {
219220
let initial = Data("initial".utf8)
@@ -230,12 +231,11 @@ private final class DataIOTests {
230231
let read = try Data(contentsOf: url)
231232
#expect(read == next)
232233

233-
#if !FOUNDATION_FRAMEWORK
234-
// The path now resolves to the new temp file (the rename operation detached the old destination). Confirm it's writable by performing a subsequent non-atomic write.
235-
// Skipped for FOUNDATION_FRAMEWORK: that path preserves and reapplies the destination's pre-rename mode on the new file, so the new file would still be read-only.
236-
// TODO: Preserve the destination's mode across platforms.
237-
let later = Data("later".utf8)
238-
try later.write(to: url)
234+
#if !os(Windows)
235+
// Check mode bits directly. Testing by expecting `try later.write(to: url)` to throw would fail for root (e.g. in Linux CI containers), since root bypasses permission checks even on a read-only file.
236+
let attributes = try FileManager.default.attributesOfItem(atPath: url.path)
237+
let mode = attributes[.posixPermissions] as? UInt ?? 0
238+
#expect(mode == 0o400)
239239
#endif
240240
}
241241
}

0 commit comments

Comments
 (0)