]> git.cworth.org Git - apitrace-tests/blob - cli_driver.py
cli: Use python differ. Fix failures.
[apitrace-tests] / cli_driver.py
1 #!/usr/bin/env python
2
3 # Copyright 2012 Intel Corporation
4 #
5 # Permission is hereby granted, free of charge, to any person
6 # obtaining a copy of this software and associated documentation files
7 # (the "Software"), to deal in the Software without restriction,
8 # including without limitation the rights to use, copy, modify, merge,
9 # publish, distribute, sublicense, and/or sell copies of the Software,
10 # and to permit persons to whom the Software is furnished to do so,
11 # subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 # SOFTWARE.
24
25 '''Test driver for scripts in the cli directory.'''
26
27 import json
28 import errno
29 import shutil
30 import subprocess
31 import difflib
32
33 from base_driver import *
34
35 class CliDriver(Driver):
36
37     def do_apitrace(self, args):
38         cmd = [self.options.apitrace] + args.split()
39  
40         print " ".join(cmd)
41         proc = subprocess.Popen(cmd, stdout = subprocess.PIPE)
42         self.output = proc.communicate()[0]
43
44         proc.wait()
45
46         if (self.expect_failure):
47             if (proc.returncode == 0):
48                 fail("Command unexpectedly passed when expecting failure:\n    " + " ".join(cmd))
49         else:
50             if (proc.returncode != 0):
51                 fail("Command failed (returned non-zero):\n    " + " ".join(cmd))
52
53     def do_expect(self, args):
54         expected = eval(args)
55         if (self.output != expected):
56             differ = difflib.Differ()
57             diff = differ.compare(expected.splitlines(1), self.output.splitlines(1))
58             diff = ''.join(diff)
59             fail("Unexpected output:\n%s\n" % diff)
60
61     def do_rm_and_mkdir(self, args):
62
63         args = args.split()
64
65         # Operate only on local directories
66         dir = './' + args[0]
67
68         # Failing to delete a directory that doesn't exist is no failure
69         def rmtree_onerror(function, path, excinfo):
70             if excinfo[0] == OSError and excinfo[1].errno != errno.ENOENT:
71                 raise
72
73         shutil.rmtree(dir, onerror = rmtree_onerror)
74         os.makedirs(dir)
75
76     def unknown_command(self, args):
77         fail('Broken test script: Unknown command: %s' % (args))
78
79     def run_script(self, cli_script):
80         "Execute the commands in the given cli script."
81
82         commands = {
83             'apitrace': self.do_apitrace,
84             'expect': self.do_expect,
85             'rm_and_mkdir': self.do_rm_and_mkdir
86         }
87
88         script = open(cli_script, 'rt')
89
90         while True:
91
92             self.expect_failure = False
93
94             line = script.readline()
95
96             # Exit loop on EOF
97             if (line == ''):
98                 break
99
100             line = line.rstrip()
101
102             if " " in line:
103                 (cmd, args) = line.split(None,1)
104                 if args.startswith('r"""'):
105                     while not line.endswith('"""'):
106                         line = script.readline()
107                         line = line.rstrip()
108                         args += '\n' + line
109             else:
110                 cmd = line
111                 args = ''
112
113             # Ignore blank lines and comments
114             if (len(cmd) == 0 or cmd == '\n' or cmd[0] == '#'):
115                 continue
116
117             if (cmd == 'EXPECT_FAILURE:'):
118                 self.expect_failure = True
119                 if " " in args:
120                     (cmd, args) = args.split(None, 1)
121                 else:
122                     cmd = args
123                     args = ''
124
125             commands.get(cmd, self.unknown_command)(args)
126
127     def run(self):
128         self.parseOptions()
129
130         self.run_script(self.args[0])
131
132         pass_()
133
134 if __name__ == '__main__':
135     CliDriver().run()