summaryrefslogtreecommitdiffstats
path: root/etc/TestSCons.py
blob: 14c2e0b6b28038b481e2780be5ca56b32b3c7df0 (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
"""
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 Steven Knight

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import os
import os.path
import string
import TestCmd

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

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

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'):
	    if os.path.exists('scons'):
		kw['program'] = 'scons'
	    else:
		kw['program'] = 'scons.py'
	if not kw.has_key('interpreter'):
	    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, 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. 
    

        By default, this does not test standard output (stdout = None),
        and expects that error output is empty (stderr = "").
	"""
	try:
	    apply(TestCmd.TestCmd.run, [self], kw)
	except:
	    print "STDOUT ============"
	    print self.stdout()
	    print "STDERR ============"
	    print self.stderr()
	    raise
	if self.status>>8 != status:
	    print "%s returned %d" % (self.program, self.status >> 8)
	    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 "Expected STDERR =========="
	    print stderr
	    print "Actual STDERR ============"
	    print self.stderr()
            print "STDOUT ==================="
            print self.stdout()
	    raise TestFailed

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