File: IntegrationTests.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (1218 lines) | stat: -rw-r--r-- 40,687 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2022-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import AsyncAlgorithms
import AsyncProcess
import Atomics
import Logging
import NIO
import NIOConcurrencyHelpers
import XCTest

#if canImport(Darwin)
import Darwin
#else
import Glibc
#endif

final class IntegrationTests: XCTestCase {
  private var group: EventLoopGroup!
  private var logger: Logger!

  func testTheBasicsWork() async throws {
    let exe = ProcessExecutor(
      group: self.group,
      executable: "/bin/sh", ["-c", "exit 0"],
      standardInput: EOFSequence(),
      logger: self.logger
    )
    try await withThrowingTaskGroup(of: Void.self) { group in
      group.addTask {
        for try await chunk in await merge(exe.standardOutput, exe.standardError) {
          XCTFail("unexpected output: \(chunk.debugDescription)")
        }
      }
      let result = try await exe.run()
      XCTAssertEqual(.exit(CInt(0)), result)
    }
  }

  func testExitCodesWork() async throws {
    for exitCode in UInt8.min...UInt8.max {
      let exe = ProcessExecutor(
        group: self.group,
        executable: "/bin/sh", ["-c", "exit \(exitCode)"],
        standardInput: EOFSequence(),
        logger: self.logger
      )
      try await withThrowingTaskGroup(of: Void.self) { group in
        group.addTask {
          for try await chunk in await merge(exe.standardOutput, exe.standardError) {
            XCTFail("unexpected output: \(chunk.debugDescription)")
          }
        }

        let result = try await exe.run()
        XCTAssertEqual(.exit(CInt(exitCode)), result)
      }
    }
  }

  func testSignalsWork() async throws {
    #if os(Linux)
    // workaround for https://github.com/apple/swift-corelibs-foundation/issues/4772
    let signalsToTest: [CInt] = [SIGKILL]
    #else
    let signalsToTest: [CInt] = [SIGKILL, SIGTERM, SIGINT]
    #endif
    for signal in signalsToTest {
      let exe = ProcessExecutor(
        group: self.group,
        executable: "/bin/sh", ["-c", "kill -\(signal) $$"],
        standardInput: EOFSequence(),
        logger: self.logger
      )

      try await withThrowingTaskGroup(of: Void.self) { group in
        group.addTask {
          for try await chunk in await merge(exe.standardOutput, exe.standardError) {
            XCTFail("unexpected output: \(chunk.debugDescription)")
          }
        }

        let result = try await exe.run()
        XCTAssertEqual(.signal(CInt(signal)), result)
      }
    }
  }

  func testStreamingInputAndOutputWorks() async throws {
    let input = AsyncStream.justMakeIt(elementType: ByteBuffer.self)
    let exe = ProcessExecutor(
      group: self.group,
      executable: "/bin/cat", ["-nu"], // sh", ["-c", "while read -r line; do echo $line; done"],
      standardInput: input.consumer,
      logger: self.logger
    )
    try await withThrowingTaskGroup(of: ProcessExitReason?.self) { group in
      group.addTask {
        var lastLine: String? = nil
        for try await line in await exe.standardOutput.splitIntoLines(dropTerminator: false) {
          if line.readableBytes > 72 {
            lastLine = String(buffer: line)
            break
          }
          input.producer.yield(line)
        }
        XCTAssertEqual(
          "    10\t     9\t     8\t     7\t     6\t     5\t     4\t     3\t     2\t     1\tGO\n",
          lastLine
        )
        return nil
      }

      group.addTask {
        for try await chunk in await exe.standardError {
          XCTFail("unexpected stderr output: \(chunk.debugDescription)")
        }
        return nil
      }

      group.addTask {
        try await exe.run()
      }

      input.producer.yield(ByteBuffer(string: "GO\n"))

      // The stdout-reading task will exit first (the others will only return when we explicitly cancel because
      // the sub process would keep going forever).
      let stdoutReturn = try await group.next()
      var totalTasksReturned = 1
      XCTAssertEqual(.some(nil), stdoutReturn)
      group.cancelAll()

      while let furtherReturn = try await group.next() {
        totalTasksReturned += 1
        switch furtherReturn {
        case let .some(result):
          // the `exe.run()` task
          XCTAssert(.signal(SIGKILL) == result || .exit(0) == result)
        case .none:
          // stderr task
          ()
        }
      }
      XCTAssertEqual(3, totalTasksReturned)
    }
  }

  func testAbsorbing1MBOfDevZeroWorks() async throws {
    let totalAmountInBytes = 1024 * 1024
    let exe = ProcessExecutor(
      group: self.group,
      executable: "/bin/sh",
      [
        "-c",
        // spawn two `dd`s that output 1 MiB of zeros (but no diagnostics output). One bunch of zeroes will
        // go to stdout, the other one to stderr.
        "/bin/dd     2>/dev/null bs=\(totalAmountInBytes) count=1 if=/dev/zero; "
          + "/bin/dd >&2 2>/dev/null bs=\(totalAmountInBytes) count=1 if=/dev/zero; ",
      ],
      standardInput: EOFSequence(),
      logger: self.logger
    )
    try await withThrowingTaskGroup(of: ByteBuffer.self) { group in
      group.addTask {
        var accumulation = ByteBuffer()
        accumulation.reserveCapacity(totalAmountInBytes)

        for try await chunk in await exe.standardOutput {
          accumulation.writeImmutableBuffer(chunk)
        }

        return accumulation
      }

      group.addTask {
        var accumulation = ByteBuffer()
        accumulation.reserveCapacity(totalAmountInBytes)

        for try await chunk in await exe.standardError {
          accumulation.writeImmutableBuffer(chunk)
        }

        return accumulation
      }

      let result = try await exe.run()

      // once for stdout, once for stderr
      let stream1 = try await group.next()
      let stream2 = try await group.next()
      XCTAssertEqual(ByteBuffer(repeating: 0, count: totalAmountInBytes), stream1)
      XCTAssertEqual(ByteBuffer(repeating: 0, count: totalAmountInBytes), stream2)

      XCTAssertEqual(.exit(0), result)
    }
  }

