From 9287bbedc7a3bee4a046d277d585f1900c70fdcc Mon Sep 17 00:00:00 2001 From: Alex Hall <534841+alexhall@users.noreply.github.com> Date: Fri, 13 Sep 2024 15:07:32 -0400 Subject: [PATCH 1/2] Make bypassing capture context-aware --- src/kaocha/plugin/capture_output.cljc | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/kaocha/plugin/capture_output.cljc b/src/kaocha/plugin/capture_output.cljc index 0dfb63af..807ca1cd 100644 --- a/src/kaocha/plugin/capture_output.cljc +++ b/src/kaocha/plugin/capture_output.cljc @@ -13,6 +13,8 @@ (def ^:dynamic *test-buffer* nil) +(def ^:dynamic *previous-writers* nil) + (def active-buffers (atom #{})) (defn make-buffer [] @@ -24,11 +26,11 @@ (defmacro with-test-buffer [buffer & body] `(try - (swap! active-buffers conj ~buffer) - (binding [*test-buffer* ~buffer] - ~@body) - (finally - (swap! active-buffers disj ~buffer)))) + (swap! active-buffers conj ~buffer) + (binding [*test-buffer* ~buffer] + ~@body) + (finally + (swap! active-buffers disj ~buffer)))) (defn- doto-capture-buffer [f] (if *test-buffer* @@ -54,6 +56,7 @@ (System/setOut new-stream) (System/setErr new-stream) {:captured-writer new-writer + :previous-writers {:out *out*, :err *err*} :old-system-out old-out :old-system-err old-err})) @@ -65,7 +68,9 @@ `(let [context# (init-capture) writer# (:captured-writer context#)] (try - (binding [*out* writer#, *err* writer#] + (binding [*out* writer# + *err* writer# + *previous-writers* (:previous-writers context#)] (with-redefs [*out* writer#, *err* writer#] ~@body)) (finally @@ -110,6 +115,7 @@ "Bypass output-capturing within this code block, so any print statements go to the process out/err streams without being captured." [& body] - `(binding [*out* (io/writer (PrintStream. (FileOutputStream. java.io.FileDescriptor/out))) - *err* (io/writer (PrintStream. (FileOutputStream. java.io.FileDescriptor/err)))] - ~@body))) + `(let [{out# :out, err# :err} (or *previous-writers* {:out *out*, :err *err*})] + (binding [*out* out# + *err* err#] + ~@body)))) From 1a7a7adb9b1949f384ef3b4f41c455d9b0b9ef9e Mon Sep 17 00:00:00 2001 From: Alex Hall <534841+alexhall@users.noreply.github.com> Date: Fri, 13 Sep 2024 15:36:12 -0400 Subject: [PATCH 2/2] Handle nested with-capture calls --- src/kaocha/plugin/capture_output.cljc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/kaocha/plugin/capture_output.cljc b/src/kaocha/plugin/capture_output.cljc index 807ca1cd..f3aa7139 100644 --- a/src/kaocha/plugin/capture_output.cljc +++ b/src/kaocha/plugin/capture_output.cljc @@ -1,13 +1,13 @@ (ns kaocha.plugin.capture-output (:require - [clojure.java.io :as io] - [kaocha.hierarchy :as hierarchy] - [kaocha.plugin :as plugin :refer [defplugin]]) - (:import (java.io ByteArrayOutputStream - FileOutputStream ;; false positive - OutputStream - PrintStream - PrintWriter))) + [kaocha.hierarchy :as hierarchy] + [kaocha.plugin :as plugin :refer [defplugin]]) + (:import + (java.io + ByteArrayOutputStream + OutputStream + PrintStream + PrintWriter))) ;; Many props to eftest for much of this code @@ -70,7 +70,7 @@ (try (binding [*out* writer# *err* writer# - *previous-writers* (:previous-writers context#)] + *previous-writers* (or *previous-writers* (:previous-writers context#))] (with-redefs [*out* writer#, *err* writer#] ~@body)) (finally