summaryrefslogtreecommitdiff
path: root/admin/runtests
blob: db7a647e9a6df5c5c0f418ac5267aa7631020fbc (plain)
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
#!/usr/bin/env python
import os.path
import sys
import warnings

test_modules = [
    'cryptutil',
    'oidutil',
    'dh',
]


def fixpath():
    try:
        d = os.path.dirname(__file__)
    except NameError:
        d = os.path.dirname(sys.argv[0])
    parent = os.path.normpath(os.path.join(d, '..'))
    if parent not in sys.path:
        print "putting %s in sys.path" % (parent,)
        sys.path.insert(0, parent)


def otherTests():
    failed = []
    for module_name in test_modules:
        print 'Testing %s...' % (module_name,),
        sys.stdout.flush()
        module_name = 'openid.test.' + module_name
        try:
            test_mod = __import__(module_name, {}, {}, [None])
        except ImportError:
            print 'Failed to import test %r' % (module_name,)
            failed.append(module_name)
        else:
            try:
                test_mod.test()
            except Exception:
                sys.excepthook(*sys.exc_info())
                failed.append(module_name)
            else:
                print 'Succeeded.'

    return failed


def pyunitTests():
    import unittest
    pyunit_module_names = [
        'server',
        'consumer',
        'message',
        'symbol',
        'etxrd',
        'xri',
        'xrires',
        'association_response',
        'auth_request',
        'negotiation',
        'verifydisco',
        'sreg',
        'ax',
        'pape',
        'pape_draft2',
        'pape_draft5',
        'rpverify',
        'extension',
    ]

    pyunit_modules = [
        __import__('openid.test.test_%s' % (name,), {}, {}, ['unused'])
        for name in pyunit_module_names
    ]

    try:
        from openid.test import test_examples
    except ImportError as e:
        if 'twill' in str(e):
            warnings.warn("Could not import twill; skipping test_examples.")
        else:
            raise
    else:
        pyunit_modules.append(test_examples)

    # Some modules have data-driven tests, and they use custom methods
    # to build the test suite:
    custom_module_names = [
        'kvform',
        'linkparse',
        'oidutil',
        'storetest',
        'test_accept',
        'test_association',
        'test_discover',
        'test_fetchers',
        'test_htmldiscover',
        'test_nonce',
        'test_openidyadis',
        'test_parsehtml',
        'test_urinorm',
        'test_yadis_discover',
        'trustroot',
    ]

    loader = unittest.TestLoader()
    s = unittest.TestSuite()

    for m in pyunit_modules:
        s.addTest(loader.loadTestsFromModule(m))

    for name in custom_module_names:
        m = __import__('openid.test.%s' % (name,), {}, {}, ['unused'])
        try:
            s.addTest(m.pyUnitTests())
        except AttributeError as ex:
            # because the AttributeError doesn't actually say which
            # object it was.
            print "Error loading tests from %s:" % (name,)
            raise

    runner = unittest.TextTestRunner()  # verbosity=2)

    return runner.run(s)


def splitDir(d, count):
    # in python2.4 and above, it's easier to spell this as
    # d.rsplit(os.sep, count)
    for i in xrange(count):
        d = os.path.dirname(d)
    return d


def _import_djopenid():
    """Import djopenid from examples/

    It's not in sys.path, and I don't really want to put it in sys.path.
    """
    import types
    thisfile = os.path.abspath(sys.modules[__name__].__file__)
    topDir = splitDir(thisfile, 2)
    djdir = os.path.join(topDir, 'examples', 'djopenid')

    djinit = os.path.join(djdir, '__init__.py')

    djopenid = types.ModuleType('djopenid')
    execfile(djinit, djopenid.__dict__)
    djopenid.__file__ = djinit

    # __path__ is the magic that makes child modules of the djopenid package
    # importable.  New feature in python 2.3, see PEP 302.
    djopenid.__path__ = [djdir]
    sys.modules['djopenid'] = djopenid


def django_tests():
    """Runs tests from examples/djopenid.

    @returns: number of failed tests.
    """
    import os
    # Django uses this to find out where its settings are.
    os.environ['DJANGO_SETTINGS_MODULE'] = 'djopenid.settings'

    _import_djopenid()

    try:
        import django.test.simple
    except ImportError as e:
        warnings.warn("django.test.simple not found; "
                      "django examples not tested.")
        return 0
    import djopenid.server.models
    import djopenid.consumer.models
    print "Testing Django examples:"

    # These tests do get put in to a pyunit test suite, so we could run them
    # with the other pyunit tests, but django also establishes a test database
    # for them, so we let it do that thing instead.
    return django.test.simple.run_tests([djopenid.server.models,
                                         djopenid.consumer.models])


try:
    bool
except NameError:
    def bool(x):
        return not not x


def main():
    fixpath()
    other_failed = otherTests()
    pyunit_result = pyunitTests()
    django_failures = django_tests()

    if other_failed:
        print 'Failures:', ', '.join(other_failed)

    failed = (bool(other_failed) or
              bool(not pyunit_result.wasSuccessful()) or
              (django_failures > 0))
    return failed


if __name__ == '__main__':
    sys.exit(main() and 1 or 0)