Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #148: accept Path in :out, :err and :in #149

Merged
merged 4 commits into from
Feb 27, 2024
Merged
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
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
25 changes: 17 additions & 8 deletions src/babashka/process.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -250,18 +250,20 @@
extra-env (add-env extra-env))]
(case out
:inherit (.redirectOutput pb ProcessBuilder$Redirect/INHERIT)
:write (.redirectOutput pb (ProcessBuilder$Redirect/to (io/file out-file)))
:append (.redirectOutput pb (ProcessBuilder$Redirect/appendTo (io/file out-file)))
:write (.redirectOutput pb (ProcessBuilder$Redirect/to (io/file (str out-file))))
:append (.redirectOutput pb (ProcessBuilder$Redirect/appendTo (io/file (str out-file))))
nil)
(case err
:out (.redirectErrorStream pb true)
:inherit (.redirectError pb ProcessBuilder$Redirect/INHERIT)
:write (.redirectError pb (ProcessBuilder$Redirect/to (io/file err-file)))
:append (.redirectError pb (ProcessBuilder$Redirect/appendTo (io/file err-file)))
:write (.redirectError pb (ProcessBuilder$Redirect/to (io/file (str err-file))))
:append (.redirectError pb (ProcessBuilder$Redirect/appendTo (io/file (str err-file))))
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: " (.getMessage e)))))))
(let [;; bb doesn't support map->Process at the moment
res (->Process proc
nil
Expand Down
32 changes: 25 additions & 7 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 Expand Up @@ -376,15 +386,23 @@
(is (= (u/ols "hello\n") (slurp out)))
@(p/process (format "%s %s :out goodbye" bb u/wd)
{:out :append :out-file out})
(is (= (u/ols "hello\ngoodbye\n") (slurp out)))))
(is (= (u/ols "hello\ngoodbye\n") (slurp out)))
(testing "out path"
@(p/process (format "%s %s :out goodbye" bb u/wd)
{:out :append :out-file (fs/path out)})
(is (= (u/ols "hello\ngoodbye\ngoodbye\n") (slurp out))))))
(fs/with-temp-dir [tmp {}]
(let [out (fs/file tmp "err.txt")]
@(p/process (format "%s %s :err 'err,hello'" bb u/wd)
{:err :write :err-file out})
(is (= (u/ols "err,hello\n") (slurp out)))
@(p/process (format "%s %s :err 'grrr-oodbye'" bb u/wd)
{:err :append :err-file out})
(is (= (u/ols "err,hello\ngrrr-oodbye\n") (slurp out)))))))
(is (= (u/ols "err,hello\ngrrr-oodbye\n") (slurp out)))
(testing "err path"
@(p/process (format "%s %s :err goodbye" bb u/wd)
{:err :append :err-file (fs/path out)})
(is (= (u/ols "err,hello\ngrrr-oodbye\ngoodbye\n") (slurp out))))))))

(deftest pprint-test
;; #?(:bb nil ;; in bb we already required the babashka.process.pprint namespace
Expand Down
Loading