|
1 | 1 | #!/usr/bin/env bb
|
2 | 2 |
|
3 | 3 | (ns ci-unit-tests
|
4 |
| - (:require [helper.fs :as fs] |
| 4 | + (:require [cheshire.core :as json] |
| 5 | + [clojure.string :as string] |
| 6 | + [doric.core :as doric] |
| 7 | + [helper.fs :as fs] |
| 8 | + [helper.jdk :as jdk] |
5 | 9 | [helper.main :as main]
|
6 | 10 | [helper.os :as os]
|
7 | 11 | [helper.shell :as shell]
|
8 | 12 | [lread.status-line :as status]))
|
9 | 13 |
|
10 |
| -(defn clean [] |
11 |
| - (doseq [dir ["target" ".cpcache" ".shadow-cljs"]] |
12 |
| - (fs/delete-file-recursively dir true))) |
| 14 | +(defn- matrix-os [] |
| 15 | + (case (os/get-os) |
| 16 | + :win "windows" |
| 17 | + :mac "macos" |
| 18 | + ;; else assume ubuntu |
| 19 | + "ubuntu")) |
13 | 20 |
|
14 |
| -(defn lint [] |
15 |
| - (shell/command "bb lint")) |
| 21 | +;; matrix params to be used on ci |
| 22 | +(def ^:private all-oses ["ubuntu" "macos" "windows"]) |
| 23 | +(def ^:private all-jdks ["8" "11" "17"]) |
16 | 24 |
|
17 |
| -(defn check-import-vars [] |
18 |
| - (shell/command "bb apply-import-vars check")) |
| 25 | +(defn- test-tasks [] |
| 26 | + (concat [;; run lintish tasks across all oses to verify that they will work for all devs regardless of their os choice |
| 27 | + {:desc "import-vars" :cmd "bb apply-import-vars check" :oses all-oses :jdks ["8"]} |
| 28 | + {:desc "lint" :cmd "bb lint" :oses all-oses :jdks ["8"]} |
| 29 | + ;; test-docs on default clojure version across all oses and jdks |
| 30 | + {:desc "test-doc" :cmd "bb test-doc" :oses all-oses :jdks all-jdks}] |
| 31 | + (for [version ["1.8" "1.9" "1.10" "1.11"]] |
| 32 | + {:desc (str "clj-" version) |
| 33 | + :cmd (str "bb test-clj --clojure-version " version) |
| 34 | + :oses all-oses |
| 35 | + :jdks all-jdks}) |
| 36 | + ;; I'm not sure there's much value testing across jdks for ClojureScript tests, for now we'll stick with jdk 8 only |
| 37 | + (for [env [{:param "node" :desc "node"} |
| 38 | + {:param "chrome-headless" :desc "browser"}] |
| 39 | + opt [{:param "none"} |
| 40 | + {:param "advanced" :desc "adv"}]] |
| 41 | + {:desc (str "cljs-" |
| 42 | + (:desc env) |
| 43 | + (when (:desc opt) (str "-" (:desc opt)))) |
| 44 | + :cmd (str "bb test-cljs --env " (:param env) " --optimizations " (:param opt)) |
| 45 | + :oses all-oses |
| 46 | + :jdks ["8"]}) |
| 47 | + ;; shadow-cljs requires a min of jdk 11 so we'll test on that |
| 48 | + [{:desc "shadow-cljs" :cmd "bb test-shadow-cljs" :oses all-oses :jdks ["11"] |
| 49 | + :skip-reason-fn (fn [{:keys [jdk]}] (when (< (parse-long jdk) 11) |
| 50 | + "jdk must be >= 11"))}] |
| 51 | + ;; planck does not run on windows, and I don't think it needs a jdk |
| 52 | + [{:desc "cljs-bootstrap" :cmd "bb test-cljs --env planck --optimizations none" |
| 53 | + :oses ["macos" "ubuntu"] :jdks ["8"]}])) |
19 | 54 |
|
20 |
| -(defn doc-tests[] |
21 |
| - (shell/command "bb test-doc")) |
| 55 | +(defn- ci-test-matrix [] |
| 56 | + (for [{:keys [desc cmd oses jdks]} (test-tasks) |
| 57 | + os oses |
| 58 | + jdk jdks] |
| 59 | + {:desc (str desc " " os " jdk" jdk) |
| 60 | + :cmd cmd |
| 61 | + :os os |
| 62 | + :jdk jdk})) |
22 | 63 |
|
23 |
| -(defn clojure-tests [] |
24 |
| - (doseq [version ["1.8" "1.9" "1.10" "1.11"]] |
25 |
| - (shell/command "bb test-clj --clojure-version" version)) ) |
| 64 | +(defn- local-test-list [local-os local-jdk] |
| 65 | + (for [{:keys [desc cmd oses skip-reason-fn]} (test-tasks)] |
| 66 | + (let [skip-reasons (cond-> [] |
| 67 | + (not (some #{local-os} oses)) |
| 68 | + (conj (str "os must be among " oses)) |
| 69 | + (and skip-reason-fn (skip-reason-fn {:jdk local-jdk})) |
| 70 | + (conj (skip-reason-fn {:jdk local-jdk})))] |
| 71 | + (cond-> {:desc desc |
| 72 | + :cmd cmd} |
| 73 | + (seq skip-reasons) |
| 74 | + (assoc :skip-reasons skip-reasons))))) |
26 | 75 |
|
27 |
| -(defn cljs-tests [] |
28 |
| - (doseq [env ["node" "chrome-headless"] |
29 |
| - opt ["none" "advanced"]] |
30 |
| - (shell/command "bb" "test-cljs" "--env" env "--optimizations" opt))) |
| 76 | +(defn- clean [] |
| 77 | + (doseq [dir ["target" ".cpcache" ".shadow-cljs"]] |
| 78 | + (fs/delete-file-recursively dir true))) |
31 | 79 |
|
32 |
| -(defn shadow-cljs-tests [] |
33 |
| - (shell/command "bb test-shadow-cljs")) |
| 80 | +(def args-usage "Valid args: |
| 81 | + [matrix-for-ci [--format=json]] |
| 82 | + --help |
34 | 83 |
|
35 |
| -(defn cljs-bootstrap-tests [] |
36 |
| - (if (some #{(os/get-os)} '(:mac :unix)) |
37 |
| - (shell/command "bb test-cljs --env planck --optimizations none") |
38 |
| - (status/line :warn "skipping planck tests, they can only be run on linux and macOS")) ) |
| 84 | +Commands: |
| 85 | + matrix-for-ci Return a matrix for use within GitHub Actions workflow |
| 86 | +
|
| 87 | +Options: |
| 88 | + --help Show this help |
| 89 | +
|
| 90 | +By default, will run all tests applicable to your current jdk and os.") |
39 | 91 |
|
40 | 92 | (defn -main [& args]
|
41 |
| - (when (main/doc-arg-opt args) |
42 |
| - (clean) |
43 |
| - (check-import-vars) |
44 |
| - (lint) |
45 |
| - (doc-tests) |
46 |
| - (clojure-tests) |
47 |
| - (cljs-tests) |
48 |
| - (shadow-cljs-tests) |
49 |
| - (cljs-bootstrap-tests)) |
| 93 | + (when-let [opts (main/doc-arg-opt args-usage args)] |
| 94 | + (if (get opts "matrix-for-ci") |
| 95 | + (let [matrix (ci-test-matrix)] |
| 96 | + (if (= "json" (get opts "--format")) |
| 97 | + (status/line :detail (json/generate-string matrix)) |
| 98 | + (do |
| 99 | + (status/line :detail (doric/table [:os :jdk :desc :cmd] matrix)) |
| 100 | + (status/line :detail "Total jobs found: %d" (count matrix))))) |
| 101 | + (let [cur-os (matrix-os) |
| 102 | + cur-jdk (jdk/version) |
| 103 | + cur-major-jdk (str (:major cur-jdk)) |
| 104 | + test-list (local-test-list cur-os cur-major-jdk) |
| 105 | + tests-skipped (filter :skip-reasons test-list) |
| 106 | + tests-to-run (remove :skip-reasons test-list)] |
| 107 | + (when (not (some #{cur-major-jdk} all-jdks)) |
| 108 | + (status/line :warn "CI runs only on jdks %s but you are on jdk %s\nWe'll run tests anyway." |
| 109 | + all-jdks (:version cur-jdk))) |
| 110 | + |
| 111 | + (status/line :head "Test plan") |
| 112 | + (status/line :detail "Found %d runnable tests for jdk %s on %s:" |
| 113 | + (count tests-to-run) cur-major-jdk cur-os) |
| 114 | + (doseq [{:keys [cmd]} tests-to-run] |
| 115 | + (status/line :detail (str " " cmd))) |
| 116 | + (doseq [{:keys [cmd skip-reasons]} tests-skipped] |
| 117 | + (status/line :warn (string/join "\n* " |
| 118 | + (concat [(str "Skipping: " cmd)] |
| 119 | + skip-reasons)))) |
| 120 | + (clean) |
| 121 | + (doseq [{:keys [cmd]} tests-to-run] |
| 122 | + (shell/command cmd))))) |
50 | 123 | nil)
|
51 | 124 |
|
52 | 125 | (main/when-invoked-as-script
|
|
0 commit comments