Skip to content

Commit

Permalink
Added possibility to provide a list of files to add_source_files. Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
kraigher committed Feb 4, 2016
1 parent 9c74e3e commit f4a09c6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
42 changes: 42 additions & 0 deletions vunit/test/unit/test_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2014-2016, Lars Asplund [email protected]
# pylint: disable=too-many-public-methods

"""
Acceptance test of the VUnit public interface class
Expand Down Expand Up @@ -167,6 +168,47 @@ def test_no_exception_on_adding_zero_files_when_allowed(self):
lib = ui.library("lib")
lib.add_source_files(join(dirname(__file__), 'missing.vhd'), allow_empty=True)

def test_add_source_files(self):
files = ["file1.vhd",
"file2.vhd",
"file3.vhd",
"file_foo.vhd"]

for file_name in files:
self.create_file(file_name)

ui = self._create_ui()
lib = ui.library("lib")
lib.add_source_files("file*.vhd")
lib.add_source_files("file_foo.vhd")
for file_name in files:
lib.get_source_file(file_name)

ui = self._create_ui()
lib = ui.library("lib")
lib.add_source_files(["file*.vhd", "file_foo.vhd"])
for file_name in files:
lib.get_source_file(file_name)

ui = self._create_ui()
lib = ui.library("lib")
lib.add_source_files(("file*.vhd", "file_foo.vhd"))
for file_name in files:
lib.get_source_file(file_name)

ui = self._create_ui()
lib = ui.library("lib")
lib.add_source_files(iter(["file*.vhd", "file_foo.vhd"]))
for file_name in files:
lib.get_source_file(file_name)

def test_add_source_files_errors(self):
ui = self._create_ui()
lib = ui.library("lib")
self.create_file("file.vhd")
self.assertRaisesRegexp(ValueError, r"missing\.vhd", lib.add_source_files, ["missing.vhd", "file.vhd"])
self.assertRaisesRegexp(ValueError, r"missing\.vhd", lib.add_source_files, "missing.vhd")

def test_get_source_files(self):
ui = self._create_ui()
lib1 = ui.add_library("lib1")
Expand Down
28 changes: 22 additions & 6 deletions vunit/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,11 @@ def get_source_files(self, pattern="*", library_name=None, allow_empty=False):
return SourceFileList(results)

def add_source_files(self, # pylint: disable=too-many-arguments
pattern, library_name, preprocessors=None, include_dirs=None, allow_empty=False):
files, library_name, preprocessors=None, include_dirs=None, allow_empty=False):
"""
Add source files matching wildcard pattern to library
:param pattern: A wildcard pattern match the files to add
:param files: A wildcard pattern matching the files to add or a list of files
:param library_name: The name of the library to add files into
:param include_dirs: A list of include directories
:param allow_empty: To disable an error if no files matched the pattern
Expand All @@ -470,11 +470,17 @@ def add_source_files(self, # pylint: disable=too-many-arguments
prj.add_source_files("*.vhd", "lib")
"""
file_names = glob(pattern)
if _is_iterable_not_string(files):
files = [files]

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)
file_names = []
for pattern in files:
new_file_names = glob(pattern)

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

return SourceFileList(source_files=[
self.add_source_file(file_name, library_name, preprocessors, include_dirs)
Expand Down Expand Up @@ -1330,3 +1336,13 @@ def lower_generics(generics):
@TODO Maybe warn in case of conflict. VHDL forbids this though so the user will notice anyway.
"""
return dict((name.lower(), value) for name, value in generics.items())


def _is_iterable_not_string(value):
"""
Returns True if value is an iterable that is not a string
"""
if sys.version_info.major == 3:
return isinstance(value, str)
else:
return isinstance(value, (str, unicode))

0 comments on commit f4a09c6

Please sign in to comment.