From 8e71dd3e0dce75964c930d54090876bc74397d4d Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Tue, 17 Nov 2026 14:49:54 -0800 Subject: [PATCH] Fix tests when testing with an SDK mismatch --- Tests/SystemTests/FileOperationsTest.swift | 24 ++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/Tests/SystemTests/FileOperationsTest.swift b/Tests/SystemTests/FileOperationsTest.swift index 2434bb5..4f295a2 100644 --- a/Tests/SystemTests/FileOperationsTest.swift +++ b/Tests/SystemTests/FileOperationsTest.swift @@ -179,11 +179,12 @@ final class FileOperationsTest: XCTestCase { } func testAdHocPipeWithOptions() throws { +#if !SYSTEM_PACKAGE_DARWIN || canImport(Darwin, _version: 428) guard #available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *) else { throw XCTSkip("pipe2 and PipeOptions require Darwin 27 or newer") } - // Ad-hoc test testing `Pipe` functionality. - // We cannot test `Pipe` using `MockTestCase` because it calls `pipe` with a pointer to an array local to the `Pipe`, the address of which we do not know prior to invoking `Pipe`. + // Ad-hoc test: `MockTestCase` can't match the pointer to the local array + // that `pipe` receives, because its address isn't known in advance. let options: FileDescriptor.PipeOptions = [.closeOnExec] let pipe: (readEnd: FileDescriptor, writeEnd: FileDescriptor) pipe = try FileDescriptor.pipe(options: options) @@ -194,16 +195,20 @@ final class FileOperationsTest: XCTestCase { _ = try pipe.writeEnd.write(UnsafeRawBufferPointer($0)) } let readLen = 3 - let readBytes = try Array(unsafeUninitializedCapacity: readLen) { buf, count in + let readBytes = try Array(unsafeUninitializedCapacity: readLen) { + buf, count in count = try pipe.readEnd.read(into: UnsafeMutableRawBufferPointer(buf)) } XCTAssertEqual(readBytes, Array(abc.utf8)) } } +#else + throw XCTSkip("pipe2 requires a Darwin SDK for anyAppleOS 27 or newer") +#endif } - #if canImport(Darwin, _version: 428) // The macOS 27 SDK defines O_CLOFORK. func testAdHocPipeCloseOnFork() throws { +#if canImport(Darwin, _version: 428) guard #available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *) else { throw XCTSkip("pipe2 and PipeOptions require anyAppleOS 27 or newer") } @@ -212,11 +217,14 @@ final class FileOperationsTest: XCTestCase { let pipe = try FileDescriptor.pipe(options: .closeOnFork) try pipe.readEnd.closeAfter { try pipe.writeEnd.closeAfter { - XCTAssertEqual(fcntl(pipe.readEnd.rawValue, F_GETFD) & FD_CLOFORK, FD_CLOFORK) + let selectedFlag = fcntl(pipe.readEnd.rawValue, F_GETFD) & FD_CLOFORK + XCTAssertEqual(selectedFlag, FD_CLOFORK) } } +#else + throw XCTSkip("O_CLOFORK requires a Darwin SDK for anyAppleOS 27 or newer") +#endif } - #endif #endif // !os(WASI) #if !os(Windows) @@ -258,6 +266,7 @@ final class FileOperationsTest: XCTestCase { } func testAdHocDuplicate3() throws { +#if !SYSTEM_PACKAGE_DARWIN || canImport(Darwin, _version: 428) guard #available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *) else { throw XCTSkip("dup3 and DuplicateOptions require Darwin 27 or newer") } @@ -292,6 +301,9 @@ final class FileOperationsTest: XCTestCase { } } } +#else + throw XCTSkip("dup3 requires a Darwin SDK for anyAppleOS 27 or newer") +#endif } #endif // !os(Windows)