~launchpad-pqm/launchpad/devel

3944.1.1 by Francis J. Lacoste
Use system version python2.4 for scripts.
1
#!/usr/bin/python2.4
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
2
##############################################################################
3
#
4
# Copyright (c) 2004 Zope Corporation and Contributors.
5
# All Rights Reserved.
6
#
7
# This software is subject to the provisions of the Zope Public License,
8
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
9
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
10
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
12
# FOR A PARTICULAR PURPOSE.
13
#
14
##############################################################################
15
"""Test script
16
17
$Id: test.py 25177 2004-06-02 13:17:31Z jim $
18
"""
5925.1.1 by Curtis Hovey
Added a mechanism to select the correct conf to load. The switch
19
import sys, os, time, logging, warnings, re
809 by Canonical.com Patch Queue Manager
Nuke some arch-tags
20
5796.2.1 by Maris Fogels
Fixed the Unix process session handling so that test.py can be run from the Emacs compile buffer.
21
22
if os.getsid(0) == os.getsid(os.getppid()):
23
    # We need to become the process group leader so test_on_merge.py
24
    # can reap its children.
25
    #
26
    # Note that if setpgrp() is used to move a process from one
27
    # process group to another (as is done by some shells when
28
    # creating pipelines), then both process groups must be part of
29
    # the same session.
30
    os.setpgrp()
2083 by Canonical.com Patch Queue Manager
[r=jamesh] testrunner improvements (?)
31
32
# Make tests run in a timezone no launchpad developers live in.
33
# Our tests need to run in any timezone.
34
# (No longer actually required, as PQM does this)
35
os.environ['TZ'] = 'Asia/Calcutta'
36
time.tzset()
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
37
38
here = os.path.dirname(os.path.realpath(__file__))
1806 by Canonical.com Patch Queue Manager
Always use sys.path.insert instead of sys.path.append to give precedence for launchpad libs that are inside our tree. r=stub
39
sys.path.insert(0, os.path.join(here, 'lib'))
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
40
41
# Set PYTHONPATH environment variable for spawned processes
42
os.environ['PYTHONPATH'] = ':'.join(sys.path)
43
4385.1.2 by Stuart Bishop
Add profiling information on test suite infrastructure
44
# Set a flag if this is the main testrunner process
45
if len(sys.argv) > 1 and sys.argv[1] == '--resume-layer':
46
    main_process = False
47
else:
48
    main_process = True
49
2097 by Canonical.com Patch Queue Manager
[trivial] more import fascist improvements
50
# Install the import fascist import hook and atexit handler.
51
import importfascist
52
importfascist.install_import_fascist()
1968 by Canonical.com Patch Queue Manager
DatabaseCodeImportFascist: warn about database imports in browser code after running tests. [r=SteveA]
53
2103 by Canonical.com Patch Queue Manager
[trivial] improved warning output for tests, and fixed a bunch of ambiguous use of select results warnings. some xxx comments left in person code. one untested codepath in hct backend.
54
# Install the warning handler hook and atexit handler.
55
import warninghandler
56
warninghandler.install_warning_handler()
57
2976.10.126 by Stuart Bishop
Ensure ZCML overrides are generated
58
# Ensure overrides are generated
59
from configs import generate_overrides
60
generate_overrides()
2103 by Canonical.com Patch Queue Manager
[trivial] improved warning output for tests, and fixed a bunch of ambiguous use of select results warnings. some xxx comments left in person code. one untested codepath in hct backend.
61
5925.1.1 by Curtis Hovey
Added a mechanism to select the correct conf to load. The switch
62
# Tell canonical.config to use the testrunner config instance.
1628 by Canonical.com Patch Queue Manager
Configuration, Librian and Librarian test harness work
63
from canonical.config import config
5925.1.1 by Curtis Hovey
Added a mechanism to select the correct conf to load. The switch
64
config.setInstance('testrunner')
1628 by Canonical.com Patch Queue Manager
Configuration, Librian and Librarian test harness work
65
3498.5.2 by Bjorn Tillenius
clarify a comment.
66
# Remove this module's directory from path, so that zope.testbrowser
67
# can import pystone from test:
3498.4.1 by Bjorn Tillenius
patch test.py to remove '.' from the python path, to allow testbrowser import pystone from test.
68
sys.path[:] = [p for p in sys.path if os.path.abspath(p) != here]
69
70
1210 by Canonical.com Patch Queue Manager
Refactor and document test harnesses
71
# Turn on psycopg debugging wrapper
72
#import canonical.database.debug
73
#canonical.database.debug.install()
74
2770.1.30 by Guilherme Salgado
Clean up the http_proxy env var before running tests, as we don't want the requests we make to localhost to be proxied
75
# Unset the http_proxy environment variable, because we're going to make
76
# requests to localhost and we don't wand this to be proxied.
77
try:
78
    os.environ.pop('http_proxy')
79
except KeyError:
80
    pass
81
2976.10.30 by Stuart Bishop
filter some test suite warnings
82
# Silence spurious warnings. Note that this does not propagate to subprocesses
83
# so this is not always as easy as it seems. Warnings caused by our code that
84
# need to be silenced should have an accomponied Bug reference.
2976.10.19 by Stuart Bishop
Update test.py for Zope3.2
85
#
86
warnings.filterwarnings(
87
        'ignore', 'PyCrypto', RuntimeWarning, 'twisted[.]conch[.]ssh'
88
        )
