]> git.cworth.org Git - apitrace-tests/blob - tool_driver.py
Split the driver code.
[apitrace-tests] / tool_driver.py
1 #!/usr/bin/env python
2 ##########################################################################
3 #
4 # Copyright 2011 Jose Fonseca
5 # All Rights Reserved.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # THE SOFTWARE.
24 #
25 ##########################################################################/
26
27 '''Tool test driver.'''
28
29
30 import os.path
31 import subprocess
32 import sys
33
34
35 from base_driver import *
36
37
38 class AsciiComparer:
39
40     def __init__(self, srcStream, refFileName, verbose=False):
41         self.srcStream = srcStream
42         self.refFileName = refFileName
43         if refFileName:
44             self.refStream = open(refFileName, 'rt')
45         else:
46             self.refStream = None
47     
48     def readLines(self, stream):
49         lines = []
50         for line in stream:
51             line = line[:-1]
52             lines.append(line)
53         return lines
54
55     def compare(self):
56         refLines = self.readLines(self.refStream)
57         srcLines = self.readLines(self.srcStream)
58
59         numLines = max(len(refLines), len(srcLines))
60
61         for lineNo in xrange(numLines):
62             try:
63                 refLine = refLines[lineNo]
64             except IndexError:
65                 fail('unexpected junk: %r' % self.srcLines[lineNo])
66
67             try:
68                 srcLine = srcLines[lineNo]
69             except IndexError:
70                 fail('unexpected EOF: %r expected' % refLine)
71
72             if refLine != srcLine:
73                 fail('mismatch: expected %r but got %r' % (refLine ,srcLine))
74
75
76
77 class TestCase:
78
79     cmd = None
80     cwd = None
81     ref_dump = None
82
83     verbose = False
84
85     def __init__(self):
86         pass
87     
88     def runTool(self):
89         '''Run the application standalone, skipping this test if it fails by
90         some reason.'''
91
92         if not self.cmd:
93             return
94
95         if self.ref_dump:
96             stdout = subprocess.PIPE
97         else:
98             stdout = None
99
100         cmd = [options.apitrace] + self.cmd
101         p = popen(cmd, cwd=self.cwd, stdout=stdout)
102
103         if self.ref_dump:
104             comparer = AsciiComparer(p.stdout, self.ref_dump, self.verbose)
105             comparer.compare()
106
107         p.wait()
108         if p.returncode != 0:
109             fail('tool returned code %i' % p.returncode)
110
111     def run(self):
112         self.runTool()
113
114         pass_()
115
116
117 class ToolMain(Main):
118
119     def createOptParser(self):
120         optparser = Main.createOptParser(self)
121
122         optparser.add_option(
123             '--ref-dump', metavar='PATH',
124             type='string', dest='ref_dump', default=None,
125             help='reference dump')
126
127         return optparser
128
129     def main(self):
130         global options
131
132         (options, args) = self.parseOptions()
133
134         test = TestCase()
135         test.verbose = options.verbose
136
137         test.cmd = args
138         test.cwd = options.cwd
139         test.ref_dump = options.ref_dump
140
141         test.run()
142
143
144 if __name__ == '__main__':
145     ToolMain().main()