Implement a hacky first pass at discovery.
[third_party/subunit] / python / subunit / run.py
index 9d867f83661f04eabc1f42c8eacf8f58da3c5daa..5c440eb9bd796a6b808dc98a3f33df5e6652cf7a 100755 (executable)
@@ -3,18 +3,16 @@
 # Simple subunit testrunner for python
 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
 #   
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 3 of the License, or
-# (at your option) any later version.
-#   
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#   
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#  Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
+#  license at the users choice. A copy of both licenses are available in the
+#  project source as Apache-2.0 and BSD. You may not use this file except in
+#  compliance with one of these two licences.
+#  
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
+#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+#  license you chose for the specific language governing permissions and
+#  limitations under that license.
 #
 
 """Run a unittest testcase reporting results as Subunit.
 
 import sys
 
-from subunit import TestProtocolClient
+from subunit import TestProtocolClient, get_default_formatter
+
+try:
+    import discover
+    has_discover = True
+except ImportError:
+    has_discover = False
 
 
 class SubunitTestRunner(object):
@@ -40,9 +44,20 @@ class SubunitTestRunner(object):
 
 if __name__ == '__main__':
     import optparse
-    from unittest import TestProgram
+    from unittest import TestProgram, TestSuite
     parser = optparse.OptionParser(__doc__)
-    args = parser.parse_args()[1]
-    runner = SubunitTestRunner()
+    if has_discover:
+        parser.add_option("--discover", dest="discover", action="store_true",
+                help="Use test discovery on the given testspec.")
+    options, args = parser.parse_args()
+    stream = get_default_formatter()
+    runner = SubunitTestRunner(stream)
+    if has_discover and options.discover:
+        loader = discover.DiscoveringTestLoader()
+        test = TestSuite()
+        for arg in args:
+            test.addTest(loader.discover(arg))
+        result = runner.run(test)
+        sys.exit(not result.wasSuccessful())
     program = TestProgram(module=None, argv=[sys.argv[0]] + args,
                           testRunner=runner)