Skip to content

Commit

Permalink
tests: update and extend tests for Clojure linters
Browse files Browse the repository at this point in the history
  • Loading branch information
practicalli-johnny committed Dec 17, 2022
1 parent cea98d4 commit 2495266
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 76 deletions.
50 changes: 40 additions & 10 deletions .automation/test/clojure/clojure_bad_1.clj
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
(ns foo
(:require
[clojure.string :as str]
;; We're never using this namespace. Also, the namespaces aren't sorted.
[clojure.set :as set]))
[clojure.set :as set]
[clojure.string :as str]))


;; Here we made a typo, so the symbol is unresolved:
(but-last [1 2 3])


;; Clj-kondo knows about arities of clojure namespaces, but you can also teach
;; it about your libraries or own namespaces
(str/join)


;; foo has an arity of 2, but we're not actually using y
(defn foo-fn [x y]
(defn foo-fn
[x y]
;; this do is redundant:
(do
;; this is handy for debugging, but please remove it before pushing your code:
Expand All @@ -25,39 +29,65 @@
;; also wrong:
(recur)))))


(letfn
[(f [] (h 1))
(h [] (f 1))])
[(f [] (h 1))
(h [] (f 1))])


(defn- private-fn
[])


(defn- private-fn [])
;; redefining it...
(defn- private-fn [])
(defn- private-fn
[])


(defn foo
[]
:foo)


(defn foo [] :foo)
;; Type error, because foo doesn't return a number!
(inc (foo))


;; I'm tired now, let's sleep...
;; Oops, not happening because of wrong amount of args:
(Thread/sleep 1000 1 2)


;; Here we switch to another namespace and require the previous:
(ns bar (:require [foo :as f]))
(ns bar
(:require
[foo :as f]))


;; Wrong arity when calling a function from the previous namespace:
(f/foo-fn)


;; private:
(f/private-fn)


;; this won't pass the reader:
{:a 1 :a 2}


;; and neither will this:
#{1 1}


;; nor this:
{:a 1 :b}

(ns bar-test (:require [clojure.test :as t]))

(ns bar-test
(:require
[clojure.test :as t]))


(t/deftest my-tests
;; you're not actually testing something here:
Expand Down
31 changes: 17 additions & 14 deletions .automation/test/clojure/clojure_bad_2.clj
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
( ns
foo.bar.baz "some doc"
(:require (foo.bar [abc :as abc]
def))
(:use foo.bar.qux)
(:import foo.bar.qux.Foo
(ns foo.bar.baz
"some doc"
(:require
[foo.bar.abc :as abc]
[foo.bar.def]
[foo.bar.qux :refer :all])
(:import
(foo.bar.qux
;; Need this for the thing
foo.bar.qux.Bar)
)
Bar
Foo)))

(defn hello "says hi" (
[] (hello "world")
) ([name]
( println "Hello," name )
))

(defn hello
"says hi"
([] (hello "world"))
([name]
(println "Hello," name)))


(comment
Expand All @@ -22,4 +25,4 @@
)(* y y)
))

)
)
52 changes: 36 additions & 16 deletions .automation/test/clojure/clojure_good_1.clj
Original file line number Diff line number Diff line change
@@ -1,37 +1,57 @@
#_{:clj-kondo/ignore [:namespace-name-mismatch]}
(ns foo
(ns clojure-good-1
(:require
[clojure.string :as str]))
[clojure.string :as string]))


(butlast [1 2 3])

(str/join "" "")
(string/join "" "")


(defn calculation
[x]
(let [y (fn [] (inc x))]
(y)))

(defn foo-fn [x]
(let [y (fn [] (inc x))]
(y)))

(letfn
[(f [g] (h g))
(h [i] (f i))])
[(f [g] (h g))
(h [i] (f i))])

(defn foo [] 1)
(inc (foo))

(defn incremetal
[]
1)


(inc (incremetal))

(Thread/sleep 1000 1)


;; Here we switch to another namespace and require the previous:
#_{:clj-kondo/ignore [:namespace-name-mismatch]}
(ns bar (:require [foo :as f]))

(f/foo-fn 1)

(ns bar
(:require
[clojure-good-1 :as good]))


(good/calculation 1)

{:a 1 :b 2}
#{1 2}
{:a 1 :b 2}

