qapi: Speed up frontend tests
[qemu/ar7.git] / tests / qapi-schema / test-qapi.py
blob42baa702b6517680ec32ddaf4d05e447a003f643
1 #!/usr/bin/env python
3 # QAPI parser test harness
5 # Copyright (c) 2013 Red Hat Inc.
7 # Authors:
8 # Markus Armbruster <armbru@redhat.com>
10 # This work is licensed under the terms of the GNU GPL, version 2 or later.
11 # See the COPYING file in the top-level directory.
14 from __future__ import print_function
15 import argparse
16 import difflib
17 import os
18 import sys
19 if sys.version_info[0] < 3:
20 from cStringIO import StringIO
21 else:
22 from io import StringIO
23 from qapi.common import QAPIError, QAPISchema, QAPISchemaVisitor
26 class QAPISchemaTestVisitor(QAPISchemaVisitor):
28 def visit_module(self, name):
29 print('module %s' % name)
31 def visit_include(self, name, info):
32 print('include %s' % name)
34 def visit_enum_type(self, name, info, ifcond, members, prefix):
35 print('enum %s' % name)
36 if prefix:
37 print(' prefix %s' % prefix)
38 for m in members:
39 print(' member %s' % m.name)
40 self._print_if(m.ifcond, indent=8)
41 self._print_if(ifcond)
43 def visit_array_type(self, name, info, ifcond, element_type):
44 if not info:
45 return # suppress built-in arrays
46 print('array %s %s' % (name, element_type.name))
47 self._print_if(ifcond)
49 def visit_object_type(self, name, info, ifcond, base, members, variants,
50 features):
51 print('object %s' % name)
52 if base:
53 print(' base %s' % base.name)
54 for m in members:
55 print(' member %s: %s optional=%s'
56 % (m.name, m.type.name, m.optional))
57 self._print_if(m.ifcond, 8)
58 self._print_variants(variants)
59 self._print_if(ifcond)
60 if features:
61 for f in features:
62 print(' feature %s' % f.name)
63 self._print_if(f.ifcond, 8)
65 def visit_alternate_type(self, name, info, ifcond, variants):
66 print('alternate %s' % name)
67 self._print_variants(variants)
68 self._print_if(ifcond)
70 def visit_command(self, name, info, ifcond, arg_type, ret_type, gen,
71 success_response, boxed, allow_oob, allow_preconfig):
72 print('command %s %s -> %s'
73 % (name, arg_type and arg_type.name,
74 ret_type and ret_type.name))
75 print(' gen=%s success_response=%s boxed=%s oob=%s preconfig=%s'
76 % (gen, success_response, boxed, allow_oob, allow_preconfig))
77 self._print_if(ifcond)
79 def visit_event(self, name, info, ifcond, arg_type, boxed):
80 print('event %s %s' % (name, arg_type and arg_type.name))
81 print(' boxed=%s' % boxed)
82 self._print_if(ifcond)
84 @staticmethod
85 def _print_variants(variants):
86 if variants:
87 print(' tag %s' % variants.tag_member.name)
88 for v in variants.variants:
89 print(' case %s: %s' % (v.name, v.type.name))
90 QAPISchemaTestVisitor._print_if(v.ifcond, indent=8)
92 @staticmethod
93 def _print_if(ifcond, indent=4):
94 if ifcond:
95 print('%sif %s' % (' ' * indent, ifcond))
98 def test_frontend(fname):
99 schema = QAPISchema(fname)
100 schema.visit(QAPISchemaTestVisitor())
102 for doc in schema.docs:
103 if doc.symbol:
104 print('doc symbol=%s' % doc.symbol)
105 else:
106 print('doc freeform')
107 print(' body=\n%s' % doc.body.text)
108 for arg, section in doc.args.items():
109 print(' arg=%s\n%s' % (arg, section.text))
110 for section in doc.sections:
111 print(' section=%s\n%s' % (section.name, section.text))
114 def test_and_diff(test_name, dir_name, update):
115 sys.stdout = StringIO()
116 try:
117 test_frontend(os.path.join(dir_name, test_name + '.json'))
118 except QAPIError as err:
119 if err.info.fname is None:
120 print("%s" % err, file=sys.stderr)
121 return 2
122 errstr = str(err) + '\n'
123 if dir_name:
124 errstr = errstr.replace(dir_name + '/', '')
125 actual_err = errstr.splitlines(True)
126 else:
127 actual_err = []
128 finally:
129 actual_out = sys.stdout.getvalue().splitlines(True)
130 sys.stdout.close()
131 sys.stdout = sys.__stdout__
133 mode = 'r+' if update else 'r'
134 try:
135 outfp = open(os.path.join(dir_name, test_name + '.out'), mode)
136 errfp = open(os.path.join(dir_name, test_name + '.err'), mode)
137 expected_out = outfp.readlines()
138 expected_err = errfp.readlines()
139 except IOError as err:
140 print("%s: can't open '%s': %s"
141 % (sys.argv[0], err.filename, err.strerror),
142 file=sys.stderr)
143 return 2
145 if actual_out == expected_out and actual_err == expected_err:
146 return 0
148 print("%s %s" % (test_name, 'UPDATE' if update else 'FAIL'),
149 file=sys.stderr)
150 out_diff = difflib.unified_diff(expected_out, actual_out, outfp.name)
151 err_diff = difflib.unified_diff(expected_err, actual_err, errfp.name)
152 sys.stdout.writelines(out_diff)
153 sys.stdout.writelines(err_diff)
155 if not update:
156 return 1
158 try:
159 outfp.truncate(0)
160 outfp.seek(0)
161 outfp.writelines(actual_out)
162 errfp.truncate(0)
163 errfp.seek(0)
164 errfp.writelines(actual_err)
165 except IOError as err:
166 print("%s: can't write '%s': %s"
167 % (sys.argv[0], err.filename, err.strerror),
168 file=sys.stderr)
169 return 2
171 return 0
174 def main(argv):
175 parser = argparse.ArgumentParser(
176 description='QAPI schema tester')
177 parser.add_argument('-d', '--dir', action='store', default='',
178 help="directory containing tests")
179 parser.add_argument('-u', '--update', action='store_true',
180 help="update expected test results")
181 parser.add_argument('tests', nargs='*', metavar='TEST', action='store')
182 args = parser.parse_args()
184 status = 0
185 for t in args.tests:
186 (dir_name, base_name) = os.path.split(t)
187 dir_name = dir_name or args.dir
188 test_name = os.path.splitext(base_name)[0]
189 status |= test_and_diff(test_name, dir_name, args.update)
191 exit(status)
194 if __name__ == '__main__':
195 main(sys.argv)
196 exit(0)