Added test case

pull/4858/head
Daniel Mensinger 6 years ago
parent c64d80cf2f
commit 5655865246
No known key found for this signature in database
GPG Key ID: 54DD94C131E277D4
  1. 6
      mesonbuild/ast/introspection.py
  2. 42
      mesonbuild/rewriter.py
  3. 64
      run_unittests.py
  4. 29
      test cases/rewrite/3 kwargs/add.json
  5. 20
      test cases/rewrite/3 kwargs/delete.json
  6. 14
      test cases/rewrite/3 kwargs/info.json
  7. 5
      test cases/rewrite/3 kwargs/meson.build
  8. 29
      test cases/rewrite/3 kwargs/remove.json
  9. 24
      test cases/rewrite/3 kwargs/set.json

@ -163,10 +163,8 @@ class IntrospectionInterpreter(AstInterpreter):
if elemetary_nodes: if elemetary_nodes:
source_nodes += [curr] source_nodes += [curr]
# Filter out kwargs from other target types. For example 'soversion' # Make sure nothing can crash when creating the build class
# passed to library() when default_library == 'static'. kwargs = {}
kwargs = {k: v for k, v in kwargs.items() if k in targetclass.known_kwargs}
is_cross = False is_cross = False
objects = [] objects = []
empty_sources = [] # Passing the unresolved sources list causes errors empty_sources = [] # Passing the unresolved sources list causes errors

