Skip to content

Commit

Permalink
Makes add_source_file(s) method raise exception when no files match
Browse files Browse the repository at this point in the history
  • Loading branch information
kraigher committed Jan 7, 2016
1 parent 0edafdf commit 6d9a840
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
5 changes: 4 additions & 1 deletion vunit/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2014-2015, Lars Asplund [email protected]
# Copyright (c) 2014-2016, Lars Asplund [email protected]

"""
Functionality to represent and operate on a HDL code project
Expand Down Expand Up @@ -76,6 +76,9 @@ def add_source_file(self, file_name, library_name, file_type='vhdl', include_dir
"""
Add a file_name as a source file in library_name with file_type
"""
if not ostools.file_exists(file_name):
raise ValueError("File %r does not exist" % file_name)

LOGGER.info('Adding source file %s to library %s', file_name, library_name)
self._validate_library_name(library_name)
library = self._libraries[library_name]
Expand Down
15 changes: 14 additions & 1 deletion vunit/test/unit/test_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2014-2015, Lars Asplund [email protected]
# Copyright (c) 2014-2016, Lars Asplund [email protected]

"""
Acceptance test of the VUnit public interface class
Expand Down Expand Up @@ -203,6 +203,19 @@ def test_can_add_non_ascii_encoded_files(self):
lib.add_source_files(join(dirname(__file__), 'test_ui_encoding.vhd'))
lib.entity("encoding") # Fill raise exception of not found

def test_exception_on_adding_zero_files(self):
ui = self._create_ui()
lib = ui.library("lib")
self.assertRaisesRegexp(ValueError, "Pattern.*missing1.vhd.*",
lib.add_source_files, join(dirname(__file__), 'missing1.vhd'))
self.assertRaisesRegexp(ValueError, "File.*missing2.vhd.*",
lib.add_source_file, join(dirname(__file__), 'missing2.vhd'))

def test_no_exception_on_adding_zero_files_when_allowed(self):
ui = self._create_ui()
lib = ui.library("lib")
lib.add_source_files(join(dirname(__file__), 'missing.vhd'), allow_empty=True)

def _test_pre_config_helper(self, retval, test_not_entity=False):
"""
Helper method to test pre_config where the pre config can return different values
Expand Down
21 changes: 16 additions & 5 deletions vunit/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2014-2015, Lars Asplund [email protected]
# Copyright (c) 2014-2016, Lars Asplund [email protected]

"""
The main public Python interface of VUnit.
Expand Down Expand Up @@ -247,13 +247,20 @@ def disable_ieee_warnings(self):
"""
self._configuration.disable_ieee_warnings(scope=create_scope())

def add_source_files(self, pattern, library_name, preprocessors=None, include_dirs=None):
def add_source_files(self, # pylint: disable=too-many-arguments
pattern, library_name, preprocessors=None, include_dirs=None, allow_empty=False):
"""
Add source files matching wildcard pattern to library
"""
file_names = glob(pattern)

if (not allow_empty) and len(file_names) == 0:
raise ValueError(("Pattern %r did not match any file. "
"Use allow_empty=True to avoid exception,") % pattern)

return FileSetFacade(source_files=[
self.add_source_file(file_name, library_name, preprocessors, include_dirs)
for file_name in glob(pattern)])
for file_name in file_names])

def add_source_file(self, file_name, library_name, preprocessors=None, include_dirs=None):
"""
Expand Down Expand Up @@ -559,8 +566,12 @@ def disable_ieee_warnings(self):
"""
self._configuration.disable_ieee_warnings(scope=self._scope)

def add_source_files(self, pattern, preprocessors=None):
return self._parent.add_source_files(pattern, self._library_name, preprocessors)
def add_source_files(self, pattern, preprocessors=None, allow_empty=False):
return self._parent.add_source_files(pattern, self._library_name, preprocessors,
allow_empty=allow_empty)

def add_source_file(self, pattern, preprocessors=None):
return self._parent.add_source_file(pattern, self._library_name, preprocessors)

def package(self, package_name):
"""
Expand Down

0 comments on commit 6d9a840

Please sign in to comment.