Skip to content

Commit 2eb2845

Browse files
committed
Libs tests: can now check for outdated libs
1 parent 9a22dbd commit 2eb2845

File tree

2 files changed

+153
-31
lines changed

2 files changed

+153
-31
lines changed

doc/02-developer-guide.adoc

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ I expect, but don't know, that most Windows editors automatically handle lf as l
3333
Someone let me know if I am wrong.
3434

3535
Note that I do explicitly set git's config `core.autocrlf` to `false` on our Windows CI unit test environment.
36-
Our import vars code generation checks currently rely on line endings remaining uncoverted.
36+
Our import vars code generation checks currently rely on line endings remaining unconverted.
3737

3838
==== Babashka
3939
The Clojure story on Windows is still in the early chapters. https://scoop.sh/[Scoop] offers an easy way to install tools.
@@ -204,7 +204,7 @@ bb ./script/sci_native_test.clj
204204
To try to ensure our changes to rewrite-clj do not inadvertently break existing popular libraries, we run their tests, or a portion thereof, against rewrite-clj.
205205

206206
----
207-
bb ./script/libs_tests.clj
207+
bb ./script/libs_tests.clj run
208208
----
209209

210210
Current libs we test against:
@@ -224,12 +224,22 @@ Additional libs are welcome.
224224
If you are troubleshooting locally, and want to only run specific tests, you can specify which ones you'd like to run. For example:
225225

226226
----
227-
bb ./script/libs_tests.clj cljfmt zprint
227+
bb ./script/libs_tests.clj run cljfmt zprint
228228
----
229229

230+
Running current versions of libs is recommended, but care must be taken when updating.
231+
We want to make sure we are patching correctly to use rewrite-clj v1 and running a lib's tests as intended.
232+
233+
To check for outdated libs:
234+
235+
----
236+
bb ./script/libs_test.clj outdated
237+
----
238+
239+
230240
Notes:
231241

232-
* `libs_tests.clj` was developped on macOS and is run on CI under Linux only under JDK 11 only.
242+
* `libs_tests.clj` was developed on macOS and is run on CI under Linux only under JDK 11 only.
233243
We can expand variations at some later date if there is any value to it.
234244
* We test the current HEAD of rewrite-clj v1 against specific versions (latest at the time of this writing) of libs.
235245
* We patch lib deps and sometimes code (ex. `require` for `rewrite-cljc` becomes `rewrite-clj`).

script/libs_tests.clj

Lines changed: 139 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@
66
[babashka.curl :as curl]
77
[babashka.deps :as deps]
88
[babashka.fs :as fs]
9+
[cheshire.core :as json]
910
[clojure.java.io :as io]
1011
[clojure.string :as string]))
1112

1213
(deps/add-deps
1314
'{:deps {io.aviso/pretty {:mvn/version "0.1.36"}
15+
docopt/docopt {:git/url "https://github.com/nubank/docopt.clj"
16+
:sha "98814f559d2e50fdf10f43cbe3b7da0ca3cca423"}
1417
doric/doric {:mvn/version "0.9.0"}}})
1518

1619
(cp/add-classpath (.getParent (io/file *file*)))
1720

18-
(require '[doric.core :as doric]
21+
(require '[docopt.core :as docopt]
22+
'[docopt.match :as docopt-match]
23+
'[doric.core :as doric]
1924
'[helper.shell :as shell]
2025
'[helper.status :as status]
2126
'[io.aviso.ansi :as ansi]
@@ -46,11 +51,49 @@
4651
(fs/move pom-bak-filename "pom.xml" {:replace-existing true}))))
4752
nil)
4853

49-
(defn- fetch-lib-release [{:keys [target-root-dir name version release-url-fmt]}]
50-
(let [target (str (fs/file target-root-dir (format "%s-%s.zip" name version)))]
54+
(defn- get-current-version
55+
"Get current available version of lib via GitHub API.
56+
57+
Note that github API does have rate limiting... so if running this a lot some calls will fail.
58+
59+
Could get from deps.edn RELEASE technique, but not all libs are on clojars.
60+
See: https://github.com/babashka/babashka/blob/master/examples/outdated.clj"
61+
[{:keys [github-release]}]
62+
(case (:via github-release)
63+
;; no official release
64+
:sha
65+
(-> (curl/get (format "https://api.github.com/repos/%s/git/refs/heads/master" (:repo github-release)))
66+
:body
67+
(json/parse-string true)
68+
:object
69+
:sha)
70+
71+
;; tags can work better than release - sometimes libs have release 1.2 that refs tag 1.1
72+
:tag
73+
(-> (curl/get (format "https://api.github.com/repos/%s/tags" (:repo github-release)))
74+
:body
75+
(json/parse-string true)
76+
first
77+
:name)
78+
79+
;; else via release which works better than tags sometimes due to the way tags sort
80+
(-> (curl/get (format "https://api.github.com/repos/%s/releases" (:repo github-release)))
81+
:body
82+
(json/parse-string true)
83+
first
84+
:tag_name)))
85+
86+
(defn- fetch-lib-release [{:keys [target-root-dir name version github-release]}]
87+
(let [target (str (fs/file target-root-dir (format "%s-%s.zip" name version)))
88+
download-url (if (= :sha (:via github-release))
89+
(format "https://github.com/%s/zipball/%s" (:repo github-release) version)
90+
(format "https://github.com/%s/archive/%s%s.zip"
91+
(:repo github-release)
92+
(or (:version-prefix github-release) "")
93+
version))]
5194
(io/make-parents target)
5295
(io/copy
53-
(:body (curl/get (format release-url-fmt version) {:as :stream}))
96+
(:body (curl/get download-url {:as :stream}))
5497
(io/file target))
5598
(let [zip-root-dir (->> (shcmd ["unzip" "-qql" target] {:out :string})
5699
:out
@@ -253,58 +296,66 @@
253296
(def libs [{:name "antq"
254297
:version "0.11.2"
255298
:platforms [:clj]
256-
:release-url-fmt "https://github.com/liquidz/antq/archive/%s.zip"
299+
:github-release {:repo "liquidz/antq"}
257300
:patch-fn antq-patch
258301
:show-deps-fn cli-deps-tree
259302
:test-cmds [["clojure" "-M:dev:test"]]}
260303
{:name "carve"
261304
:version "0.0.2"
262305
:platforms [:clj]
263-
:release-url-fmt "https://github.com/borkdude/carve/archive/v%s.zip"
306+
:github-release {:repo "borkdude/carve"
307+
:version-prefix "v"}
264308
:patch-fn carve-patch
265309
:show-deps-fn cli-deps-tree
266310
:test-cmds [["clojure" "-M:test"]]}
267311
{:name "cljfmt"
268312
:version "0.7.0"
269313
:platforms [:clj :cljs]
270314
:root "cljfmt"
271-
:release-url-fmt "https://github.com/weavejester/cljfmt/archive/%s.zip"
315+
:github-release {:repo "weavejester/cljfmt"
316+
:via :tag}
272317
:patch-fn cljfmt-patch
273318
:show-deps-fn lein-deps-tree
274319
:test-cmds [["lein" "test"]]}
275320
{:name "clojure-lsp"
276321
:platforms [:clj]
277322
:version "2021.03.01-19.18.54"
278-
:release-url-fmt "https://github.com/clojure-lsp/clojure-lsp/archive/%s.zip"
323+
:github-release {:repo "clojure-lsp/clojure-lsp" }
279324
:patch-fn clojure-lsp-patch
280325
:show-deps-fn lein-deps-tree
281326
:test-cmds [["lein" "test"]]}
282327
{:name "mranderson"
283328
:version "0.5.3"
284329
:platforms [:clj]
285-
:release-url-fmt "https://github.com/benedekfazekas/mranderson/archive/v%s.zip"
330+
:github-release {:repo "benedekfazekas/mranderson"
331+
:via :tag
332+
:version-prefix "v"}
286333
:patch-fn mranderson-patch
287334
:show-deps-fn lein-deps-tree
288335
:test-cmds [["lein" "test"]]}
289336
{:name "rewrite-edn"
290337
:version "665f61cf273c79b44baacb0897d72c2157e27b09"
291338
:platforms [:clj]
292-
:release-url-fmt "https://github.com/borkdude/rewrite-edn/zipball/%s"
339+
:github-release {:repo "borkdude/rewrite-edn"
340+
:via :sha}
293341
:patch-fn rewrite-edn-patch
294342
:show-deps-fn cli-deps-tree
295343
:test-cmds [["clojure" "-M:test"]]}
296344
{:name "refactor-nrepl"
297345
:version "2.5.1"
298346
:platforms [:clj]
299-
:release-url-fmt "https://github.com/clojure-emacs/refactor-nrepl/archive/v%s.zip"
347+
:github-release {:repo "clojure-emacs/refactor-nrepl"
348+
:via :tag
349+
:version-prefix "v"}
300350
:patch-fn refactor-nrepl-patch
301351
:show-deps-fn lein-deps-tree
302352
:prep-fn refactor-nrepl-prep
303353
:test-cmds [["lein" "with-profile" "+1.10,+plugin.mranderson/config" "test"]]}
304354
{:name "update-leiningen-dependencies-skill"
305355
:version "21c7ce794c83d6eed9c2a27e2fdd527b5da8ebb3"
306356
:platforms [:cljs]
307-
:release-url-fmt "https://github.com/atomist-skills/update-leiningen-dependencies-skill/zipball/%s"
357+
:github-release {:repo "atomist-skills/update-leiningen-dependencies-skill"
358+
:via :sha}
308359
:patch-fn update-leiningen-dependencies-skill-patch
309360
:prep-fn update-leiningen-dependencies-skill-prep
310361
:show-deps-fn cli-deps-tree
@@ -313,7 +364,7 @@
313364
:version "1.1.1"
314365
:platforms [:clj :cljs]
315366
:note "zprint src hacked to pass with rewrite-clj v1"
316-
:release-url-fmt "https://github.com/kkinnear/zprint/archive/%s.zip"
367+
:github-release {:repo "kkinnear/zprint"}
317368
:patch-fn zprint-patch
318369
:prep-fn zprint-prep
319370
:show-deps-fn (fn [lib]
@@ -367,20 +418,25 @@
367418
(status/line :detail "git init-ing target to avoid polluting our project git config/hooks with any changes libs under test might effect")
368419
(shcmd ["git" "init"] {:dir target-root-dir}))
369420

370-
(defn main [args]
371-
;; no args = test all libs
372-
;; or specify which libs, by name, to test (in order specified)
421+
;;
422+
;; cmds
423+
;;
424+
(defn- report-outdated [requested-libs]
425+
(status/line :info "Checking for outdated libs")
426+
(status/line :detail (format "Requested libs: %s" (into [] (map :name requested-libs))))
427+
(let [outdated-libs (->> requested-libs
428+
(map #(assoc %
429+
:available-version (get-current-version %)
430+
:version (str (-> % :github-release :version-prefix) (:version %))))
431+
(filter #(not= (:available-version %) (:version %))))]
432+
(if (seq outdated-libs)
433+
(-> (doric/table [:name :version :available-version] outdated-libs) println)
434+
(status/line :detail "=> All libs seems up to date"))))
435+
436+
(defn run-tests [requested-libs]
373437
(status/line :info "Testing 3rd party libs")
374438
(status/line :detail "Test popular 3rd party libs against current rewrite-clj.")
375-
(let [requested-libs (if (zero? (count args))
376-
libs
377-
(reduce (fn [ls a]
378-
(if-let [l (first (filter #(= a (:name %)) libs))]
379-
(conj ls l)
380-
ls))
381-
[]
382-
args))
383-
target-root-dir "target/libs-test"
439+
(let [target-root-dir "target/libs-test"
384440
rewrite-clj-version (str (version/calc) "-canary")]
385441
(status/line :detail (format "Requested libs: %s" (into [] (map :name requested-libs))))
386442
(install-local rewrite-clj-version)
@@ -395,7 +451,63 @@
395451
(map :exit-codes)
396452
flatten
397453
(every? zero?))
398-
0 1))))
399-
nil)
454+
0 1)))))
455+
456+
(def docopt-usage "Libs Tests
457+
458+
Usage:
459+
libs_tests.clj run [<lib-name>...]
460+
libs_tests.clj outdated [<lib-name>...]
461+
462+
Options:
463+
-h --help Show this screen.
464+
465+
Specifying no lib-names selects all libraries.")
466+
467+
(defn main [args]
468+
(if-let [opts (-> docopt-usage docopt/parse (docopt-match/match-argv args))]
469+
(let [lib-names (get opts "<lib-name>")
470+
requested-libs (if (zero? (count lib-names))
471+
libs
472+
(reduce (fn [ls a]
473+
(if-let [l (first (filter #(= a (:name %)) libs))]
474+
(conj ls l)
475+
ls))
476+
[]
477+
lib-names))]
478+
(cond
479+
(get opts "outdated")
480+
(report-outdated requested-libs)
481+
482+
(get opts "run")
483+
(run-tests requested-libs))
484+
485+
nil)
486+
(status/fatal docopt-usage)))
400487

401488
(main *command-line-args*)
489+
490+
491+
#_(-> (curl/get "https://api.github.com/repos/borkdude/rewrite-edn/releases")
492+
:body
493+
(json/parse-string true)
494+
#_#_first
495+
:tag_name)
496+
497+
#_(-> (curl/get "https://api.github.com/repos/borkdude/rewrite-edn/git/refs/heads/master")
498+
:body
499+
(json/parse-string true)
500+
:object
501+
:sha)
502+
503+
#_(-> (curl/get "https://api.github.com/repos/weavejester/cljfmt/tags")
504+
:body
505+
(json/parse-string true)
506+
first)
507+
508+
#_(get-current-version (nth libs 5))
509+
510+
511+
#_(docopt/docopt docopt-usage
512+
*command-line-args*
513+
(fn [arg-map] arg-map))

0 commit comments

Comments
 (0)