cljstyle redux, with pre-commit hook (#24)

* cljstyle redux, with pre-commit

* Use fork of lein-githooks with fixes for shell characters

* Remove stale doc
This commit is contained in:
Rob Hanlon 2020-08-09 12:38:20 -07:00 committed by GitHub
parent a327fc3f75
commit 36f3820f4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 72 additions and 32 deletions

View file

@ -17,10 +17,6 @@ jobs:
steps: steps:
- checkout - checkout
- run:
name: Install Leiningen
command: wget https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein && chmod a+x lein && ./lein
# Download and cache dependencies # Download and cache dependencies
- restore_cache: - restore_cache:
keys: keys:
@ -28,15 +24,23 @@ jobs:
# fallback to using the latest cache if no exact match is found # fallback to using the latest cache if no exact match is found
- v1-dependencies- - v1-dependencies-
- run:
name: Install Leiningen
command: wget https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein && chmod a+x lein && lein --version
- run: lein deps - run: lein deps
- save_cache: - save_cache:
paths: paths:
- ~/.lein
- ~/.m2 - ~/.m2
key: v1-dependencies-{{ checksum "project.clj" }} key: v1-dependencies-{{ checksum "project.clj" }}
# run tests! # run tests!
- run: lein test - run: lein test
# check style
- run: lein cljstyle check --report
- store_test_results: - store_test_results:
path: target path: target

3
.cljstyle Normal file
View file

@ -0,0 +1,3 @@
{:file-pattern #"\.(clj[sc]?|edn|cljstyle)$"
:list-indent-size 1
:padding-lines 1}

View file

@ -372,9 +372,6 @@ Creates a network. The optional map accepts config values for enabling ipv6 and
(init-network) (init-network)
``` ```
## License ## License
Copyright © 2020 Tim Zöller Copyright © 2020 Tim Zöller

View file

@ -9,12 +9,19 @@
:dependencies [[org.clojure/clojure "1.10.1"] :dependencies [[org.clojure/clojure "1.10.1"]
[org.testcontainers/testcontainers "1.14.3"]] [org.testcontainers/testcontainers "1.14.3"]]
:aliases {"test" ["run" "-m" "kaocha.runner"]} :aliases {"test" ["run" "-m" "kaocha.runner"]
"cljstyle" ["run" "-m" "cljstyle.main"]}
:plugins [[jainsahab/lein-githooks "1.0.0"]]
:profiles {:dev {:dependencies [[org.testcontainers/postgresql "1.14.3"] :profiles {:dev {:dependencies [[org.testcontainers/postgresql "1.14.3"]
[lambdaisland/kaocha-cloverage "1.0-45"] [lambdaisland/kaocha-cloverage "1.0-45"]
[lambdaisland/kaocha "1.0.641"] [lambdaisland/kaocha "1.0.641"]
[lambdaisland/kaocha-junit-xml "0.0.76"]]}} [lambdaisland/kaocha-junit-xml "0.0.76"]
[mvxcvi/cljstyle "0.13.0" :exclusions [org.clojure/clojure]]]
:githooks {:auto-install true
:ci-env-variable "CI"
:pre-commit ["script/pre-commit"]}}}
:target-path "target/%s") :target-path "target/%s")

21
script/pre-commit Executable file
View file

