diff --git a/CHANGELOG.md b/CHANGELOG.md index fae9277..045a8bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/babashka/process.cljc b/src/babashka/process.cljc index ab7d897..83e5f65 100644 --- a/src/babashka/process.cljc +++ b/src/babashka/process.cljc @@ -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]) @@ -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 diff --git a/test/babashka/process_test.cljc b/test/babashka/process_test.cljc index 2928569..1e9e2b5 100644 --- a/test/babashka/process_test.cljc +++ b/test/babashka/process_test.cljc @@ -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)) @@ -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)] @@ -376,7 +386,11 @@ (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) @@ -384,7 +398,11 @@ (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