Skip to content

Commit dffe774

Browse files
committed
Process: Setting stdout or stderr to nil or nullDevice set stdin instead.
- If .standardOutput or .standardError was set to nil or FileHandle.nullDevice, stdin was set to /dev/null instead.
1 parent 3592d8a commit dffe774

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

Foundation/Process.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ open class Process: NSObject {
800800
// nil or NullDevice map to /dev/null
801801
case let handle as FileHandle where handle === FileHandle._nulldeviceFileHandle: fallthrough
802802
case .none:
803-
adddup2[STDIN_FILENO] = try devNullFd()
803+
adddup2[STDOUT_FILENO] = try devNullFd()
804804

805805
// No need to dup stdout to stdout
806806
case let handle as FileHandle where handle === FileHandle._stdoutFileHandle: break
@@ -819,7 +819,7 @@ open class Process: NSObject {
819819
// nil or NullDevice map to /dev/null
820820
case let handle as FileHandle where handle === FileHandle._nulldeviceFileHandle: fallthrough
821821
case .none:
822-
adddup2[STDIN_FILENO] = try devNullFd()
822+
adddup2[STDERR_FILENO] = try devNullFd()
823823

824824
// No need to dup stderr to stderr
825825
case let handle as FileHandle where handle === FileHandle._stderrFileHandle: break

TestFoundation/TestProcess.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,15 @@ class TestProcess : XCTestCase {
8080

8181
let inputPipe = Pipe()
8282
process.standardInput = inputPipe
83+
process.standardError = FileHandle.nullDevice
8384
try process.run()
84-
inputPipe.fileHandleForWriting.write("Hello, 🐶.\n".data(using: .utf8)!)
85+
let msg = try XCTUnwrap("Hello, 🐶.\n".data(using: .utf8))
86+
do {
87+
try inputPipe.fileHandleForWriting.write(contentsOf: msg)
88+
} catch {
89+
XCTFail("Cant write to pipe: \(error)")
90+
return
91+
}
8592

8693
// Close the input pipe to send EOF to cat.
8794
inputPipe.fileHandleForWriting.closeFile()

0 commit comments

Comments
 (0)