diff --git a/.circleci/config.yml b/.circleci/config.yml index c81dc6c..1f42467 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -17,10 +17,6 @@ jobs: steps: - 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 - restore_cache: keys: @@ -28,15 +24,23 @@ jobs: # fallback to using the latest cache if no exact match is found - 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 - save_cache: paths: + - ~/.lein - ~/.m2 key: v1-dependencies-{{ checksum "project.clj" }} # run tests! - run: lein test + # check style + - run: lein cljstyle check --report + - store_test_results: path: target diff --git a/.cljstyle b/.cljstyle new file mode 100644 index 0000000..47bb398 --- /dev/null +++ b/.cljstyle @@ -0,0 +1,3 @@ +{:file-pattern #"\.(clj[sc]?|edn|cljstyle)$" + :list-indent-size 1 + :padding-lines 1} diff --git a/README.md b/README.md index ecf4ab8..1227bc9 100644 --- a/README.md +++ b/README.md @@ -372,9 +372,6 @@ Creates a network. The optional map accepts config values for enabling ipv6 and (init-network) ``` - - - ## License Copyright © 2020 Tim Zöller diff --git a/project.clj b/project.clj index 5faf86f..1a93bcf 100644 --- a/project.clj +++ b/project.clj @@ -9,12 +9,19 @@ :dependencies [[org.clojure/clojure "1.10.1"] [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"] [lambdaisland/kaocha-cloverage "1.0-45"] [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") diff --git a/script/pre-commit b/script/pre-commit new file mode 100755 index 0000000..6e50f82 --- /dev/null +++ b/script/pre-commit @@ -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!" diff --git a/src/clj_test_containers/core.clj b/src/clj_test_containers/core.clj index 9c0a5a6..6547c0d 100644 --- a/src/clj_test_containers/core.clj +++ b/src/clj_test_containers/core.clj @@ -1,10 +1,17 @@ (ns clj-test-containers.core - (:require [clojure.spec.alpha :as s]) - (:import [org.testcontainers.containers GenericContainer] - [org.testcontainers.utility MountableFile] - [org.testcontainers.containers BindMode Network] - [org.testcontainers.images.builder ImageFromDockerfile] - [java.nio.file Paths])) + (:require + [clojure.spec.alpha :as s]) + (:import + (java.nio.file + Paths) + (org.testcontainers.containers + BindMode + GenericContainer + Network) + (org.testcontainers.images.builder + ImageFromDockerfile) + (org.testcontainers.utility + MountableFile))) (defn- resolve-bind-mode [bind-mode] @@ -115,11 +122,10 @@ (dissoc :id) (dissoc :mapped-ports))) - (defn- build-network [{:keys [ipv6 driver]}] - (let [builder (Network/builder)] - + (let [builder (Network/builder)] + (when ipv6 (.enableIpv6 builder true)) @@ -127,7 +133,7 @@ (.driver builder driver)) (let [network (.build builder)] - {:network network + {:network network :id (.getId network) :name (.getName network) :ipv6 (.getEnableIpv6 network) diff --git a/test/clj_test_containers/core_test.clj b/test/clj_test_containers/core_test.clj index 52499ff..5053cc9 100644 --- a/test/clj_test_containers/core_test.clj +++ b/test/clj_test_containers/core_test.clj @@ -1,13 +1,16 @@ (ns clj-test-containers.core-test - (:require [clojure.test :refer :all] - [clj-test-containers.core :refer :all]) - (:import [org.testcontainers.containers PostgreSQLContainer])) + (:require + [clj-test-containers.core :refer :all] + [clojure.test :refer :all]) + (:import + (org.testcontainers.containers + PostgreSQLContainer))) (deftest create-test (testing "Testing basic testcontainer generic image initialisation" (let [container (create {:image-name "postgres:12.2" - :exposed-ports [5432] + :exposed-ports [5432] :env-vars {"POSTGRES_PASSWORD" "pw"}}) initialized-container (start! container) stopped-container (stop! container)] @@ -53,7 +56,7 @@ (testing "Testing mapping of a classpath resource" (let [container (-> (create {:image-name "postgres:12.2" - :exposed-ports [5432] + :exposed-ports [5432] :env-vars {"POSTGRES_PASSWORD" "pw"}}) (map-classpath-resource! {:resource-path "test.sql" :container-path "/opt/test.sql" @@ -70,7 +73,7 @@ (testing "Testing mapping of a filesystem-binding" (let [container (-> (create {:image-name "postgres:12.2" - :exposed-ports [5432] + :exposed-ports [5432] :env-vars {"POSTGRES_PASSWORD" "pw"}}) (bind-filesystem! {:host-path "." :container-path "/opt" @@ -87,7 +90,7 @@ (testing "Copying a file from the host into the container" (let [container (-> (create {:image-name "postgres:12.2" - :exposed-ports [5432] + :exposed-ports [5432] :env-vars {"POSTGRES_PASSWORD" "pw"}}) (copy-file-to-container! {:path "test.sql" :container-path "/opt/test.sql" @@ -104,7 +107,7 @@ (testing "Copying a file from the classpath into the container" (let [container (-> (create {:image-name "postgres:12.2" - :exposed-ports [5432] + :exposed-ports [5432] :env-vars {"POSTGRES_PASSWORD" "pw"}}) (copy-file-to-container! {:path "test.sql" :container-path "/opt/test.sql" @@ -119,17 +122,16 @@ (is (nil? (:id stopped-container))) (is (nil? (:mapped-ports stopped-container)))))) - (deftest networking-test - + (testing "Putting two containers into the same network and check their communication" (let [network (init-network) server-container (create {:image-name "alpine:3.5" :network network :network-aliases ["foo"] :command ["/bin/sh" - "-c" - "while true ; do printf 'HTTP/1.1 200 OK\\n\\nyay' | nc -l -p 8080; done"]}) + "-c" + "while true ; do printf 'HTTP/1.1 200 OK\\n\\nyay' | nc -l -p 8080; done"]}) client-container (create {:image-name "alpine:3.5" :network network :command ["top"]}) @@ -138,6 +140,6 @@ response (execute-command! started-client ["wget", "-O", "-", "http://foo:8080"]) stopped-server (stop! started-server) stopped-client (stop! started-client)] - + (is (= 0 (:exit-code response))) (is (= "yay" (:stdout response))))))