3 # QAPI parser test harness
5 # Copyright (c) 2013 Red Hat Inc.
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
19 if sys
.version_info
[0] < 3:
20 from cStringIO
import StringIO
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
)
37 print(' prefix %s' % prefix
)
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
):
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
,
51 print('object %s' % name
)
53 print(' base %s' % base
.name
)
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
)
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
)
85 def _print_variants(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)
93 def _print_if(ifcond
, indent
=4):
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
:
104 print('doc symbol=%s' % doc
.symbol
)
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()
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
)
122 errstr
= str(err
) + '\n'
124 errstr
= errstr
.replace(dir_name
+ '/', '')
125 actual_err
= errstr
.splitlines(True)
129 actual_out
= sys
.stdout
.getvalue().splitlines(True)
131 sys
.stdout
= sys
.__stdout
__
133 mode
= 'r+' if update
else 'r'
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
),
145 if actual_out
== expected_out
and actual_err
== expected_err
:
148 print("%s %s" % (test_name
, 'UPDATE' if update
else 'FAIL'),
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
)
161 outfp
.writelines(actual_out
)
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
),
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()
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
)
194 if __name__
== '__main__':