From 0b7fc03c76e27c85c44bbf4dc159d58abc3e2f73 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Mon, 6 Jul 2026 11:26:24 -0700 Subject: [PATCH] Add test of OpenOptions.closeOnExec for Windows --- .../FileOperationsTestWindows.swift | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Tests/SystemTests/FileOperationsTestWindows.swift b/Tests/SystemTests/FileOperationsTestWindows.swift index fbba7b5..856d74d 100644 --- a/Tests/SystemTests/FileOperationsTestWindows.swift +++ b/Tests/SystemTests/FileOperationsTestWindows.swift @@ -18,6 +18,7 @@ import XCTest #endif import WinSDK +import ucrt private let FILE_APPEND_DATA = DWORD(WinSDK.FILE_APPEND_DATA) private let FILE_EXECUTE = DWORD(WinSDK.FILE_EXECUTE) @@ -295,6 +296,38 @@ final class FileOperationsTestWindows: XCTestCase { // We can't easily test this without allocating 4GB, but the boundary is correct } } + + func testCloseOnExecOpenOption() throws { + try withTemporaryFilePath(basename: "testCloseOnExec") { path in + + func hasInheritFlag(of fd: FileDescriptor) throws -> Bool { + let osfHandle = _get_osfhandle(fd.rawValue) + let handle = try XCTUnwrap(HANDLE(bitPattern: osfHandle)) + var flags: DWORD = 0 + XCTAssertTrue(GetHandleInformation(handle, &flags)) + return flags & DWORD(HANDLE_FLAG_INHERIT) == DWORD(HANDLE_FLAG_INHERIT) + } + + var fd: FileDescriptor + fd = try FileDescriptor.open( + path.appending("wontinherit.txt"), .readWrite, + options: [.create, .truncate, .closeOnExec], + permissions: .ownerReadWrite + ) + try fd.closeAfter { + XCTAssertEqual(try hasInheritFlag(of: fd), false) + } + + fd = try FileDescriptor.open( + path.appending("inheritable.txt"), .readWrite, + options: [.create, .truncate], + permissions: .ownerReadWrite + ) + try fd.closeAfter { + XCTAssertEqual(try hasInheritFlag(of: fd), true) + } + } + } } #endif // os(Windows)