From 7b6bd0ac05973507f8114f232219e4648252f2cb Mon Sep 17 00:00:00 2001 From: Julien Vincent Date: Mon, 5 Aug 2024 10:38:12 +0100 Subject: [PATCH] Add cli interface --- deps.edn | 1 + src/k16/forge.clj | 43 +++++++++++++++++++++++++++++++++++----- src/k16/forge/runner.clj | 32 +++++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/deps.edn b/deps.edn index 74bb171..78b7d6f 100644 --- a/deps.edn +++ b/deps.edn @@ -1,5 +1,6 @@ {:deps {org.clj-commons/pretty {:mvn/version "3.0.0"} mvxcvi/puget {:mvn/version "1.3.4"} + cli-matic/cli-matic {:mvn/version "0.5.4"} lambdaisland/deep-diff2 {:mvn/version "2.11.216"}} :aliases {:kmono {:deps {com.kepler16/kmono {:mvn/version "2.0.1.0"}} diff --git a/src/k16/forge.clj b/src/k16/forge.clj index 2dd63e8..b536423 100644 --- a/src/k16/forge.clj +++ b/src/k16/forge.clj @@ -1,9 +1,42 @@ (ns k16.forge - (:refer-clojure :exclude [run!]) - (:require [k16.forge.runner :as runner])) + (:gen-class) + (:require + [cli-matic.core :as cli] + [clojure.string :as str] + [k16.forge.runner :as runner])) -(defn run! [_] - (runner/run-all)) +(def ^:private cli-configuration + {:command "forge" + :description "A simple test runner for clojure.test" + :version "0.0.0" + :opts [{:option "focus" + :short "f" + :as "Only run the given test or namespace. Alias for '--include'" + :type :string} + + {:option "include" + :short "i" + :as "Only run the test or namespace described by the given pattern" + :type :string} + + {:option "exclude" + :short "e" + :as "Exclude the test or namespace described by the given pattern from running" + :type :string} + + {:option "parallelism" + :short "p" + :as "The maximum number of tests to run concurrently. Defaults to the number of cores" + :type :int}] + :runs + (fn [props] + (let [include (or (:include props) + (:focus props)) + exclude (:exclude props) + props (cond-> props + include (assoc :include (str/split include #",")) + exclude (assoc :exclude (str/split exclude #",")))] + (runner/run-all props)))}) (defn -main [& args] - (runner/run-all)) + (cli/run-cmd args cli-configuration)) diff --git a/src/k16/forge/runner.clj b/src/k16/forge/runner.clj index ec6d0cb..6080867 100644 --- a/src/k16/forge/runner.clj +++ b/src/k16/forge/runner.clj @@ -51,10 +51,36 @@ @*reports*)) -(defn run-all [] +(defn- contains-pattern? [sym patterns] + (reduce + (fn [_ pattern] + (let [pattern (re-pattern pattern)] + (if (re-find pattern (str sym)) + (reduced true) + false))) + false + patterns)) + +(defn- filter-namespaces [namespaces include exclude] + (filter + (fn [namespace] + (let [included (if (seq include) + (contains-pattern? namespace include) + true) + excluded (if (seq exclude) + (contains-pattern? namespace exclude) + false)] + (and included (not excluded)))) + namespaces)) + +(defn run-all [props] (with-redefs [test/report report-handler] - (let [namespaces (forge.namespace/get-test-namespaces) - pool (Executors/newFixedThreadPool (.availableProcessors (Runtime/getRuntime)))] + (let [parallelism (or (:parallelism props) + (.availableProcessors (Runtime/getRuntime))) + + namespaces (-> (forge.namespace/get-test-namespaces) + (filter-namespaces (:include props) (:exclude props))) + pool (Executors/newFixedThreadPool parallelism)] (doseq [ns namespaces] (require ns))