Skip to content

Commit fc0eb0b

Browse files
lreadborkdude
andauthored
Support redirecting stderr to stdout (#115)
Fixes #113 Co-authored-by: Michiel Borkent <[email protected]>
1 parent 4238230 commit fc0eb0b

File tree

6 files changed

+43
-2
lines changed

6 files changed

+43
-2
lines changed

.circleci/config.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ jobs:
2525
wget https://download.clojure.org/install/linux-install-1.10.3.1040.sh
2626
chmod +x linux-install-1.10.3.1040.sh
2727
sudo ./linux-install-1.10.3.1040.sh
28+
- run:
29+
name: Install Babashka
30+
command: |
31+
sudo bash < <(curl -s https://raw.githubusercontent.com/babashka/babashka/master/install)
2832
- run:
2933
name: Run JVM tests
3034
command: |
@@ -50,6 +54,10 @@ jobs:
5054
wget https://download.clojure.org/install/linux-install-1.10.3.1040.sh
5155
chmod +x linux-install-1.10.3.1040.sh
5256
sudo ./linux-install-1.10.3.1040.sh
57+
- run:
58+
name: Install Babashka
59+
command: |
60+
sudo bash < <(curl -s https://raw.githubusercontent.com/babashka/babashka/master/install)
5361
- run:
5462
name: Run JVM tests
5563
command: |

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ jobs:
2929
restore-keys: "${{ runner.os }}-deps-"
3030

3131
- name: Setup Clojure
32-
uses: DeLaGuardo/setup-clojure@3.6
32+
uses: DeLaGuardo/setup-clojure@10.3
3333
with:
3434
cli: 1.10.3.1040
35+
bb: 'latest'
3536

3637
- name: Run tests not Windows
3738
if: ${{ matrix.os != 'windows-latest' }}

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Clojure library for shelling out / spawning sub-processes
55

66
## Unreleased
77

8-
- Fix [#112](https://github.com/babashka/process/issues/112): pre-start-fn in exec
8+
- [#113](https://github.com/babashka/process/issues/113): Support redirecting stderr to stdout ([@lread](https://github.com/lread))
9+
- [#112](https://github.com/babashka/process/issues/112): Support `:pre-start-fn` in exec
910

1011
## 0.4.16
1112

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ user=> (-> (shell {:out :string} "ls -la") :out str/split-lines first)
113113
"total 144"
114114
```
115115

116+
To also capture stderr to a `:string`, add in the `:err :string` option:
117+
118+
``` clojure
119+
user=> (-> (shell {:out :string :err :string} "git conpig user.name")
120+
(select-keys [:out :err]))
121+
{:out "borkdude\n", :err "WARNING: You called a Git command named 'conpig', which does not exist.\nContinuing in -1.1 seconds, assuming that you meant 'config'.\n"}
122+
```
123+
124+
To redirect stderr to stdout specify the `:err :out` option:
125+
126+
``` clojure
127+
user=> (-> (shell {:out :string :err :out} "git conpig user.name") :out)
128+
"WARNING: You called a Git command named 'conpig', which does not exist.\nContinuing in -1.1 seconds, assuming that you meant 'config'.\nborkdude\n"
129+
```
130+
116131
To change the working directory, use the `:dir` option:
117132

118133
``` clojure

src/babashka/process.cljc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@
251251
:append (.redirectOutput pb (ProcessBuilder$Redirect/appendTo (io/file out-file)))
252252
nil)
253253
(case err
254+
:out (.redirectErrorStream pb true)
254255
:inherit (.redirectError pb ProcessBuilder$Redirect/INHERIT)
255256
:write (.redirectError pb (ProcessBuilder$Redirect/to (io/file err-file)))
256257
:append (.redirectError pb (ProcessBuilder$Redirect/appendTo (io/file err-file)))
@@ -421,6 +422,7 @@
421422
The `:out` and `:err` options support `:string` for writing to a string
422423
output. You will need to `deref` the process before accessing the string
423424
via the process's `:out`.
425+
To redirect `:err` to `:out`, specify `:err :out`.
424426
For writing output to a file, you can set `:out` and `:err` to a `java.io.File` object, or a keyword:
425427
- `:write` + an additional `:out-file`/`:err-file` + file to write to the file.
426428
- `:append` + an additional `:out-file`/`:err-file` + file to append to the file.

test/babashka/process_test.cljc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@
9191
ret (:exit @proc)]
9292
(is (= 0 ret))
9393
(is (= "foo" (slurp out)))))
94+
(testing "redirect :err to :out"
95+
(let [test-cmd "bb -cp '' -e '(println :to-stdout)(binding [*out* *err*] (println :to-stderr))'"]
96+
(testing "baseline"
97+
(let [res @(process {:out :string :err :string} test-cmd)]
98+
(is (= ":to-stdout\n" (:out res)))
99+
(is (= ":to-stderr\n" (:err res)))))
100+
(testing "redirect"
101+
(let [res @(process {:out :string :err :out} test-cmd)
102+
out-string (:out res)
103+
err-null-input-stream (:err res)]
104+
(is (= ":to-stdout\n:to-stderr\n" out-string))
105+
(is (instance? java.io.InputStream err-null-input-stream))
106+
(is (= 0 (.available err-null-input-stream)))
107+
(is (= -1 (.read err-null-input-stream)))))))
94108
(testing "copy output to *out*"
95109
(let [s (with-out-str
96110
@(process '[cat] {:in "foo" :out *out*}))]

0 commit comments

Comments
 (0)