Skip to content

Commit 2d855b0

Browse files
committed
Fix compilation on Swift <=5.10
1 parent 32737a6 commit 2d855b0

File tree

9 files changed

+42
-7
lines changed

9 files changed

+42
-7
lines changed

Sources/ManualTests/main.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ do {
6565
// print("From cat: \(line.strLineOrHex())")
6666
// }
6767

68+
#if swift(>=5.7)
6869
/* Try to read one line from ProcessInvocation and then one using StreamReader. */
6970
let pi = ProcessInvocation("head", "-n", "1")
7071
for try await line in pi {
@@ -76,6 +77,7 @@ do {
7677
} else {
7778
print("Failed reading line from stream reader.")
7879
}
80+
#endif
7981
} catch {
8082
print("Failed running the process: \(error)")
8183
}
@@ -133,6 +135,8 @@ func setRequireNonBlockingIO(on fd: FileDescriptor, logChange: Bool) throws {
133135
}
134136
}
135137

138+
typealias FileDescriptorReader = GenericStreamReader
139+
136140

137141

138142

Sources/ProcessInvocation/FileDescriptor+GenericReadStream.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ import SystemPackage
55

66

77

8+
#if compiler(>=6.0)
89
extension FileDescriptor : @retroactive GenericReadStream {
910

1011
public func read(_ buffer: UnsafeMutableRawPointer, maxLength len: Int) throws -> Int {
1112
return try read(into: UnsafeMutableRawBufferPointer(start: buffer, count: len))
1213
}
1314

1415
}
16+
#else
17+
extension FileDescriptor : GenericReadStream {
18+
19+
public func read(_ buffer: UnsafeMutableRawPointer, maxLength len: Int) throws -> Int {
20+
return try read(into: UnsafeMutableRawBufferPointer(start: buffer, count: len))
21+
}
22+
23+
}
24+
#endif
25+
typealias FileDescriptorReader = GenericStreamReader

Sources/ProcessInvocation/ProcessInvocation+Pipe.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public extension ProcessInvocation {
6060
do {
6161
try reader.peekData(size: Swift.max(0, maxCacheSize - (reader.currentStreamReadPosition - reader.currentReadPosition)), allowReadingLess: true, { _ in })
6262
try reader.peekData(size: reader.currentStreamReadPosition - reader.currentReadPosition, allowReadingLess: false, { bytes in
63-
let (writtenNow, readError) = {
63+
let (writtenNow, readError) = { () -> (Int, Int32) in
6464
guard bytes.count > 0 else {
6565
return (0, Int32(0))
6666
}

Sources/ProcessInvocation/ProcessInvocation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ public struct ProcessInvocation : AsyncSequence {
529529
/* Let’s see if the fd is a master pt or not.
530530
* This is needed to detect EOF properly and not throw an error when reading from a master pt (see handleProcessOutput for more info). */
531531
if spi_ptsname(fd.rawValue) == nil {
532-
let error = Errno(rawValue: errno)
532+
let error = SystemPackage.Errno(rawValue: errno)
533533
if error.rawValue != ENOTTY {
534534
Conf.logger?.warning("Cannot determine whether fd is a master pt or not; assuming it’s not.", metadata: ["fd": "\(fd.rawValue)", "error": "\(error)"])
535535
}

Tests/ProcessInvocationTests/ProcessInvocationTests.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,31 @@ final class ProcessInvocationTests : XCTestCase {
140140
}
141141

142142
func testProcessTerminationHandler() throws {
143+
#if swift(>=5.10)
143144
nonisolated(unsafe) var wentIn = false
144-
let (_, g) = try ProcessInvocation("/bin/cat", stdinRedirect: .fromNull, signalsToProcess: []).invoke(outputHandler: { _,_,_ in }, terminationHandler: { p in
145+
#else
146+
final class BoolRef : @unchecked Sendable {
147+
var value = false
148+
}
149+
let wentIn = BoolRef()
150+
#endif
151+
/* Note: The @Sendable annotation is for Swift 5.5. */
152+
let (_, g) = try ProcessInvocation("/bin/cat", stdinRedirect: .fromNull, signalsToProcess: []).invoke(outputHandler: { _,_,_ in }, terminationHandler: { @Sendable p in
153+
#if swift(>=5.10)
145154
wentIn = true
155+
#else
156+
wentIn.value = true
157+
#endif
146158
})
147159

148160
/* No need to wait on the process anymore */
149161
g.wait()
150162

163+
#if swift(>=5.10)
151164
XCTAssertTrue(wentIn)
165+
#else
166+
XCTAssertTrue(wentIn.value)
167+
#endif
152168
}
153169

154170
func testNonStandardFdCapture() throws {

TestsData/scripts/check-cwd+env.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/usr/bin/env -S swift
1+
#!/usr/bin/env swift
2+
/* In theory there should be a -S option to env, but on Swift 5.5 docker image the option does not exist! */
23
import Foundation
34

45

TestsData/scripts/slow-and-interleaved-output.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/usr/bin/env -S swift
1+
#!/usr/bin/env swift
2+
/* In theory there should be a -S option to env, but on Swift 5.5 docker image the option does not exist! */
23
import Foundation
34

45

TestsData/scripts/spy.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/usr/bin/env -S swift
1+
#!/usr/bin/env swift
2+
/* In theory there should be a -S option to env, but on Swift 5.5 docker image the option does not exist! */
23
import Foundation
34

45
print("I spy with my little eye")

TestsData/scripts/write-500-lines.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/usr/bin/env -S swift
1+
#!/usr/bin/env swift
2+
/* In theory there should be a -S option to env, but on Swift 5.5 docker image the option does not exist! */
23
import Foundation
34

45

0 commit comments

Comments
 (0)