Fork me on GitHub
#tools-deps
<
2021-12-11
>
Chase01:12:27

Is there a way to send something like a watch command to the cognitect-labs test-runner using the cli so that whenever I save code it runs the tests automatically in the background?

seancorfield02:12:27

It would be fairly easy to create a watcher in the REPL that could kick off tests...

$ clojure -Sdeps '{:deps {juxt/dirwatch {:mvn/version "RELEASE"}}}' -M:rebel
[Rebel readline] Type :repl/help for online help info
user=> (require '[juxt.dirwatch :refer [watch-dir]] '[ :as io])
nil
user=> (watch-dir println (io/file "."))
#object[clojure.lang.Agent 0x32f5ecc4 {:status :ready, :val #object[sun.nio.fs.LinuxWatchService 0x7ef8eda7 "sun.nio.fs.LinuxWatchService@7ef8eda7"]}]
user=> (spit "ex.txt" "Hello, Watcher!")
{:file #object[java.io.File 0x713dbaa1 ./.rebel_readline_history], :count 1, :action :modify}
nil
{:file #object[java.io.File 0x662be223 ./ex.txt], :count 1, :action :create}
{:file #object[java.io.File 0x76cdf12f ./ex.txt], :count 1, :action :modify}
user=> (spit "ex.txt" "Hello, Watcher!")
nil
{:file #object[java.io.File
0x455a35c1 ./ex.txt], :count 2, :action :modify}
user=>

seancorfield02:12:58

The Cognitect test-runner has a programmatic -X entry point that you could invoke from a watcher.

Chase02:12:58

Ok, cool, I'll look into that option. Looks like koacha has a watch command too if I want to explore that one. These exercism exercises come with the cognitect one though so I figured I would explore that

seancorfield02:12:16

You'd probably want to run it in a thread and keep track of when it is running (so you don't run tests in parallel) but also keep track of changes so you know to run it again if changes came in while the previous run was in progress...

seancorfield02:12:00

Really though you probably want incremental testing to make watching worthwhile...

seancorfield02:12:05

Frankly, I don't like that workflow. I prefer to run tests via hotkeys in my editor all the time I'm editing (and evaluating) code. I have a hot key bound to, essentially, "find the current ns's tests and run them" and another hot key bound to "run the tests in the current ns" (to allow for whether I'm in a code file or a test file.

seancorfield02:12:10

But my workflow is all in the REPL. I don't switch to a terminal for any of that. Edit, eval, edit, eval, test. A lot of it without saving files 🙂

Chase02:12:49

That's what I've been currently doing as well and it works great. But I like to tinker with my tooling and workflow every now and then

Chase03:12:52

By that I mean I was somehow getting too lazy to even type ,tn lol

practicalli-johnny15:12:30

@U9J50BY4C I've added a :test/watch alias that runs koacha in watch mode (with fail-fast) to https://practical.li/clojure/clojure-tools/install/community-tools.html, so I can run quickly run koacha with any Clojure project.

clojure -M:test/watch
I like to run koacha before pushing commits to a project as a sanity check https://practical.li/clojure/testing/test-runners/kaocha-test-runner.html

Alex Miller (Clojure team)02:12:13

Nothing in test-runner but you can use os watch stuff for that