Skip to content

Instantly share code, notes, and snippets.

@javahippie
Created March 19, 2022 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javahippie/c5e85360896eef1f53ee21a3f81418c7 to your computer and use it in GitHub Desktop.
Save javahippie/c5e85360896eef1f53ee21a3f81418c7 to your computer and use it in GitHub Desktop.
An early, naive draft for a testcontainers kaocha plugin
(ns com.lambdaschmiede.kaocha-testcontainers
(:require [kaocha.plugin :as p]
[clj-test-containers.core :as tc]))
(def ^:dynamic for-all-scope {})
(def ^:dynamic for-each-scope {})
(defn get-container [id]
(get (merge for-each-scope for-all-scope) id))
(defn- containers-for [type config]
(filter (fn [{:keys [for]}] (= type for)) config))
(defn- start-containers [type kaocha-testcontainers]
(into {}
(for [{:keys [id config]} (containers-for type kaocha-testcontainers)]
[id (tc/start! (tc/create config))])))
(p/defplugin com.lambdaschmiede.kaocha-testcontainers/plugin
;; Runs before each individual test
(pre-test [test {:keys [kaocha-testcontainers]}]
(def tst test)
(let [test-var (:kaocha.var/test test)]
(assoc test :kaocha.var/test (fn []
(binding [for-each-scope (start-containers :each kaocha-testcontainers)]
(let [res (test-var)]
(for [[_ container] for-each-scope]
(tc/stop! container))
res))))))
;; Allows "wrapping" the run function
(wrap-run [run {:keys [kaocha-testcontainers]}]
(fn [test test-plan]
(binding [for-all-scope (start-containers :all kaocha-testcontainers)]
(let [result (run test test-plan)]
(tc/perform-cleanup!)
result)))))
(comment
(require '[kaocha.repl :as k])
(k/run :unit))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment