summaryrefslogtreecommitdiffstats
path: root/etc/TestSCons.py
blob: 028a81520bdeee7a067ae904878d1a165b1989b8 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""
TestSCons.py:  a testing framework for the SCons software construction
tool.

A TestSCons environment object is created via the usual invocation:

    test = TestSCons()

TestScons is a subclass of TestCmd, and hence has available all of its
methods and attributes, as well as any overridden or additional methods
or attributes defined in this subclass.
"""

# Copyright 2001, 2002, 2003 Steven Knight

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import os
import os.path
import string
import sys

import TestCmd

python = TestCmd.python_executable


def gccFortranLibs():
    """Test whether -lfrtbegin is required.  This can probably be done in
    a more reliable way, but using popen3 is relatively efficient."""

    libs = ['g2c']

    try:
        import popen2
        stderr = popen2.popen3('gcc -v')[2]
    except OSError:
        return libs

    for l in stderr.readlines():
        list = string.split(l)
        if len(list) > 3 and list[:2] == ['gcc', 'version']:
            if list[2][:2] == '3.':
                libs = ['frtbegin'] + libs
                break
    return libs


if sys.platform == 'win32':
    _exe   = '.exe'
    _obj   = '.obj'
    _shobj = '.obj'
    lib_   = ''
    _lib   = '.lib'
    dll_   = ''
    _dll   = '.dll'
    fortran_lib = gccFortranLibs()
elif sys.platform == 'cygwin':
    _exe   = '.exe'
    _obj   = '.o'
    _shobj = '.os'
    lib_   = 'lib'
    _lib   = '.a'
    dll_   = ''
    _dll   = '.dll'
    fortran_lib = gccFortranLibs()
elif string.find(sys.platform, 'irix') != -1:
    _exe   = ''
    _obj   = '.o'
    _shobj = '.o'
    lib_   = 'lib'
    _lib   = '.a'
    dll_   = 'lib'
    _dll   = '.so'
    fortran_lib = ['ftn']
else:
    _exe   = ''
    _obj   = '.o'
    _shobj = '.os'
    lib_   = 'lib'
    _lib   = '.a'
    dll_   = 'lib'
    _dll   = '.so'
    fortran_lib = gccFortranLibs()


class TestFailed(Exception):
    def __init__(self, args=None):
        self.args = args

class TestNoResult(Exception):
    def __init__(self, args=None):
        self.args = args

if os.name == 'posix':
    def _failed(self, status = 0):
        if self.status is None or status is None:
            return None
        if os.WIFSIGNALED(self.status):
            return None
        return _status(self) != status
    def _status(self):
        if os.WIFEXITED(self.status):
            return os.WEXITSTATUS(self.status)
        else:
            return None
elif os.name == 'nt':
    def _failed(self, status = 0):
        return not (self.status is None or status is None) and \
               self.status != status
    def _status(self):
        return self.status

class TestSCons(TestCmd.TestCmd):
    """Class for testing SCons.

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

    def __init__(self, **kw):
	"""Initialize an SCons testing object.

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

		program = 'scons' if it exists,
			  else 'scons.py'
		interpreter = 'python'
		match = TestCmd.match_exact
		workdir = ''

        The workdir value means that, by default, a temporary workspace
        directory is created for a TestSCons environment.  In addition,
	this method changes directory (chdir) to the workspace directory,
	so an explicit "chdir = '.'" on all of the run() method calls
	is not necessary.
	"""
	if not kw.has_key('program'):
            kw['program'] = os.environ.get('SCONS')
            if not kw['program']:
                if os.path.exists('scons'):
                    kw['program'] = 'scons'
                else:
                    kw['program'] = 'scons.py'
	if not kw.has_key('interpreter') and not os.environ.get('SCONS_EXEC'):
	    kw['interpreter'] = python
	if not kw.has_key('match'):
	    kw['match'] = TestCmd.match_exact
	if not kw.has_key('workdir'):
	    kw['workdir'] = ''
	apply(TestCmd.TestCmd.__init__, [self], kw)
	os.chdir(self.workdir)

    def run(self, options = None, arguments = None,
                  stdout = None, stderr = '', status = 0, **kw):
	"""Runs SCons.

        This is the same as the base TestCmd.run() method, with
        the addition of:

		stdout	The expected standard output from
			the command.  A value of None means
			don't test standard output.

		stderr	The expected error output from
			the command.  A value of None means
			don't test error output.

                status  The expected exit status from the 
                        command.  A value of None means don't
                        test exit status.

        By default, this does not test standard output (stdout = None),
        and expects that error output is empty (stderr = "").
	"""
        if options:
            arguments = options + " " + arguments
        kw['arguments'] = arguments
	try:
	    apply(TestCmd.TestCmd.run, [self], kw)
	except:
	    print "STDOUT ============"
	    print self.stdout()
	    print "STDERR ============"
	    print self.stderr()
	    raise
	if _failed(self, status):
            expect = ''
            if status != 0:
                expect = " (expected %s)" % str(status)
            print "%s returned %s%s" % (self.program, str(_status(self)), expect)
            print "STDOUT ============"
            print self.stdout()
	    print "STDERR ============"
	    print self.stderr()
	    raise TestFailed
	if not stdout is None and not self.match(self.stdout(), stdout):
                print "Expected STDOUT =========="
                print stdout
                print "Actual STDOUT ============"
                print self.stdout()
                stderr = self.stderr()
                if stderr:
                    print "STDERR ==================="
                    print stderr
                raise TestFailed
	if not stderr is None and not self.match(self.stderr(), stderr):
            print "STDOUT ==================="
            print self.stdout()
	    print "Expected STDERR =========="
	    print stderr
	    print "Actual STDERR ============"
	    print self.stderr()
	    raise TestFailed

    def detect(self, var, prog=None):
        """
        Detect a program named 'prog' by first checking the construction
        variable named 'var' and finally searching the path used by
        SCons. If either method fails to detect the program, then false
        is returned, otherwise the full path to prog is returned. If
        prog is None, then the value of the environment variable will be
        used as prog.
        """

        import SCons.Environment
        env = SCons.Environment.Environment()
        try:
            if prog is None:
                prog = env[var]
            return env[var] == prog and env.WhereIs(prog)
        except KeyError:
            return None

    def detect_tool(self, tool, prog=None):
        """
        Given a tool (i.e., tool specification that would be passed
        to the "tools=" parameter of Environment()) and one a program that
        corresponds to that tool, return true if and only if we can find
        that tool using Environment.Detect().

        By default, progs is set to the value passed into the tools parameter.
        """

        if not prog:
            prog = tool
        import SCons.Environment
        import SCons.Errors
        try:
            env=SCons.Environment.Environment(tools=[tool])
        except (SCons.Errors.UserError, SCons.Errors.InternalError):
            return None
        return env.Detect([prog])

    def wrap_stdout(self, build_str = "", read_str = "", error = 0):
        """Wraps standard output string(s) in the normal
        "Reading ... done" and "Building ... done" strings
        """
        if error:
            term = "scons: building terminated because of errors.\n"
        else:
            term = "scons: done building targets.\n"
        return "scons: Reading SConscript files ...\n" + \
               read_str + \
               "scons: done reading SConscript files.\n" + \
               "scons: Building targets ...\n" + \
               build_str + \
               term

    def up_to_date(self, options = None, arguments = None, **kw):
        s = ""
        for arg in string.split(arguments):
            s = s + "scons: `%s' is up to date.\n" % arg
            if options:
                arguments = options + " " + arguments
        kw['arguments'] = arguments
        kw['stdout'] = self.wrap_stdout(build_str = s)
        apply(self.run, [], kw)

    def not_up_to_date(self, options = None, arguments = None, **kw):
        """Asserts that none of the targets listed in arguments is
        up to date, but does not make any assumptions on other targets.
        This function is most useful in conjunction with the -n option.
        """
        s = ""
        for  arg in string.split(arguments):
            s = s + "(?!scons: `%s' is up to date.)" % arg
            if options:
                arguments = options + " " + arguments
        kw['arguments'] = arguments
        kw['stdout'] = self.wrap_stdout(build_str="("+s+"[^\n]*\n)*")
        kw['stdout'] = string.replace(kw['stdout'],'\n','\\n')
        kw['stdout'] = string.replace(kw['stdout'],'.','\\.')
        old_match_func = self.match_func
        self.match_func = TestCmd.match_re_dotall
        apply(self.run, [], kw)
        self.match_func = old_match_func