Skip to content

Commit 035cc6d

Browse files
committed
Change the manual test to something meaningful
1 parent 79591b5 commit 035cc6d

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

Package.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ let package = Package(
116116
/* Some manual tests for stdin redirect behavior. */
117117
res.append(.executableTarget(name: "ManualTests", dependencies: [
118118
.target(name: "ProcessInvocation"), /* <- Tested package */
119-
.product(name: "CLTLogger", package: "clt-logger"),
120-
.product(name: "Logging", package: "swift-log")
119+
.product(name: "CLTLogger", package: "clt-logger"),
120+
.product(name: "Logging", package: "swift-log"),
121+
.product(name: "StreamReader", package: "stream-reader"),
121122
], swiftSettings: noSwiftSettings))
122123

123124
return res

Sources/ManualTests/main.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@ import SystemPackage
66
#endif
77

88
import CLTLogger
9-
import CMacroExports
109
import Logging
1110
import ProcessInvocation
11+
import StreamReader
1212

1313
/* Old remark (fixed not):
1414
* Launch the manual test like so:
1515
* echo yolo | ./.build/debug/ManualTests
1616
* For some reasons actually typing stuff in the Terminal (no “echo yolo |”) does not work
17-
* (probably some buffered read as stdin is not a tty for cat, or some other sh*t I don’t really get).
18-
*
19-
* Everything seems to work correctly when not sending file descriptors.
20-
* If we do send fds, I’m not sure… */
17+
* (probably some buffered read as stdin is not a tty for cat, or some other sh*t I don’t really get). */
2118

2219

2320

@@ -34,7 +31,6 @@ let logger = Logger(label: "com.xcode-actions.manual-process-invocation-tests")
3431
//let ptr = UnsafeMutableRawPointer.allocate(byteCount: 42, alignment: 1)
3532
//print("yolo: \(read(stdin, ptr, 1))")
3633

37-
//import StreamReader
3834
//let reader = FileHandleReader(stream: FileHandle.standardInput, bufferSize: 30, bufferSizeIncrement: 30, underlyingStreamReadSizeLimit: 1)
3935
//while let line = try reader.readLine()?.line {
4036
// print(line.reduce("", { $0 + String(format: "%02x", $1) }))
@@ -68,9 +64,21 @@ do {
6864
// """, stdinRedirect: .none/*, fileDescriptorsToSend: [fd: fd]*/
6965
// )
7066
// let pi = ProcessInvocation("/bin/cat", stdinRedirect: .send(Data("yo".utf8)))
71-
let pi = ProcessInvocation("/bin/cat", stdinRedirect: .none(), stdoutRedirect: .none, stderrRedirect: .none, fileDescriptorsToSend: [fd: fd])
67+
// let pi = ProcessInvocation("/bin/cat", stdinRedirect: .none(), stdoutRedirect: .none, stderrRedirect: .none, fileDescriptorsToSend: [fd: fd])
68+
// for try await line in pi {
69+
// print("From cat: \(line.strLineOrHex())")
70+
// }
71+
72+
/* Try to read one line from ProcessInvocation and then one using StreamReader. */
73+
let pi = ProcessInvocation("head", "-n", "1")
7274
for try await line in pi {
73-
print("From cat: \(line.strLineOrHex())")
75+
print("From head: \(line.strLineOrHex())")
76+
}
77+
let reader = FileDescriptorReader(stream: FileDescriptor.standardInput, bufferSize: 32, bufferSizeIncrement: 16, underlyingStreamReadSizeLimit: 1)
78+
if let line = try reader.readLine()?.line {
79+
print("From reader: \(String(data: line, encoding: .utf8) ?? line.reduce("", { $0 + String(format: "%02x", $1) }))")
80+
} else {
81+
print("Failed reading line from stream reader.")
7482
}
7583
} catch {
7684
print("Failed running the process: \(error)")

0 commit comments

Comments
 (0)