Skip to content

Commit 15021fd

Browse files
committed
xunit preprocessor
Transform nested `testsuite` tags into flattened list of `testuite` tags preserving the information from the hierarchy in the name of the flattened `testsuite` tags.
1 parent 139e344 commit 15021fd

10 files changed

Lines changed: 105 additions & 4 deletions

File tree

.clj-kondo/babashka/fs/config.edn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{:lint-as {babashka.fs/with-temp-dir clojure.core/let}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
{:hooks
3+
{:analyze-call {org.httpkit.server/with-channel httpkit.with-channel/with-channel}}}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(ns httpkit.with-channel
2+
(:require [clj-kondo.hooks-api :as api]))
3+
4+
(defn with-channel [{node :node}]
5+
(let [[request channel & body] (rest (:children node))]
6+
(when-not (and request channel) (throw (ex-info "No request or channel provided" {})))
7+
(when-not (api/token-node? channel) (throw (ex-info "Missing channel argument" {})))
8+
(let [new-node
9+
(api/list-node
10+
(list*
11+
(api/token-node 'let)
12+
(api/vector-node [channel (api/vector-node [])])
13+
request
14+
body))]
15+
16+
{:node new-node})))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{:lint-as
2+
{rewrite-clj.zip/subedit-> clojure.core/->
3+
rewrite-clj.zip/subedit->> clojure.core/->>
4+
rewrite-clj.zip/edit-> clojure.core/->
5+
rewrite-clj.zip/edit->> clojure.core/->>}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{:hooks {:analyze-call {taoensso.encore/defalias taoensso.encore/defalias}}}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(ns taoensso.encore
2+
(:require
3+
[clj-kondo.hooks-api :as hooks]))
4+
5+
(defn defalias [{:keys [node]}]
6+
(let [[sym-raw src-raw] (rest (:children node))
7+
src (if src-raw src-raw sym-raw)
8+
sym (if src-raw
9+
sym-raw
10+
(symbol (name (hooks/sexpr src))))]
11+
{:node (with-meta
12+
(hooks/list-node
13+
[(hooks/token-node 'def)
14+
(hooks/token-node (hooks/sexpr sym))
15+
(hooks/token-node (hooks/sexpr src))])
16+
(meta src))}))

bb.edn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{:tasks
2+
{dev {:doc "Run dev repl"
3+
:task (clojure "-M:dev:repl")}}}

deps.edn

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
:aliases
1414
{:dev {:extra-paths ["dev"]}
1515
:package {:extra-paths ["resources" "target/cljs/"]}
16-
:nrepl {:extra-deps {nrepl/nrepl {:mvn/version "0.6.0"}}}
16+
:repl {:extra-deps {nrepl/nrepl {:mvn/version "1.0.0"}
17+
cider/cider-nrepl {:mvn/version "0.41.0"}}
18+
:main-opts ["-m" "nrepl.cmdline"
19+
"--middleware" "[cider.nrepl/cider-middleware]"
20+
"--interactive"]}
1721
:uberdeps {:extra-deps {uberdeps/uberdeps {:mvn/version "0.1.8"}}
1822
:main-opts ["-m" "uberdeps.uberjar"]}
1923
:depstar {:extra-deps
2024
{seancorfield/depstar {:mvn/version "1.0.94"}}}
2125
:webassets {:extra-paths ["dev"]}
2226
:reveal {:extra-deps {vlaaad/reveal {:mvn/version "1.3.280"}}
2327
;; optional: preferences
24-
:jvm-opts ["-Dvlaaad.reveal.prefs={:theme,:light}"]}}}
28+
:jvm-opts ["-Dvlaaad.reveal.prefs={:theme,:light}"]}}}

dev/user.clj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
(ns user
22
(:require
33
[clojure.repl :refer [doc source]]
4-
[clojure.pprint :refer [pprint]]))
4+
[clojure.pprint :refer [pprint]]
5+
[practitest-firecracker.parser.core :as parser]))
6+
7+
(comment
8+
9+
(parser/zip-str (slurp "test-data/xunit/robot-framework-globe-example.xml"))
10+
)

src/practitest_firecracker/parser/core.clj

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,42 @@
1919
(defn str-to-number [val]
2020
(if (not (nil? val)) (.parse (NumberFormat/getInstance) val) 0))
2121

22+
(defn has-nested-testsuite? [element]
23+
(some #(= :testsuite (:tag %)) (:content element)))
24+
25+
(defn flatten-testsuite
26+
"xUnit sometimes has hierarchy of `testsuite` tags.
27+
This function will flatten the hierarchy, capturing the parent testsuite names.
28+
For example, if there is a testsuite A which has a child B, which has testcases,
29+
we will leave only B, but change the name attribute to 'A: B'."
30+
[element]
31+
(loop [acc [] children (:content element) prefix (-> element :attrs :name)]
32+
(if (empty? children)
33+
acc
34+
(let [head (first children)
35+
tail (rest children)]
36+
(if (= :testsuite (:tag head))
37+
(let [head (update-in head [:attrs :name] #(str prefix ": " %))]
38+
(if (has-nested-testsuite? head)
39+
(recur (concat acc (flatten-testsuite head)) tail prefix)
40+
(recur (conj acc head) tail prefix)))
41+
(recur (conj acc head) tail prefix))))))
42+
43+
(defn preprocess-xunit
44+
"Support for xUnit.
45+
Since xUnit does not have root testsuites tag, but does have hierarchy of testsuite tags (sometimes),
46+
Here we will wrap it in a fake testsuites tag.
47+
This is done to support the case when `flatten-testsuite` returns a list of the testsuites."
48+
[root]
49+
(if (and (= :testsuite (:tag root))
50+
(has-nested-testsuite? root))
51+
{:tag :testsuites :content (flatten-testsuite root)}
52+
root))
53+
2254
(defn zip-str [s]
2355
(zip/xml-zip
24-
(xml/parse (ByteArrayInputStream. (.getBytes s "UTF-8")))))
56+
(preprocess-xunit
57+
(xml/parse (ByteArrayInputStream. (.getBytes s "UTF-8"))))))
2558

2659
(defn filter-tags [tag-key xml-content]
2760
(let [filter-result (filter #(= (:tag %) tag-key) xml-content)]
@@ -198,3 +231,16 @@
198231
(let [result (get-dir-by-path arg (= "true" multi-test-cases) (= "true" multi-testsets) testset-name (= "true" sample))]
199232
result)
200233
(throw (Exception. "Must have at least one argument!"))))
234+
235+
(comment
236+
(let [s (slurp "test-data/robot/RobotDemo/xunit-combinded.xml")]
237+
(zip/xml-zip
238+
(preprocess-xunit
239+
(xml/parse (ByteArrayInputStream. (.getBytes s "UTF-8"))))))
240+
241+
(let [s (slurp "test-data/carmit/junit-report.xml")]
242+
(zip/xml-zip
243+
(preprocess-xunit
244+
(xml/parse (ByteArrayInputStream. (.getBytes s "UTF-8"))))))
245+
246+
)

0 commit comments

Comments
 (0)