89
warnings.filterwarnings(
2976.10.30 by Stuart Bishop
filter some test suite warnings
90
        'ignore', 'twisted.python.plugin', DeprecationWarning, 'buildbot'
91
        )
92
warnings.filterwarnings(
93
        'ignore', 'The concrete concept of a view has been deprecated.',
94
        DeprecationWarning
95
        )
2976.10.116 by Stuart Bishop
Fix and update tests
96
warnings.filterwarnings(
97
        'ignore', 'bzrlib.*was deprecated', DeprecationWarning
98
        )
2976.10.30 by Stuart Bishop
filter some test suite warnings
99
2976.10.61 by Stuart Bishop
Update warning filters
100
# This warning will be triggered if the beforeTraversal hook fails. We
101
# want to ensure it is not raised as an error, as this will mask the real
102
# problem.
103
warnings.filterwarnings(
104
        'always',
105
        re.escape('clear_request_started() called outside of a request'),
106
        UserWarning
107
        )
108
2976.10.30 by Stuart Bishop
filter some test suite warnings
109
# Any warnings not explicitly silenced are errors
110
warnings.filterwarnings('error', append=True)
111
1210 by Canonical.com Patch Queue Manager
Refactor and document test harnesses
112
1224 by Canonical.com Patch Queue Manager
Test isolation enforcement
113
from canonical.ftests import pgsql
1617 by Canonical.com Patch Queue Manager
Refactor PostgreSQL harnesses to avoid bug found by ddaa. Add more tests
114
# If this is removed, make sure canonical.ftests.pgsql is updated
115
# because the test harness there relies on the Connection wrapper being
116
# installed.
1224 by Canonical.com Patch Queue Manager
Test isolation enforcement
117
pgsql.installFakeConnect()
118
2976.10.19 by Stuart Bishop
Update test.py for Zope3.2
119
from zope.testing import testrunner
120
121
defaults = [
122
    # Find tests in the tests and ftests directories
123
    '--tests-pattern=^f?tests$',
124
    '--test-path=%s' % os.path.join(here, 'lib'),
125
    '--package=canonical',
126
    ]
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
127
6233.2.1 by Celso Providelo
Monkey-patch os.listdir to return randomised results (random.shuffle) in our test environment. Also fixes the two tests relying on unstable ordering.
128
# Monkey-patch os.listdir to randomise the results
129
original_listdir = os.listdir
130
131
import random
132
133
def listdir(path):
134
    """Randomise the results of os.listdir.
135
136
    It uses random.suffle to randomise os.listdir results, this way tests
137
    relying on unstable ordering will have a higher chance to fail in the
138
    development environment.
139
    """
140
    directory_contents = original_listdir(path)
141
    random.shuffle(directory_contents)
142
    return directory_contents
143
144
os.listdir = listdir
145
146
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
147
if __name__ == '__main__':
4385.1.5 by Stuart Bishop
Docstrings and comments
148
5925.1.1 by Curtis Hovey
Added a mechanism to select the correct conf to load. The switch
149
    # Extract arguments so we can see them too. We need to strip
150
    # --resume-layer and --default stuff if found as get_options can't
151
    # handle it.
4385.1.10 by Stuart Bishop
Review updates
152
    if len(sys.argv) > 1 and sys.argv[1] == '--resume-layer':
153
        args = list(sys.argv)
154
        args.pop(1) # --resume-layer
155
        args.pop(1) # The layer name
156
        while len(args) > 1 and args[1] == '--default':
157
            args.pop(1) # --default
158
            args.pop(1) # The default value
159
        args.insert(0, sys.argv[0])
160
    else:
161
        args = sys.argv
162
    options = testrunner.get_options(args=args, defaults=defaults)
4385.1.5 by Stuart Bishop
Docstrings and comments
163
4385.1.7 by Stuart Bishop
Don't want profiling overhead if profiling not requested
164
    # Turn on Layer profiling if requested.
165
    from canonical.testing import profiled
4385.1.10 by Stuart Bishop
Review updates
166
    if options.verbose >= 3 and main_process:
4385.1.11 by Stuart Bishop
Review feedback
167
        profiled.setup_profiling()
4385.1.7 by Stuart Bishop
Don't want profiling overhead if profiling not requested
168
5642.2.1 by Elliot Murphy
Set the working directory for the test runner script.
169
    # The working directory change is just so that the test script
170
    # can be invoked from places other than the root of the source
171
    # tree. This is very useful for IDE integration, so an IDE can
172
    # e.g. run the test that you are currently editing.
173
    try:
174
        there = os.getcwd()
175
        os.chdir(here)
176
        result = testrunner.run(defaults)
177
    finally:
178
        os.chdir(there)
2976.10.19 by Stuart Bishop
Update test.py for Zope3.2
179
    # Cribbed from sourcecode/zope/test.py - avoid spurious error during exit.
180
    logging.disable(999999999)
4385.1.2 by Stuart Bishop
Add profiling information on test suite infrastructure
181
4385.1.7 by Stuart Bishop
Don't want profiling overhead if profiling not requested
182
    # Print Layer profiling report if requested.
4385.1.5 by Stuart Bishop
Docstrings and comments
183
    if main_process and options.verbose >= 3:
4385.1.7 by Stuart Bishop
Don't want profiling overhead if profiling not requested
184
        profiled.report_profile_stats()
2976.10.19 by Stuart Bishop
Update test.py for Zope3.2
185
    sys.exit(result)
186