Skip to content

Commit 2492dea

Browse files
authored
dev: add clj-kondo source code linting (#30)
Includes the addressing of reported lint findings. Closes #28
1 parent e7ef923 commit 2492dea

File tree

14 files changed

+149
-25
lines changed

14 files changed

+149
-25
lines changed

.clj-kondo/babashka/fs/config.edn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{:lint-as {babashka.fs/with-temp-dir clojure.core/let}}

.clj-kondo/config.edn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
;; don't adopt any user preferences
2+
{:config-paths ^:replace []}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{:lint-as {lread.status-line/line clojure.tools.logging/infof
2+
lread.status-line/die clojure.tools.logging/infof}}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{:lint-as
2+
{rewrite-clj.zip/subedit-> clojure.core/->
3+
rewrite-clj.zip/subedit->> clojure.core/->>
4+
rewrite-clj.zip/edit-> clojure.core/->
5+
rewrite-clj.zip/edit->> clojure.core/->>}}

.github/workflows/ci.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,47 @@ name: Tests
22
on: [push, pull_request]
33

44
jobs:
5+
lint:
6+
runs-on: ubuntu-latest
7+
8+
steps:
9+
- name: Checkout
10+
uses: actions/checkout@v3.0.2
11+
12+
- name: Clojure deps cache
13+
uses: actions/cache@v3
14+
with:
15+
path: |
16+
~/.m2/repository
17+
~/.deps.clj
18+
~/.gitlibs
19+
key: cljdeps-${{ hashFiles('deps.edn', 'bb.edn') }}
20+
restore-keys: cljdeps-
21+
22+
- name: Setup Java
23+
uses: actions/setup-java@v3
24+
with:
25+
distribution: 'temurin'
26+
java-version: '17'
27+
28+
- name: Setup Clojure Tooling
29+
uses: DeLaGuardo/setup-clojure@9.4
30+
with:
31+
bb: 'latest'
32+
33+
- name: Tools versions
34+
run: |
35+
echo "bb --version"
36+
bb --version
37+
echo "java -version"
38+
java -version
39+
40+
- name: Bring down deps
41+
run: bb deps
42+
43+
- name: Lint
44+
run: bb lint
45+
546
test:
647
runs-on: ${{ matrix.os }}-latest
748
strategy:
@@ -101,6 +142,7 @@ jobs:
101142
deploy:
102143
runs-on: ubuntu-latest
103144
needs:
145+
- lint
104146
- test
105147
- bb-test
106148

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ To run a small suite of sanity tests for babashka (found under ./bb]):
233233

234234
$ bb test:bb
235235

236+
### Linting
237+
238+
Our CI workflow lints sources with clj-kondo, and you can too!
239+
240+
$ bb lint
241+
236242
### Release
237243

238244
To release a new version, run `bb publish` which will push a new tag. CI will

bb.edn

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{:paths ["." "bb"]
2-
:deps {org.clj-commons/clj-http-lite {:local/root "."}}
2+
:deps {org.clj-commons/clj-http-lite {:local/root "."}
3+
lread/status-line {:git/url "https://github.com/lread/status-line.git"
4+
:sha "35ed39645038e81b42cb15ed6753b8462e60a06d"}}
35
:tasks
46
{:requires ([tasks :as t])
57
deps
@@ -11,6 +13,9 @@
1113
test:bb
1214
{:doc "Run babashka tests"
1315
:task clj-http.lite.test-runner/-main}
16+
lint
17+
{:doc "[--rebuild] Lint source code"
18+
:task lint/-main}
1419
publish
1520
{:doc "Trigger a release to clojars"
1621
:task (t/publish)}}}