  func testInteractiveShell() async throws {
    let input = AsyncStream.justMakeIt(elementType: ByteBuffer.self)
    let exe = ProcessExecutor(
      group: self.group,
      executable: "/bin/sh", [],
      standardInput: input.consumer,
      logger: self.logger
    )

    try await withThrowingTaskGroup(of: Void.self) { group in
      group.addTask {
        var allOutput: [String] = []
        for try await (stream, line) in await merge(
          exe.standardOutput.splitIntoLines(dropTerminator: true).map { ("stdout", $0) },
          exe.standardError.splitIntoLines(dropTerminator: true).map { ("stderr", $0) }
        ) {
          let formattedOutput = "\(String(buffer: line)) [\(stream)]"
          allOutput.append(formattedOutput)
        }

        XCTAssertEqual(
          [
            "hello stderr [stderr]",
            "hello stdout [stdout]",
          ],
          allOutput.sorted()
        )
      }

      group.addTask {
        let result = try await exe.run()
        XCTAssertEqual(.exit(0), result)
      }

      input.producer.yield(ByteBuffer(string: "echo hello stdout\n"))
      input.producer.yield(ByteBuffer(string: "echo >&2 hello stderr\n"))
      input.producer.yield(ByteBuffer(string: "exit 0\n"))
      input.producer.finish()

      try await group.waitForAll()
    }
  }

  func testEnvironmentVariables() async throws {
    let exe = ProcessExecutor(
      group: self.group,
      executable: "/bin/sh",
      ["-c", "echo $MY_VAR"],
      environment: ["MY_VAR": "value of my var"],
      standardInput: EOFSequence(),
      logger: self.logger
    )
    let all = try await exe.runGetAllOutput()
    XCTAssertEqual(.exit(0), all.exitReason)
    XCTAssertEqual("value of my var\n", String(buffer: all.standardOutput))
    XCTAssertEqual("", String(buffer: all.standardError))
  }

  func testSimplePipe() async throws {
    self.logger.logLevel = .debug
    let echo = ProcessExecutor(
      group: self.group,
      executable: "/bin/sh",
      ["-c", "echo foo;"],
      standardInput: EOFSequence(),
      standardError: .discard,
      logger: self.logger
    )

    try await withThrowingTaskGroup(of: Void.self) { group in
      group.addTask {
        try await echo.run().throwIfNonZero()
      }
      group.addTask { [elg = self.group!, logger = self.logger!] in
        let echoOutput = await echo.standardOutput

        let sed = ProcessExecutor(
          group: elg,
          executable: "/usr/bin/tr",
          ["[:lower:]", "[:upper:]"],
          standardInput: echoOutput,
          logger: logger
        )
        let output = try await sed.runGetAllOutput()
        XCTAssertEqual(String(buffer: output.standardOutput), "FOO\n")
      }
      try await group.waitForAll()
    }
  }

  func testStressTestVeryLittleOutput() async throws {
    for _ in 0..<128 {
      let exe = ProcessExecutor(
        group: self.group,
        executable: "/bin/sh",
        ["-c", "echo x; echo >&2 y;"],
        standardInput: EOFSequence(),
        logger: self.logger
      )
      let all = try await exe.runGetAllOutput()
      XCTAssertEqual(.exit(0), all.exitReason)
      XCTAssertEqual("x\n", String(buffer: all.standardOutput))
      XCTAssertEqual("y\n", String(buffer: all.standardError))
    }
  }

  func testOutputWithoutNewlinesThatIsSplitIntoLines() async throws {
    let exe = ProcessExecutor(
      group: self.group,
      executable: "/bin/sh",
      ["-c", "/bin/echo -n x; /bin/echo >&2 -n y"],
      standardInput: EOFSequence(),
      logger: self.logger
    )
    try await withThrowingTaskGroup(of: (String, ByteBuffer)?.self) { group in
      group.addTask {
        try await exe.run().throwIfNonZero()
        return nil
      }
      group.addTask {
        var things: [ByteBuffer] = []
        for try await chunk in await exe.standardOutput.splitIntoLines() {
          things.append(chunk)
        }
        XCTAssertEqual(1, things.count)
        return ("stdout", things.first.flatMap { $0 } ?? ByteBuffer(string: "n/a"))
      }
      group.addTask {
        var things: [ByteBuffer?] = []
        for try await chunk in await exe.standardError.splitIntoLines() {
          things.append(chunk)
        }
        XCTAssertEqual(1, things.count)
        return ("stderr", things.first.flatMap { $0 } ?? ByteBuffer(string: "n/a"))
      }

      let everything = try await Array(group).sorted { l, r in
        guard let l else {
          return true
        }
        guard let r else {
          return false
        }
        return l.0 < r.0
      }
      XCTAssertEqual(
        [nil, "stderr", "stdout"],
        everything.map { $0?.0 }
      )

      XCTAssertEqual(
        [nil, ByteBuffer(string: "y"), ByteBuffer(string: "x")],
        everything.map { $0?.1 }
      )
    }
  }

  func testDiscardingStdoutWorks() async throws {
    let exe = ProcessExecutor(
      group: self.group,
      executable: "/bin/dd",
      ["if=/dev/zero", "bs=\(1024 * 1024)", "count=1024", "status=none"],
      standardInput: EOFSequence(),
      standardOutput: .discard,
      standardError: .stream,
      logger: self.logger
    )
    async let stderr = exe.standardError.pullAllOfIt()
    try await exe.run().throwIfNonZero()
    let stderrBytes = try await stderr
    XCTAssertEqual(ByteBuffer(), stderrBytes)
  }

  func testDiscardingStderrWorks() async throws {
    let exe = ProcessExecutor(
      group: self.group,
      executable: "/bin/sh",
      ["-c", "/bin/dd >&2 if=/dev/zero bs=\(1024 * 1024) count=1024 status=none; echo OK"],
      standardInput: EOFSequence(),
      standardOutput: .stream,
      standardError: .discard,
      logger: self.logger
    )
    async let stdout = exe.standardOutput.pullAllOfIt()
    try await exe.run().throwIfNonZero()
    let stdoutBytes = try await stdout
    XCTAssertEqual(ByteBuffer(string: "OK\n"), stdoutBytes)
  }

  func testStdoutToFileWorks() async throws {
    let tempDir = URL(fileURLWithPath: NSTemporaryDirectory())
      .appendingPathComponent("AsyncProcessTests-\(getpid())-\(UUID())")
    try FileManager.default.createDirectory(at: tempDir, withIntermediateDirectories: false)
    defer {
      XCTAssertNoThrow(try FileManager.default.removeItem(at: tempDir))
    }

    let file = tempDir.appendingPathComponent("file")

    let exe = try ProcessExecutor(
      group: self.group,
      executable: "/bin/dd",
      ["if=/dev/zero", "bs=\(1024 * 1024)", "count=3", "status=none"],
      standardInput: EOFSequence(),
      standardOutput: .fileDescriptor(
        takingOwnershipOf: .open(
          .init(file.path.removingPercentEncoding!),
          .writeOnly,
          options: .create,
          permissions: [.ownerRead, .ownerWrite]
        )
      ),
      standardError: .stream,
      logger: self.logger
    )
    async let stderr = exe.standardError.pullAllOfIt()
    try await exe.run().throwIfNonZero()
    let stderrBytes = try await stderr
    XCTAssertEqual(Data(repeating: 0, count: 3 * 1024 * 1024), try Data(contentsOf: file))
    XCTAssertEqual(ByteBuffer(), stderrBytes)
  }

  func testStderrToFileWorks() async throws {
    let tempDir = URL(fileURLWithPath: NSTemporaryDirectory())
      .appendingPathComponent("AsyncProcessTests-\(getpid())-\(UUID())")
    try FileManager.default.createDirectory(at: tempDir, withIntermediateDirectories: false)
    defer {
      XCTAssertNoThrow(try FileManager.default.removeItem(at: tempDir))
    }

    let file = tempDir.appendingPathComponent("file")

    let exe = ProcessExecutor(
      group: self.group,
      executable: "/bin/sh",
      ["-c", "/bin/dd >&2 if=/dev/zero bs=\(1024 * 1024) count=3 status=none; echo OK"],
      standardInput: EOFSequence(),
      standardOutput: .stream,
      standardError: .fileDescriptor(
        takingOwnershipOf: try! .open(
          .init(file.path.removingPercentEncoding!),
          .writeOnly,
          options: .create,
          permissions: [.ownerRead, .ownerWrite]
        )
      ),
      logger: self.logger
    )
    async let stdout = exe.standardOutput.pullAllOfIt()
    try await exe.run().throwIfNonZero()
    let stdoutBytes = try await stdout
    XCTAssertEqual(ByteBuffer(string: "OK\n"), stdoutBytes)
    XCTAssertEqual(Data(repeating: 0, count: 3 * 1024 * 1024), try Data(contentsOf: file))
  }

  func testInheritingStdoutAndStderrWork() async throws {
    let exe = ProcessExecutor(
      group: self.group,
      executable: "/bin/sh",
      ["-c", "echo this is stdout; echo >&2 this is stderr"],
      standardInput: EOFSequence(),
      standardOutput: .inherit,
      standardError: .inherit,
      logger: self.logger
    )
    try await exe.run().throwIfNonZero()
  }

  func testDiscardingAndConsumingOutputYieldsAnError() async throws {
    let exe = ProcessExecutor(
      group: self.group,
      executable: "/bin/sh",
      ["-c", "echo this is stdout; echo >&2 this is stderr"],
      standardInput: EOFSequence(),
      standardOutput: .discard,
      standardError: .discard,
      logger: self.logger
    )
    try await exe.run().throwIfNonZero()
    var stdoutIterator = await exe.standardOutput.makeAsyncIterator()
    var stderrIterator = await exe.standardError.makeAsyncIterator()
    do {
      _ = try await stdoutIterator.next()
      XCTFail("expected this to throw")
    } catch is IllegalStreamConsumptionError {
      // OK
    }
    do {
      _ = try await stderrIterator.next()
      XCTFail("expected this to throw")
    } catch is IllegalStreamConsumptionError {
      // OK
    }
  }

