File: runner.py

package info (click to toggle)
ufw 0.36.2-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,824 kB
  • sloc: python: 8,731; sh: 5,950; makefile: 200
file content (117 lines) | stat: -rwxr-xr-x 3,497 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/python
#
# Copyright 2012-2018 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# 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/>.
#
# find_tests(), runtest() and main() inspired by regrtest.py from Python 3.1
# Copyright (c) 2001-2010 Python Software Foundation; All Rights Reserved
#


from __future__ import print_function
import os
import sys


def find_tests(testdir=None, testscripts=[]):
    '''Find tests'''
    if not testdir:
        if __name__ == '__main__':
            fn = sys.argv[0]
        else:
            print("TODO: find_tests() when imported")
            sys.exit(1)

        testdir = os.path.dirname(fn)

    if len(testscripts) > 1:
        names = testscripts[1:]
    else:
        names = os.listdir(testdir)
    tests = []
    for name in names:
        if name[:5] == "test_" and name[-3:] == ".py":
            tests.append(name[:-3])
    tests.sort()
    return tests


def runtest(test):
    '''Run test'''
    pkg = __import__("tests.unit." + test, globals(), locals(), [])
    unit_pkg = getattr(pkg, "unit")
    mod = getattr(unit_pkg, test)
    print(test)
    mod.test_main()


if __name__ == '__main__':
    # Create the unittest symlink so imports work
    if not os.path.islink("./ufw"):
        os.symlink("./src", "./ufw")

    # Replace runner.py's directory from the search path, and add our own
    # so we can properly namespace our modules
    d = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
    testdir = os.path.dirname(d)
    testdir = os.path.dirname(os.path.dirname(d))
    i = len(sys.path)
    while i >= 0:
        i -= 1
        if os.path.abspath(os.path.normpath(sys.path[i])) == d:
            sys.path[i] = testdir

    print("DEBUG: sys.path=%s" % sys.path)
    tests = find_tests(testscripts=sys.argv)
    print("DEBUG: test=%s" % str(tests))

    # Import this here, so we are guaranteed to get ours from topdir
    from tests.unit.support import TestFailed

    passed = []
    failed = []
    skipped = []
    for test in tests:
        try:
            runtest(test)
            passed.append(test)
        except KeyboardInterrupt: # kill this test, but still do others
            print("")
            break
        except TestFailed as e:
            failed.append(test)
        except Exception:
            raise

        # cleanup
        for m in list(sys.modules.keys()):
            if m.startswith("tests.unit.") and m != "tests.unit.support":
                try:
                    del sys.modules[m]
                except KeyError:
                    pass

    # Cleanup our symlink
    if os.path.islink("./ufw"):
        os.unlink("./ufw")

    print("")
    print("------------------")
    print("Unit tests summary")
    print("------------------")
    print("Total=%d (Passed=%d, Failed=%d)" % (len(passed) + len(failed),
                                               len(passed),
                                               len(failed)))
    if len(failed) > 0:
        sys.exit(1)