|
19 | 19 | (defn str-to-number [val] |
20 | 20 | (if (not (nil? val)) (.parse (NumberFormat/getInstance) val) 0)) |
21 | 21 |
|
| 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 | + |
22 | 54 | (defn zip-str [s] |
23 | 55 | (zip/xml-zip |
24 | | - (xml/parse (ByteArrayInputStream. (.getBytes s "UTF-8"))))) |
| 56 | + (preprocess-xunit |
| 57 | + (xml/parse (ByteArrayInputStream. (.getBytes s "UTF-8")))))) |
25 | 58 |
|
26 | 59 | (defn filter-tags [tag-key xml-content] |
27 | 60 | (let [filter-result (filter #(= (:tag %) tag-key) xml-content)] |
|
198 | 231 | (let [result (get-dir-by-path arg (= "true" multi-test-cases) (= "true" multi-testsets) testset-name (= "true" sample))] |
199 | 232 | result) |
200 | 233 | (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