  func testStressTestDiscardingOutput() async throws {
    for _ in 0..<128 {
      let exe = ProcessExecutor(
        group: self.group,
        executable: "/bin/sh",
        [
          "-c",
          "/bin/dd if=/dev/zero bs=\(1024 * 1024) count=1; /bin/dd >&2 if=/dev/zero bs=\(1024 * 1024) count=1;",
        ],
        standardInput: EOFSequence(),
        standardOutput: .discard,
        standardError: .discard,
        logger: self.logger
      )
      try await exe.run().throwIfNonZero()
    }
  }

  func testLogOutputToMetadata() async throws {
    let sharedRecorder = LogRecorderHandler()
    var recordedLogger = Logger(label: "recorder", factory: { _ in sharedRecorder })
    recordedLogger.logLevel = .info // don't give us the normal messages
    recordedLogger[metadataKey: "yo"] = "hey"

    try await ProcessExecutor.runLogOutput(
      group: self.group,
      executable: "/bin/sh",
      ["-c", "echo 1; echo >&2 2; echo 3; echo >&2 4; echo 5; echo >&2 6; echo 7; echo >&2 8;"],
      standardInput: EOFSequence(),
      logger: recordedLogger,
      logConfiguration: OutputLoggingSettings(logLevel: .critical, to: .metadata(logMessage: "msg", key: "key"))
    ).throwIfNonZero()
    XCTAssert(sharedRecorder.recordedMessages.allSatisfy { $0.level == .critical })
    XCTAssert(sharedRecorder.recordedMessages.allSatisfy { $0.message == "msg" })
    XCTAssert(sharedRecorder.recordedMessages.allSatisfy { $0.metadata["key"] != nil })
    XCTAssert(sharedRecorder.recordedMessages.allSatisfy { $0.metadata["yo"] == "hey" })
    let loggedLines = sharedRecorder.recordedMessages.compactMap { $0.metadata["key"]?.description }.sorted()
    XCTAssertEqual(["1", "2", "3", "4", "5", "6", "7", "8"], loggedLines)
  }

  func testLogOutputToMessage() async throws {
    let sharedRecorder = LogRecorderHandler()
    var recordedLogger = Logger(label: "recorder", factory: { _ in sharedRecorder })
    recordedLogger.logLevel = .info // don't give us the normal messages
    recordedLogger[metadataKey: "yo"] = "hey"

    try await ProcessExecutor.runLogOutput(
      group: self.group,
      executable: "/bin/sh",
      ["-c", "echo 1; echo >&2 2; echo 3; echo >&2 4; echo 5; echo >&2 6; echo 7; echo >&2 8;"],
      standardInput: EOFSequence(),
      logger: recordedLogger,
      logConfiguration: OutputLoggingSettings(logLevel: .critical, to: .logMessage)
    ).throwIfNonZero()
    XCTAssert(sharedRecorder.recordedMessages.allSatisfy { $0.level == .critical })
    XCTAssert(sharedRecorder.recordedMessages.allSatisfy { $0.metadata["key"] == nil })
    XCTAssert(sharedRecorder.recordedMessages.allSatisfy { $0.metadata["yo"] == "hey" })
    let loggedLines = sharedRecorder.recordedMessages.map(\.message.description).sorted()
    XCTAssertEqual(["1", "2", "3", "4", "5", "6", "7", "8"], loggedLines)
  }

  func testProcessOutputByLine() async throws {
    let collectedLines: NIOLockedValueBox<[(String, String)]> = NIOLockedValueBox([])
    try await ProcessExecutor.runProcessingOutput(
      group: self.group,
      executable: "/bin/sh",
      [
        "-c",
        """
        ( echo 1; echo >&2 2; echo 3; echo >&2 4; echo 5; echo >&2 6; echo 7; echo >&2 8; ) | \
        /bin/dd bs=1000 status=none
        """,
      ],
      standardInput: EOFSequence(),
      outputProcessor: { stream, line in
        collectedLines.withLockedValue { collection in
          collection.append((stream.description, String(buffer: line)))
        }
      },
      splitOutputIntoLines: true,
      logger: self.logger
    ).throwIfNonZero()
    XCTAssertEqual(
      ["1", "2", "3", "4", "5", "6", "7", "8"],
      collectedLines.withLockedValue { $0.map(\.1) }.sorted()
    )
  }

  func testProcessOutputInChunks() async throws {
    let collectedBytes = ManagedAtomic<Int>(0)
    try await ProcessExecutor.runProcessingOutput(
      group: self.group,
      executable: "/bin/dd",
      ["if=/dev/zero", "bs=\(1024 * 1024)", "count=20", "status=none"],
      standardInput: EOFSequence(),
      outputProcessor: { stream, chunk in
        XCTAssertEqual(stream, .standardOutput)
        XCTAssert(chunk.withUnsafeReadableBytes { $0.allSatisfy { $0 == 0 } })
        collectedBytes.wrappingIncrement(by: chunk.readableBytes, ordering: .relaxed)
      },
      splitOutputIntoLines: true,
      logger: self.logger
    ).throwIfNonZero()
    XCTAssertEqual(20 * 1024 * 1024, collectedBytes.load(ordering: .relaxed))
  }

