From db9cea77f3c9e3719014c24f4ebfb192ce8d6bba Mon Sep 17 00:00:00 2001 From: Joseph Wilk Date: Fri, 5 Apr 2013 16:19:16 +0200 Subject: [PATCH] Junit formatter for Midje. --- src/midje/emission/plugins/junit.clj | 88 +++++++++++++++++++++++++ test/midje/emission/plugins/t_junit.clj | 53 +++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 src/midje/emission/plugins/junit.clj create mode 100644 test/midje/emission/plugins/t_junit.clj diff --git a/src/midje/emission/plugins/junit.clj b/src/midje/emission/plugins/junit.clj new file mode 100644 index 000000000..b46e31d2b --- /dev/null +++ b/src/midje/emission/plugins/junit.clj @@ -0,0 +1,88 @@ +(ns ^{:doc "JUnit formatter for Midje output"} + midje.emission.plugins.junit + (:use + [midje.emission.util]) + (:require [midje.data.fact :as fact] + [midje.emission.state :as state] + [midje.emission.plugins.silence :as silence] + [midje.emission.plugins.default-failure-lines :as lines] + [clojure.string :as str] + [clojure.xml :as xml :only [emit-element]])) + +(def report-file "report.xml") + +(defn log-fn [] + (fn [text] (spit report-file text :append true))) + +(defn- log [string] + (let [log-fn (log-fn)] + (log-fn string))) + +(defn- reset-log [] + (spit report-file "")) + +(defn def-fact-cache [] + (defonce last-fact (atom {}))) + +(defn- fact-name [fact] + (or (fact/name fact) + (fact/description fact) + (str (fact/file fact) ":" (fact/line fact)))) + +(defn pass [] + (log + (with-out-str + (xml/emit-element @last-fact)))) + +(defn- testcase-with-failure [failure-map] + (let [testcase @last-fact + failure-content (str "") + fail-type (:type failure-map) + fail-element {:tag :failure + :content [failure-content] + :attrs {:type fail-type}} + testcase-with-failure (assoc testcase :content [fail-element])] + testcase-with-failure)) + +(defn escape [s] + (if s + (str/escape s {\' "\\'"}) + "")) + +(defn fail [failure-map] + (let [testcase (testcase-with-failure failure-map)] + ; FIXME: currently there is a bug in midje that prevents us emitting this map as xml + ;(xml/emit-element testcase) + + (log (str "\n")) + (log (str "")) + (log (-> testcase :content first :content :first)) + (log "\n") + (log ""))) + +(defn starting-to-check-fact [fact] + (let [fact-namespace (str (fact/namespace fact)) + fact-name (fact-name fact)] + (reset! last-fact {:tag :testcase + :attrs {:classname (escape fact-namespace) :name (escape fact-name)}}))) + +(defn starting-fact-stream [] + (def-fact-cache) + (reset-log) + (log "")) + +(defn finishing-fact-stream [midje-counters clojure-test-map] + (log "")) + +(defn make-map [& keys] + (zipmap keys + (map #(ns-resolve *ns* (symbol (name %))) keys))) + +(def emission-map (merge silence/emission-map + (make-map :fail + :pass + :starting-fact-stream + :finishing-fact-stream + :starting-to-check-fact))) + +(state/install-emission-map emission-map) diff --git a/test/midje/emission/plugins/t_junit.clj b/test/midje/emission/plugins/t_junit.clj new file mode 100644 index 000000000..be0138fd5 --- /dev/null +++ b/test/midje/emission/plugins/t_junit.clj @@ -0,0 +1,53 @@ +(ns midje.emission.plugins.t-junit + (:use [midje sweet util test-util]) + (:require [midje.emission.plugins.junit :as plugin] + [midje.config :as config] + [midje.emission.plugins.default-failure-lines :as failure-lines])) + +(defn innocuously [key & args] + (config/with-augmented-config {:emitter 'midje.emission.plugins.junit + :print-level :print-facts} + (captured-output (apply (key plugin/emission-map) args)))) + +(def test-fact + (with-meta (fn[]) {:midje/name "named" :midje/description "desc" :midje/namespace "blah"})) + +(def test-failure-map + {:type :some-prerequisites-were-called-the-wrong-number-of-times, + :namespace "midje.emission.plugins.t-junit"}) + +(fact "starting a fact stream opens a " + (innocuously :starting-fact-stream) => (contains "") + (provided + (plugin/log-fn) => #(println %))) + +(fact "closing a fact stream closes " + (plugin/def-fact-cache) + + (innocuously :finishing-fact-stream {} {}) => (contains "") + (provided + (plugin/log-fn) => #(println %))) + +(fact "pass produces a tag" + (plugin/def-fact-cache) + (plugin/starting-to-check-fact test-fact) + + (innocuously :pass) => (contains "") + (provided + (plugin/log-fn) => #(println %))) + +(fact "failure produces a tag" + (plugin/def-fact-cache) + (plugin/starting-to-check-fact test-fact) + + (innocuously :fail test-failure-map) => (contains "") + (provided + (plugin/log-fn) => #(println %))) + +(fact "failure also produces a tag" + (plugin/def-fact-cache) + (plugin/starting-to-check-fact test-fact) + + (innocuously :fail test-failure-map) => (contains "") + (provided + (plugin/log-fn) => #(println %)))