summaryrefslogtreecommitdiffstats
path: root/QMTest/TestRuntest.py
blob: 94527469e97dc83765ee00a8e0e7e2f3ec46a52d (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
208
209
"""
TestRuntest.py:  a testing framework for the runtest.py command used to
invoke SCons tests.

A TestRuntest environment object is created via the usual invocation:

    test = TestRuntest()

TestRuntest is a subclass of TestCommon, which is in turn is a subclass
of TestCmd), and hence has available all of the methods and attributes
from those classes, as well as any overridden or additional methods or
attributes defined in this subclass.
"""

# __COPYRIGHT__

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import os
import os.path
import string
import shutil
import sys

from TestCommon import *
from TestCommon import __all__

__all__.extend([ 'TestRuntest',
                 'python',
                 '_python_',
               ])

python = python_executable
_python_ = '"' + python_executable + '"'


failing_test_template = """\
import sys
sys.stdout.write('FAILING TEST STDOUT\\n')
sys.stderr.write('FAILING TEST STDERR\\n')
sys.exit(1)
"""

no_result_test_template = """\
import sys
sys.stdout.write('NO RESULT TEST STDOUT\\n')
sys.stderr.write('NO RESULT TEST STDERR\\n')
sys.exit(2)
"""

passing_test_template = """\
import sys
sys.stdout.write('PASSING TEST STDOUT\\n')
sys.stderr.write('PASSING TEST STDERR\\n')
sys.exit(0)
"""

fake_scons_py = """
__version__ = '1.2.3'
__build__ = 'D123'
__buildsys__ = 'fake_system'
__date__ = 'Jan 1 1970'
__developer__ = 'Anonymous'
"""

fake___init___py = """
__version__ = '4.5.6'
__build__ = 'D456'
__buildsys__ = 'another_fake_system'
__date__ = 'Dec 31 1999'
__developer__ = 'John Doe'
"""

class TestRuntest(TestCommon):
    """Class for testing the runtest.py script.

    This provides a common place for initializing Runtest tests,
    eliminating the need to begin every test with the same repeated
    initializations.
    """

    def __init__(self, **kw):
        """Initialize a Runtest testing object.

        If they're not overridden by keyword arguments, this
        initializes the object with the following default values:

                program = 'runtest.py'
                interpreter = ['python', '-tt']
                match = match_exact
                workdir = ''

        The workdir value means that, by default, a temporary
        workspace directory is created for a TestRuntest environment.
        The superclass TestCommon.__init__() will change directory (chdir)
        to the workspace directory, so an explicit "chdir = '.'" on all
        of the run() method calls is not necessary.  This initialization
        also copies the runtest.py and QMTest/ subdirectory tree to the
        temporary directory, duplicating how this test infrastructure
        appears in a normal workspace.
        """
        set_workpath_runtest = None
        if not kw.has_key('program'):
            kw['program'] = 'runtest.py'
            set_workpath_runtest = 1
        if not kw.has_key('interpreter'):
            kw['interpreter'] = [python, '-tt']
        if not kw.has_key('match'):
            kw['match'] = match_exact
        if not kw.has_key('workdir'):
            kw['workdir'] = ''

        try:
            noqmtest = kw['noqmtest']
        except KeyError:
            noqmtest = 0
        else:
            del kw['noqmtest']

        orig_cwd = os.getcwd()
        apply(TestCommon.__init__, [self], kw)
  
        if not noqmtest:
            qmtest_py = self.where_is('qmtest.py')
            if not qmtest_py:
                self.skip_test("Could not find 'qmtest.py'; skipping test(s).\n")

        things_to_copy = [
            'runtest.py',
            'QMTest',
        ]

        dirs = [os.environ.get('SCONS_RUNTEST_DIR', orig_cwd)]
        
        spe = os.environ.get('SCONS_SOURCE_PATH_EXECUTABLE', orig_cwd)
        for d in string.split(spe, os.pathsep):
            dirs.append(os.path.join(d, 'build'))
            dirs.append(d)

        for thing in things_to_copy:
            for dir in dirs:
                t = os.path.join(dir, thing)
                if os.path.exists(t):
                    if os.path.isdir(t):
                        copy_func = shutil.copytree
                    else:
                        copy_func = shutil.copyfile
                    copy_func(t, self.workpath(thing))
                    break

        if set_workpath_runtest:
            self.program_set(self.workpath('runtest.py'))

        for key in os.environ.keys():
            if key[:5] == 'AEGIS':
                os.environ[key] = ''

        os.environ['PYTHONPATH'] = ''
        os.environ['SCONS_SOURCE_PATH_EXECUTABLE'] = ''

    def skip_test(self, message="Skipping test.\n"):
        """Skips a test.

        Proper test-skipping behavior is dependent on whether we're being
        executed as part of development of a change under Aegis.

        Technically, skipping a test is a NO RESULT, but Aegis will
        treat that as a test failure and prevent the change from going
        to the next step.  We don't want to force anyone using Aegis
        to have to install absolutely every tool used by the tests,
        so we actually report to Aegis that a skipped test has PASSED
        so that the workflow isn't held up.
        """
        if message:
            sys.stdout.write(message)
            sys.stdout.flush()
        devdir = os.popen("aesub '$dd' 2>/dev/null", "r").read()[:-1]
        intdir = os.popen("aesub '$intd' 2>/dev/null", "r").read()[:-1]
        if devdir and self._cwd[:len(devdir)] == devdir or \
           intdir and self._cwd[:len(intdir)] == intdir:
            # We're under the development directory for this change,
            # so this is an Aegis invocation; pass the test (exit 0).
            self.pass_test()
        else:
            # skip=1 means skip this function when showing where this
            # result came from.  They only care about the line where the
            # script called test.skip_test(), not the line number where
            # we call test.no_result().
            self.no_result(skip=1)

    def write_fake_scons_source_tree(self):
        os.mkdir('src')
        os.mkdir('src/script')
        self.write('src/script/scons.py', fake_scons_py)

        os.mkdir('src/engine')
        os.mkdir('src/engine/SCons')
        self.write('src/engine/SCons/__init__.py', fake___init___py)
        os.mkdir('src/engine/SCons/Script')
        self.write('src/engine/SCons/Script/__init__.py', fake___init___py)

    def write_failing_test(self, name):
        self.write(name, failing_test_template)

    def write_no_result_test(self, name):
        self.write(name, no_result_test_template)

    def write_passing_test(self, name):
        self.write(name, passing_test_template)