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
25 changes: 15 additions & 10 deletions src/babashka/process.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,18 @@
(io/copy in out :encoding encoding)
(post-fn out)))

(defn- copy-input [in ^java.io.OutputStream stdin encoding]
(when (and in
(not (or (instance? java.io.File in)
(instance? java.nio.file.Path in)))
(not (keyword? in)))
(future
(try (with-open [stdin stdin]
(io/copy in stdin :encoding encoding))
(catch Exception e
(binding [*out* *err*]
(println "ERROR while copying :in option: " (.getMessage e))))))))

(defn process*
"Same as with `process` but called with parsed arguments (the result from `parse-args`)"
[{:keys [prev cmd opts]}]
Expand Down Expand Up @@ -409,16 +421,7 @@
(future (copy stderr err err-enc))
stderr)]
;; wrap in futures, see https://github.com/clojure/clojure/commit/7def88afe28221ad78f8d045ddbd87b5230cb03e
(when (and in
(not (or (instance? java.io.File in)
(instance? java.nio.file.Path in)))
(not (keyword? in)))
(future
(try (with-open [stdin stdin] ;; needed to close stdin after writing
(io/copy in stdin :encoding in-enc))
(catch Exception e
(binding [*out* *err*]
(println "ERROR while copying :in option: " (.getMessage e)))))))
(copy-input in stdin in-enc)
(let [;; bb doesn't support map->Process at the moment
res (->Process proc
nil
Expand Down Expand Up @@ -547,6 +550,8 @@
cmd (.command ^java.lang.ProcessBuilder pb)
new-proc (proc->Process proc cmd prev)
new-procs (conj procs new-proc)]
(when-not prev
(copy-input (:in opts) (:in new-proc) (:in-enc opts)))
(when shutdown
(-> (Runtime/getRuntime)
(.addShutdownHook (Thread.
Expand Down
42 changes: 42 additions & 0 deletions test/babashka/process_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,48 @@
(is (= (u/ols "HELLO\n") (slurp (:out chain))))
(is (= [[resolved-bb u/wd ":out" "hello"]
[resolved-bb u/wd ":upper"]] (map :cmd (pipeline chain))))))))
(deftest pipeline-input-test
(when-let [bb (u/find-bb)]
(let [text "hello \u03bb"
bytes (.getBytes text "UTF-16LE")
echo [bb u/wd ":echo"]]
(doseq [[input expected] [[text bytes] [bytes bytes]
[(java.io.ByteArrayInputStream. bytes) bytes]
["" (byte-array 0)]]]
(let [[source sink] (pipeline (pb echo {:in input :in-enc "UTF-16LE"})
(pb echo))]
(try
(let [finished (.waitFor ^Process (:proc sink) 5 java.util.concurrent.TimeUnit/SECONDS)]
(is finished "the first process receives EOF after its input")
(when finished
(is (zero? (:exit @source)))
(is (zero? (:exit @sink)))
(let [out (java.io.ByteArrayOutputStream.)]
(clojure.java.io/copy (:out sink) out)
(is (= (seq expected) (seq (.toByteArray out)))))))
(finally
(p/destroy source)
(p/destroy sink))))))))
(deftest pipeline-input-redirection-test
(when-let [bb (u/find-bb)]
(let [echo [bb u/wd ":echo"]
file (doto (fs/create-temp-file) fs/delete-on-exit)
text "manual or redirected"]
(spit (fs/file file) text)
(doseq [input [nil (fs/file file) file :inherit]]
(let [cmd (if (= :inherit input) [bb u/wd ":out" text] echo)
[a b] (pipeline (pb cmd {:in input}) (pb echo))]
(try
(when (nil? input)
(with-open [writer (io/writer (:in a))] (.write writer text)))
(let [finished (.waitFor ^Process (:proc b) 5 java.util.concurrent.TimeUnit/SECONDS)]
(is finished)
(when finished
(is (= [0 0] [(:exit @a) (:exit @b)]))
(is (= text (str/trim-newline (slurp (:out b)))))))
(finally
(p/destroy a)
(p/destroy b))))))))
(deftest exit-fn-test
(when-let [bb (u/find-bb)]
(let [exit-code (promise)]
Expand Down
Loading