  func testBasicRunMethodWorks() async throws {
    try await ProcessExecutor.run(
      group: self.group,
      executable: "/bin/dd", ["if=/dev/zero", "bs=\(1024 * 1024)", "count=100"],
      standardInput: EOFSequence(),
      logger: self.logger
    ).throwIfNonZero()
  }

  func testCollectJustStandardOutput() async throws {
    let allInfo = try await ProcessExecutor.runCollectingOutput(
      group: self.group,
      executable: "/bin/dd", ["if=/dev/zero", "bs=\(1024 * 1024)", "count=1"],
      standardInput: EOFSequence(),
      collectStandardOutput: true,
      collectStandardError: false,
      perStreamCollectionLimitBytes: 1024 * 1024,
      logger: self.logger
    )
    XCTAssertNoThrow(try allInfo.exitReason.throwIfNonZero())
    XCTAssertNil(allInfo.standardError)
    XCTAssertEqual(ByteBuffer(repeating: 0, count: 1024 * 1024), allInfo.standardOutput)
  }

  func testCollectJustStandardError() async throws {
    let allInfo = try await ProcessExecutor.runCollectingOutput(
      group: self.group,
      executable: "/bin/sh", ["-c", "/bin/dd >&2 if=/dev/zero bs=\(1024 * 1024) count=1 status=none"],
      standardInput: EOFSequence(),
      collectStandardOutput: false,
      collectStandardError: true,
      perStreamCollectionLimitBytes: 1024 * 1024,
      logger: self.logger
    )
    XCTAssertNoThrow(try allInfo.exitReason.throwIfNonZero())
    XCTAssertNil(allInfo.standardOutput)
    XCTAssertEqual(ByteBuffer(repeating: 0, count: 1024 * 1024), allInfo.standardError)
  }

  func testCollectNothing() async throws {
    let allInfo = try await ProcessExecutor.runCollectingOutput(
      group: self.group,
      executable: "/bin/sh", ["-c", "/bin/dd >&2 if=/dev/zero bs=\(1024 * 1024) count=100 status=none"],
      standardInput: EOFSequence(),
      collectStandardOutput: false,
      collectStandardError: false,
      perStreamCollectionLimitBytes: 1024 * 1024,
      logger: self.logger
    )
    XCTAssertNoThrow(try allInfo.exitReason.throwIfNonZero())
    XCTAssertNil(allInfo.standardOutput)
    XCTAssertNil(allInfo.standardError)
  }

  func testCollectStdOutAndErr() async throws {
    let allInfo = try await ProcessExecutor.runCollectingOutput(
      group: self.group,
      executable: "/bin/sh",
      [
        "-c",
        """
        /bin/dd >&2 if=/dev/zero bs=\(1024 * 1024) count=1 status=none;
        /bin/dd if=/dev/zero bs=100 count=1 status=none;
        """,
      ],
      standardInput: EOFSequence(),
      collectStandardOutput: true,
      collectStandardError: true,
      perStreamCollectionLimitBytes: 1024 * 1024,
      logger: self.logger
    )
    XCTAssertNoThrow(try allInfo.exitReason.throwIfNonZero())
    XCTAssertEqual(ByteBuffer(repeating: 0, count: 1024 * 1024), allInfo.standardError)
    XCTAssertEqual(ByteBuffer(repeating: 0, count: 100), allInfo.standardOutput)
  }

  func testTooMuchToCollectStdout() async throws {
    do {
      let result = try await ProcessExecutor.runCollectingOutput(
        group: self.group,
        executable: "/bin/dd", ["if=/dev/zero", "bs=\(1024 * 1024)", "count=1"],
        standardInput: EOFSequence(),
        collectStandardOutput: true,
        collectStandardError: false,
        perStreamCollectionLimitBytes: 1024 * 1024 - 1,
        logger: self.logger
      )
      XCTFail("should've thrown but got result: \(result)")
    } catch {
      XCTAssertTrue(error is ProcessExecutor.TooMuchProcessOutputError)
      XCTAssertEqual(
        ProcessOutputStream.standardOutput,
        (error as? ProcessExecutor.TooMuchProcessOutputError)?.stream
      )
    }
  }

  func testTooMuchToCollectStderr() async throws {
    do {
      let result = try await ProcessExecutor.runCollectingOutput(
        group: self.group,
        executable: "/bin/dd",
        ["if=/dev/zero", "bs=\(1024 * 1024)", "of=/dev/stderr", "count=1", "status=none"],
        standardInput: EOFSequence(),
        collectStandardOutput: false,
        collectStandardError: true,
        perStreamCollectionLimitBytes: 1024 * 1024 - 1,
        logger: self.logger
      )
      XCTFail("should've thrown but got result: \(result)")
    } catch {
      XCTAssertTrue(error is ProcessExecutor.TooMuchProcessOutputError)
      XCTAssertEqual(
        ProcessOutputStream.standardError,
        (error as? ProcessExecutor.TooMuchProcessOutputError)?.stream
      )
    }
  }

  func testCollectEmptyStringFromStdoutAndErr() async throws {
    let allInfo = try await ProcessExecutor.runCollectingOutput(
      group: self.group,
      executable: "/bin/sh",
      ["-c", ""],
      standardInput: EOFSequence(),
      collectStandardOutput: true,
      collectStandardError: true,
      perStreamCollectionLimitBytes: 1024 * 1024,
      logger: self.logger
    )
    XCTAssertNoThrow(try allInfo.exitReason.throwIfNonZero())
    XCTAssertEqual(ByteBuffer(), allInfo.standardError)
    XCTAssertEqual(ByteBuffer(), allInfo.standardOutput)
  }

  func testExecutableDoesNotExist() async throws {
    let exe = ProcessExecutor(
      group: self.group,
      executable: "/dev/null/does/not/exist",
      [],
      standardInput: EOFSequence(),
      standardOutput: .discard,
      standardError: .discard,
      logger: self.logger
    )
    do {
      let result = try await exe.run()
      XCTFail("got result for bad executable: \(result)")
    } catch {
      XCTAssertEqual(NSCocoaErrorDomain, (error as NSError).domain)
      #if canImport(Darwin)
      // https://github.com/apple/swift-corelibs-foundation/issues/4810
      XCTAssertEqual(NSFileNoSuchFileError, (error as NSError).code)
      #endif
    }
  }

  func testAPIsWithoutELGOrLoggerArguments() async throws {
    let exe = ProcessExecutor(
      executable: "/bin/sh", ["-c", "true"],
      standardInput: EOFSequence(),
      standardOutput: .discard,
      standardError: .discard
    )
    try await exe.run().throwIfNonZero()

    try await ProcessExecutor.run(
      executable: "/bin/sh", ["-c", "true"],
      standardInput: EOFSequence()
    ).throwIfNonZero()

    try await ProcessExecutor.runCollectingOutput(
      executable: "/bin/sh",
      ["-c", "true"],
      standardInput: EOFSequence(),
      collectStandardOutput: false,
      collectStandardError: false
    ).exitReason.throwIfNonZero()

    try await ProcessExecutor.runProcessingOutput(
      executable: "/bin/sh",
      ["-c", "true"],
      standardInput: EOFSequence()
    ) { _, _ in
    }.throwIfNonZero()

    try await ProcessExecutor.runLogOutput(
      executable: "/bin/sh",
      ["-c", "true"],
      standardInput: EOFSequence(),
      logger: self.logger,
      logConfiguration: .init(logLevel: .critical, to: .logMessage)
    ).throwIfNonZero()
  }

  func testAPIsWithoutELGStandardInputOrLoggerArguments() async throws {
    let exe = ProcessExecutor(
      executable: "/bin/sh", ["-c", "true"],
      standardOutput: .discard,
      standardError: .discard
    )
    try await exe.run().throwIfNonZero()

    let exeStream = ProcessExecutor(executable: "/bin/sh", ["-c", "true"])
    #if compiler(>=5.8)
    async let stdout = Array(exeStream.standardOutput)
    async let stderr = Array(exeStream.standardError)
    #else
    async let stdout = {
      var chunks: [ByteBuffer] = []
      for try await chunk in await exeStream.standardOutput {
        chunks.append(chunk)
      }
      return chunks
    }()
    async let stderr = {
      var chunks: [ByteBuffer] = []
      for try await chunk in await exeStream.standardError {
        chunks.append(chunk)
      }
      return chunks
    }()
    #endif
    try await exeStream.run().throwIfNonZero()
    let out = try await stdout
    let err = try await stderr
    XCTAssertEqual([], out)
    XCTAssertEqual([], err)

    try await ProcessExecutor.run(executable: "/bin/sh", ["-c", "true"]).throwIfNonZero()

    try await ProcessExecutor.runCollectingOutput(
      executable: "/bin/sh",
      ["-c", "true"],
      collectStandardOutput: false,
      collectStandardError: false
    ).exitReason.throwIfNonZero()

    try await ProcessExecutor.runProcessingOutput(executable: "/bin/sh", ["-c", "true"]) { _, _ in
    }.throwIfNonZero()

    try await ProcessExecutor.runLogOutput(
      executable: "/bin/sh",
      ["-c", "true"],
      logger: self.logger,
      logConfiguration: .init(logLevel: .critical, to: .logMessage)
    ).throwIfNonZero()
  }

  func testStdoutAndStderrToSameFileWorks() async throws {
    let tempDir = URL(fileURLWithPath: NSTemporaryDirectory())
      .appendingPathComponent("AsyncProcessTests-\(getpid())-\(UUID())")
    try FileManager.default.createDirectory(at: tempDir, withIntermediateDirectories: false)
    defer {
      XCTAssertNoThrow(try FileManager.default.removeItem(at: tempDir))
    }

    for (stdoutMode, stderrMode) in [("shared", "shared"), ("shared", "owned"), ("owned", "shared")] {
      let filePath = tempDir.appendingPathComponent("file-\(stdoutMode)-\(stderrMode)")
      let fd = try FileDescriptor.open(
        .init(filePath.path.removingPercentEncoding!),
        .writeOnly,
        options: .create,
        permissions: [.ownerRead, .ownerWrite]
      )
      defer {
        if stdoutMode == "shared" && stderrMode == "shared" {
          XCTAssertNoThrow(try fd.close())
        }
      }

      let stdout: ProcessOutput
      let stderr: ProcessOutput

      if stdoutMode == "owned" {
        stdout = .fileDescriptor(takingOwnershipOf: fd)
      } else {
        stdout = .fileDescriptor(sharing: fd)
      }
      if stderrMode == "owned" {
        stderr = .fileDescriptor(takingOwnershipOf: fd)
      } else {
        stderr = .fileDescriptor(sharing: fd)
      }

      #if canImport(Darwin)
      let command =
        "for o in 1 2; do i=1000; while [ $i -gt 0 ]; do echo $o >&$o; i=$(( $i - 1 )); done & done; wait"
      #else
      // workaround for
      // https://github.com/apple/swift-corelibs-foundation/issues/4772
      // which causes `SIGCHLD` being blocked in the shell so it can't wait for its children :|
      let command =
        "for o in 1 2; do i=1000; while [ $i -gt 0 ]; do echo $o >&$o; i=$(( $i - 1 )); done & done; sleep 10"
      #endif

      let exe = ProcessExecutor(
        group: self.group,
        executable: "/bin/sh",
        ["-c", command],
        standardInput: EOFSequence(),
        standardOutput: stdout,
        standardError: stderr,
        logger: self.logger
      )
      try await exe.run().throwIfNonZero()
      let actualOutput = try Data(contentsOf: filePath)
      XCTAssertEqual(4000, actualOutput.count, "\(stdoutMode)-\(stderrMode)")

      var expectedOutput = Data()
      expectedOutput.append(Data(repeating: UInt8(ascii: "\n"), count: 2000))
      expectedOutput.append(Data(repeating: UInt8(ascii: "1"), count: 1000))
      expectedOutput.append(Data(repeating: UInt8(ascii: "2"), count: 1000))
      XCTAssertEqual(expectedOutput, Data(actualOutput.sorted()), "\(stdoutMode)-\(stderrMode)")
    }
  }