@ -232,7 +232,7 @@ class MesonIDList(MesonList):
super().__init__(node) super().__init__(node)
def _new_element_node(self, value): def _new_element_node(self, value):
return mparser.StringNode(mparser.Token('', '', 0, 0, 0, None, str(value))) return mparser.IdNode(mparser.Token('', '', 0, 0, 0, None, str(value)))
def _check_is_equal(self, node, value): def _check_is_equal(self, node, value):
if isinstance(node, mparser.IdNode): if isinstance(node, mparser.IdNode):
@ -246,7 +246,7 @@ rewriter_keys = {
'kwargs': { 'kwargs': {
'function': (str, None, None), 'function': (str, None, None),
'id': (str, None, None), 'id': (str, None, None),
'operation': (str, None, ['set', 'delete', 'add', 'remove']), 'operation': (str, None, ['set', 'delete', 'add', 'remove', 'info']),
'kwargs': (dict, {}, None) 'kwargs': (dict, {}, None)
}, },
'target': { 'target': {
@ -355,16 +355,40 @@ class Rewriter:
# Find the function node to modify # Find the function node to modify
node = None node = None
arg_node = None
if cmd['function'] == 'project': if cmd['function'] == 'project':
node = self.interpreter.project_node node = self.interpreter.project_node
arg_node = node.args
elif cmd['function'] == 'target': elif cmd['function'] == 'target':
tmp = self.find_target(cmd['id']) tmp = self.find_target(cmd['id'])
if tmp: if tmp:
node = tmp['node'] node = tmp['node']
arg_node = node.args
if not node: if not node:
mlog.error('Unable to find the function node') mlog.error('Unable to find the function node')
assert(isinstance(node, mparser.FunctionNode)) assert(isinstance(node, mparser.FunctionNode))
assert(isinstance(arg_node, mparser.ArgumentNode))
# Print kwargs info
if cmd['operation'] == 'info':
info_data = {}
for key, val in arg_node.kwargs.items():
info_data[key] = None
if isinstance(val, mparser.ElementaryNode):
info_data[key] = val.value
elif isinstance(val, mparser.ArrayNode):
data_list = []
for i in val.args.arguments:
element = None
if isinstance(i, mparser.ElementaryNode):
element = i.value
data_list += [element]
info_data[key] = data_list
self.add_info('kwargs', '{}#{}'.format(cmd['function'], cmd['id']), info_data)
return # Nothing else to do
# Modify the kwargs
num_changed = 0 num_changed = 0
for key, val in cmd['kwargs'].items(): for key, val in cmd['kwargs'].items():
if key not in kwargs_def: if key not in kwargs_def:
@ -373,17 +397,17 @@ class Rewriter:
# Remove the key from the kwargs # Remove the key from the kwargs
if cmd['operation'] == 'delete': if cmd['operation'] == 'delete':
if key in node.args.kwargs: if key in arg_node.kwargs:
mlog.log(' -- Deleting', mlog.bold(key), 'from the kwargs') mlog.log(' -- Deleting', mlog.bold(key), 'from the kwargs')
del node.args.kwargs[key] del arg_node.kwargs[key]
num_changed += 1 num_changed += 1
else: else:
mlog.log(' -- Key', mlog.bold(key), 'is already deleted') mlog.log(' -- Key', mlog.bold(key), 'is already deleted')
continue continue
if key not in node.args.kwargs: if key not in arg_node.kwargs:
node.args.kwargs[key] = None arg_node.kwargs[key] = None
modifyer = kwargs_def[key](node.args.kwargs[key]) modifyer = kwargs_def[key](arg_node.kwargs[key])
if not modifyer.can_modify(): if not modifyer.can_modify():
mlog.log(' -- Skipping', mlog.bold(key), 'because it is to complex to modify') mlog.log(' -- Skipping', mlog.bold(key), 'because it is to complex to modify')
@ -400,7 +424,7 @@ class Rewriter:
modifyer.remove_value(val) modifyer.remove_value(val)
# Write back the result # Write back the result
node.args.kwargs[key] = modifyer.get_node() arg_node.kwargs[key] = modifyer.get_node()
num_changed += 1 num_changed += 1
if num_changed > 0 and node not in self.modefied_nodes: if num_changed > 0 and node not in self.modefied_nodes:

@ -5130,6 +5130,70 @@ class RewriterTests(BasePlatformTests):
out = self.extract_test_data(out) out = self.extract_test_data(out)
self.assertDictEqual(list(out['target'].values())[0], expected) self.assertDictEqual(list(out['target'].values())[0], expected)
def test_kwargs_info(self):
self.prime('3 kwargs')
out = self.rewrite(self.builddir, os.path.join(self.builddir, 'info.json'))
out = self.extract_test_data(out)
expected = {
'kwargs': {
'project#': {'version': '0.0.1'},
'target#tgt1': {'build_by_default': True}
}
}
self.assertDictEqual(out, expected)
def test_kwargs_set(self):
self.prime('3 kwargs')
self.rewrite(self.builddir, os.path.join(self.builddir, 'set.json'))
out = self.rewrite(self.builddir, os.path.join(self.builddir, 'info.json'))
out = self.extract_test_data(out)
expected = {
'kwargs': {
'project#': {'version': '0.0.2', 'meson_version': '0.50.0', 'license': ['GPL', 'MIT']},
'target#tgt1': {'build_by_default': False, 'build_rpath': '/usr/local', 'dependencies': 'dep1'}
}
}
self.assertDictEqual(out, expected)
def test_kwargs_add(self):
self.prime('3 kwargs')
self.rewrite(self.builddir, os.path.join(self.builddir, 'add.json'))
out = self.rewrite(self.builddir, os.path.join(self.builddir, 'info.json'))
out = self.extract_test_data(out)
expected = {
'kwargs': {
'project#': {'version': '0.0.1', 'license': ['GPL', 'MIT', 'BSD']},
'target#tgt1': {'build_by_default': True}
}
}
self.assertDictEqual(out, expected)
def test_kwargs_remove(self):
self.prime('3 kwargs')
self.rewrite(self.builddir, os.path.join(self.builddir, 'remove.json'))
out = self.rewrite(self.builddir, os.path.join(self.builddir, 'info.json'))
out = self.extract_test_data(out)
expected = {
'kwargs': {
'project#': {'version': '0.0.1', 'license': 'GPL'},
'target#tgt1': {'build_by_default': True}
}
}
self.assertDictEqual(out, expected)
def test_kwargs_delete(self):
self.prime('3 kwargs')
self.rewrite(self.builddir, os.path.join(self.builddir, 'delete.json'))
out = self.rewrite(self.builddir, os.path.join(self.builddir, 'info.json'))
out = self.extract_test_data(out)
expected = {
'kwargs': {
'project#': {},
'target#tgt1': {}
}
}
self.assertDictEqual(out, expected)
class NativeFileTests(BasePlatformTests): class NativeFileTests(BasePlatformTests):
def setUp(self): def setUp(self):

@ -0,0 +1,29 @@
[
{
"type": "kwargs",
"function": "project",
"id": "",
"operation": "set",
"kwargs": {
"license": "GPL"
}
},
{
"type": "kwargs",
"function": "project",
"id": "",
"operation": "add",
"kwargs": {
"license": ["MIT"]
}
},
{
"type": "kwargs",
"function": "project",
"id": "",
"operation": "add",
"kwargs": {
"license": "BSD"
}
}
]

@ -0,0 +1,20 @@
[
{
"type": "kwargs",
"function": "project",
"id": "",
"operation": "delete",
"kwargs": {
"version": null
}
},
{
"type": "kwargs",
"function": "target",
"id": "helloWorld",
"operation": "delete",
"kwargs": {
"build_by_default": false
}
}
]

@ -0,0 +1,14 @@
[
{
"type": "kwargs",
"function": "project",
"id": "",
"operation": "info"
},
{
"type": "kwargs",
"function": "target",
"id": "tgt1",
"operation": "info"
}
]

@ -0,0 +1,5 @@
project('rewritetest', 'cpp', version: '0.0.1')
dep1 = dependency('zlib', required: false)
tgt1 = executable('helloWorld', 'main.cpp', build_by_default: true)

@ -0,0 +1,29 @@
[
{
"type": "kwargs",
"function": "project",
"id": "",
"operation": "set",
"kwargs": {
"license": ["GPL", "MIT", "BSD"]
}
},
{
"type": "kwargs",
"function": "project",
"id": "",
"operation": "remove",
"kwargs": {
"license": ["MIT"]
}
},
{
"type": "kwargs",
"function": "project",
"id": "",
"operation": "remove",
"kwargs": {
"license": "BSD"
}
}
]

@ -0,0 +1,24 @@
[
{
"type": "kwargs",
"function": "project",
"id": "",
"operation": "set",
"kwargs": {
"version": "0.0.2",
"meson_version": "0.50.0",
"license": ["GPL", "MIT"]
}
},
{
"type": "kwargs",
"function": "target",
"id": "helloWorld",
"operation": "set",
"kwargs": {
"build_by_default": false,
"build_rpath": "/usr/local",
"dependencies": "dep1"
}
}
]
Loading…
Cancel
Save