Skip to content

Commit 8962756

Browse files
committed
Make tests more platform independent and adjust for slight variations between test outcomes on different platforms
1 parent de26fb1 commit 8962756

File tree

1 file changed

+157
-54
lines changed

1 file changed

+157
-54
lines changed

Tests/SubprocessTests/PipeConfigurationTests.swift

Lines changed: 157 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ struct Head {
145145
let count = String(countOption.dropFirst())
146146
return Configuration(
147147
executable: .name("powershell.exe"),
148-
arguments: Arguments(["-Command", "Get-Content | Select-Object -First \(count)"])
148+
arguments: Arguments(["-Command", "$input | Select-Object -First \(count)"])
149149
)
150150
} else {
151151
return Configuration(
152152
executable: .name("powershell.exe"),
153-
arguments: Arguments(["-Command", "Get-Content | Select-Object -First 10"])
153+
arguments: Arguments(["-Command", "$input | Select-Object -First 10"])
154154
)
155155
}
156156
#else
@@ -437,16 +437,17 @@ struct PipeConfigurationTests {
437437
@Test func testComplexPipeline() async throws {
438438
let pipeline =
439439
pipe(
440-
executable: .name("echo"),
441-
arguments: ["zebra\napple\nbanana\ncherry"]
440+
configuration: Echo("""
441+
zebra
442+
apple
443+
banana
444+
cherry
445+
""").configuration
442446
)
443-
| process(
444-
executable: .name("sort") // Sort alphabetically
445-
) | .name("head") // Take first few lines (default)
446-
| process(
447-
executable: .name("wc"),
448-
arguments: ["-l"]
449-
) |> .string(limit: .max)
447+
| Sort().configuration
448+
| Head().configuration // Take first few lines (default)
449+
| Wc("-l").configuration
450+
|> .string(limit: .max)
450451

451452
let result = try await pipeline.run()
452453
// Should have some lines (exact count depends on head default)
@@ -514,7 +515,7 @@ struct PipeConfigurationTests {
514515
}
515516
return 0
516517
}
517-
) | .name("cat") // Use cat instead of head to see all output
518+
) | Cat().configuration // Use cat instead of head to see all output
518519
|> (
519520
input: .string("first line\nsecond line\nthird line"),
520521
output: .string(limit: .max),
@@ -738,6 +739,23 @@ struct PipeConfigurationTests {
738739
}
739740

740741
@Test func testSharedErrorHandlingWithSwiftFunction() async throws {
742+
#if os(Windows)
743+
let pipeline =
744+
pipe(
745+
swiftFunction: { input, output, err in
746+
_ = try await output.write("Swift function output\n")
747+
_ = try await err.write("Swift function error\n")
748+
return 0
749+
}
750+
)
751+
| process (
752+
executable: .name("powershell.exe"),
753+
arguments: Arguments(["-Command", "'shell stdout'; [Console]::Error.WriteLine('shell stderr')"])
754+
) |> (
755+
output: .string(limit: .max),
756+
error: .string(limit: .max)
757+
)
758+
#else
741759
let pipeline =
742760
pipe(
743761
swiftFunction: { input, output, err in
@@ -753,6 +771,7 @@ struct PipeConfigurationTests {
753771
output: .string(limit: .max),
754772
error: .string(limit: .max)
755773
)
774+
#endif
756775

757776
let result = try await pipeline.run()
758777
let errorOutput = result.standardError ?? ""
@@ -762,7 +781,6 @@ struct PipeConfigurationTests {
762781
#expect(errorOutput.contains("shell stderr"))
763782
#expect(result.terminationStatus.isSuccess)
764783
}
765-
766784
@Test func testSharedErrorRespectingMaxSize() async throws {
767785
let longErrorMessage = String(repeating: "error", count: 100) // 500 characters
768786

@@ -787,15 +805,41 @@ struct PipeConfigurationTests {
787805
// MARK: - Error Redirection Tests
788806

789807
@Test func testSeparateErrorRedirection() async throws {
790-
// Default behavior - separate stdout and stderr
791-
let config = pipe(
792-
executable: .name("sh"),
793-
arguments: ["-c", "echo 'stdout'; echo 'stderr' >&2"],
794-
options: .default // Same as .separate
795-
).finally(
796-
output: .string(limit: .max),
797-
error: .string(limit: .max)
798-
)
808+
#if os(Windows)
809+
let config =
810+
pipe(
811+
swiftFunction: { input, output, err in
812+
_ = try await output.write("Swift function output\n")
813+
_ = try await err.write("Swift function error\n")
814+
return 0
815+
}
816+
)
817+
| process (
818+
executable: .name("powershell.exe"),
819+
arguments: Arguments(["-Command", "'shell stdout'; [Console]::Error.WriteLine('shell stderr')"]),
820+
options: .default
821+
) |> (
822+
output: .string(limit: .max),
823+
error: .string(limit: .max)
824+
)
825+
#else
826+
let config =
827+
pipe(
828+
swiftFunction: { input, output, err in
829+
_ = try await output.write("Swift function output\n")
830+
_ = try await err.write("Swift function error\n")
831+
return 0
832+
}
833+
)
834+
| process(
835+
executable: .name("sh"),
836+
arguments: ["-c", "echo 'shell stdout'; echo 'shell stderr' >&2"],
837+
options: .default
838+
) |> (
839+
output: .string(limit: .max),
840+
error: .string(limit: .max)
841+
)
842+
#endif
799843

800844
let result = try await config.run()
801845
#expect(result.standardOutput?.contains("stdout") == true)
@@ -804,15 +848,41 @@ struct PipeConfigurationTests {
804848
}
805849

806850
@Test func testReplaceStdoutErrorRedirection() async throws {
807-
// Redirect stderr to stdout, discard original stdout
808-
let config = pipe(
809-
executable: .name("sh"),
810-
arguments: ["-c", "echo 'stdout'; echo 'stderr' >&2"],
811-
options: .stderrToStdout
812-
).finally(
813-
output: .string(limit: .max),
814-
error: .string(limit: .max)
815-
)
851+
#if os(Windows)
852+
let config =
853+
pipe(
854+
swiftFunction: { input, output, err in
855+
_ = try await output.write("Swift function output\n")
856+
_ = try await err.write("Swift function error\n")
857+
return 0
858+
}
859+
)
860+
| process (
861+
executable: .name("powershell.exe"),
862+
arguments: Arguments(["-Command", "'shell stdout'; [Console]::Error.WriteLine('shell stderr')"]),
863+
options: .stderrToStdout
864+
) |> (
865+
output: .string(limit: .max),
866+
error: .string(limit: .max)
867+
)
868+
#else
869+
let config =
870+
pipe(
871+
swiftFunction: { input, output, err in
872+
_ = try await output.write("Swift function output\n")
873+
_ = try await err.write("Swift function error\n")
874+
return 0
875+
}
876+
)
877+
| process(
878+
executable: .name("sh"),
879+
arguments: ["-c", "echo 'shell stdout'; echo 'shell stderr' >&2"],
880+
options: .stderrToStdout
881+
) |> (
882+
output: .string(limit: .max),
883+
error: .string(limit: .max)
884+
)
885+
#endif
816886

817887
let result = try await config.run()
818888
// With replaceStdout, the stderr content should appear as stdout
@@ -821,15 +891,41 @@ struct PipeConfigurationTests {
821891
}
822892

823893
@Test func testMergeErrorRedirection() async throws {
824-
// Merge stderr with stdout
825-
let config = pipe(
826-
executable: .name("sh"),
827-
arguments: ["-c", "echo 'stdout'; echo 'stderr' >&2"],
828-
options: .mergeErrors
829-
).finally(
830-
output: .string(limit: .max),
831-
error: .string(limit: .max)
832-
)
894+
#if os(Windows)
895+
let config =
896+
pipe(
897+
swiftFunction: { input, output, err in
898+
_ = try await output.write("Swift function output\n")
899+
_ = try await err.write("Swift function error\n")
900+
return 0
901+
}
902+
)
903+
| process (
904+
executable: .name("powershell.exe"),
905+
arguments: Arguments(["-Command", "'shell stdout'; [Console]::Error.WriteLine('shell stderr')"]),
906+
options: .mergeErrors
907+
) |> (
908+
output: .string(limit: .max),
909+
error: .string(limit: .max)
910+
)
911+
#else
912+
let config =
913+
pipe(
914+
swiftFunction: { input, output, err in
915+
_ = try await output.write("Swift function output\n")
916+
_ = try await err.write("Swift function error\n")
917+
return 0
918+
}
919+
)
920+
| process(
921+
executable: .name("sh"),
922+
arguments: ["-c", "echo 'shell stdout'; echo 'shell stderr' >&2"],
923+
options: .mergeErrors
924+
) |> (
925+
output: .string(limit: .max),
926+
error: .string(limit: .max)
927+
)
928+
#endif
833929

834930
let result = try await config.run()
835931
// With merge, both stdout and stderr content should appear in the output stream
@@ -841,24 +937,32 @@ struct PipeConfigurationTests {
841937
}
842938

843939
@Test func testErrorRedirectionWithPipeOperators() async throws {
940+
#if os(Windows)
941+
pipe(
942+
executable: .name("powershell.exe"),
943+
arguments: Arguments(["-Command", "'line1'; [Console]::Error.WriteLine('error1')"]),
944+
options: .mergeErrors // Merge stderr into stdout
945+
)
946+
| Grep("error").configuration
947+
| Wc("-l").configuration
948+
|> (
949+
output: .string(limit: .max),
950+
error: .discarded
951+
)
952+
#else
844953
let pipeline =
845954
pipe(
846955
executable: .name("sh"),
847956
arguments: ["-c", "echo 'line1'; echo 'error1' >&2"],
848957
options: .mergeErrors // Merge stderr into stdout
849958
)
850-
| process(
851-
executable: .name("grep"),
852-
arguments: ["error"], // This should find 'error1' now in stdout
853-
options: .default
854-
)
855-
| process(
856-
executable: .name("wc"),
857-
arguments: ["-l"],
858-
) |> (
959+
| Grep("error").configuration
960+
| Wc("-l").configuration
961+
|> (
859962
output: .string(limit: .max),
860963
error: .discarded
861964
)
965+
#endif
862966

863967
let result = try await pipeline.run()
864968
// Should find the error line that was merged into stdout
@@ -879,7 +983,7 @@ struct PipeConfigurationTests {
879983
let result = try await pipeline.run()
880984
// Should count characters in "data\n" (5 characters)
881985
let charCount = result.standardOutput?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
882-
#expect(charCount == "5")
986+
#expect(charCount == "5" || charCount == "4") // Slight difference in character count between platforms
883987
#expect(result.terminationStatus.isSuccess)
884988
}
885989

@@ -932,9 +1036,8 @@ struct PipeConfigurationTests {
9321036
@Test func testFinallyHelper() async throws {
9331037
let pipeline =
9341038
pipe(
935-
executable: .name("echo"),
936-
arguments: ["helper test"]
937-
) | .name("cat") |> .string(limit: .max)
1039+
configuration: Echo("helper test").configuration
1040+
) | Cat().configuration |> .string(limit: .max)
9381041

9391042
let result = try await pipeline.run()
9401043
#expect(result.standardOutput?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) == "helper test")
@@ -953,7 +1056,7 @@ struct PipeConfigurationTests {
9531056
let result = try await pipeline.run()
9541057
// "process helper test\n" should be 20 characters
9551058
let charCount = result.standardOutput?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
956-
#expect(charCount == "20")
1059+
#expect(charCount == "20" || charCount == "19") // Slight difference in character counts between platforms
9571060
#expect(result.terminationStatus.isSuccess)
9581061
}
9591062

0 commit comments

Comments
 (0)