@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.."
FILES=$(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g')
if [[ -z "$FILES" ]]; then
exit 0
fi
echo "Fixing Clojure style..."
# Format all selected files
echo "$FILES" | xargs lein cljstyle fix --report
# Add back the modified/prettified files to staging
echo "$FILES" | xargs git add
echo "Done!"

View file

@ -1,10 +1,17 @@
(ns clj-test-containers.core (ns clj-test-containers.core
(:require [clojure.spec.alpha :as s]) (:require
(:import [org.testcontainers.containers GenericContainer] [clojure.spec.alpha :as s])
[org.testcontainers.utility MountableFile] (:import
[org.testcontainers.containers BindMode Network] (java.nio.file
[org.testcontainers.images.builder ImageFromDockerfile] Paths)
[java.nio.file Paths])) (org.testcontainers.containers
BindMode
GenericContainer
Network)
(org.testcontainers.images.builder
ImageFromDockerfile)
(org.testcontainers.utility
MountableFile)))
(defn- resolve-bind-mode (defn- resolve-bind-mode
[bind-mode] [bind-mode]
@ -115,11 +122,10 @@
(dissoc :id) (dissoc :id)
(dissoc :mapped-ports))) (dissoc :mapped-ports)))
(defn- build-network (defn- build-network
[{:keys [ipv6 driver]}] [{:keys [ipv6 driver]}]
(let [builder (Network/builder)] (let [builder (Network/builder)]
(when ipv6 (when ipv6
(.enableIpv6 builder true)) (.enableIpv6 builder true))
@ -127,7 +133,7 @@
(.driver builder driver)) (.driver builder driver))
(let [network (.build builder)] (let [network (.build builder)]
{:network network {:network network
:id (.getId network) :id (.getId network)
:name (.getName network) :name (.getName network)
:ipv6 (.getEnableIpv6 network) :ipv6 (.getEnableIpv6 network)

View file

@ -1,13 +1,16 @@
(ns clj-test-containers.core-test (ns clj-test-containers.core-test
(:require [clojure.test :refer :all] (:require
[clj-test-containers.core :refer :all]) [clj-test-containers.core :refer :all]
(:import [org.testcontainers.containers PostgreSQLContainer])) [clojure.test :refer :all])
(:import
(org.testcontainers.containers
PostgreSQLContainer)))
(deftest create-test (deftest create-test
(testing "Testing basic testcontainer generic image initialisation" (testing "Testing basic testcontainer generic image initialisation"
(let [container (create {:image-name "postgres:12.2" (let [container (create {:image-name "postgres:12.2"
:exposed-ports [5432] :exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}}) :env-vars {"POSTGRES_PASSWORD" "pw"}})
initialized-container (start! container) initialized-container (start! container)
stopped-container (stop! container)] stopped-container (stop! container)]
@ -53,7 +56,7 @@
(testing "Testing mapping of a classpath resource" (testing "Testing mapping of a classpath resource"
(let [container (-> (create {:image-name "postgres:12.2" (let [container (-> (create {:image-name "postgres:12.2"
:exposed-ports [5432] :exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}}) :env-vars {"POSTGRES_PASSWORD" "pw"}})
(map-classpath-resource! {:resource-path "test.sql" (map-classpath-resource! {:resource-path "test.sql"
:container-path "/opt/test.sql" :container-path "/opt/test.sql"
@ -70,7 +73,7 @@
(testing "Testing mapping of a filesystem-binding" (testing "Testing mapping of a filesystem-binding"
(let [container (-> (create {:image-name "postgres:12.2" (let [container (-> (create {:image-name "postgres:12.2"
:exposed-ports [5432] :exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}}) :env-vars {"POSTGRES_PASSWORD" "pw"}})
(bind-filesystem! {:host-path "." (bind-filesystem! {:host-path "."
:container-path "/opt" :container-path "/opt"
@ -87,7 +90,7 @@
(testing "Copying a file from the host into the container" (testing "Copying a file from the host into the container"
(let [container (-> (create {:image-name "postgres:12.2" (let [container (-> (create {:image-name "postgres:12.2"
:exposed-ports [5432] :exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}}) :env-vars {"POSTGRES_PASSWORD" "pw"}})
(copy-file-to-container! {:path "test.sql" (copy-file-to-container! {:path "test.sql"
:container-path "/opt/test.sql" :container-path "/opt/test.sql"
@ -104,7 +107,7 @@
(testing "Copying a file from the classpath into the container" (testing "Copying a file from the classpath into the container"
(let [container (-> (create {:image-name "postgres:12.2" (let [container (-> (create {:image-name "postgres:12.2"
:exposed-ports [5432] :exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}}) :env-vars {"POSTGRES_PASSWORD" "pw"}})
(copy-file-to-container! {:path "test.sql" (copy-file-to-container! {:path "test.sql"
:container-path "/opt/test.sql" :container-path "/opt/test.sql"
@ -119,17 +122,16 @@
(is (nil? (:id stopped-container))) (is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container)))))) (is (nil? (:mapped-ports stopped-container))))))
(deftest networking-test (deftest networking-test
(testing "Putting two containers into the same network and check their communication" (testing "Putting two containers into the same network and check their communication"
(let [network (init-network) (let [network (init-network)
server-container (create {:image-name "alpine:3.5" server-container (create {:image-name "alpine:3.5"
:network network :network network
:network-aliases ["foo"] :network-aliases ["foo"]
:command ["/bin/sh" :command ["/bin/sh"
"-c" "-c"
"while true ; do printf 'HTTP/1.1 200 OK\\n\\nyay' | nc -l -p 8080; done"]}) "while true ; do printf 'HTTP/1.1 200 OK\\n\\nyay' | nc -l -p 8080; done"]})
client-container (create {:image-name "alpine:3.5" client-container (create {:image-name "alpine:3.5"
:network network :network network
:command ["top"]}) :command ["top"]})
@ -138,6 +140,6 @@
response (execute-command! started-client ["wget", "-O", "-", "http://foo:8080"]) response (execute-command! started-client ["wget", "-O", "-", "http://foo:8080"])
stopped-server (stop! started-server) stopped-server (stop! started-server)
stopped-client (stop! started-client)] stopped-client (stop! started-client)]
(is (= 0 (:exit-code response))) (is (= 0 (:exit-code response)))
(is (= "yay" (:stdout response)))))) (is (= "yay" (:stdout response))))))