Skip to content

Commit 6d9a840

Browse files
committed
Makes add_source_file(s) method raise exception when no files match
1 parent 0edafdf commit 6d9a840

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

vunit/project.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
55
# You can obtain one at http://mozilla.org/MPL/2.0/.
66
#
7-
# Copyright (c) 2014-2015, Lars Asplund lars.anders.asplund@gmail.com
7+
# Copyright (c) 2014-2016, Lars Asplund lars.anders.asplund@gmail.com
88

99
"""
1010
Functionality to represent and operate on a HDL code project
@@ -76,6 +76,9 @@ def add_source_file(self, file_name, library_name, file_type='vhdl', include_dir
7676
"""
7777
Add a file_name as a source file in library_name with file_type
7878
"""
79+
if not ostools.file_exists(file_name):
80+
raise ValueError("File %r does not exist" % file_name)
81+
7982
LOGGER.info('Adding source file %s to library %s', file_name, library_name)
8083
self._validate_library_name(library_name)
8184
library = self._libraries[library_name]

vunit/test/unit/test_ui.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
33
# You can obtain one at http://mozilla.org/MPL/2.0/.
44
#
5-
# Copyright (c) 2014-2015, Lars Asplund lars.anders.asplund@gmail.com
5+
# Copyright (c) 2014-2016, Lars Asplund lars.anders.asplund@gmail.com
66

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

206+
def test_exception_on_adding_zero_files(self):
207+
ui = self._create_ui()
208+
lib = ui.library("lib")
209+
self.assertRaisesRegexp(ValueError, "Pattern.*missing1.vhd.*",
210+
lib.add_source_files, join(dirname(__file__), 'missing1.vhd'))
211+
self.assertRaisesRegexp(ValueError, "File.*missing2.vhd.*",
212+
lib.add_source_file, join(dirname(__file__), 'missing2.vhd'))
213+
214+
def test_no_exception_on_adding_zero_files_when_allowed(self):
215+
ui = self._create_ui()
216+
lib = ui.library("lib")
217+
lib.add_source_files(join(dirname(__file__), 'missing.vhd'), allow_empty=True)
218+
206219
def _test_pre_config_helper(self, retval, test_not_entity=False):
207220
"""
208221
Helper method to test pre_config where the pre config can return different values

vunit/ui.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
33
# You can obtain one at http://mozilla.org/MPL/2.0/.
44
#
5-
# Copyright (c) 2014-2015, Lars Asplund lars.anders.asplund@gmail.com
5+
# Copyright (c) 2014-2016, Lars Asplund lars.anders.asplund@gmail.com
66

77
"""
88
The main public Python interface of VUnit.
@@ -247,13 +247,20 @@ def disable_ieee_warnings(self):
247247
"""
248248
self._configuration.disable_ieee_warnings(scope=create_scope())
249249

250-
def add_source_files(self, pattern, library_name, preprocessors=None, include_dirs=None):
250+
def add_source_files(self, # pylint: disable=too-many-arguments
251+
pattern, library_name, preprocessors=None, include_dirs=None, allow_empty=False):
251252
"""
252253
Add source files matching wildcard pattern to library
253254
"""
255+
file_names = glob(pattern)
256+
257+
if (not allow_empty) and len(file_names) == 0:
258+
raise ValueError(("Pattern %r did not match any file. "
259+
"Use allow_empty=True to avoid exception,") % pattern)
260+
254261
return FileSetFacade(source_files=[
255262
self.add_source_file(file_name, library_name, preprocessors, include_dirs)
256-
for file_name in glob(pattern)])
263+
for file_name in file_names])
257264

258265
def add_source_file(self, file_name, library_name, preprocessors=None, include_dirs=None):
259266
"""
@@ -559,8 +566,12 @@ def disable_ieee_warnings(self):
559566
"""
560567
self._configuration.disable_ieee_warnings(scope=self._scope)
561568

562-
def add_source_files(self, pattern, preprocessors=None):
563-
return self._parent.add_source_files(pattern, self._library_name, preprocessors)
569+
def add_source_files(self, pattern, preprocessors=None, allow_empty=False):
570+
return self._parent.add_source_files(pattern, self._library_name, preprocessors,
571+
allow_empty=allow_empty)
572+
573+
def add_source_file(self, pattern, preprocessors=None):
574+
return self._parent.add_source_file(pattern, self._library_name, preprocessors)
564575

565576
def package(self, package_name):
566577
"""

0 commit comments

Comments
 (0)