From 22164d44f5bb83e77cd5f6a292b173732bb8cd0c Mon Sep 17 00:00:00 2001
From: Mike Hinchey <hincheymg@gmail.com>
Date: Wed, 23 Dec 2009 22:14:01 -0800
Subject: [PATCH] tests: improve running tests from ant, fixes #52

Build will fail cleanly if tests fail, by using exit.
Fork in ant so exit can be used.
Similar code will be submitted to clojure, but this does not depend on that patch.
---
 build.xml                                  |   10 ++++++----
 src/clojure/contrib/datalog/tests/test.clj |   12 +++++++-----
 src/clojure/contrib/test_contrib.clj       |   19 +++++++++++++------
 3 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/build.xml b/build.xml
index 91eb082..f547a14 100644
--- a/build.xml
+++ b/build.xml
@@ -35,14 +35,15 @@
   <target name="test_contrib"
           description="Run contrib tests"
           if="hasclojure">
-    <java classname="clojure.main" fork="true">
+    <java classname="clojure.main" taskname="clj-test"
+          fork="true" failonerror="true">
       <classpath>
         <path location="${build}"/>
         <path location="${src}"/>
         <path location="${clojure.jar}"/>
       </classpath>
       <arg value="-e"/>
-      <arg value="(require '(clojure.contrib [test-contrib :as main])) (main/run)"/>
+      <arg value="(require '(clojure.contrib [test-contrib :as t])) (t/-main)"/>
       <jvmarg value="-Djava.security.policy=config/jmx.policy"/>
     </java>
   </target>
@@ -50,14 +51,15 @@
   <target name="test_datalog"
           description="Run datalog tests"
           if="hasclojure">
-    <java classname="clojure.main">
+    <java classname="clojure.main" taskname="clj-test"
+          fork="true" failonerror="true">
       <classpath>
         <path location="${build}"/>
         <path location="${src}"/>
         <path location="${clojure.jar}"/>
       </classpath>
       <arg value="-e"/>
-      <arg value="(require '(clojure.contrib.datalog.tests [test :as main])) (main/run)"/>
+      <arg value="(require '(clojure.contrib.datalog.tests [test :as t])) (t/-main)"/>
     </java>
   </target>
 
diff --git a/src/clojure/contrib/datalog/tests/test.clj b/src/clojure/contrib/datalog/tests/test.clj
index 121d264..f1e5582 100644
--- a/src/clojure/contrib/datalog/tests/test.clj
+++ b/src/clojure/contrib/datalog/tests/test.clj
@@ -14,7 +14,8 @@
 ;;  Created 11 Feburary 2009
 
 (ns clojure.contrib.datalog.tests.test
-  (:use [clojure.test :only (run-tests)])
+  (:require [clojure.test :as t])
+  (:require [clojure.contrib.test-contrib :as tc])
   (:gen-class))
 
 (def test-names [:test-util
@@ -33,13 +34,14 @@
   []
   (println "Loading tests...")
   (apply require :reload-all test-namespaces)
-  (apply run-tests test-namespaces))
+  (apply t/run-tests test-namespaces))
 
 (defn -main
-  "Run all defined tests from the command line"
+  "Run all defined tests from the command line."
   [& args]
-  (run)
-  (System/exit 0))
+  (println "Loading tests...")
+  (apply require :reload-all test-namespaces)
+  (apply tc/run-tests-and-exit test-namespaces))
 
 
 ;; End of file
diff --git a/src/clojure/contrib/test_contrib.clj b/src/clojure/contrib/test_contrib.clj
index 724445e..8fd6b88 100644
--- a/src/clojure/contrib/test_contrib.clj
+++ b/src/clojure/contrib/test_contrib.clj
@@ -13,7 +13,7 @@
 ;;  stuart.halloway (gmail)
 
 (ns clojure.contrib.test-contrib
-  (:use [clojure.test :only (run-tests)])
+  (:require [clojure.test :as t])
   (:gen-class))
 
 (def test-names
@@ -34,12 +34,19 @@
   []
   (println "Loading tests...")
   (apply require :reload-all test-namespaces)
-  (apply run-tests test-namespaces))
+  (apply t/run-tests test-namespaces))
+
+(defn run-tests-exit
+  "Run all tests in all given namespaces; print results; exit.
+  Calls System/exit with 0 for success, -1 for errors and failures.
+  For use in ant, use fork=true and failonerror=true.
+  Note, ant fork=false in some cases prevents test output from printing."
+  [& namespaces]
+  (System/exit (int (if (t/successful? (apply t/run-tests namespaces)) 0 -1))))
 
 (defn -main
   "Run all defined tests from the command line"
   [& args]
-  (run)
-  (System/exit 0))
-
-
+  (println "Loading tests...")
+  (apply require :reload-all test-namespaces)
+  (apply run-tests-exit test-namespaces))
-- 
1.6.3.3

