~brendan-donegan/checkbox/0.15.2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
#
# This file is part of Checkbox.
#
# Copyright 2008 Canonical Ltd.
#
# Checkbox 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.
#
# Checkbox 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 Checkbox.  If not, see <http://www.gnu.org/licenses/>.

import optparse
import os
import posixpath
import sys

lib_dir = posixpath.abspath(posixpath.dirname(__file__))
sys.path.insert(0, lib_dir)


def find_tests(testpaths=()):
    """Find all test paths, or test paths contained in the provided sequence.

    @param testpaths: If provided, only tests in the given sequence will
                      be considered.  If not provided, all tests are
                      considered.
    @return: (unittests, doctests) tuple, with lists of unittests and
             doctests found, respectively.
    """
    topdir = posixpath.abspath(posixpath.dirname(__file__))
    testpaths = set(testpaths)
    unittests = []
    doctests = []
    for root, dirnames, filenames in os.walk(topdir):
        for filename in filenames:
            filepath = posixpath.join(root, filename)
            relpath = filepath[len(topdir) + 1:]

            if (filename == "__init__.py"
               or filename.endswith(".pyc")
               or "/tests/" not in relpath):
                # Skip non-tests.
                continue

            if testpaths:
                # Skip any tests not in testpaths.
                for testpath in testpaths:
                    if relpath.startswith(testpath):
                        break
                else:
                    continue

            if filename.endswith(".py"):
                unittests.append(relpath)
            elif filename.endswith(".txt"):
                doctests.append(relpath)

    return unittests, doctests


def parse_sys_argv():
    """Extract any arguments not starting with '-' from sys.argv."""
    testpaths = []
    for i in range(len(sys.argv) - 1, 0, -1):
        arg = sys.argv[i]
        if not arg.startswith("-"):
            testpaths.append(arg)
            del sys.argv[i]
    return testpaths


def test_with_subunit(options):
    import unittest
    import subunit

    runner = unittest.TextTestRunner()
    if options.verbose:
        runner.verbosity = 2

    result = subunit.IsolatedTestSuite()
    loader = unittest.TestLoader()
    unittests, doctests = find_tests(options.args)

    if unittests:
        for relpath in unittests:
            modpath = relpath.replace('/', '.')[:-3]
            module = __import__(modpath, None, None, [""])
            tests = loader.loadTestsFromModule(module)
            result.addTests(tests)

        runner.run(result)

    return 0


def test_with_unittest(options):
    import unittest
    import doctest

    runner = unittest.TextTestRunner()
    if options.verbose:
        runner.verbosity = 2

    loader = unittest.TestLoader()
    unittests, doctests = find_tests(options.args)

    class Summary:

        def __init__(self):
            self.total_failures = 0
            self.total_errors = 0
            self.total_tests = 0

        def __call__(self, tests, failures, errors):
            self.total_tests += tests
            self.total_failures += failures
            self.total_errors += errors
            print("(tests=%d, failures=%d, errors=%d)" %
                  (tests, failures, errors))

    unittest_summary = Summary()
    doctest_summary = Summary()

    if unittests:
        print("Running unittests...")
        for relpath in unittests:
            print("[%s]" % relpath)
            modpath = relpath.replace('/', '.')[:-3]
            module = __import__(modpath, None, None, [""])
            test = loader.loadTestsFromModule(module)
            result = runner.run(test)
            unittest_summary(test.countTestCases(),
                             len(result.failures), len(result.errors))
            print()

    if doctests:
        print("Running doctests...")
        doctest_flags = doctest.ELLIPSIS
        for relpath in doctests:
            print("[%s]" % relpath)
            failures, total = doctest.testfile(relpath,
                                               optionflags=doctest_flags)
            doctest_summary(total, failures, 0)
            print()

    print("Total test cases: %d" % unittest_summary.total_tests)
    print("Total doctests: %d" % doctest_summary.total_tests)
    print("Total failures: %d" % (unittest_summary.total_failures +
                                  doctest_summary.total_failures))
    print("Total errors: %d" % (unittest_summary.total_errors +
                                doctest_summary.total_errors))

    failed = bool(unittest_summary.total_failures or
                  unittest_summary.total_errors or
                  doctest_summary.total_failures or
                  doctest_summary.total_errors)

    return failed


def setup_test_locale(test_lang="C"):
    """
    Configure locale for testing

    This effectively sets the LANG environment variable C (or any custom value,
    if specified) and removes all other settings.

    This particular setting matches the default locale on ubuntu systems
    created by Ubuntu package builders (buildds). This setting makes the
    built-in open to use 'ansi' encoding for text files (something that we want
    to avoid entirely but that's why there is the explicit encoding argument
    that we should all be using).
    """
    # Unset LANG and LANGUAGE, if set
    for key in ('LANG', 'LANGUAGE'):
        if key in os.environ:
            del os.environ[key]
    # Unset all LC_* variables
    for key in [key for key in os.environ.keys() if key.startswith("LC_")]:
        del os.environ[key]
    # Set LANG to the test value
    os.environ['LANG'] = test_lang


def main():
    setup_test_locale()
    usage = "test [options] [<test filename>, ...]"

    parser = optparse.OptionParser(usage=usage)
    parser.add_option('--verbose', action='store_true')
    opts, args = parser.parse_args()
    opts.args = args

    runner = os.environ.get("CHECKBOX_TEST_RUNNER", "unittest")
    runner_func = globals().get("test_with_%s" % runner.replace(".", "_"))
    if not runner_func:
        sys.exit("Test runner not found: %s" % runner)

    sys.exit(runner_func(opts))


if __name__ == "__main__":
    main()

# vim:ts=4:sw=4:et