Skip to content

Disable pipes code on WASI. #699

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -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])),
]
}

2 changes: 2 additions & 0 deletions Sources/Testing/ABI/EntryPoints/EntryPoint.swift
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions Sources/Testing/ExitTests/ExitTest.swift
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion Sources/Testing/Support/FileHandle.swift
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 additions & 1 deletion Tests/TestingTests/Support/FileHandleTests.swift
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions cmake/modules/shared/CompilerSettings.cmake
Original file line number Diff line number Diff line change
@@ -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()