  func testCanReliablyKillProcessesEvenWithSigmask() async throws {
    let exitReason = try await withThrowingTaskGroup(
      of: ProcessExitReason?.self,
      returning: ProcessExitReason.self
    ) { group in
      group.addTask {
        try await ProcessExecutor.run(
          executable: "/bin/sh",
          ["-c", "trap 'echo no' TERM; while true; do sleep 1; done"]
        )
      }
      group.addTask {
        try? await Task.sleep(nanoseconds: 10_000_000)
        return nil
      }

      while let result = try await group.next() {
        group.cancelAll()
        if let result {
          return result
        }
      }
      preconditionFailure("this should be impossible, task should've returned a result")
    }
    XCTAssertEqual(.signal(SIGKILL), exitReason)
  }

  func testCancelProcessVeryEarlyOnStressTest() async throws {
    for i in 0..<1000 {
      self.logger.debug("iteration go", metadata: ["iteration-number": "\(i)"])
      let exitReason = try await withThrowingTaskGroup(
        of: ProcessExitReason?.self,
        returning: ProcessExitReason.self
      ) { group in
        group.addTask { [logger = self.logger!] in
          try await ProcessExecutor.run(
            executable: "/bin/sleep", ["100000"],
            logger: logger
          )
        }
        group.addTask { [logger = self.logger!] in
          let waitNS = UInt64.random(in: 0..<10_000_000)
          logger.info("waiting", metadata: ["wait-ns": "\(waitNS)"])
          try? await Task.sleep(nanoseconds: waitNS)
          return nil
        }

        while let result = try await group.next() {
          group.cancelAll()
          if let result {
            return result
          }
        }
        preconditionFailure("this should be impossible, task should've returned a result")
      }
      XCTAssertEqual(.signal(SIGKILL), exitReason, "iteration \(i)")
    }
  }

  func testShortestManuallyMergedOutput() async throws {
    let exe = ProcessExecutor(executable: "/bin/bash", ["-c", "echo hello world"])
    async let result = exe.run()
    let lines = try await Array(
      merge(exe.standardOutput.splitIntoLines(), exe.standardError.splitIntoLines()).strings
    )
    XCTAssertEqual(["hello world"], lines)
    try await result.throwIfNonZero()
  }

  func testShortestJustGiveMeTheOutput() async throws {
    let result = try await ProcessExecutor.runCollectingOutput(
      executable: "/bin/bash",
      ["-c", "echo hello world"],
      collectStandardOutput: true,
      collectStandardError: true
    )
    XCTAssertEqual("hello world\n", result.standardOutput.map { String(buffer: $0) })
    XCTAssertEqual("", result.standardError.map { String(buffer: $0) })
    XCTAssertEqual(.exit(0), result.exitReason)
  }

  func testKillProcess() async throws {
    let p = ProcessExecutor(
      executable: "/bin/bash",
      ["-c", "while true; do echo A; sleep 1; done"],
      standardError: .discard
    )
    async let result = p.run()
    var outputIterator = await p.standardOutput.makeAsyncIterator()
    let firstChunk = try await outputIterator.next()
    XCTAssertEqual(UInt8(ascii: "A"), firstChunk?.readableBytesView.first)
    try await p.sendSignal(SIGKILL)
    let finalResult = try await result
    XCTAssertEqual(.signal(SIGKILL), finalResult)
  }

