Skip to content

Commit 8ff71ea

Browse files
committed
Fix exit code handling and tests
1 parent a1ae935 commit 8ff71ea

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

Sources/Subprocess/PipeConfiguration.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@ private func createIODescriptor(from fd: FileDescriptor, closeWhenDone: Bool) ->
291291
#endif
292292
}
293293

294+
private func createTerminationStatus(_ exitCode: UInt32) -> TerminationStatus {
295+
#if canImport(WinSD)
296+
return .exited(exitCode)
297+
#else
298+
return .exited(Int32(exitCode))
299+
#endif
300+
}
301+
294302
// MARK: - Internal Functions
295303

296304
extension PipeConfiguration {
@@ -560,18 +568,12 @@ extension PipeConfiguration {
560568
return 0
561569
}
562570

563-
#if canImport(WinSD)
564-
let terminationStatus: TerminationStatus = .exited(result)
565-
#else
566-
let terminationStatus: TerminationStatus = .exited(Int32(result))
567-
#endif
568-
569571
return PipelineTaskResult.success(
570572
0,
571573
SendableCollectedResult(
572574
CollectedResult<FileDescriptorOutput, DiscardedOutput>(
573575
processIdentifier: currentProcessIdentifier(),
574-
terminationStatus: terminationStatus,
576+
terminationStatus: createTerminationStatus(result),
575577
standardOutput: (),
576578
standardError: ()
577579
)))
@@ -660,7 +662,7 @@ extension PipeConfiguration {
660662
let errorWriteFileDescriptor: IODescriptor = createIODescriptor(from: sharedErrorPipe.writeEnd, closeWhenDone: false)
661663
var errorWriteEnd: IOChannel? = errorWriteFileDescriptor.createIOChannel()
662664

663-
let result = try await withThrowingTaskGroup(of: Int32.self) { group in
665+
let result = try await withThrowingTaskGroup(of: UInt32.self) { group in
664666
// FIXME figure out how to propagate a preferred buffer size to this sequence
665667
let inSequence = AsyncBufferSequence(diskIO: inputReadEnd.take()!.consumeIOChannel(), preferredBufferSize: nil)
666668
let outWriter = StandardInputWriter(diskIO: outputWriteEnd.take()!)
@@ -684,7 +686,7 @@ extension PipeConfiguration {
684686
SendableCollectedResult(
685687
CollectedResult<FileDescriptorOutput, DiscardedOutput>(
686688
processIdentifier: currentProcessIdentifier(),
687-
terminationStatus: .exited(result),
689+
terminationStatus: createTerminationStatus(result),
688690
standardOutput: (),
689691
standardError: ()
690692
)))
@@ -821,7 +823,7 @@ extension PipeConfiguration {
821823
let errorWriteFileDescriptor = createIODescriptor(from: sharedErrorPipe.writeEnd, closeWhenDone: false)
822824
var errorWriteEnd: IOChannel? = errorWriteFileDescriptor.createIOChannel()
823825

824-
let result: (Int32, Output.OutputType) = try await withThrowingTaskGroup(of: (Int32, OutputCapturingState<Output.OutputType, ()>?).self) { group in
826+
let result: (UInt32, Output.OutputType) = try await withThrowingTaskGroup(of: (UInt32, OutputCapturingState<Output.OutputType, ()>?).self) { group in
825827
// FIXME figure out how to propagate a preferred buffer size to this sequence
826828
let inSequence = AsyncBufferSequence(diskIO: inputReadEnd.take()!.consumeIOChannel(), preferredBufferSize: nil)
827829
let outWriter = StandardInputWriter(diskIO: outputWriteEnd.take()!)
@@ -842,7 +844,7 @@ extension PipeConfiguration {
842844
return (retVal, .none)
843845
}
844846

845-
var exitCode: Int32 = 0
847+
var exitCode: UInt32 = 0
846848
var output: Output.OutputType? = nil
847849
for try await r in group {
848850
if r.0 != 0 {
@@ -862,7 +864,7 @@ extension PipeConfiguration {
862864
SendableCollectedResult(
863865
CollectedResult<Output, DiscardedOutput>(
864866
processIdentifier: currentProcessIdentifier(),
865-
terminationStatus: .exited(result.0),
867+
terminationStatus: createTerminationStatus(result.0),
866868
standardOutput: result.1,
867869
standardError: ()
868870
)))

Tests/SubprocessTests/PipeConfigurationTests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ struct PipeConfigurationTests {
5252
}
5353

5454
guard foundHello else {
55-
return Int32(1)
55+
return 1
5656
}
5757

5858
let written = try await output.write("Hello World")
5959
guard written == "Hello World".utf8.count else {
60-
return Int32(1)
60+
return 1
6161
}
62-
return Int32(0)
62+
return 0
6363
} | .name("cat")
6464
|> (
6565
input: .string("Hello"),
@@ -86,14 +86,14 @@ struct PipeConfigurationTests {
8686
}
8787

8888
guard foundHello else {
89-
return Int32(1)
89+
return 1
9090
}
9191

9292
let written = try await output.write("Hello World")
9393
guard written == "Hello World".utf8.count else {
94-
return Int32(1)
94+
return 1
9595
}
96-
return Int32(0)
96+
return 0
9797
} | .name("cat")
9898
|> (
9999
output: .string(limit: .max),
@@ -119,14 +119,14 @@ struct PipeConfigurationTests {
119119
}
120120

121121
guard foundHello else {
122-
return Int32(1)
122+
return 1
123123
}
124124

125125
let written = try await output.write("Hello World")
126126
guard written == "Hello World".utf8.count else {
127-
return Int32(1)
127+
return 1
128128
}
129-
return Int32(0)
129+
return 0
130130
} |> .string(limit: .max)
131131

132132
let result = try await config.run()

0 commit comments

Comments
 (0)