From 58981a841a05abadfb758764298efe1155255a7c Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Mon, 16 Sep 2024 23:33:31 -0400 Subject: [PATCH 1/2] Disable pipes code on WASI. WASI does not support pipes. Resolves #698. --- Package.swift | 1 + Sources/Testing/ABI/EntryPoints/EntryPoint.swift | 2 ++ Sources/Testing/ExitTests/ExitTest.swift | 4 ++++ Sources/Testing/Support/FileHandle.swift | 6 +++++- Tests/TestingTests/ExitTestTests.swift | 1 + Tests/TestingTests/Support/FileHandleTests.swift | 8 +++++++- cmake/modules/shared/CompilerSettings.cmake | 4 ++++ 7 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index 072027dc2..32ea88bc3 100644 --- a/Package.swift +++ b/Package.swift @@ -133,6 +133,7 @@ extension Array where Element == PackageDescription.SwiftSetting { .define("SWT_NO_EXIT_TESTS", .when(platforms: [.iOS, .watchOS, .tvOS, .visionOS, .wasi, .android])), .define("SWT_NO_SNAPSHOT_TYPES", .when(platforms: [.linux, .windows, .wasi])), .define("SWT_NO_DYNAMIC_LINKING", .when(platforms: [.wasi])), + .define("SWT_NO_PIPES", .when(platforms: [.wasi])), ] } diff --git a/Sources/Testing/ABI/EntryPoints/EntryPoint.swift b/Sources/Testing/ABI/EntryPoints/EntryPoint.swift index 1164a2944..904333c59 100644 --- a/Sources/Testing/ABI/EntryPoints/EntryPoint.swift +++ b/Sources/Testing/ABI/EntryPoints/EntryPoint.swift @@ -660,12 +660,14 @@ extension Event.ConsoleOutputRecorder.Options { return true } +#if !SWT_NO_PIPES // If the file handle is a pipe, assume the other end is using it to forward // output from this process to its own stderr file. This is how `swift test` // invokes the testing library, for example. if fileHandle.isPipe { return true } +#endif return false } diff --git a/Sources/Testing/ExitTests/ExitTest.swift b/Sources/Testing/ExitTests/ExitTest.swift index c0a29cfc4..7e5009279 100644 --- a/Sources/Testing/ExitTests/ExitTest.swift +++ b/Sources/Testing/ExitTests/ExitTest.swift @@ -11,6 +11,10 @@ private import _TestingInternals #if !SWT_NO_EXIT_TESTS +#if SWT_NO_PIPES +#error("Support for exit tests requires support for (anonymous) pipes.") +#endif + /// A type describing an exit test. /// /// Instances of this type describe an exit test defined by the test author and diff --git a/Sources/Testing/Support/FileHandle.swift b/Sources/Testing/Support/FileHandle.swift index a4b179a3a..6bf55f018 100644 --- a/Sources/Testing/Support/FileHandle.swift +++ b/Sources/Testing/Support/FileHandle.swift @@ -425,6 +425,7 @@ extension FileHandle { } } +#if !SWT_NO_PIPES // MARK: - Pipes extension FileHandle { @@ -495,6 +496,7 @@ extension FileHandle { } } } +#endif // MARK: - Attributes @@ -525,9 +527,10 @@ extension FileHandle { #endif } +#if !SWT_NO_PIPES /// Is this file handle a pipe or FIFO? var isPipe: Bool { -#if SWT_TARGET_OS_APPLE || os(Linux) || os(FreeBSD) || os(Android) || os(WASI) +#if SWT_TARGET_OS_APPLE || os(Linux) || os(FreeBSD) || os(Android) withUnsafePOSIXFileDescriptor { fd in guard let fd else { return false @@ -547,6 +550,7 @@ extension FileHandle { return false #endif } +#endif } // MARK: - General path utilities diff --git a/Tests/TestingTests/ExitTestTests.swift b/Tests/TestingTests/ExitTestTests.swift index 5c4ab7b47..2ed4848ec 100644 --- a/Tests/TestingTests/ExitTestTests.swift +++ b/Tests/TestingTests/ExitTestTests.swift @@ -23,6 +23,7 @@ private import _TestingInternals } } await #expect(exitsWith: .success) { + sleep(100) exit(EXIT_SUCCESS) } await #expect(exitsWith: .exitCode(123)) { diff --git a/Tests/TestingTests/Support/FileHandleTests.swift b/Tests/TestingTests/Support/FileHandleTests.swift index b352a1ec7..5d93a249f 100644 --- a/Tests/TestingTests/Support/FileHandleTests.swift +++ b/Tests/TestingTests/Support/FileHandleTests.swift @@ -149,14 +149,16 @@ struct FileHandleTests { } #endif +#if !SWT_NO_PIPES @Test("Can recognize opened pipe") func isPipe() throws { let pipe = try FileHandle.Pipe() #expect(pipe.readEnd.isPipe as Bool) #expect(pipe.writeEnd.isPipe as Bool) } +#endif -#if SWT_TARGET_OS_APPLE +#if SWT_TARGET_OS_APPLE && !SWT_NO_PIPES @Test("Can close ends of a pipe") func closeEndsOfPipe() async throws { try await confirmation("File handle closed", expectedCount: 2) { closed in @@ -175,7 +177,9 @@ struct FileHandleTests { func devNull() throws { let fileHandle = try FileHandle.null(mode: "wb") #expect(!Bool(fileHandle.isTTY)) +#if !SWT_NO_PIPES #expect(!Bool(fileHandle.isPipe)) +#endif } #if !os(Windows) @@ -186,7 +190,9 @@ struct FileHandleTests { let file = try #require(fmemopen(nil, 1, "wb+")) let fileHandle = FileHandle(unsafeCFILEHandle: file, closeWhenDone: true) #expect(!Bool(fileHandle.isTTY)) +#if !SWT_NO_PIPES #expect(!Bool(fileHandle.isPipe)) +#endif } #endif } diff --git a/cmake/modules/shared/CompilerSettings.cmake b/cmake/modules/shared/CompilerSettings.cmake index 183d73c04..cc8a9b2fb 100644 --- a/cmake/modules/shared/CompilerSettings.cmake +++ b/cmake/modules/shared/CompilerSettings.cmake @@ -27,3 +27,7 @@ endif() if(NOT APPLE) add_compile_definitions("SWT_NO_SNAPSHOT_TYPES") endif() +if(WASI) + add_compile_definitions("SWT_NO_DYNAMIC_LINKING") + add_compile_definitions("SWT_NO_PIPES") +endif() From ba392ae1fb3d2ae07981eba49734601832bf854a Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Mon, 16 Sep 2024 23:35:36 -0400 Subject: [PATCH 2/2] Remove stray sleep --- Tests/TestingTests/ExitTestTests.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Tests/TestingTests/ExitTestTests.swift b/Tests/TestingTests/ExitTestTests.swift index 2ed4848ec..5c4ab7b47 100644 --- a/Tests/TestingTests/ExitTestTests.swift +++ b/Tests/TestingTests/ExitTestTests.swift @@ -23,7 +23,6 @@ private import _TestingInternals } } await #expect(exitsWith: .success) { - sleep(100) exit(EXIT_SUCCESS) } await #expect(exitsWith: .exitCode(123)) {