title: Using a dynamic variable binding in a leiningen plugin
body: <p>I’ve got a lein plugin that manually runs my clojure.test code.  It declares a dynamic variable <code>baseuri</code> that I wish to access from within my tests.  I’ll strip out and change the code to get straight to the point.  Here, inside of my plugin, I have a config file that creates the dynamic <code>baseuri</code> variable and sets it to a blank string.</p>
  
  <pre><code>;; myplugin
  ;; src/myplugin/config.clj
  (ns leiningen.myplugin.config)    
  (def ^:dynamic baseuri "")
  </code></pre>
  
  <p>A task from within the plugin sets the dynamic <code>baseuri</code> variable and runs tests with clojure.test:</p>
  
  <pre><code>;; src/myplugin/runtests.clj
  (ns leiningen.myplugin.runtests
        (:require [leiningen.myplugin.config :as config]
                  [clojure.test]
                  [e2e.sometest]))
  
  (defn run [project]
    (binding [config/baseuri "https://google.com/"]
      (println config/baseuri) ;; &lt;-- prints google url
      ;; run clojure.test test cases from e2e.sometest namespace
      ;; This will call the `sampletest` test case
      (clojure.test/run-tests e2e.sometest)
    ))
  </code></pre>
  
  <p>And inside of my clojure.test I try to use the baseuri variable but the binding doesn’t hold.  It’s value is what I originally declared <code>baseuri</code> to be (an empty string)</p>
  
  <pre><code>;; tests/e2e/sometest.clj
  (ns e2e.sometest
    (:require [leiningen.myplugin.config :as config]))
  
  (deftest sampletest
    (println config/baseuri))  ;; &lt;-- Prints an empty string instead of google url
  </code></pre>
  
  <p>I've edited the code to show in a basic manner how the clojure.test cases are run.  I simply pass in the namespace I want to be run to the <code>clojure.test/run-tests</code> method.</p>