softmmu: split off vl.c:main() into main.c
[qemu/ar7.git] / tests / qapi-schema / test-qapi.py
blob41232c11a3901abcdf12110dd2b7cb01d1dbe471
1 #!/usr/bin/env python3
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.
15 import argparse
16 import difflib
17 import os
18 import sys
20 from qapi.error import QAPIError
21 from qapi.schema import QAPISchema, QAPISchemaVisitor
23 if sys.version_info[0] < 3:
24 from cStringIO import StringIO
25 else:
26 from io import StringIO
29 class QAPISchemaTestVisitor(QAPISchemaVisitor):
31 def visit_module(self, name):
32 print('module %s' % name)
34 def visit_include(self, name, info):
35 print('include %s' % name)
37 def visit_enum_type(self, name, info, ifcond, members, prefix):
38 print('enum %s' % name)
39 if prefix:
40 print(' prefix %s' % prefix)
41 for m in members:
42 print(' member %s' % m.name)
43 self._print_if(m.ifcond, indent=8)
44 self._print_if(ifcond)
46 def visit_array_type(self, name, info, ifcond, element_type):
47 if not info:
48 return # suppress built-in arrays
49 print('array %s %s' % (name, element_type.name))
50 self._print_if(ifcond)
52 def visit_object_type(self, name, info, ifcond, base, members, variants,
53 features):
54 print('object %s' % name)
55 if base:
56 print(' base %s' % base.name)
57 for m in members:
58 print(' member %s: %s optional=%s'
59 % (m.name, m.type.name, m.optional))
60 self._print_if(m.ifcond, 8)
61 self._print_variants(variants)
62 self._print_if(ifcond)
63 self._print_features(features)
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 features):
73 print('command %s %s -> %s'
74 % (name, arg_type and arg_type.name,
75 ret_type and ret_type.name))
76 print(' gen=%s success_response=%s boxed=%s oob=%s preconfig=%s'
77 % (gen, success_response, boxed, allow_oob, allow_preconfig))
78 self._print_if(ifcond)
79 self._print_features(features)
81 def visit_event(self, name, info, ifcond, arg_type, boxed):
82 print('event %s %s' % (name, arg_type and arg_type.name))
83 print(' boxed=%s' % boxed)
84 self._print_if(ifcond)
86 @staticmethod
87 def _print_variants(variants):
88 if variants:
89 print(' tag %s' % variants.tag_member.name)
90 for v in variants.variants:
91 print(' case %s: %s' % (v.name, v.type.name))
92 QAPISchemaTestVisitor._print_if(v.ifcond, indent=8)
94 @staticmethod
95 def _print_if(ifcond, indent=4):
96 if ifcond:
97 print('%sif %s' % (' ' * indent, ifcond))
99 @classmethod
100 def _print_features(cls, features):
101 if features:
102 for f in features:
103 print(' feature %s' % f.name)
104 cls._print_if(f.ifcond, 8)
107 def test_frontend(fname):
108 schema = QAPISchema(fname)
109 schema.visit(QAPISchemaTestVisitor())
111 for doc in schema.docs:
112 if doc.symbol:
113 print('doc symbol=%s' % doc.symbol)
114 else:
115 print('doc freeform')
116 print(' body=\n%s' % doc.body.text)
117 for arg, section in doc.args.items():
118 print(' arg=%s\n%s' % (arg, section.text))
119 for feat, section in doc.features.items():
120 print(' feature=%s\n%s' % (feat, section.text))
121 for section in doc.sections:
122 print(' section=%s\n%s' % (section.name, section.text))
125 def test_and_diff(test_name, dir_name, update):
126 sys.stdout = StringIO()
127 try:
128 test_frontend(os.path.join(dir_name, test_name + '.json'))
129 except QAPIError as err:
130 if err.info.fname is None:
131 print("%s" % err, file=sys.stderr)
132 return 2
133 errstr = str(err) + '\n'
134 if dir_name:
135 errstr = errstr.replace(dir_name + '/', '')
136 actual_err = errstr.splitlines(True)
137 else:
138 actual_err = []
139 finally:
140 actual_out = sys.stdout.getvalue().splitlines(True)
141 sys.stdout.close()
142 sys.stdout = sys.__stdout__
144 mode = 'r+' if update else 'r'
145 try:
146 outfp = open(os.path.join(dir_name, test_name + '.out'), mode)
147 errfp = open(os.path.join(dir_name, test_name + '.err'), mode)
148 expected_out = outfp.readlines()
149 expected_err = errfp.readlines()
150 except IOError as err:
151 print("%s: can't open '%s': %s"
152 % (sys.argv[0], err.filename, err.strerror),
153 file=sys.stderr)
154 return 2
156 if actual_out == expected_out and actual_err == expected_err:
157 return 0
159 print("%s %s" % (test_name, 'UPDATE' if update else 'FAIL'),
160 file=sys.stderr)
161 out_diff = difflib.unified_diff(expected_out, actual_out, outfp.name)
162 err_diff = difflib.unified_diff(expected_err, actual_err, errfp.name)
163 sys.stdout.writelines(out_diff)
164 sys.stdout.writelines(err_diff)
166 if not update:
167 return 1
169 try:
170 outfp.truncate(0)
171 outfp.seek(0)
172 outfp.writelines(actual_out)
173 errfp.truncate(0)
174 errfp.seek(0)
175 errfp.writelines(actual_err)
176 except IOError as err:
177 print("%s: can't write '%s': %s"
178 % (sys.argv[0], err.filename, err.strerror),
179 file=sys.stderr)
180 return 2
182 return 0
185 def main(argv):
186 parser = argparse.ArgumentParser(
187 description='QAPI schema tester')
188 parser.add_argument('-d', '--dir', action='store', default='',
189 help="directory containing tests")
190 parser.add_argument('-u', '--update', action='store_true',
191 help="update expected test results")
192 parser.add_argument('tests', nargs='*', metavar='TEST', action='store')
193 args = parser.parse_args()
195 status = 0
196 for t in args.tests:
197 (dir_name, base_name) = os.path.split(t)
198 dir_name = dir_name or args.dir
199 test_name = os.path.splitext(base_name)[0]
200 status |= test_and_diff(test_name, dir_name, args.update)
202 exit(status)
205 if __name__ == '__main__':
206 main(sys.argv)
207 exit(0)