How to disable Clojure assertions, including preconditions? - clojure

What are some good ways to disable Clojure assertions (including preconditions and postconditions) in the REPL? For an arbitrary Leiningen profile?

Per https://github.com/technomancy/leiningen/blob/master/sample.project.clj#L286
Set :global-vars to {*assert* false} in your profiles.clj.
You can put the above setting in whatever Leiningen profiles you want.

Related

Detect leiningen test mode

Is there a not too dirty way to detect at runtime, whether the code was started with lein test? I just want to select a different redis database, so solutions like environ or using different resource files seem to be a bit overkill.
For example, leiningen automatically enables the test profile, but I haven't found a way to get a list of currently enabled profiles.
There is no simple way to do it. Neither lein.test nor clojure.test expose such information. Even if you find a way to hack into some private var of lein test or clojure.test and check it to determine if your code is run as part of lein test.
However, it would have a very big issue: your production code would need to require testing library code (e.g. clojure.test) or even worse your build tool code (lein test plugin code).
You might try to define such configuration var (dynamic or not) in your production code and set it in your tests using fixtures.
The best solution would be to configure your application dynamically based on the external variable like system property or environment variable (e.g. by using suggested environ). This way you can have as many different configuration sets as you need (e.g. prod vs unit test vs integration test vs performance tests and so on) and not just two (prod vs test).
It might seem like overkill, but component for instance is invented for exact usecases like this. Or dependency injection in general.
I know that feeling, it's just a private project, no need for difficult stuff etc. Thats why I put together my own template so that all I need to get started is run lein new ...
This is my solution to circumvent the "just want to select a different redis database" usecases.
Edit It is a template for a web framework: https://github.com/sveri/closp but a lot of these parts are not specific to web dev, especially the components part: https://github.com/sveri/closp/tree/master/resources/leiningen/new/closp/clj/components
There is also an integration test where I make use of test components specifically: https://github.com/sveri/closp/blob/master/resources/leiningen/new/closp/integtest/clj/web/setup.clj
I found a way with Cprop. Set a var in your "env/{test|prod|test}/config.clj" file:
(System/setProperty "lein.profile" "dev")
then you can read the value:
(require '[cprop.source :as source])
(str "from-system-props: >> " (:lein-profile (source/from-system-props)))
other option is to search for the key ":conf" in the system-props:
:conf "test-config.edn"
because the config file changes according to the profile.

What's the right way to include a browser REPL on a page, but only in development?

I'm using Austin to set up a browser-connected REPL, and following the example of its sample project, which uses Enlive to add the REPL script to the page.
Now I'd like to deploy my app, but I don't wan't Austin or my REPL to be on the page in production. What's the intended way to use the REPL only in development?
Is there a way to use Enlive as a middleware I could use in development and not in production?
There's almost always something that uniquely distinguishes a production environment from :dev that you can use as a conditional: if in :dev, inject the result of (browser-connected-repl-js); if not, don't.
If your deployment environment doesn't have such a property, I'd suggest adding one, as this sort of "only in environment X" use case is pretty common for a lot of things.
On the other hand, if you're looking to avoid having Austin and its dependencies included in your production-targeted builds entirely without changing any of your code that uses browser-connected-repl-js, one solution might be to simply dummy up the relevant Austin namespace, e.g.:
(ns cemerick.austin.repls)
(defn browser-connected-repl-js [& _] "")
Put that in cemerick/austin/repls.clj in a directory that is included in your project.clj's non-:dev profile:source-paths. Now your code will deploy to production without Austin and its dependencies, and your code will transparently call the above dummy function (injecting nothing into your page(s)).

Get the Clojurescript repl/connect to not compile when in production?

In my clojurescript code I have the following:
(defn onload [] (repl/connect "http://localhost:9000/repl"))
(set! (.-onload js/window) onload)
The Clojurescript repl is very useful in development, but I am hesitant to leave it in the code during production. What is the cleanest way to have the above code present during development (simple compilation), but absent during production (advanced compilation)?
The modern-cljs tutorial actually describes exactly how to solve this here.
Hope that helps!
Unfortunately there aren't currently any well defined ways to do conditional compilation in ClojureScript.
You could add configuration variables to control whether to start a REPL in a variety of ways, but one quick and easy way would be to get the hostname of the current page, and only invoke repl/connnect if it was "localhost" or whatever other domains you're using for development work.
I think a combination of lein2 profiles and cljsbuild src-paths munging can do the trick. eg, create a namespace that simply loads your repl, and exclude it with a profile run for the final compile (possibly might need to create a dummy namespace in another src-path directory).

What autotest tools exist for Clojure

I was wondering what autotest tools exists for clojure. In Ruby I have ZenTest, redgreeen etc to continuously keep on testing my code. I would like to have something similar for Clojure
So far I have found this simple script https://github.com/devn/clojure-autotest on the github. A bit crude for my taste. All tests run when a change occurrs. Also it may blurt out a long stacktrace in case of syntax errors, obscuring what goes wrong.
Take a look at the Testing section on the Leiningen plugin page.
Notably there's lein-autotest for Stuart Sierras lazytest framework and speclj.
If you are using clojure.test there are a few options available. lein-test-refresh is what I use (I'm also the author). Other options include quickie and prism.
If you use expectations then there is lein-autoexpect. I'm also the author of that.
Midje has built in support for autotesting. I'm not sure what the options are for Speclj.

History in Clojure REPL

I'm doing my first steps with Clojure, but for some reason the up/down keys don't allow me to step through the command history in the REPL like in REPLs of other languages. Does the Clojure REPL use different keys to access the command history, or is this feature just not (yet) implemented?
If you are, by any chance, using a Unix-like environment you can use rlwrap to to achieve nice things like command line history and Ctrl-R for searching through it.
Just prepend your command which you use to start repl, e.g:
rlwrap java -cp clojure.jar clojure.main
Or use Leiningen or Cake. Either of these are useful in general, and you'll be happy to learn to use them, but specifically they use readline or jline automatically where available.
You can enhance the REPL to do this by using JLine, see here.
Depends which REPL you use. Most good REPLs have this functionality and more.
If you're coming from Java as an Eclipse user, for example, you may want to use the embedded nREPL provided by the Counterclockwise plugin. I found this easier to use than configuring a separate dedicated Clojure environment.
C-, M-x slime-repl-forward-input
C-, M-x slime-repl-backward-input
Go to the next/previous history item.
http://common-lisp.net/project/slime/doc/html/Input-Navigation.html