 
 ;; test helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
+(defn command-str
+  "Given a seq of command segments, the input to the runboot task,
+  return its string representation. Useful for Java interop, for example
+  it can be key in a HashMap."
+  [command-seq]
+  (string/join " " command-seq))
+
 (defn initial-sync-map
   "Generates the (mutable) sync map necessary for the test task.
   This map will be modified during parallel testing so make sure the

    [boot.util               :as util]
    [boot.tmpdir             :as tmpd])
  (:import java.lang.InterruptedException
           [java.util HashMap
                      Collections
                      concurrent.ConcurrentHashMap
                      concurrent.CountDownLatch]))


    (.countDown (get data "start-latch"))))

(defn- print-summary!
  "Print out (info) the summary. Note that it has to be in Clojure form
  already, see clojurize-summary."
  [summary]
  ;; (util/dbug "%s" (into {} summary))
  (util/info "Ran %s tests, %s assertions, %s failures, %s errors.\n"
             (get summary :test 0)
             (+ (get summary :pass 0) (get summary :fail 0) (get summary :error 0))
             (get summary :fail 0)
             (get summary :error 0)))

(defn- clojurize-summaries
  "Transform Java to Clojure summaries, for instance converting keys to
  keywords. Returns a map of summaries indexed by command string (the
  boot in boot command executed, see command-str)."
  [^Map java-summaries]
  (assert (every? (partial instance? Map) (vals java-summaries)))
  (zipmap (keys java-summaries)
          (map (comp walk/keywordize-keys (partial into {})) (vals java-summaries))))

(comment
  (def m (doto (HashMap.)
      (.put "a" {"test" 2 "pass" 1 "error" 1})
      (.put "b" {})
      (.put "c" {"test" 1 "fail" 1})))
  (print-summary! (second (vals (clojurize-summaries m))))
  (print-summary! (merge-summaries (vals (clojurize-summaries m)))))

(defn- merge-summaries
  "Merge summaries. Note that it has to be in Clojure form
  already, see clojurize-summary."
  [summaries]
  (assert (every? map? summaries))
  (apply merge-with + summaries))

(core/deftask test-report
  [d data OBJECT ^:! code "The data for this task"]
  (fn [next-handler]
    (fn [fileset]
      (let [summaries (clojurize-summaries (get data "test-summaries"))]
        ;; Individual report
        (doseq [[command-str summary] summaries]
          (util/info "\n* boot %s\n" command-str)
          (print-summary! summary))
        ;; Summary
        (util/info "\nSummary\n")
        (print-summary! (merge-summaries (vals summaries)))))))

(defn done!
  "Signal that this pod is shutting down"
