From 1e432c65442fd47870f785f4e459adf738c850f6 Mon Sep 17 00:00:00 2001 From: Kenny Williams Date: Thu, 17 Mar 2022 09:17:13 -0700 Subject: [PATCH 1/6] Allow :kaocha.plugin.randomize/randomize? to be set at any test level. --- CHANGELOG.md | 4 ++- src/kaocha/plugin/randomize.clj | 58 ++++++++++++++++++++++++--------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index adcfc672..2c6cf7b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Added +- Allow :kaocha.plugin.randomize/randomize? to be set at any test level. + ## Fixed ## Changed @@ -855,4 +857,4 @@ namespace. - The configuration format has changed, you should now start with the `#kaocha {}` tagged reader literal in `tests.edn` to provide defaults. If you want more control then overwrite `tests.edn` with the output of `--print-config` and - tweak. \ No newline at end of file + tweak. diff --git a/src/kaocha/plugin/randomize.clj b/src/kaocha/plugin/randomize.clj index 1ee611ff..281b2d46 100644 --- a/src/kaocha/plugin/randomize.clj +++ b/src/kaocha/plugin/randomize.clj @@ -16,16 +16,41 @@ (map straight-sort))) test-plan)) -(defn rng-sort [rng test-plan] +(defmacro or-some + ([] nil) + ([x] x) + ([x & next] + `(let [or# ~x] + (if (some? or#) or# (or-some ~@next))))) + +(defn get-randomize + "Returns the randomize? value for the test plan. Metadata takes precedence + over test-plan." + [test-plan] + (or-some + (get-in test-plan [:kaocha.testable/meta ::randomize?]) + (::randomize? test-plan))) + +(defn rng-sort [rng parent-randomize? test-plan] (if-let [tests (:kaocha.test-plan/tests test-plan)] (assoc test-plan :kaocha.test-plan/tests - (->> tests - (map #(assoc % ::sort-key (rng))) - (sort-by ::sort-key) - (map (partial rng-sort rng)))) + (let [randomize? (or-some (get-randomize test-plan) parent-randomize?)] + (map (partial rng-sort rng randomize?) + (if randomize? + (->> tests + (map #(assoc % ::sort-key (rng))) + (sort-by ::sort-key)) + tests)))) test-plan)) +(defn uses-randomize? + "Returns true if the test-plan uses ::randomize? at any test level." + [test-plan] + (if (get-randomize test-plan) + true + (some uses-randomize? (:kaocha.test-plan/tests test-plan)))) + (defplugin kaocha.plugin/randomize (cli-options [opts] (conj opts @@ -34,25 +59,26 @@ :parse-fn #(Integer/parseInt %)])) (config [config] - (let [randomize? (get-in config [:kaocha/cli-options :randomize]) - seed (get-in config [:kaocha/cli-options :seed]) - config (merge {::randomize? true} - config - (when (some? randomize?) - {::randomize? randomize?}))] - (if (::randomize? config) + (let [randomize? (get-in config [:kaocha/cli-options :randomize]) + no-randomize? (= false randomize?) + seed (get-in config [:kaocha/cli-options :seed]) + config (merge {::randomize? true} + config + (when (some? randomize?) + {::randomize? randomize?}))] + (if (not no-randomize?) (merge {::seed (or seed (rand-int Integer/MAX_VALUE))} config) config))) (post-load [test-plan] - (if (::randomize? test-plan) - (let [rng (rng (::seed test-plan))] + (if-let [seed (::seed test-plan)] + (let [rng (rng seed)] (->> test-plan straight-sort - (rng-sort rng))) + (rng-sort rng (get-randomize test-plan)))) test-plan)) (post-run [test-plan] - (if (and (::randomize? test-plan) (result/failed? test-plan)) + (if (and (::seed test-plan) (uses-randomize? test-plan) (result/failed? test-plan)) (print "\nRandomized with --seed" (::seed test-plan))) test-plan)) From 9d5f3ed5c037c2fffabac0c9c103ddc5a4194c6e Mon Sep 17 00:00:00 2001 From: Kenny Williams Date: Sat, 2 Apr 2022 06:05:14 -0700 Subject: [PATCH 2/6] Remove straight-sort call since it's gets later overriden by rng-sort. --- src/kaocha/plugin/randomize.clj | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/kaocha/plugin/randomize.clj b/src/kaocha/plugin/randomize.clj index 281b2d46..af7a809e 100644 --- a/src/kaocha/plugin/randomize.clj +++ b/src/kaocha/plugin/randomize.clj @@ -7,15 +7,6 @@ (let [rng (java.util.Random. seed)] (fn [& _] (.nextInt rng)))) -(defn straight-sort [test-plan] - (if-let [tests (:kaocha.test-plan/tests test-plan)] - (assoc test-plan - :kaocha.test-plan/tests - (->> tests - (sort-by :kaocha.testable/id) - (map straight-sort))) - test-plan)) - (defmacro or-some ([] nil) ([x] x) @@ -73,9 +64,7 @@ (post-load [test-plan] (if-let [seed (::seed test-plan)] (let [rng (rng seed)] - (->> test-plan - straight-sort - (rng-sort rng (get-randomize test-plan)))) + (rng-sort rng (get-randomize test-plan) test-plan)) test-plan)) (post-run [test-plan] From 5cd4553160c9ebdb7c566d81a62222edaba4d443 Mon Sep 17 00:00:00 2001 From: Kenny Williams Date: Sat, 2 Apr 2022 06:05:35 -0700 Subject: [PATCH 3/6] Update print_config.feature. --- .../command_line/print_config.feature | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/test/features/command_line/print_config.feature b/test/features/command_line/print_config.feature index 902c7a63..36e2dbdc 100644 --- a/test/features/command_line/print_config.feature +++ b/test/features/command_line/print_config.feature @@ -13,18 +13,17 @@ Feature: CLI: Print the Kaocha configuration When I run `bin/kaocha --print-config` Then the output should contain: """ clojure - {:kaocha.plugin.randomize/randomize? false, - :kaocha/reporter [kaocha.report/dots], + :kaocha.plugin.randomize/randomize? false, + """ + And the output should contain: + """ clojure + :kaocha/reporter [kaocha.report/dots] + """ + And the output should contain: + """ clojure :kaocha/color? false, - :kaocha/fail-fast? false, """ And the output should contain: """ clojure - :kaocha/tests - [{:kaocha.testable/type :kaocha.type/clojure.test, - :kaocha.testable/id :unit, - :kaocha/ns-patterns ["-test$"], - :kaocha/source-paths ["src"], - :kaocha/test-paths ["test"], - :kaocha.filter/skip-meta [:kaocha/skip]}], + :kaocha/fail-fast? false, """ From 5583cda19ba6ccd7ec67a7fb675f6137e12097e4 Mon Sep 17 00:00:00 2001 From: Kenny Williams Date: Sat, 2 Apr 2022 06:07:40 -0700 Subject: [PATCH 4/6] Add rng-sort-test unit test --- test/unit/kaocha/plugin/randomize_test.clj | 23 +++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/test/unit/kaocha/plugin/randomize_test.clj b/test/unit/kaocha/plugin/randomize_test.clj index 6dd6bc3c..1cec12fb 100644 --- a/test/unit/kaocha/plugin/randomize_test.clj +++ b/test/unit/kaocha/plugin/randomize_test.clj @@ -2,7 +2,8 @@ (:require [clojure.test :refer :all] [kaocha.test-helper :refer :all] [kaocha.plugin :as plugin] - [kaocha.testable :as testable])) + [kaocha.testable :as testable] + [kaocha.plugin.randomize :as randomize])) (def plugin-chain (plugin/register :kaocha.plugin/randomize [])) @@ -14,6 +15,26 @@ :kaocha/test-paths ["fixtures/c-tests"] :kaocha/ns-patterns [".*"]}) +(deftest rng-sort-test + (let [rng (randomize/rng 123)] + (is (= {} + (randomize/rng-sort rng true + {}))) + (is (= {:kaocha.test-plan/tests [{:kaocha.plugin.randomize/sort-key -1188957731}]} + (randomize/rng-sort rng true + {:kaocha.test-plan/tests [{}]}))) + (is (= {:kaocha.test-plan/tests [{}]} + (randomize/rng-sort rng false + {:kaocha.test-plan/tests [{}]}))) + (is (match? {:kaocha.test-plan/tests [{}]} + (randomize/rng-sort rng true + {:kaocha.testable/meta {:kaocha.plugin.randomize/randomize? false} + :kaocha.test-plan/tests [{}]}))) + (is (match? {:kaocha.test-plan/tests [{}]} + (randomize/rng-sort rng true + {:kaocha.plugin.randomize/randomize? false + :kaocha.test-plan/tests [{}]}))))) + (deftest randomize-test (plugin/with-plugins plugin-chain (is (match? {:kaocha.plugin.randomize/randomize? true From e5f8a55baf362e8e163f84dabb99ee51fc1cbd73 Mon Sep 17 00:00:00 2001 From: Kenny Williams Date: Sat, 2 Apr 2022 06:37:33 -0700 Subject: [PATCH 5/6] Add randomize plugin docs. Co-authored-by: Arne Brasseur --- doc/08_plugins.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/doc/08_plugins.md b/doc/08_plugins.md index d297c336..027df62f 100644 --- a/doc/08_plugins.md +++ b/doc/08_plugins.md @@ -180,3 +180,54 @@ To customize which keys to print, use Kaocha's "bindings" functionality, in `tes ``` clojure :kaocha/bindings {kaocha.plugin.debug/*keys* [,,,]} ``` + +## Randomize + +The randomize plugin picks a random seed during the `config` hook and uses that +seed to randomize the order of test suites, namespaces, and test vars during the +`post-load` hook. + +Randomization can be toggled at the following places. + +1. top level config +2. test suite config +3. namespace metadata + +At each level, changing the randomize value will override the previously set +value. For example, you could: + +- randomize by default (top level `:kaocha.plugin.randomize/randomize? true`, + which is the default) +- exclude a specific test suite by setting `:kaocha.plugin.randomize/randomize? false` + on the suite in tests.edn +- but randomize a single namespace within that suite `(ns ^{:kaocha.plugin.randomize/randomize? true} ...)` + +Or the inverse (case 2) + +- turn it off at the top level +- but turn it on for certain tests suites +- with the exception of certain namespaces + +Passing the `--no-randomize` CLI flag will force all randomization to be disabled. + +### Enabling + +The randomize plugin is enabled by default. Enable or disable randomization by +setting the `:kaocha.plugin.randomize/randomize?` key in tests.edn at the top-level, +in a specific test suite, or on the metadata of a namespace. + +### Plugin-specific command line flags + +```shell +--[no-]randomize Run test namespaces and vars in random order. +--seed SEED Provide a seed to determine the random order of tests. +``` + +### Plugin-specific configuration options + +Shown with their default values: + +```clojure +#kaocha/v1 +{:kaocha.plugin.randomize/randomize? true} +``` From 4edf40943136b1a087e100c08e956182fd6c2631 Mon Sep 17 00:00:00 2001 From: Kenny Williams Date: Sat, 2 Apr 2022 06:37:45 -0700 Subject: [PATCH 6/6] Link to randomize documentation. --- doc/03_configuration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/03_configuration.md b/doc/03_configuration.md index e770cb91..30612b78 100644 --- a/doc/03_configuration.md +++ b/doc/03_configuration.md @@ -216,7 +216,7 @@ bin/kaocha --plugin profiling Some plugins are needed for the normal functioning of Kaocha. These are added automatically when using the `#kaocha/v1 {}` reader literal. They are -- `:kaocha.plugin/randomize`: randomize test order +- `:kaocha.plugin/randomize`: randomize test order ([documentation](08_plugins.md#randomize)) - `:kaocha.plugin/filter`: allow filtering and "focusing" of tests - `:kaocha.plugin/capture-output`: implements output capturing during tests