Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
anyfailures-v1.clj
;; Require lein-exec plugin so that we can run this as a standalone
;; script
;; Usage:
;; - 'lein exec anyfailures.clj' (looks for tests under ./test/system/results/
;; - 'lein exec anyfailures.clj foo' (looks for tests under ./foo/
(require 'leiningen.exec)
;; Add and require Enlive so we can parse HTML
(leiningen.exec/deps '[[enlive/enlive "1.1.4"]])
(require '[net.cgrand.enlive-html :as html])
;; Read the input directory from command line arguments
(def input-dir (second *command-line-args*))
;; Read all the files from the user-specified (or default)
;; directory
(def files (file-seq (clojure.java.io/file
(if (nil? input-dir)
"./test/system/results"
input-dir))))
;; Initialize the output file by writing an empty string to it
;; (so that any previous results are cleared)
(spit "failed-tests.txt" "")
;; Go through the files. Filter only actual files since the file-seq
;; will contain directories also
(doseq [f (filter #(.isFile %) files)]
;; For each file f
(let [
;; read the file contents with `slurp` (got to love that name)
contents (slurp f)
;; parse the html into an Enlive html-resource data structure
parsed-content (html/html-resource (java.io.StringReader. contents))
;; create a sequence containing the failed test names:
;; the html/select function finds every <a> element inside
;; a <td> element inside a <tr> element with a class "status_failed"
;; in the parsed-content data structure
;;
;; We get a sequence of maps each representing and <a> element, whose
;; text content is under a key :content so we call
;; (map :content seq-of-maps-with-key-:content) to get the contents of
;; each <a> element.
;;
;; Finally, because the contents of each element is in a list already,
;; we call flatten on the sequence of lists to get just a single list
;; with the content strings as elements
failed-tests (flatten
(map :content
(html/select
parsed-content [:tr.status_failed :td :a])))]
;; Then for each failed test case name, we write the name to the file
;; failed-tests.txt using spit (opposite of slurp..) with the :append true
;; flag so that it appends to an existing file rather than always creating
;; the file from scratch
(doseq [f failed-tests]
(spit "failed-tests.txt" (str f "\n") :append true))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment