20 Feb 2014

Leiningen makes it really easy to test your project against multiple versions of Clojure. Really staggeringly easy. Like, “I don’t know a single other language where this is so seamless,” easy. It’s a little trick I picked up from clojure.java.jdbc, and I make good use of it in Yesql. Here’s how it goes:

Since version 2, Leiningen lets you specify multiple profiles, with their own dependency lists. What’s interesting is that Clojure itself can be one of those dependencies, so we can meaningfully add this to project.clj:

:profiles {:1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]}
           :1.5 {:dependencies [[org.clojure/clojure "1.5.1"]]}
           :1.6 {:dependencies [[org.clojure/clojure "1.6.0-beta1"]]}}

Now calling lein with-profile +1.4 test means “run lein test under default profile dev, plus merge in the 1.4 profile”. Upshot: the code runs as normal, but under Clojure 1.4.

Now just add this alias:

:aliases {"test-all" ["with-profile" "+1.4:+1.5:+1.6" "test"]}

…and lein test-all will run the test suite three times, under each of Clojure 1.4, 1.5 and 1.6, automatically.

You can use that same trick for any dependency variations you like - so you could check against different versions of your database driver, for instance - but it’s this 4-line use case that really made my jaw drop.

comments powered by Disqus