|
69 | 69 | (defn verbose-enough? [ctx expected-lvl] |
70 | 70 | (>= (or (:verbosity ctx) 0) expected-lvl)) |
71 | 71 |
|
72 | | -(defn run-step [ctx step] |
| 72 | +(defn exec-step [{:keys [conf steps] :as ctx} step] |
73 | 73 | (cond |
74 | 74 | ;; skip next steps if some previous one in the test-case was failed |
75 | | - (:failed ctx) |
| 75 | + (or (:failed? ctx) (:skip step)) |
76 | 76 | (do |
77 | | - (i-or-vv ctx |
78 | | - (println (colors/yellow "skip step") (:id step))) |
79 | | - ctx) |
| 77 | + (println (colors/yellow "skip step") (:id step)) |
| 78 | + (assoc step :skipped? true)) |
80 | 79 |
|
81 | 80 | :else |
82 | 81 | (do |
83 | | - (i-or-vv ctx |
| 82 | + (i-or-vv conf |
84 | 83 | (print (colors/yellow "run step") (:id step)) |
85 | 84 | (flush) |
86 | | - (if (:interactive ctx) (read-line) (println))) |
87 | | - (if-let [req (mk-req ctx step)] |
| 85 | + (if (:interactive conf) (read-line) (println))) |
| 86 | + (if-let [req (mk-req conf step)] |
88 | 87 | (let [{s :status :as resp} (http/request req) |
89 | 88 | b (when-let [b (:body resp)] |
90 | 89 | (cheshire.core/parse-string b keyword)) |
|
93 | 92 | (->> (matcho/match resp m) |
94 | 93 | (reduce (fn [acc {pth :path exp :expected}] |
95 | 94 | (assoc-in acc pth {:expected exp})) {})))] |
96 | | - (vv ctx |
| 95 | + (vv conf |
97 | 96 | (when (:desc step) |
98 | 97 | (println (str (or (:desc step) "")))) |
99 | 98 | (println "Request") |
|
105 | 104 |
|
106 | 105 | (if (empty? errs) |
107 | 106 | (do |
108 | | - (i ctx (println (colors/green "passed"))) |
109 | | - ctx) |
| 107 | + (i conf (println (colors/green "passed"))) |
| 108 | + step) |
110 | 109 | (do |
111 | | - (i ctx (println (colors/red "failed"))) |
112 | | - (assoc ctx |
113 | | - :failed true |
114 | | - :failed-step (:id step) |
| 110 | + (i conf (println (colors/red "failed"))) |
| 111 | + (pprint/pretty {:ident 0 :path [] :errors errs} resp) |
| 112 | + (assoc step |
| 113 | + :failed? true |
115 | 114 | :errors errs |
116 | 115 | :resp resp)))) |
117 | | - (assoc ctx :failed true :message "Cannot create requrest"))))) |
| 116 | + (assoc step :failed? true :message "Cannot create requrest"))))) |
118 | 117 |
|
119 | 118 | (defn get-id [test-case] |
120 | 119 | (or (:id test-case) (:filename test-case))) |
121 | 120 |
|
122 | | -(defn run-test-case [ctx test-case] |
123 | | - (i ctx |
124 | | - (println "run test case" (:id test-case))) |
125 | | - (let [result (reduce #(run-step %1 %2) ctx (:steps test-case))] |
126 | 121 |
|
127 | | - (if (:failed result) |
| 122 | +(defn run-step [{:keys [conf steps] :as ctx} step] |
| 123 | + (let [result (exec-step ctx step)] |
| 124 | + (-> ctx |
| 125 | + (update |
| 126 | + :steps #(conj % result)) |
| 127 | + (merge (if (:failed? result) {:failed? true |
| 128 | + :failed-step (:id result) |
| 129 | + :resp (:resp result) |
| 130 | + :errors (:errors result)} |
| 131 | + nil))))) |
| 132 | + |
| 133 | +(defn- run-steps |
| 134 | + [conf test-case] |
| 135 | + (reduce run-step {:conf conf :steps []} (:steps test-case))) |
| 136 | + |
| 137 | +(defn run-test-case [conf test-case] |
| 138 | + (println "run test case" (:id test-case)) |
| 139 | + |
| 140 | + (let [result (run-steps conf test-case)] |
| 141 | + (if (:failed? result) |
128 | 142 | (println (colors/red "failed") (str (get-id test-case) "." (:failed-step result))) |
129 | 143 | (println (colors/green "passed") (:id test-case))) |
130 | 144 |
|
131 | | - (when (:errors result) |
| 145 | + #_(when (:errors result) |
132 | 146 | (pprint/pretty {:ident 0 :path [] :errors (:errors result)} (:resp result))) |
133 | 147 | (assoc result :id (:id test-case)))) |
134 | 148 |
|
135 | | -(defn run-file [ctx filename] |
136 | | - (let [test-case (update (assoc (yaml/parse-string (slurp filename)) |
137 | | - :filename filename) |
138 | | - :id #(if (nil? %) filename %))] |
| 149 | +(defn- read-test-case |
| 150 | + [filename] |
| 151 | + (-> filename |
| 152 | + slurp |
| 153 | + yaml/parse-string |
| 154 | + (assoc |
| 155 | + :filename filename) |
| 156 | + (update |
| 157 | + :id #(if (nil? %) filename %)))) |
| 158 | + |
| 159 | +(defn run-file [{conf :conf test-cases :test-cases :as ctx} filename] |
| 160 | + (let [test-case (read-test-case filename)] |
139 | 161 | (when (valid? ctx test-case) |
140 | | - (let [result (run-test-case ctx test-case)] |
| 162 | + (let [result (run-test-case conf test-case)] |
141 | 163 | (update-in ctx [:test-cases] #(conj % result)))))) |
142 | 164 |
|
143 | | -(defn get-summary [{tests :test-cases}] |
144 | | - (let [failed-tests (filter :failed tests) |
145 | | - passed-tests (filter #(-> % :failed not) tests)] |
| 165 | +(defn get-summary [{test-cases :test-cases}] |
| 166 | + (let [failed-tests (filter :failed? test-cases) |
| 167 | + passed-tests (filter #(-> % :failed? not) test-cases)] |
146 | 168 | {:failed-tests failed-tests |
147 | 169 | :passed-tests passed-tests |
148 | 170 | :count-failed-tests (count failed-tests) |
149 | | - :count-all-tests (count tests) |
| 171 | + :count-all-tests (count test-cases) |
150 | 172 | :count-passed-tests (count passed-tests)})) |
151 | 173 |
|
152 | | -(defn run [ctx files] |
153 | | - (let [result (reduce run-file (assoc ctx :test-cases []) files) |
| 174 | +(defn run [conf files] |
| 175 | + (let [result (reduce run-file {:conf conf :test-cases []} files) |
154 | 176 | summary (get-summary result) |
155 | 177 | passed? (zero? (:count-failed-tests summary))] |
156 | 178 |
|
|
161 | 183 | (println (:count-passed-tests summary) "passed," |
162 | 184 | (:count-failed-tests summary) "failed.") |
163 | 185 | (println "Failed tests:") |
164 | | - (doseq [test-case (filter :failed (:test-cases result))] |
| 186 | + (doseq [test-case (filter :failed? (:test-cases result))] |
165 | 187 | (println (:id test-case) (str "(" (:filename result) ")"))))) |
166 | 188 |
|
| 189 | + #_(clojure.pprint/pprint result) |
167 | 190 | (assoc result :passed? passed?))) |
168 | 191 |
|
169 | 192 | (comment |
|
0 commit comments