Skip to content

Commit

Permalink
Add cli interface
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Aug 5, 2024
1 parent 7566c8b commit 7b6bd0a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
1 change: 1 addition & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -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"}}
Expand Down
43 changes: 38 additions & 5 deletions src/k16/forge.clj
Original file line number Diff line number Diff line change
@@ -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))
32 changes: 29 additions & 3 deletions src/k16/forge/runner.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 7b6bd0a

Please sign in to comment.