bb/lint.clj

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
(ns lint
2+
(:require [babashka.classpath :as bbcp]
3+
[babashka.cli :as cli]
4+
[babashka.fs :as fs]
5+
[babashka.tasks :as t]
6+
[clojure.string :as string]
7+
[lread.status-line :as status]))
8+
9+
(def clj-kondo-cache ".clj-kondo/.cache")
10+
11+
(defn- cache-exists? []
12+
(fs/exists? clj-kondo-cache))
13+
14+
(defn- delete-cache []
15+
(when (cache-exists?)
16+
(fs/delete-tree clj-kondo-cache)))
17+
18+
(defn- build-cache []
19+
(when (cache-exists?)
20+
(delete-cache))
21+
(let [clj-cp (-> (t/clojure {:out :string}
22+
"-Spath -M:test")
23+
with-out-str
24+
string/trim)
25+
bb-cp (bbcp/get-classpath)]
26+
(status/line :detail "- copying configs")
27+
(t/clojure "-M:clj-kondo --skip-lint --copy-configs --lint" clj-cp bb-cp)
28+
(status/line :detail "- creating cache")
29+
(t/clojure "-M:clj-kondo --dependencies --lint" clj-cp bb-cp)))
30+
31+
(defn- check-cache [{:keys [rebuild]}]
32+
(status/line :head "clj-kondo: cache check")
33+
(if-let [rebuild-reason (cond
34+
rebuild
35+
"Rebuild requested"
36+
37+
(not (cache-exists?))
38+
"Cache not found"
39+
40+
:else
41+
(let [updated-dep-files (fs/modified-since clj-kondo-cache ["deps.edn" "bb.edn"])]
42+
(when (seq updated-dep-files)
43+
(format "Found deps files newer than lint cache: %s" (mapv str updated-dep-files)))))]
44+
(do (status/line :detail rebuild-reason)
45+
(build-cache))
46+
(status/line :detail "Using existing cache")))
47+
48+
(defn- lint [opts]
49+
(check-cache opts)
50+
(status/line :head "clj-kondo: linting")
51+
(let [{:keys [exit]}
52+
(t/clojure {:continue true}
53+
"-M:clj-kondo --lint src test bb deps.edn bb.edn")]
54+
(cond
55+
(= 2 exit) (status/die exit "clj-kondo found one or more lint errors")
56+
(= 3 exit) (status/die exit "clj-kondo found one or more lint warnings")
57+
(> exit 0) (status/die exit "clj-kondo returned unexpected exit code"))))
58+
59+
(defn -main [& args]
60+
(when-let [opts (cli/parse-opts args)]
61+
(lint opts)))
62+
63+
(when (= *file* (System/getProperty "babashka.file"))
64+
(apply -main *command-line-args*))

deps.edn

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@
2121
org.slf4j/log4j-over-slf4j {:mvn/version "1.7.36"}}
2222
:main-opts ["-m" "cognitect.test-runner"
2323
;; our test namespaces do not always end in -test, hence the override:
24-
"--namespace-regex" "clj-http\\.test\\..*"]}}}
24+
"--namespace-regex" "clj-http\\.test\\..*"]}
25+
;; for consistent linting we use a specific version of clj-kondo through the jvm
26+
:clj-kondo {:extra-deps {clj-kondo/clj-kondo {:mvn/version "2022.08.03"}}
27+
:override-deps {org.clojure/clojure {:mvn/version "1.11.1"}}
28+
:main-opts ["-m" "clj-kondo.main"]}}}

src/clj_http/lite/core.clj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"Core HTTP request/response implementation."
33
(:require [clojure.java.io :as io])
44
(:import (java.io ByteArrayOutputStream InputStream)
5-
(java.net URL HttpURLConnection)
6-
(java.security SecureRandom)))
5+
(java.net URL HttpURLConnection)))
76

87
(set! *warn-on-reflection* true)
98

@@ -82,7 +81,7 @@
8281
the clj-http uses ByteArrays for the bodies."
8382
[{:keys [request-method scheme server-name server-port uri query-string
8483
headers content-type character-encoding body socket-timeout
85-
conn-timeout debug insecure? save-request? follow-redirects
84+
conn-timeout insecure? save-request? follow-redirects
8685
chunk-size] :as req}]
8786
(let [http-url (str (name scheme) "://" server-name
8887
(when server-port (str ":" server-port))

0 commit comments

Comments
 (0)