is there some way to ask a live clojure session whether some file has changed since it was most recently loaded? Sometimes during editing I forget to reload some code I changed, and it is really difficult to figure out which. So I end up just restarting. Not the end of the world, but if there’s an easier way it could help.
So I end up just restarting.Can always reload everything. > Sometimes during editing I forget to reload some code I changed Exactly where the "reloaded" workflow shines. :D I haven't tracked what exactly I changed in years. And to answer the question - nothing built-in. But there are libraries that do it, e.g. https://github.com/tonsky/clj-reload.
I tend to use things built into my text editors to "reload namespace on save" type of deal. Calva supports this with some vscode settings so thats what I do.
"On save" can be a bit finicky when you save a file that depends on another, unsaved, file. Also conflicts somewhat with the "save on the editor losing focus" approach. But it's good if those aren't a concern for you.
I'm a big fan of tools.namespace for managing this.
For visually tracking evaluated code, Emacs Cider marks code that has been evaluated in the margin. The mark is removed when the code is changed. In my view, loading (evaluating code) is an editor/user aspect. I eval code as I change it, that way I get feedback straight away. Code that is expetimental or expected to break is done within a rich comment form until ready.
I’m looking for some examples of using clojure.test to test for exceptions thrown by (throw (ex-info …)) … I’ve already asked chatgpt, but I don’t think it has given me the correct information.
In particular I want to assert that the following code was encountered/executed. I’d prefer to check for the :code but I’d settle for matching the string error message.
(throw (ex-info (cl-format false "transitions ~A has a duplication of types: ~A"
transitions (find-duplicates types))
{:transitions transitions
:code :xymbolyco-493
:duplicates (find-duplicates types)}))With clojure.test you can conveniently check only for the class of the thrown exception, with (is (thrown? TheExClass (something ...)).
If you need a much more in-depth check, specifically for the (ex-data ...), you can always do something like this:
(try
(something-that-should-throw)
(is false "not thrown")
(catch Exception e
(let [data (ex-data e)]
...all the checks...)))yes I’d like to distinguish between two different but similar errors to make sure they are each handled correctly.
chatgpt suggested thrown-with-msg? does that ring a bell?
ahhh I figured it out. I was tying to include thrown-with-msg? into the require …
I think someone recently suggested that is returns the tested value so you can alternatively do something like (let [ex (is (thrown? SomeException ..)) data (ex-data ex)]
https://github.com/nubank/matcher-combinators also offers better support for asserting about ex-data
it is interesting that as soon as I tried to test for expected exceptions, I started finding bugs in my code. so just a little bit of testing helps a lot.
i sometimes just go more manual rather than the thrown with message style
(let [e (try (f)
(is? false "didn't throw")
(catch Exception e e))]
(is (= {} (ex-data e))))
then you can do all the assertions on type, ex-data, message, whatever you wantyes, in that case it’s pretty easy for me to write my own macro to do what I want.