diff --git a/vunit/test/acceptance/test_ui.py b/vunit/test/acceptance/test_ui.py index 71fd0f361..fa7ea2429 100644 --- a/vunit/test/acceptance/test_ui.py +++ b/vunit/test/acceptance/test_ui.py @@ -16,7 +16,7 @@ from os import remove import re from re import MULTILINE -from vunit.ui import VUnit +from vunit.ui import VUnit, VHDL_EXTENSIONS, VERILOG_EXTENSIONS class TestPreprocessor(object): @@ -86,7 +86,7 @@ def _create_ui(self): ui.add_library('lib') return ui - def _create_temp_files(self, num_files): + def _create_temp_files(self, num_files, file_suffix='.vhd'): """ Create and return num_files temporary files containing the same source code but with different entity names depending on @@ -94,7 +94,7 @@ def _create_temp_files(self, num_files): """ files = [None] * num_files for i in range(num_files): - with NamedTemporaryFile(mode='w', suffix='.vhd', delete=False) as files[i]: + with NamedTemporaryFile(mode='w', suffix=file_suffix, delete=False) as files[i]: files[i].write(self._source.substitute(entity='foo%d' % i)) return files @@ -196,6 +196,27 @@ def test_locally_specified_preprocessors_should_be_used_instead_of_any_globally_ self._delete_temp_files(files) + def test_supported_source_file_suffixes(self): + """Test adding a supported filetype, of any case, is accepted.""" + ui = self._create_ui() + accepted_extensions = VHDL_EXTENSIONS + VERILOG_EXTENSIONS + allowable_extensions = [ext for ext in accepted_extensions] + allowable_extensions.extend([ext.upper() for ext in accepted_extensions]) + allowable_extensions.append(VHDL_EXTENSIONS[0][0] + VHDL_EXTENSIONS[0][1].upper() + + VHDL_EXTENSIONS[0][2:]) # mixed case + for ext in allowable_extensions: + files = self._create_temp_files(1, ext) + ui.add_source_files(files[0].name, 'lib') + self._delete_temp_files(files) + + def test_unsupported_source_file_suffixes(self): + """Test adding an unsupported filetype is rejected""" + ui = self._create_ui() + bogus_ext = '.docx' # obviously not an HDL file + files = self._create_temp_files(1, bogus_ext) + self.assertRaises(RuntimeError, ui.add_source_files, files[0].name, 'lib') + self._delete_temp_files(files) + class TestUiEncoding(unittest.TestCase): """ diff --git a/vunit/ui.py b/vunit/ui.py index a025761f3..3a9a07870 100644 --- a/vunit/ui.py +++ b/vunit/ui.py @@ -43,6 +43,10 @@ import logging LOGGER = logging.getLogger(__name__) +# lower case representation of supported extensions +VHDL_EXTENSIONS = (".vhd", ".vhdl") +VERILOG_EXTENSIONS = (".v", ".vp", ".sv") + class VUnit(object): # pylint: disable=too-many-instance-attributes, too-many-public-methods """ @@ -665,9 +669,9 @@ def file_type_of(file_name): Return the file type of file_name based on the file ending """ _, ext = splitext(file_name) - if ext in (".vhd", ".vhdl"): + if ext.lower() in VHDL_EXTENSIONS: return "vhdl" - elif ext in (".v", ".vp", ".sv"): + elif ext.lower() in VERILOG_EXTENSIONS: return "verilog" else: raise RuntimeError("Unknown file ending '%s' of %s" % (ext, file_name))