  #if os(macOS)
  // This test will hang on anything that uses swift-corelibs-foundation because of
  // https://github.com/apple/swift-corelibs-foundation/issues/4795
  // Foundation.Process on Linux doesn't correctly detect when child process dies (creating zombie processes)
  func testCanDealWithRunawayChildProcesses() async throws {
    self.logger = Logger(label: "x")
    self.logger.logLevel = .info
    let p = ProcessExecutor(
      executable: "/bin/bash",
      [
        "-c",
        """
        set -e
        /usr/bin/yes "Runaway process from \(#function), please file a swift-sdk-generator bug." > /dev/null &
        child_pid=$!
        trap "echo >&2 killing $child_pid; kill -KILL $child_pid" INT
        echo "$child_pid" # communicate the child pid to our parent
        exec >&- # close stdout
        echo "waiting for $child_pid" >&2
        wait
        """,
      ],
      standardError: .discard,
      teardownSequence: [
        .sendSignal(SIGINT, allowedTimeToExitNS: 10_000_000_000),
      ],
      logger: self.logger
    )

    try await withThrowingTaskGroup(of: pid_t?.self) { group in
      group.addTask {
        let result = try await p.run()
        XCTAssertEqual(.exit(128 + SIGINT), result)
        return nil
      }

      group.addTask {
        let pidString = try await String(buffer: p.standardOutput.pullAllOfIt())
        guard let pid = pid_t(pidString.dropLast()) else {
          XCTFail("couldn't get pid from \(pidString)")
          return nil
        }
        return pid
      }

      let maybePid = try await group.next()!
      let pid = try XCTUnwrap(maybePid)
      group.cancelAll()
      try await group.waitForAll()

      // Let's check that the subprocess (/usr/bin/yes) of our subprocess (/bin/bash) is actually dead
      // This is a tiny bit racy because the pid isn't immediately invalidated, so let's allow a few failures
      for attempt in 0 ..< .max {
        let killRet = kill(pid, 0)
        let errnoCode = errno
        guard killRet == -1 || attempt > 5 else {
          logger.error("kill didn't fail on attempt \(attempt), trying again...")
          usleep(100_000)
          continue
        }
        XCTAssertEqual(-1, killRet)
        XCTAssertEqual(ESRCH, errnoCode)
        break
      }    
    }
  }
  #endif

  func testShutdownSequenceWorks() async throws {
    let p = ProcessExecutor(
      executable: "/bin/bash",
      [
        "-c",
        """
        set -e
        trap 'echo saw SIGQUIT; echo >&2 saw SIGQUIT' QUIT
        trap 'echo saw SIGTERM; echo >&2 saw SIGTERM' TERM
        trap 'echo saw SIGINT; echo >&2 saw SIGINT; exit 3;' INT
        echo OK
        while true; do sleep 0.1; done
        exit 2
        """,
      ],
      standardError: .discard,
      teardownSequence: [
        .sendSignal(SIGQUIT, allowedTimeToExitNS: 10_000_000),
        .sendSignal(SIGTERM, allowedTimeToExitNS: 10_000_000),
        .sendSignal(SIGINT, allowedTimeToExitNS: 1_000_000_000),
      ],
      logger: self.logger
    )

    try await withThrowingTaskGroup(of: Void.self) { group in
      group.addTask {
        let result = try await p.run()
        #if os(macOS)
        // won't work on SCLF: https://github.com/apple/swift-corelibs-foundation/issues/4772
        XCTAssertEqual(.exit(3), result)
        #endif
      }
      var allLines: [String] = []
      for try await line in await p.standardOutput.splitIntoLines().strings {
        if line == "OK" {
          group.cancelAll()
        }
        allLines.append(line)
      }
      try await group.waitForAll()
      #if os(macOS)
      // won't work on SCLF: https://github.com/apple/swift-corelibs-foundation/issues/4772
      XCTAssertEqual(["OK", "saw SIGQUIT", "saw SIGTERM", "saw SIGINT"], allLines)
      #endif
    }
  }

  // MARK: - Setup/teardown

  override func setUp() async throws {
    self.group = MultiThreadedEventLoopGroup(numberOfThreads: 3)
    self.logger = Logger(label: "test", factory: { _ in SwiftLogNoOpLogHandler() })
  }

  override func tearDown() {
    self.logger = nil

    XCTAssertNoThrow(try self.group.syncShutdownGracefully())
    self.group = nil
  }
}

extension AsyncStream {
  static func justMakeIt(elementType: Element.Type = Element.self) -> (
    consumer: AsyncStream<Element>, producer: AsyncStream<Element>.Continuation
  ) {
    var _producer: AsyncStream<Element>.Continuation?
    let stream = AsyncStream { producer in
      _producer = producer
    }

    return (stream, _producer!)
  }
}

extension AsyncSequence where Element == ByteBuffer {
  func pullAllOfIt() async throws -> ByteBuffer {
    var buffer: ByteBuffer? = nil
    for try await chunk in self {
      buffer.setOrWriteImmutableBuffer(chunk)
    }
    return buffer ?? ByteBuffer()
  }
}

extension ProcessExecutor {
  struct AllOfAProcess: Sendable {
    var exitReason: ProcessExitReason
    var standardOutput: ByteBuffer
    var standardError: ByteBuffer
  }

  private enum What {
    case exit(ProcessExitReason)
    case stdout(ByteBuffer)
    case stderr(ByteBuffer)
  }

  func runGetAllOutput() async throws -> AllOfAProcess {
    try await withThrowingTaskGroup(of: What.self, returning: AllOfAProcess.self) { group in
      group.addTask {
        try await .exit(self.run())
      }
      group.addTask {
        try await .stdout(self.standardOutput.pullAllOfIt())
      }
      group.addTask {
        try await .stderr(self.standardError.pullAllOfIt())
      }

      var exitReason: ProcessExitReason?
      var stdout: ByteBuffer?
      var stderr: ByteBuffer?

      while let next = try await group.next() {
        switch next {
        case let .exit(value):
          exitReason = value
        case let .stderr(value):
          stderr = value
        case let .stdout(value):
          stdout = value
        }
      }

      return AllOfAProcess(exitReason: exitReason!, standardOutput: stdout!, standardError: stderr!)
    }
  }
}