]> git.cworth.org Git - apitrace-tests/blob - cli_driver.py
Add a new 'cli' subdirectory for higher-level testing of apitrace CLI.
[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, errno, shutil, subprocess
28
29 from base_driver import *
30
31 class CliDriver(Driver):
32
33     def do_apitrace(self, args):
34         cmd = [self.options.apitrace] + args[1:]
35  
36         self.output = subprocess.check_output(cmd)
37
38     def do_expect(self, args):
39         expected = json.loads(" ".join(args[1:]))
40         if (self.output != expected):
41             fail("Unexpected output:\n    Expected: %s\n    Received: %s\n" % (expected, self.output))
42
43     def do_rm_and_mkdir(self, args):
44
45         # Operate only on local directories
46         dir = './' + args[1]
47
48         # Failing to delete a directory that doesn't exist is no failure
49         def rmtree_onerror(function, path, excinfo):
50             if excinfo[0] == OSError and excinfo[1].errno != errno.ENOENT:
51                 raise
52
53         shutil.rmtree(dir, onerror = rmtree_onerror)
54         os.makedirs(dir)
55
56     def unknown_command(self, args):
57         fail('Broken test script: Unknown command: %s' % (args[0]))
58
59     def run_script(self, cli_script):
60         "Execute the commands in the given cli script."
61
62         commands = {
63             'apitrace': self.do_apitrace,
64             'expect': self.do_expect,
65             'rm_and_mkdir': self.do_rm_and_mkdir
66         }
67
68         script = open(cli_script, 'rt')
69
70         while True:
71             line = script.readline()
72
73             if (line == ''):
74                 break
75
76             cmd = line.split()
77
78             if (len(cmd) == 0):
79                 continue
80
81             commands.get(cmd[0], self.unknown_command)(cmd)
82
83     def run(self):
84         self.parseOptions()
85
86         self.run_script(self.args[0])
87
88         pass_()
89
90 if __name__ == '__main__':
91     CliDriver().run()