Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/babashka/process.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,17 @@
[p]
(.isAlive ^java.lang.Process (:proc p)))

(defn close-stdin!
"Closes the stdin stream of `proc`, a process map returned by `process`
or `process*`, sending EOF to the child. A missing or already-closed
stream is a no-op. Returns `proc`, or nil when `proc` is nil."
[proc]
(when-let [in (:in proc)]
(try
(.close ^java.io.Closeable in)
(catch Exception _ nil)))
proc)

#?(:bb nil
:clj
(when (contains? (loaded-libs) 'clojure.pprint) ;; pprint was already loaded, e.g. by nREPL
Expand Down
11 changes: 11 additions & 0 deletions test/babashka/process_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,17 @@
@res
(is (false? (p/alive? res))))))

(deftest close-stdin-test
(testing "nil and missing streams are no-ops"
(is (nil? (p/close-stdin! nil)))
(is (= {} (p/close-stdin! {}))))
(when-let [bb (u/find-bb)]
(let [res (process [bb u/wd ":echo"] {:in :pipe :out :string})]
(is (identical? res (p/close-stdin! res)))
(is (identical? res (p/close-stdin! res)) "closing again is a no-op")
(is (zero? (:exit @res)))
(is (= "" (:out @res))))))

(deftest byte-array-out-test
(when-let [bb (u/find-bb)]
(let [ba (byte-array (range 1000))
Expand Down
Loading