#_{:clj-kondo/ignore [:namespace-name-mismatch]}
(ns bar-test (:require [clojure.test :as t]))

(t/deftest my-tests
(t/is (odd? (inc 1))))

(ns bar-test
(:require
[clojure.test :refer [deftest is testing]]))


(deftest oddity-test
(testing "Is something odd?"
(is (odd? (inc 1)))))
46 changes: 10 additions & 36 deletions .automation/test/clojure/clojure_good_2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,22 @@
;; --------------------------------------------------


(ns practicalli.gameboard.service
(ns clojure-good-2
"Fraud API service component lifecycle management"
(:gen-class)
(:require
;; Application dependencies
[practicalli.gameboard.router :as router]
[practicalli.gameboard.environment :as environment]

;; System dependencies
[org.httpkit.server :as http-server]
[integrant.core :as ig]
[com.brunobonacci.mulog :as mulog]))
[com.brunobonacci.mulog :as mulog]
[integrant.core :as ig]
;; System dependencies
[org.httpkit.server :as http-server]
[practicalli.gameboard.environment :as environment]
;; Application dependencies
[practicalli.gameboard.router :as router]))


;; --------------------------------------------------
;; Configure and start application components

;; Start mulog publisher for the given publisher type, i.e. console, cloud-watch
#_{:clj-kondo/ignore [:unused-binding]}
(defmethod ig/init-key ::log-publish
[_ {:keys [mulog] :as config}]
(mulog/log ::log-publish-component :publisher-config mulog :local-time (java.time.LocalDateTime/now))
(let [publisher (mulog/start-publisher! mulog)]
publisher))

;; Connection values for Relational Database
;; return hash-map of connection values: endpoint, access-key, secret-key
(defmethod ig/init-key ::relational-store
[_ {:keys [connection] :as config}]
(mulog/log ::persistence-component :connection connection :local-time (java.time.LocalDateTime/now))
config)

;; Configure environment for router application, e.g. database connection details, etc.
(defmethod ig/init-key ::router
[_ config]
Expand All @@ -57,21 +41,11 @@
(http-server-instance))


;; Shutdown Log publishing
(defmethod ig/halt-key! ::log-publish
[_ publisher]
(mulog/log ::log-publish-component-shutdown :publisher-object publisher :local-time (java.time.LocalDateTime/now))
;; Pause so final messages have chance to be published
(Thread/sleep 250)
;; Call publisher again to stop publishing
(publisher))


(defn stop
"Stop service using Integrant halt!"
[system]
(mulog/log ::http-server-sigterm :system system :local-time (java.time.LocalDateTime/now))
;; (println "Shutdown of Practicalli Gameboard service via Integrant")
;; (println "Shutdown of Practicalli service via Integrant")
(ig/halt! system))


Expand All @@ -92,7 +66,7 @@

;; Add keys to every event / publish profile use to start the service
_ (mulog/set-global-context!
{:app-name "Practicalli Gameboard Service" :version "0.1.0" :env profile})
{:app-name "Practicalli Service" :version "0.1.0" :env profile})

system (ig/init (environment/aero-prep profile))

Expand Down
1 change: 1 addition & 0 deletions flavors/cupcake/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ RUN apk add --update --no-cache \
php8-simplexml \
composer \
dpkg \
zlib \
nodejs \
npm \
yarn \
Expand Down
1 change: 1 addition & 0 deletions flavors/java/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ RUN apk add --update --no-cache \
openssh \
go \
openjdk11 \
zlib \
nodejs \
npm \
yarn \
Expand Down
6 changes: 6 additions & 0 deletions megalinter/descriptors/clojure.megalinter-descriptor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ linters:
- "cljstyle fix"
- "clj-kondo fix myfile.clj path/to/myfile.clj"
install:
# Add compression library for cljstyle fix
apk:
- zlib
- zlib-dev
# https://github.com/abogoyavlensky/docker/blob/master/cljstyle/Dockerfile
# TODO: create practicalli/cljstyle docker image that works with megalinter
dockerfile:
- FROM abogoyavlensky/cljstyle:0.15.0 as cljstyle
- COPY --from=cljstyle /usr/local/bin/cljstyle /usr/bin/
Expand Down

0 comments on commit 2495266

Please sign in to comment.