Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
clean up temporary namespaces, fixes #54
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthalloway committed Dec 30, 2009
1 parent 3764e2e commit f24f641
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/clojure/contrib/test_contrib.clj
Expand Up @@ -21,7 +21,7 @@
:pprint.cl-format :str-utils :shell-out :test-graph
:test-dataflow :test-java-utils :test-lazy-seqs
:test-trace :test-jmx :java-utils :mock-test :mock-test.test-adapter-test
:seq-utils-test])
:seq-utils-test :with-ns-test])

(def test-namespaces
(concat
Expand Down
19 changes: 19 additions & 0 deletions src/clojure/contrib/test_contrib/with_ns_test.clj
@@ -0,0 +1,19 @@
(ns clojure.contrib.test-contrib.with-ns-test
(:use clojure.test
clojure.contrib.with-ns
[clojure.contrib.seq-utils :only (includes?)]))

(deftest test-namespace-gets-removed
(let [all-ns-names (fn [] (map #(.name %) (all-ns)))]
(testing "unexceptional return"
(let [ns-name (with-temp-ns (ns-name *ns*))]
(is (not (includes? (all-ns-names) ns-name)))))
(testing "when an exception is thrown"
(let [ns-name-str
(try
(with-temp-ns
(throw (RuntimeException. (str (ns-name *ns*)))))
(catch clojure.lang.Compiler$CompilerException e
(-> e .getCause .getMessage)))]
(is (re-find #"^sym.*$" ns-name-str))
(is (not (includes? (all-ns-names) (symbol ns-name-str))))))))
13 changes: 7 additions & 6 deletions src/clojure/contrib/with_ns.clj
Expand Up @@ -29,9 +29,10 @@
"Evaluates body in an anonymous namespace, which is then immediately
removed. The temporary namespace will 'refer' clojure.core."
[& body]
`(do (create-ns 'sym#)
(let [result# (with-ns 'sym#
(clojure.core/refer-clojure)
~@body)]
(remove-ns 'sym#)
result#)))
`(try
(create-ns 'sym#)
(let [result# (with-ns 'sym#
(clojure.core/refer-clojure)
~@body)]
result#)
(finally (remove-ns 'sym#))))

0 comments on commit f24f641

Please sign in to comment.