Skip to content

Commit 2de983e

Browse files
committed
Add a manual tests target (for stdin redirects)
Maybe later we’ll convert this to actual tests, but right now, no.
1 parent 1c5a46c commit 2de983e

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

Package.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ let package = Package(
113113
res.append(.target(name: "CGNUSourceExportsForTests", swiftSettings: noSwiftSettings))
114114
}
115115

116+
/* Some manual tests for stdin redirect behavior. */
117+
res.append(.executableTarget(name: "ManualTests", dependencies: ["ProcessInvocation"], swiftSettings: noSwiftSettings))
118+
116119
return res
117120
}()
118121
)

Sources/ManualTests/main.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Foundation
2+
#if canImport(System)
3+
import System
4+
#else
5+
import SystemPackage
6+
#endif
7+
8+
import ProcessInvocation
9+
10+
11+
12+
/* Launch like so:
13+
* echo yolo | ./.build/debug/ManualTests
14+
* For some reasons actually typing stuff in the Terminal does not work (probably some buffered read as stdin is not a tty for cat, or some other sh*t I don’t really get).
15+
*
16+
* Apart from the glitch above, it seems to work correctly when not sending file descriptors.
17+
* If we do send fds, not much works… */
18+
//let fd = FileDescriptor(rawValue: FileHandle(forReadingAtPath: "/dev/null")!.fileDescriptor)
19+
do {
20+
for try await line in ProcessInvocation("/bin/cat", stdinRedirect: .none/*, fileDescriptorsToSend: [fd: fd]*/) {
21+
print("From cat: \(line.strLineOrHex())")
22+
}
23+
} catch {
24+
print("Failed running the process: \(error)")
25+
}

Sources/ProcessInvocation/ProcessInvocation.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ public struct ProcessInvocation : AsyncSequence {
566566
let actualExecutablePath: FilePath
567567
if fileDescriptorsToSend.isEmpty {
568568
switch stdinRedirect {
569-
case .none: (/*nop*/)
569+
case .none: p.standardInput = FileHandle.standardInput
570570
case .fromNull: p.standardInput = nil
571571
case .sendFromReader(let reader):
572572
let fd = try Self.readFdOfPipeForStreaming(dataFromReader: reader, maxCacheSize: 32 * 1024 * 1024)
@@ -633,14 +633,15 @@ public struct ProcessInvocation : AsyncSequence {
633633
/* We must send the modified stdin in the list of file descriptors to send! */
634634
switch stdinRedirect {
635635
case .none: fileDescriptorsToSend[.standardInput] = .standardInput
636-
case .fromNull: (/*nop*/)
636+
case .fromNull:
637+
let fd = try Self.readFdOfPipeForStreaming(dataFromReader: DataReader(data: Data()), maxCacheSize: 1)
638+
fileDescriptorsToSend[.standardInput] = fd
637639
case .sendFromReader(let reader):
638640
let fd = try Self.readFdOfPipeForStreaming(dataFromReader: reader, maxCacheSize: 32 * 1024 * 1024)
639641
fileDescriptorsToSend[.standardInput] = fd
640-
// fdsToCloseAfterRun.insert(fd)
641642
case .fromFd(let fd, let shouldClose):
643+
assert(shouldClose, "Not giving ownership of fd to send is not supported (from stdin redirect).")
642644
fileDescriptorsToSend[.standardInput] = fd
643-
// if shouldClose {fdsToCloseAfterRun.insert(fd)}
644645
}
645646
}
646647

0 commit comments

Comments
 (0)