918195e84294fb6792ab9a9e1415ffff4f27e9cc
[sfrench/samba-autobuild/.git] / source4 / scripting / bin / subunitrun
1 #!/usr/bin/env python
2
3 # Simple subunit testrunner for python
4
5 # NOTE: This is deprecated - Using the standard subunit runner is
6 # preferred - e.g. "python -m subunit.run YOURMODULE".
7 #
8 # This wrapper will be removed once all tests can be run
9 # without it. At the moment there are various tests which still
10 # get e.g. credentials passed via command-line options to this
11 # script.
12
13 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2011
14 #   
15 # This program is free software; you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation; either version 3 of the License, or
18 # (at your option) any later version.
19 #   
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #   
25 # You should have received a copy of the GNU General Public License
26 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 #
28
29 import sys
30
31 # make sure the script dies immediately when hitting control-C,
32 # rather than raising KeyboardInterrupt. As we do all database
33 # operations using transactions, this is safe.
34 import signal
35 signal.signal(signal.SIGINT, signal.SIG_DFL)
36
37 # Find right directory when running from source tree
38 sys.path.insert(0, "bin/python")
39
40 import optparse
41 import samba
42 samba.ensure_external_module("testtools", "testtools")
43 samba.ensure_external_module("subunit", "subunit/python")
44 from subunit.run import SubunitTestRunner
45 import samba.getopt as options
46 import samba.tests
47
48
49 usage = 'subunitrun [options] <tests>'
50 description = '''
51 This runs a Samba python test suite. The tests are typically located in
52 python/samba/tests/*.py
53
54 To run the tests from one of those modules, specify the test as
55 samba.tests.MODULE. For example, to run the tests in common.py:
56
57    subunitrun samba.tests.common
58
59 To list the tests in that module, use:
60
61    subunitrun -l samba.tests.common
62
63 NOTE: This script is deprecated in favor of "python -m subunit.run". Don't use
64 it unless it can be avoided.
65 '''
66
67 def format_description(formatter):
68     '''hack to prevent textwrap of the description'''
69     return description
70
71 parser = optparse.OptionParser(usage=usage, description=description)
72 parser.format_description = format_description
73 credopts = options.CredentialsOptions(parser)
74 sambaopts = options.SambaOptions(parser)
75 parser.add_option_group(credopts)
76 parser.add_option_group(sambaopts)
77 try:
78     from subunit.run import TestProgram
79 except ImportError:
80     from unittest import TestProgram
81 else:
82     parser.add_option('-l', '--list', dest='listtests', default=False,
83                       help='List tests rather than running them.',
84                       action="store_true")
85
86 opts, args = parser.parse_args()
87
88 if getattr(opts, "listtests", False):
89     args.insert(0, "--list")
90 else:
91     lp = sambaopts.get_loadparm()
92     samba.tests.cmdline_credentials = credopts.get_credentials(lp)
93
94 runner = SubunitTestRunner()
95 program = TestProgram(module=None, argv=[sys.argv[0]] + args, testRunner=runner)