Skip to content

Commit a81ca2d

Browse files
Fix compilation on platforms without Foundation.Process (#1209)
### Motivation: There's an open PR to add CI for Apple platforms. This is currently failing since it tries to build for all Apple platforms, not just macOS. This is because there are targets that provide support for testing that rely on `Foundation.process`, which is unavailable on non-macOS Apple platforms. ### Modifications: Guard the use of `Foundation.Process` with an `#if canImport`. ### Result: Package builds for non-macOS Apple platforms: iOS, tvOS, watchOS, etc.
1 parent fb67bfc commit a81ca2d

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

Sources/DistributedActorsTestKit/InspectKit.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ internal struct InspectKit {
2222
FileManager.default.currentDirectoryPath
2323
}
2424

25+
#if canImport(Foundation.Process)
2526
private static func runCommand(cmd: String, args: String...) -> (output: [Substring], error: [Substring], exitCode: Int32) {
2627
var output: [Substring] = []
2728
var error: [Substring] = []
@@ -54,6 +55,7 @@ internal struct InspectKit {
5455

5556
return (output, error, status)
5657
}
58+
#endif
5759

5860
struct ActorStats {
5961
var stats: [String: Row] = [:]
@@ -144,6 +146,10 @@ internal struct InspectKit {
144146

145147
/// Actor names to their counts
146148
static func actorStats() throws -> ActorStats {
149+
#if !canImport(Foundation.Process)
150+
struct UnsupportedPlatform: Error {}
151+
throw UnsupportedPlatform()
152+
#else
147153
// FIXME(regex): rdar://98705227 can't use regex on 5.7 on Linux because of a bug that crashes String.starts(with:) at runtime then
148154
let (out, err, _) = Self.runCommand(cmd: "\(self.baseDir)/scripts/dump_actors.sh")
149155

@@ -172,11 +178,13 @@ internal struct InspectKit {
172178
}
173179

174180
return ActorStats(stats: stats)
181+
#endif
175182
}
176183
}
177184

178185
extension [Substring: InspectKit.ActorStats] {}
179186

187+
#if canImport(Foundation.Process)
180188
// Compatible with Swift on all macOS versions as well as Linux
181189
extension Process {
182190
var binaryPath: String? {
@@ -204,3 +212,4 @@ extension Process {
204212
}
205213
}
206214
}
215+
#endif

Sources/MultiNodeTestKitRunner/Process+Extensions.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ import NIOCore
1818
import NIOPosix
1919

2020
import class Foundation.FileHandle
21-
import class Foundation.Process
2221
import struct Foundation.URL
2322

23+
#if canImport(Foundation.Process)
24+
import class Foundation.Process
25+
#endif
26+
27+
#if canImport(Foundation.Process)
2428
// Compatible with Swift on all macOS versions as well as Linux
2529
extension Process {
2630
var binaryPath: String? {
@@ -48,3 +52,4 @@ extension Process {
4852
}
4953
}
5054
}
55+
#endif

Sources/MultiNodeTestKitRunner/Terminal+Rainbow.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import OrderedCollections
2121

2222
import struct Foundation.Date
2323
import class Foundation.FileHandle
24-
import class Foundation.Process
2524
import struct Foundation.URL
2625

2726
enum Rainbow: String {

Sources/MultiNodeTestKitRunner/boot+MultiNodeTestKitRunner+Test.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ import NIOPosix
2020
import OrderedCollections
2121

2222
import struct Foundation.Date
23-
import class Foundation.FileHandle
24-
import class Foundation.Process
2523
import class Foundation.ProcessInfo
2624
import struct Foundation.URL
2725

26+
#if canImport(Foundation.Process)
27+
import class Foundation.Process
28+
#endif
29+
2830
// ==== ----------------------------------------------------------------------------------------------------------------
2931
// MARK: Code executing on each specific process/node
3032

@@ -115,6 +117,10 @@ extension MultiNodeTestKitRunnerBoot {
115117
}
116118

117119
func runMultiNodeTest(suite: String, testName: String, binary: String) async throws -> InterpretedRunResult {
120+
#if !canImport(Foundation.Process)
121+
struct UnsupportedPlatform: Error {}
122+
throw UnsupportedPlatform()
123+
#else
118124
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
119125
defer {
120126
try! elg.syncShutdownGracefully()
@@ -218,8 +224,10 @@ extension MultiNodeTestKitRunnerBoot {
218224

219225
// all nodes passed okey
220226
return .passedAsExpected
227+
#endif
221228
}
222229

230+
#if canImport(Foundation.Process)
223231
private func startTestTimeoutReaperTask(nodeName: String, process: Process, settings: MultiNodeTestSettings) -> Task<Void, Never> {
224232
Task.detached {
225233
try? await Task.sleep(until: .now + settings.execRunHardTimeout, clock: .continuous)
@@ -235,6 +243,7 @@ extension MultiNodeTestKitRunnerBoot {
235243
process.terminate()
236244
}
237245
}
246+
#endif
238247
}
239248

240249
struct NodeInterpretedRunResult {

Sources/MultiNodeTestKitRunner/boot+MultiNodeTestKitRunner.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,21 @@ import OrderedCollections
2121

2222
import struct Foundation.Date
2323
import class Foundation.FileHandle
24-
import class Foundation.Process
2524
import struct Foundation.URL
2625

26+
#if canImport(Foundation.Process)
27+
import class Foundation.Process
28+
#endif
29+
2730
@main
2831
struct MultiNodeTestKitRunnerBoot {
2932
static func main() async throws {
33+
#if !canImport(Foundation.Process)
34+
print("Platform does not support multi-node tests")
35+
throw ExitCode.failure
36+
#else
3037
try await Self().run()
38+
#endif
3139
}
3240

3341
struct TestFilter {

0 commit comments

Comments
 (0)