Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check dependencies for duplicated types. #106

Merged
merged 2 commits into from Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 35 additions & 1 deletion test/dsdl/test_parser.py
Expand Up @@ -7,11 +7,45 @@
# Pavel Kirienko <pavel.kirienko@zubax.com>
#

import os
import unittest
from uavcan.dsdl import parser
from uavcan.dsdl import parser, parse_namespaces
from uavcan.dsdl.common import DsdlException


# TODO once module structure is clearer
class TestParseNamespaces(unittest.TestCase):
'''
Unittests of parse_namespaces function.
'''

def test_builtin(self):
'''
Test the ability to load all the UAVCAN v0 messages
'''
built_in_dir = '{}/../../uavcan/dsdl_files/uavcan'.format(os.path.dirname(__file__))
parse_namespaces([built_in_dir])

def test_duplicate_in_search_dir(self):
'''
Validate the parser allows and handles duplicate definitions in the search dir
'''
ns0_dir = '{}/fake_dsdl/ns0_base/ns0'.format(os.path.dirname(__file__))
ns0_dir_with_duplicate = '{}/fake_dsdl/ns0_duplicated/ns0'.format(os.path.dirname(__file__))

parse_namespaces([ns0_dir], [ns0_dir_with_duplicate])

def test_redefinition_in_search_dir(self):
'''
Validate the parser does not allow redefinitions in the search dir
'''
ns0_dir = '{}/fake_dsdl/ns0_base/ns0'.format(os.path.dirname(__file__))
ns0_dir_with_redefinition = '{}/fake_dsdl/ns0_redefined/ns0'.format(os.path.dirname(__file__))
try:
parse_namespaces([ns0_dir], [ns0_dir_with_redefinition])
self.assertTrue(false) # parse_namespaces should raise an exception, shouldn't get here
except DsdlException as e:
self.assertTrue(e.args[0].startswith("Redefinition of data type ID"))


if __name__ == '__main__':
Expand Down
11 changes: 8 additions & 3 deletions uavcan/dsdl/parser.py
Expand Up @@ -832,13 +832,13 @@ def parse_namespaces(source_dirs, search_dirs=None):
"""

# noinspection PyShadowingNames
def walk():
def walk(walk_dirs):
import fnmatch
from functools import partial

def on_walk_error(directory, ex):
raise DsdlException('OS error in [%s]: %s' % (directory, str(ex)))
for source_dir in source_dirs:
for source_dir in walk_dirs:
walker = os.walk(source_dir, onerror=partial(on_walk_error, source_dir), followlinks=True)
for root, _dirnames, filenames in walker:
for filename in fnmatch.filter(filenames, '*.uavcan'):
Expand Down Expand Up @@ -866,7 +866,12 @@ def ensure_unique_dtid(t, filename):

parser = Parser(source_dirs + (search_dirs or []))
output_types = []
for filename in walk():
if search_dirs:
for filename in walk(search_dirs):
t = parser.parse(filename)
ensure_unique_dtid(t, filename)

for filename in walk(source_dirs):
t = parser.parse(filename)
ensure_unique_dtid(t, filename)
output_types.append(t)
Expand Down