Skip to content

Commit

Permalink
Fix #148: accept Path in :out, :err and :in
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Feb 26, 2024
1 parent d1ee682 commit 8623def
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Clojure library for shelling out / spawning sub-processes

- [#123](https://github.com/babashka/process/issues/123): `exec` now converts `:env` and `:extra-env` keywords ([@lread](https://github.com/lread))
- [#140](https://github.com/babashka/process/issues/140): accept `java.nio.file.Path` as `:dir` argument
- [#148](https://github.com/babashka/process/issues/148): accept `Path` in `:out`, `:err` and `:in`

## 0.5.21 (2023-05-18)

Expand Down
17 changes: 13 additions & 4 deletions src/babashka/process.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@
nil)
(case in
:inherit (.redirectInput pb ProcessBuilder$Redirect/INHERIT)
nil)
(when (or (instance? java.io.File in)
(instance? java.nio.file.Path in))
(.redirectInput pb (fs/file in))))
pb)))

(defrecord ProcessBuilder [pb opts prev])
Expand Down Expand Up @@ -379,9 +381,16 @@
(future (copy stderr err err-enc))
stderr)]
;; wrap in futures, see https://github.com/clojure/clojure/commit/7def88afe28221ad78f8d045ddbd87b5230cb03e
(when (and in (not (identical? :inherit in)))
(future (with-open [stdin stdin] ;; needed to close stdin after writing
(io/copy in stdin :encoding in-enc))))
(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: "(ex-message e)))))))
(let [;; bb doesn't support map->Process at the moment
res (->Process proc
nil
Expand Down
20 changes: 15 additions & 5 deletions test/babashka/process_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
[clojure.string :as str]
[clojure.test :as t :refer [deftest is testing use-fixtures]]))

(defmethod clojure.test/report :begin-test-var [m]
(println "===" (-> m :var meta :name))
(println))

(defn print-env [f]
(u/print-test-env)
(println "- testing clojure version:" (clojure-version))
Expand Down Expand Up @@ -111,11 +115,17 @@

(deftest process-copy-input-from-string-test
(when-let [bb (u/find-bb)]
(let [proc (process [(symbol bb) (symbol u/wd) ':upper] {:in "foo"})
out (:out proc)
ret (:exit @proc)]
(is (= 0 ret))
(is (= (u/ols "FOO\n") (slurp out))))))
(doseq [inf [identity fs/file fs/path]]
(let [tmp-file (doto (fs/create-temp-file)
fs/delete-on-exit)]
(spit (fs/file tmp-file) "foo")
(let [proc (process [(symbol bb) (symbol u/wd) ':upper] {:in (if (= inf identity)
"foo"
(inf tmp-file))})
out (:out proc)
ret (:exit @proc)]
(is (= 0 ret))
(is (= (u/ols "FOO\n") (slurp out))))))))

(deftest process-redirect-err-out-test
(when-let [bb (u/find-bb)]
Expand Down

0 comments on commit 8623def

Please sign in to comment.