Skip to content

Commit 0e93abd

Browse files
authored
Ensure stdout is line-buffered when testing starts. (#568)
Swift Package Manager reroutes test output through pipes which are block-buffered by default, but convention in C is that `stdout` is line-buffered. This results (when using `swift test`) in output to `stdout` (e.g. from `print()`) not being displayed until after most test events have been displayed. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 2802522 commit 0e93abd

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Sources/Testing/ABI/EntryPoints/SwiftPMEntryPoint.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ public func __swiftPMEntryPoint(passing args: __CommandLineArguments_v0? = nil)
6969
/// - Warning: This function is used by Swift Package Manager. Do not call it
7070
/// directly.
7171
public func __swiftPMEntryPoint(passing args: __CommandLineArguments_v0? = nil) async -> Never {
72+
#if !SWT_NO_FILE_IO
73+
// Ensure that stdout is line- rather than block-buffered. Swift Package
74+
// Manager reroutes standard I/O through pipes, so we tend to end up with
75+
// block-buffered streams.
76+
FileHandle.stdout.withUnsafeCFILEHandle { stdout in
77+
_ = setvbuf(stdout, nil, _IOLBF, Int(BUFSIZ))
78+
}
79+
#endif
80+
7281
let exitCode: CInt = await __swiftPMEntryPoint(passing: args)
7382
exit(exitCode)
7483
}

0 commit comments

Comments
 (0)