Created
January 27, 2017 22:41
-
-
Save Deraen/bcee211411a1154ed5437f88528b8c35 to your computer and use it in GitHub Desktop.
Proof of Concept, catch test output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defonce ^:private log-buffer (atom nil)) | |
(defn get-log-message [] | |
(let [message (first @log-buffer)] | |
(swap! log-buffer #(vec (rest %))) | |
message)) | |
(defn catch-logging [f] | |
(reset! log-buffer nil) | |
(let [printer (java.io.PrintStream. | |
(proxy [java.io.ByteArrayOutputStream] [] | |
(flush [] | |
; deal with reflection in proxy-super | |
(let [^java.io.ByteArrayOutputStream this this] | |
(proxy-super flush) | |
(let [message (.trim (.toString this))] | |
(proxy-super reset) | |
(if (> (.length message) 0) | |
(swap! log-buffer conj message)))))) | |
true) | |
orig-out System/out] | |
(System/setOut printer) | |
(try | |
(f) | |
(finally | |
(System/setOut orig-out))))) | |
(test/use-fixtures :each catch-logging) | |
(deftest foo-test | |
(log/infof "Hello world") | |
(is (re-find #"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} \+\d{4} \[[^\]]*\] INFO \[lokit.core-test\] - Hello world$" (get-log-message)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment