1(ns leiningen.test.test
2  (:refer-clojure :exclude [test])
3  (:require [clojure.test :refer :all]
4            [leiningen.test :refer :all]
5            [leiningen.test.helper :refer [tmp-dir sample-no-aot-project
6                                           lein-test-exit-code-project
7                                           lein-test-reload-bug-project
8                                           sample-reader-cond-project
9                                           sample-failing-project
10                                           sample-fixture-error-project
11                                           with-system-err-str]]
12            [clojure.java.io :as io]
13            [leiningen.core.main :as main]
14            [leiningen.core.project :as project]))
15
16(use-fixtures :each
17              (fn [f]
18                (f)
19                (.delete (java.io.File. tmp-dir "lein-test-ran"))))
20
21(defn runs []
22  (let [ran-file (io/file tmp-dir "lein-test-ran")]
23    (and (.exists ran-file)
24         (-> ran-file
25             (slurp)
26             (.split "\n")
27             (->> (map read-string)
28                  (frequencies))))))
29
30(defn ran? [] (-> (runs) keys set))
31
32(deftest test-project-selectors
33  (is (= #{:default :integration :int2 :no-custom}
34         (set (keys (:test-selectors sample-no-aot-project)))))
35  (is (every? ifn? (map eval (vals (:test-selectors sample-no-aot-project))))))
36
37(deftest test-default-selector
38  (test sample-no-aot-project ":default")
39  (is (= (ran?) #{:regular :int2 :not-custom :fixture})))
40
41(deftest fixture-runs-appropriate-number-of-times
42  ;; Issue #1269
43  (test sample-no-aot-project)
44  ;; Because three tests ran
45  (is (= 3 ((runs) :fixture))))
46
47(deftest test-no-args-defaults-to-default-selector
48  (test sample-no-aot-project)
49  (is (= (ran?) #{:regular :int2 :not-custom :fixture})))
50
51(deftest test-basic-selector
52  (test sample-no-aot-project ":integration")
53  (is (= (ran?) #{:integration :integration-ns :fixture})))
54
55(deftest test-complex-selector
56  (test sample-no-aot-project ":no-custom")
57  (is (= (ran?) #{:integration :integration-ns :regular :int2 :fixture})))
58
59(deftest test-two-selectors
60  (test sample-no-aot-project ":integration" ":int2")
61  (is (= (ran?) #{:integration :integration-ns :int2 :fixture})))
62
63(deftest test-override-namespace-selector
64  (test sample-no-aot-project ":int2")
65  (is (= (ran?) #{:integration-ns :int2 :fixture})))
66
67(deftest test-only-selector
68  (test sample-no-aot-project ":only" "selectors/regular")
69  (is (= (ran?) #{:regular :fixture})))
70
71(deftest test-namespace-argument
72  (test sample-no-aot-project "selectors")
73  (is (= (ran?) #{:regular :not-custom :int2 :fixture})))
74
75(deftest test-reader-conditional-tests
76  (test sample-reader-cond-project)
77  (is (= (ran?) #{:clj-test :cljc-test})))
78
79(deftest test-namespaces-load-in-order
80  ;; Issue #2715
81  (test lein-test-reload-bug-project))
82
83(deftest test-failure-exit-code
84  (is (= 1
85         (try
86           ;; suppress output; there's a lot of bad-looking stuff here
87           (with-out-str (test lein-test-exit-code-project))
88           false
89           (catch clojure.lang.ExceptionInfo e
90             (:exit-code (ex-data e)))))))
91
92(deftest test-invalid-namespace-argument
93  (is (.contains
94       (with-system-err-str
95         (try
96           (test sample-no-aot-project "boom")
97           (catch clojure.lang.ExceptionInfo e
98             (when-not (:exit-code (ex-data e))
99               (throw e)))))
100       "java.io.FileNotFoundException: Could not locate")))
101
102(deftest test-file-argument
103  (let [file (io/file (first (:test-paths sample-no-aot-project)) "selectors.clj")]
104    (test sample-no-aot-project (.getPath file)))
105  (is (= (ran?) #{:regular :not-custom :int2 :fixture})))
106
107(deftest test-unreadable-test-fails
108  (let [project (project/merge-profiles sample-failing-project
109                                        [{:aot ^:replace []
110                                          :dependencies ^:replace
111                                          [['org.clojure/clojure (clojure-version)]]}])]
112    (binding [main/*exit-process?* false]
113      (is (= "EOF while reading" (try (test project) false
114                                      (catch Exception e
115                                        (.getMessage e))))))))
116
117(deftest test-catch-fixture-errors
118  (test sample-fixture-error-project)
119  (is (= (ran?) #{:test-a :test-c})))
120