diff --git a/tests/context/test_main.py b/tests/context/test_main.py index b65b9e055..95abeafa6 100644 --- a/tests/context/test_main.py +++ b/tests/context/test_main.py @@ -6,6 +6,7 @@ import unittest from tempfile import TemporaryDirectory +from tests import utils from vsg import __main__, severity sFileName = "context_classification_test_input.vhd" @@ -29,9 +30,9 @@ def tearDown(self): def test_classification_file(self): self.maxDiff = None - subprocess.check_output(["bin/vsg", "-f", self._sFileName, "--fix"]).decode("utf-8").split("\n") + subprocess.check_output([*utils.vsg_exec(), "-f", self._sFileName, "--fix"]).decode("utf-8").splitlines() - lActual = pathlib.Path(self._sFileName).read_text().split("\n") - lExpected = pathlib.Path(sFixedFile).read_text().split("\n") + lActual = pathlib.Path(self._sFileName).read_text().splitlines() + lExpected = pathlib.Path(sFixedFile).read_text().splitlines() self.assertEqual(lExpected, lActual) diff --git a/tests/severity/test_main.py b/tests/severity/test_main.py index 0722f0e4d..924e3caac 100644 --- a/tests/severity/test_main.py +++ b/tests/severity/test_main.py @@ -40,75 +40,75 @@ def tearDown(self): def test_entity_without_configuration(self): try: - subprocess.check_output(["bin/vsg", "-f", self._sEntityFileName]) + subprocess.check_output([*utils.vsg_exec(), "-f", self._sEntityFileName]) except subprocess.CalledProcessError as e: - lActual = e.output.decode("utf-8").split("\n") + lActual = e.output.decode("utf-8").splitlines() iExitStatus = e.returncode - lExpected = pathlib.Path(sOutputFileWoConfig).read_text().split("\n") + lExpected = pathlib.Path(sOutputFileWoConfig).read_text().splitlines() self.assertEqual(iExitStatus, 1) self.assertEqual(utils.replace_token(utils.replace_total_count(lActual), self._sEntityFileName, sEntityFileName), lExpected) def test_entity_with_configuration(self): try: - subprocess.check_output(["bin/vsg", "-f", self._sEntityFileName, "-c", sConfigFile]) + subprocess.check_output([*utils.vsg_exec(), "-f", self._sEntityFileName, "-c", sConfigFile]) except subprocess.CalledProcessError as e: - lActual = e.output.decode("utf-8").split("\n") + lActual = e.output.decode("utf-8").splitlines() iExitStatus = e.returncode - lExpected = pathlib.Path(sOutputFileWithConfig).read_text().split("\n") + lExpected = pathlib.Path(sOutputFileWithConfig).read_text().splitlines() self.assertEqual(iExitStatus, 1) self.assertEqual(utils.replace_token(utils.replace_total_count(lActual), self._sEntityFileName, sEntityFileName), lExpected) def test_entity_with_configuration_and_fixed(self): - lActual = subprocess.check_output(["bin/vsg", "-f", self._sEntityFileName, "-c", sConfigFile, "--fix"]).decode("utf-8").split("\n") + lActual = subprocess.check_output([*utils.vsg_exec(), "-f", self._sEntityFileName, "-c", sConfigFile, "--fix"]).decode("utf-8").splitlines() - lExpected = pathlib.Path(sOutputFileWithConfigFixed).read_text().split("\n") + lExpected = pathlib.Path(sOutputFileWithConfigFixed).read_text().splitlines() self.assertEqual(utils.replace_token(utils.replace_total_count(lActual), self._sEntityFileName, sEntityFileName), lExpected) def test_architecture_without_configuration(self): try: - subprocess.check_output(["bin/vsg", "-f", self._sArchitectureFileName]) + subprocess.check_output([*utils.vsg_exec(), "-f", self._sArchitectureFileName]) except subprocess.CalledProcessError as e: - lActual = e.output.decode("utf-8").split("\n") + lActual = e.output.decode("utf-8").splitlines() iExitStatus = e.returncode - lExpected = pathlib.Path(sArchitectureOutputFileWoConfig).read_text().split("\n") + lExpected = pathlib.Path(sArchitectureOutputFileWoConfig).read_text().splitlines() self.assertEqual(iExitStatus, 1) self.assertEqual(utils.replace_token(utils.replace_total_count(lActual), self._sArchitectureFileName, sArchitectureFileName), lExpected) def test_architecture_with_configuration(self): try: - subprocess.check_output(["bin/vsg", "-f", self._sArchitectureFileName, "-c", sConfigFile]) + subprocess.check_output([*utils.vsg_exec(), "-f", self._sArchitectureFileName, "-c", sConfigFile]) except subprocess.CalledProcessError as e: - lActual = e.output.decode("utf-8").split("\n") + lActual = e.output.decode("utf-8").splitlines() iExitStatus = e.returncode - lExpected = pathlib.Path(sArchitectureOutputFileWithConfig).read_text().split("\n") + lExpected = pathlib.Path(sArchitectureOutputFileWithConfig).read_text().splitlines() self.assertEqual(iExitStatus, 1) self.assertEqual(utils.replace_token(utils.replace_total_count(lActual), self._sArchitectureFileName, sArchitectureFileName), lExpected) def test_architecture_with_configuration_and_fixed(self): - lActual = subprocess.check_output(["bin/vsg", "-f", self._sArchitectureFileName, "-c", sConfigFile, "--fix"]).decode("utf-8").split("\n") + lActual = subprocess.check_output([*utils.vsg_exec(), "-f", self._sArchitectureFileName, "-c", sConfigFile, "--fix"]).decode("utf-8").splitlines() - lExpected = pathlib.Path(sArchitectureOutputFileWithConfigFixed).read_text().split("\n") + lExpected = pathlib.Path(sArchitectureOutputFileWithConfigFixed).read_text().splitlines() self.assertEqual(utils.replace_token(utils.replace_total_count(lActual), self._sArchitectureFileName, sArchitectureFileName), lExpected) def test_both_with_configuration(self): try: - subprocess.check_output(["bin/vsg", "-f", self._sEntityFileName, self._sArchitectureFileName, "-c", sConfigFile]) + subprocess.check_output([*utils.vsg_exec(), "-f", self._sEntityFileName, self._sArchitectureFileName, "-c", sConfigFile]) except subprocess.CalledProcessError as e: - lActual = e.output.decode("utf-8").split("\n") + lActual = e.output.decode("utf-8").splitlines() iExitStatus = e.returncode - lExpected1 = pathlib.Path(sOutputFileWithConfig).read_text().rstrip("\n").split("\n") - lExpected2 = pathlib.Path(sArchitectureOutputFileWithConfig).read_text().split("\n") + lExpected1 = pathlib.Path(sOutputFileWithConfig).read_text().splitlines() + lExpected2 = pathlib.Path(sArchitectureOutputFileWithConfig).read_text().splitlines() self.assertEqual(iExitStatus, 1) self.assertEqual( @@ -122,13 +122,13 @@ def test_both_with_configuration(self): def test_both_with_configuration_and_fixed(self): lActual = ( - subprocess.check_output(["bin/vsg", "-f", self._sEntityFileName, self._sArchitectureFileName, "-c", sConfigFile, "--fix"]) + subprocess.check_output([*utils.vsg_exec(), "-f", self._sEntityFileName, self._sArchitectureFileName, "-c", sConfigFile, "--fix"]) .decode("utf-8") - .split("\n") + .splitlines() ) - lExpected1 = pathlib.Path(sOutputFileWithConfigFixed).read_text().rstrip("\n").split("\n") - lExpected2 = pathlib.Path(sArchitectureOutputFileWithConfigFixed).read_text().split("\n") + lExpected1 = pathlib.Path(sOutputFileWithConfigFixed).read_text().splitlines() + lExpected2 = pathlib.Path(sArchitectureOutputFileWithConfigFixed).read_text().splitlines() self.assertEqual( utils.replace_token( @@ -141,12 +141,12 @@ def test_both_with_configuration_and_fixed(self): def test_junit_output(self): try: - subprocess.check_output(["bin/vsg", "-f", self._sEntityFileName, "-c", sConfigFile, "-j", self._sJUnitFileName]) + subprocess.check_output([*utils.vsg_exec(), "-f", self._sEntityFileName, "-c", sConfigFile, "-j", self._sJUnitFileName]) except subprocess.CalledProcessError as e: iExitStatus = e.returncode - lActual = pathlib.Path(self._sJUnitFileName).read_text().split("\n") - lExpected = pathlib.Path(sJUnitFile).read_text().split("\n") + lActual = pathlib.Path(self._sJUnitFileName).read_text().splitlines() + lExpected = pathlib.Path(sJUnitFile).read_text().splitlines() self.assertEqual(iExitStatus, 1) diff --git a/tests/source_file/test_source_file.py b/tests/source_file/test_source_file.py index 49e680d89..866a54f8b 100644 --- a/tests/source_file/test_source_file.py +++ b/tests/source_file/test_source_file.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import os import pathlib +import stat import subprocess import sys import unittest @@ -30,37 +31,38 @@ def tearDown(self): def test_file_not_found(self): try: - subprocess.check_output(["bin/vsg", "-f", "no_file.vhd"], stderr=subprocess.STDOUT) + subprocess.check_output([*utils.vsg_exec(), "-f", "no_file.vhd"], stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: exit_status: int = e.returncode self.assertEqual(exit_status, 1) - @unittest.skipIf("SUDO_UID" in os.environ.keys() or os.geteuid() == 0, "We are root. Root always has permissions so test will fail.") + @unittest.skipIf(utils.is_user_admin(), "We are root. Root always has permissions so test will fail.") + @unittest.skipIf(utils.is_windows(), "Permissions can not be tested on windows.") def test_file_no_permission(self): sNoPermissionTempFile = os.path.join(self._tmpdir.name, sNoPermissionFile) pathlib.Path(sNoPermissionTempFile).touch(mode=0o222, exist_ok=True) try: - subprocess.check_output(["bin/vsg", "-f", sNoPermissionTempFile]) + subprocess.check_output([*utils.vsg_exec(), "-f", sNoPermissionTempFile]) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode - lExpected = pathlib.Path(sOutputNoPermission).read_text().split("\n") + lExpected = pathlib.Path(sOutputNoPermission).read_text().splitlines() self.assertEqual(iExitStatus, 1) self.assertEqual(utils.replace_token(utils.replace_total_count(lActual), sNoPermissionTempFile, sNoPermissionFile), lExpected) def test_file_empty(self): try: - subprocess.check_output(["bin/vsg", "-f", "tests/source_file/" + sEmptyFile]) + subprocess.check_output([*utils.vsg_exec(), "-f", "tests/source_file/" + sEmptyFile]) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode - lExpected = pathlib.Path(sOutputEmptyFile).read_text().split("\n") + lExpected = pathlib.Path(sOutputEmptyFile).read_text().splitlines() self.assertEqual(iExitStatus, 1) self.assertEqual(utils.replace_total_count(lActual), lExpected) diff --git a/tests/tool_integration/quality_report/test_main.py b/tests/tool_integration/quality_report/test_main.py index c69940f40..837f67fb5 100644 --- a/tests/tool_integration/quality_report/test_main.py +++ b/tests/tool_integration/quality_report/test_main.py @@ -10,9 +10,11 @@ from unittest import mock from vsg import __main__ +from tests import utils class testMain(unittest.TestCase): + def setUp(self): self._tmpdir = TemporaryDirectory() @@ -47,4 +49,11 @@ def test_multiple_configuration_w_multiple_filelists(self, mock_stdout): mock_stdout.write.assert_has_calls(lExpected) self.assertTrue(os.path.isfile(actual_file)) - self.assertTrue(filecmp.cmp(actual_file, os.path.join("tests", "tool_integration", "quality_report", "expected.json"))) + + lActual = [] + lActual = utils.read_file(actual_file, lActual) + + lExpected = [] + lExpected = utils.read_file(os.path.join("tests", "tool_integration", "quality_report", "expected.json"), lExpected) + + self.assertEqual(lExpected, lActual) diff --git a/tests/utils.py b/tests/utils.py index e4ace6468..e1c376a14 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,7 +1,10 @@ # -*- coding: utf-8 -*- +import ctypes import os +import platform import pprint import re +import sys import yaml @@ -15,7 +18,7 @@ def debug_lines(oFile, iLineNumber, iNumberOfLines): def read_file(sFilename, lLines, bStrip=True): - with open(sFilename) as oFile: + with open(sFilename, encoding="utf-8") as oFile: for sLine in oFile: if bStrip: lLines.append(sLine.rstrip()) @@ -128,3 +131,25 @@ def replace_total_count_summary(lOutput): def replace_token(lOutput, src, dst): return [line.replace(src, dst) for line in lOutput] + + +def is_user_admin(): + if "SUDO_UID" in os.environ.keys(): + return True + + try: + return os.getuid() == 0 + except AttributeError: + pass + + return ctypes.windll.shell32.IsUserAnAdmin() == 1 + + +def vsg_exec(): + return [sys.executable, "bin/vsg"] + + +def is_windows(): + if platform.system() == "Windows": + return True + return False diff --git a/tests/vsg/read_configuration_files/test_read_configuration_files_function.py b/tests/vsg/read_configuration_files/test_read_configuration_files_function.py index 2e06c2bc7..9dbf37f77 100644 --- a/tests/vsg/read_configuration_files/test_read_configuration_files_function.py +++ b/tests/vsg/read_configuration_files/test_read_configuration_files_function.py @@ -144,5 +144,6 @@ def test_file_list_globbing_with_individual_rule_config(self): for iIndex, item in enumerate(dExpected["file_list"]): if isinstance(item, dict): sKey = list(item.keys())[0] + iActualIndex = get_index_of_dictionary_in_list(dActual["file_list"], sKey) self.assertEqual(dActual["file_list"][iActualIndex], dExpected["file_list"][iIndex]) diff --git a/tests/vsg/test_main.py b/tests/vsg/test_main.py index 4e73184a2..185ae4a09 100644 --- a/tests/vsg/test_main.py +++ b/tests/vsg/test_main.py @@ -290,7 +290,7 @@ def test_globbing_filenames_in_configuration(self): except SystemExit: pass - lActual = temp_stdout.getvalue().strip().split("\n") + lActual = temp_stdout.getvalue().strip().splitlines() if lActual[0] == lExpected[1]: lExpected = [lExpected[1], lExpected[0]] @@ -422,7 +422,7 @@ def test_globbing_filenames_in_yaml_configuration(self): except SystemExit: pass - lActual = temp_stdout.getvalue().strip().split("\n") + lActual = temp_stdout.getvalue().strip().splitlines() if lActual[0] == lExpected[1]: lExpected = [lExpected[1], lExpected[0]] diff --git a/tests/vsg/test_rc.py b/tests/vsg/test_rc.py index 6ba958559..572a9fe89 100644 --- a/tests/vsg/test_rc.py +++ b/tests/vsg/test_rc.py @@ -2,6 +2,8 @@ import subprocess import unittest +from tests import utils + class command_line_args: """This is used as an input into the version command.""" @@ -68,13 +70,12 @@ class testVsg(unittest.TestCase): def test_rc_command_line_argument_w_invalid_rule(self): lExpected = [] lExpected.append("ERROR: rule unknown_rule_001 was not found.") - lExpected.append("") iExitStatus = -1 try: - subprocess.check_output(["bin/vsg", "-rc", "unknown_rule_001"]) + subprocess.check_output([*utils.vsg_exec(), "-rc", "unknown_rule_001"]) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) diff --git a/tests/vsg/test_vsg.py b/tests/vsg/test_vsg.py index db756bb1e..cf845b50d 100644 --- a/tests/vsg/test_vsg.py +++ b/tests/vsg/test_vsg.py @@ -30,12 +30,13 @@ def test_multiple_configuration_w_multiple_filelists(self): lExpected = [] lExpected.append("ERROR: tests/vsg/entity1.vhd(7)port_007 -- Change number of spaces after *in* to 4.") lExpected.append("ERROR: tests/vsg/entity2.vhd(8)port_008 -- Change number of spaces after *out* to 3.") - lExpected.append("") try: - subprocess.check_output(["bin/vsg", "--configuration", "tests/vsg/config_1.json", "tests/vsg/config_2.json", "--output_format", "syntastic"]) + subprocess.check_output( + [*utils.vsg_exec(), "--configuration", "tests/vsg/config_1.json", "tests/vsg/config_2.json", "--output_format", "syntastic"], + ) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -45,12 +46,11 @@ def test_multiple_configuration_w_multiple_filelists(self): def test_single_configuration_w_filelist(self): lExpected = [] lExpected.append("ERROR: tests/vsg/entity1.vhd(7)port_007 -- Change number of spaces after *in* to 4.") - lExpected.append("") try: - subprocess.check_output(["bin/vsg", "--configuration", "tests/vsg/config_1.json", "--output_format", "syntastic"]) + subprocess.check_output([*utils.vsg_exec(), "--configuration", "tests/vsg/config_1.json", "--output_format", "syntastic"]) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -59,12 +59,11 @@ def test_single_configuration_w_filelist(self): lExpected = [] lExpected.append("ERROR: tests/vsg/entity2.vhd(8)port_008 -- Change number of spaces after *out* to 3.") - lExpected.append("") try: - subprocess.check_output(["bin/vsg", "--configuration", "tests/vsg/config_2.json", "--output_format", "syntastic"]) + subprocess.check_output([*utils.vsg_exec(), "--configuration", "tests/vsg/config_2.json", "--output_format", "syntastic"]) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -73,23 +72,21 @@ def test_single_configuration_w_filelist(self): def test_single_configuration_w_rule_disable(self): lExpected = [] - lExpected.append("") lActual = subprocess.check_output( - ["bin/vsg", "--configuration", "tests/vsg/config_3.json", "--output_format", "syntastic", "-f", "tests/vsg/entity1.vhd"], + [*utils.vsg_exec(), "--configuration", "tests/vsg/config_3.json", "--output_format", "syntastic", "-f", "tests/vsg/entity1.vhd"], ) - lActual = str(lActual.decode("utf-8")).split("\n") + lActual = str(lActual.decode("utf-8")).splitlines() self.assertEqual(lActual, lExpected) def test_multiple_configuration_w_rule_disable(self): lExpected = [] lExpected.append("ERROR: tests/vsg/entity1.vhd(7)port_007 -- Change number of spaces after *in* to 4.") - lExpected.append("") try: subprocess.check_output( [ - "bin/vsg", + *utils.vsg_exec(), "--configuration", "tests/vsg/config_3.json", "tests/vsg/config_4.json", @@ -100,7 +97,7 @@ def test_multiple_configuration_w_rule_disable(self): ], ) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -109,12 +106,20 @@ def test_multiple_configuration_w_rule_disable(self): def test_reverse_multiple_configuration_w_rule_disable(self): lExpected = [] - lExpected.append("") lActual = subprocess.check_output( - ["bin/vsg", "--configuration", "tests/vsg/config_4.json", "tests/vsg/config_3.json", "--output_format", "syntastic", "-f", "tests/vsg/entity1.vhd"], + [ + *utils.vsg_exec(), + "--configuration", + "tests/vsg/config_4.json", + "tests/vsg/config_3.json", + "--output_format", + "syntastic", + "-f", + "tests/vsg/entity1.vhd", + ], ) - lActual = str(lActual.decode("utf-8")).split("\n") + lActual = str(lActual.decode("utf-8")).splitlines() self.assertEqual(lActual, lExpected) def test_invalid_configuration(self): @@ -123,12 +128,11 @@ def test_invalid_configuration(self): lExpected.append("while parsing a flow node") lExpected.append("expected the node content, but found ','") lExpected.append(' in "tests/vsg/config_error.json", line 2, column 16') - lExpected.append("") config_error_file = os.path.join(self._tmpdir.name, "config_error.actual.xml") try: lActual = subprocess.check_output( [ - "bin/vsg", + *utils.vsg_exec(), "--configuration", "tests/vsg/config_error.json", "--output_format", @@ -140,7 +144,7 @@ def test_invalid_configuration(self): ], ) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(lActual, lExpected) @@ -159,15 +163,14 @@ def test_invalid_configuration(self): def test_local_rules(self): lExpected = ["ERROR: tests/vsg/entity_architecture.vhd(1)localized_001 -- Split entity and architecture into separate files."] - lExpected.append("") try: subprocess.check_output( - ["bin/vsg", "--style", "jcl", "-f", "tests/vsg/entity_architecture.vhd", "-of", "syntastic", "-lr", "tests/vsg/local_rules"], + [*utils.vsg_exec(), "--style", "jcl", "-f", "tests/vsg/entity_architecture.vhd", "-of", "syntastic", "-lr", "tests/vsg/local_rules"], ) iExitStatus = 0 except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -177,15 +180,14 @@ def test_local_rules(self): def test_invalid_local_rule_directory(self): lExpected = [ "ERROR: encountered FileNotFoundError, No such file or directory tests/vsg/invalid_local_rule_directory when trying to open local rules file.", - "", ] try: lActual = subprocess.check_output( - ["bin/vsg", "-f", "tests/vsg/entity_architecture.vhd", "-of", "syntastic", "-lr", "tests/vsg/invalid_local_rule_directory"], + [*utils.vsg_exec(), "-f", "tests/vsg/entity_architecture.vhd", "-of", "syntastic", "-lr", "tests/vsg/invalid_local_rule_directory"], ) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -195,12 +197,11 @@ def test_globbing_filenames_in_configuration(self): lExpected = [] lExpected.append("ERROR: tests/vsg/entity2.vhd(8)port_008 -- Change number of spaces after *out* to 3.") lExpected.append("ERROR: tests/vsg/entity1.vhd(7)port_007 -- Change number of spaces after *in* to 4.") - lExpected.append("") try: - subprocess.check_output(["bin/vsg", "--configuration", "tests/vsg/config_glob.json", "--output_format", "syntastic"]) + subprocess.check_output([*utils.vsg_exec(), "--configuration", "tests/vsg/config_glob.json", "--output_format", "syntastic"]) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode # print(lActual) @@ -210,19 +211,17 @@ def test_globbing_filenames_in_configuration(self): lExpected = [] lExpected.append("ERROR: tests/vsg/entity1.vhd(7)port_007 -- Change number of spaces after *in* to 4.") lExpected.append("ERROR: tests/vsg/entity2.vhd(8)port_008 -- Change number of spaces after *out* to 3.") - lExpected.append("") self.assertEqual(lActual, lExpected) def test_single_yaml_configuration_w_filelist(self): lExpected = [] lExpected.append("ERROR: tests/vsg/entity1.vhd(7)port_007 -- Change number of spaces after *in* to 4.") - lExpected.append("") try: - subprocess.check_output(["bin/vsg", "--configuration", "tests/vsg/config_1.yaml", "--output_format", "syntastic"]) + subprocess.check_output([*utils.vsg_exec(), "--configuration", "tests/vsg/config_1.yaml", "--output_format", "syntastic"]) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -231,12 +230,11 @@ def test_single_yaml_configuration_w_filelist(self): lExpected = [] lExpected.append("ERROR: tests/vsg/entity2.vhd(8)port_008 -- Change number of spaces after *out* to 3.") - lExpected.append("") try: - subprocess.check_output(["bin/vsg", "--configuration", "tests/vsg/config_2.json", "--output_format", "syntastic"]) + subprocess.check_output([*utils.vsg_exec(), "--configuration", "tests/vsg/config_2.json", "--output_format", "syntastic"]) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -247,12 +245,13 @@ def test_multiple_yaml_configuration_w_multiple_filelists(self): lExpected = [] lExpected.append("ERROR: tests/vsg/entity1.vhd(7)port_007 -- Change number of spaces after *in* to 4.") lExpected.append("ERROR: tests/vsg/entity2.vhd(8)port_008 -- Change number of spaces after *out* to 3.") - lExpected.append("") try: - subprocess.check_output(["bin/vsg", "--configuration", "tests/vsg/config_1.yaml", "tests/vsg/config_2.yaml", "--output_format", "syntastic"]) + subprocess.check_output( + [*utils.vsg_exec(), "--configuration", "tests/vsg/config_1.yaml", "tests/vsg/config_2.yaml", "--output_format", "syntastic"], + ) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -261,23 +260,21 @@ def test_multiple_yaml_configuration_w_multiple_filelists(self): def test_single_yaml_configuration_w_rule_disable(self): lExpected = [] - lExpected.append("") lActual = subprocess.check_output( - ["bin/vsg", "--configuration", "tests/vsg/config_3.yaml", "--output_format", "syntastic", "-f", "tests/vsg/entity1.vhd"], + [*utils.vsg_exec(), "--configuration", "tests/vsg/config_3.yaml", "--output_format", "syntastic", "-f", "tests/vsg/entity1.vhd"], ) - lActual = str(lActual.decode("utf-8")).split("\n") + lActual = str(lActual.decode("utf-8")).splitlines() self.assertEqual(lActual, lExpected) def test_multiple_yaml_configuration_w_rule_disable(self): lExpected = [] lExpected.append("ERROR: tests/vsg/entity1.vhd(7)port_007 -- Change number of spaces after *in* to 4.") - lExpected.append("") try: subprocess.check_output( [ - "bin/vsg", + *utils.vsg_exec(), "--configuration", "tests/vsg/config_3.yaml", "tests/vsg/config_4.yaml", @@ -288,7 +285,7 @@ def test_multiple_yaml_configuration_w_rule_disable(self): ], ) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -297,24 +294,31 @@ def test_multiple_yaml_configuration_w_rule_disable(self): def test_reverse_yaml_multiple_configuration_w_rule_disable(self): lExpected = [] - lExpected.append("") lActual = subprocess.check_output( - ["bin/vsg", "--configuration", "tests/vsg/config_4.yaml", "tests/vsg/config_3.yaml", "--output_format", "syntastic", "-f", "tests/vsg/entity1.vhd"], + [ + *utils.vsg_exec(), + "--configuration", + "tests/vsg/config_4.yaml", + "tests/vsg/config_3.yaml", + "--output_format", + "syntastic", + "-f", + "tests/vsg/entity1.vhd", + ], ) - lActual = str(lActual.decode("utf-8")).split("\n") + lActual = str(lActual.decode("utf-8")).splitlines() self.assertEqual(lActual, lExpected) def test_globbing_filenames_in_yaml_configuration(self): lExpected = [] lExpected.append("ERROR: tests/vsg/entity2.vhd(8)port_008 -- Change number of spaces after *out* to 3.") lExpected.append("ERROR: tests/vsg/entity1.vhd(7)port_007 -- Change number of spaces after *in* to 4.") - lExpected.append("") try: - subprocess.check_output(["bin/vsg", "--configuration", "tests/vsg/config_glob.yaml", "--output_format", "syntastic"]) + subprocess.check_output([*utils.vsg_exec(), "--configuration", "tests/vsg/config_glob.yaml", "--output_format", "syntastic"]) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -323,27 +327,26 @@ def test_globbing_filenames_in_yaml_configuration(self): lExpected = [] lExpected.append("ERROR: tests/vsg/entity1.vhd(7)port_007 -- Change number of spaces after *in* to 4.") lExpected.append("ERROR: tests/vsg/entity2.vhd(8)port_008 -- Change number of spaces after *out* to 3.") - lExpected.append("") self.assertEqual(lActual, lExpected) def test_oc_command_line_argument(self): lExpected = [] - lExpected.append("") - lActual = subprocess.check_output(["bin/vsg", "-oc", os.path.join(self._tmpdir.name, "deleteme.json")]) - lActual = str(lActual.decode("utf-8")).split("\n") + lActual = subprocess.check_output([*utils.vsg_exec(), "-oc", os.path.join(self._tmpdir.name, "deleteme.json")]) + lActual = str(lActual.decode("utf-8")).splitlines() self.assertEqual(lActual, lExpected) def test_missing_configuration_file(self): try: - subprocess.check_output(["bin/vsg", "-c", "missing_configuration.yaml"], stderr=subprocess.STDOUT) + subprocess.check_output([*utils.vsg_exec(), "-c", "missing_configuration.yaml"], stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) - @unittest.skipIf("SUDO_UID" in os.environ.keys() or os.geteuid() == 0, "We are root. Root always has permissions so test will fail.") + @unittest.skipIf(utils.is_user_admin(), "We are root. Root always has permissions so test will fail.") + @unittest.skipIf(utils.is_windows(), "Permission based tests can not be run on Windows.") def test_no_permission_configuration_file(self): sNoPermissionFile = os.path.join(self._tmpdir.name, "no_permission.yml") pathlib.Path(sNoPermissionFile).touch(mode=0o222, exist_ok=True) @@ -351,7 +354,7 @@ def test_no_permission_configuration_file(self): sExpected = f"ERROR: encountered PermissionError, Permission denied while opening configuration file: {sNoPermissionFile}\n" try: - subprocess.check_output(["bin/vsg", "-c", sNoPermissionFile]) + subprocess.check_output([*utils.vsg_exec(), "-c", sNoPermissionFile]) except subprocess.CalledProcessError as e: sActual = str(e.output.decode("utf-8")) iExitStatus = e.returncode @@ -363,12 +366,11 @@ def test_no_permission_configuration_file(self): def test_missing_files_in_configuration_file(self): lExpected = [] lExpected.append("ERROR: Could not find file missing_file.vhd in configuration file tests/vsg/missing_file_config.yaml") - lExpected.append("") try: - subprocess.check_output(["bin/vsg", "-c", "tests/vsg/missing_file_config.yaml", "--output_format", "syntastic"]) + subprocess.check_output([*utils.vsg_exec(), "-c", "tests/vsg/missing_file_config.yaml", "--output_format", "syntastic"]) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -376,15 +378,15 @@ def test_missing_files_in_configuration_file(self): self.assertEqual(lActual, lExpected) def test_summary_output_format_error(self): - lExpectedStdErr = ["File: tests/vsg/entity_architecture.vhd ERROR (200 rules checked) [Error: 11] [Warning: 0]", ""] - lExpectedStdOut = [""] + lExpectedStdErr = ["File: tests/vsg/entity_architecture.vhd ERROR (200 rules checked) [Error: 11] [Warning: 0]"] + lExpectedStdOut = [] try: - subprocess.check_output(["bin/vsg", "-f", "tests/vsg/entity_architecture.vhd", "-of", "summary"], stderr=subprocess.PIPE) + subprocess.check_output([*utils.vsg_exec(), "-f", "tests/vsg/entity_architecture.vhd", "-of", "summary"], stderr=subprocess.PIPE) iExitStatus = 0 except subprocess.CalledProcessError as e: - lActualStdOut = str(e.output.decode("utf-8")).split("\n") - lActualStdErr = str(e.stderr.decode("utf-8")).split("\n") + lActualStdOut = str(e.output.decode("utf-8")).splitlines() + lActualStdErr = str(e.stderr.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -393,18 +395,18 @@ def test_summary_output_format_error(self): self.assertEqual(utils.replace_total_count_summary(lActualStdOut), lExpectedStdOut) def test_summary_output_format_error_with_local_rules(self): - lExpectedStdErr = ["File: tests/vsg/entity_architecture.vhd ERROR (200 rules checked) [Error: 1] [Warning: 0]", ""] - lExpectedStdOut = [""] + lExpectedStdErr = ["File: tests/vsg/entity_architecture.vhd ERROR (200 rules checked) [Error: 1] [Warning: 0]"] + lExpectedStdOut = [] try: subprocess.check_output( - ["bin/vsg", "-f", "tests/vsg/entity_architecture.vhd", "-of", "summary", "-lr", "tests/vsg/local_rules"], + [*utils.vsg_exec(), "-f", "tests/vsg/entity_architecture.vhd", "-of", "summary", "-lr", "tests/vsg/local_rules"], stderr=subprocess.PIPE, ) iExitStatus = 0 except subprocess.CalledProcessError as e: - lActualStdOut = str(e.output.decode("utf-8")).split("\n") - lActualStdErr = str(e.stderr.decode("utf-8")).split("\n") + lActualStdOut = str(e.output.decode("utf-8")).splitlines() + lActualStdErr = str(e.stderr.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -413,12 +415,12 @@ def test_summary_output_format_error_with_local_rules(self): self.assertEqual(utils.replace_total_count_summary(lActualStdOut), lExpectedStdOut) def test_summary_output_format_ok(self): - lExpected = ["File: tests/vsg/entity_architecture.fixed.vhd OK (200 rules checked) [Error: 0] [Warning: 0]", ""] + lExpected = ["File: tests/vsg/entity_architecture.fixed.vhd OK (200 rules checked) [Error: 0] [Warning: 0]"] lActual = ( - subprocess.check_output(["bin/vsg", "-f", "tests/vsg/entity_architecture.fixed.vhd", "-of", "summary"], stderr=subprocess.STDOUT) + subprocess.check_output([*utils.vsg_exec(), "-f", "tests/vsg/entity_architecture.fixed.vhd", "-of", "summary"], stderr=subprocess.STDOUT) .decode("utf-8") - .split("\n") + .splitlines() ) self.assertEqual(utils.replace_total_count_summary(lActual), lExpected) @@ -428,14 +430,13 @@ def test_summary_output_format_multiple_mixed(self): "File: tests/vsg/entity_architecture.vhd ERROR (200 rules checked) [Error: 11] [Warning: 0]", "File: tests/vsg/entity1.vhd ERROR (200 rules checked) [Error: 1] [Warning: 0]", "File: tests/vsg/entity2.vhd ERROR (200 rules checked) [Error: 1] [Warning: 0]", - "", ] - lExpectedStdOut = ["File: tests/vsg/entity_architecture.fixed.vhd OK (200 rules checked) [Error: 0] [Warning: 0]", ""] + lExpectedStdOut = ["File: tests/vsg/entity_architecture.fixed.vhd OK (200 rules checked) [Error: 0] [Warning: 0]"] try: subprocess.check_output( [ - "bin/vsg", + *utils.vsg_exec(), "-f", "tests/vsg/entity_architecture.vhd", "tests/vsg/entity_architecture.fixed.vhd", @@ -448,8 +449,8 @@ def test_summary_output_format_multiple_mixed(self): ) iExitStatus = 0 except subprocess.CalledProcessError as e: - lActualStdOut = str(e.output.decode("utf-8")).split("\n") - lActualStdErr = str(e.stderr.decode("utf-8")).split("\n") + lActualStdOut = str(e.output.decode("utf-8")).splitlines() + lActualStdErr = str(e.stderr.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -462,14 +463,13 @@ def test_summary_output_format_multiple_mixed_jobs_1(self): "File: tests/vsg/entity_architecture.vhd ERROR (200 rules checked) [Error: 11] [Warning: 0]", "File: tests/vsg/entity1.vhd ERROR (200 rules checked) [Error: 1] [Warning: 0]", "File: tests/vsg/entity2.vhd ERROR (200 rules checked) [Error: 1] [Warning: 0]", - "", ] - lExpectedStdOut = ["File: tests/vsg/entity_architecture.fixed.vhd OK (200 rules checked) [Error: 0] [Warning: 0]", ""] + lExpectedStdOut = ["File: tests/vsg/entity_architecture.fixed.vhd OK (200 rules checked) [Error: 0] [Warning: 0]"] try: subprocess.check_output( [ - "bin/vsg", + *utils.vsg_exec(), "-f", "tests/vsg/entity_architecture.vhd", "tests/vsg/entity_architecture.fixed.vhd", @@ -483,8 +483,8 @@ def test_summary_output_format_multiple_mixed_jobs_1(self): ) iExitStatus = 0 except subprocess.CalledProcessError as e: - lActualStdOut = str(e.output.decode("utf-8")).split("\n") - lActualStdErr = str(e.stderr.decode("utf-8")).split("\n") + lActualStdOut = str(e.output.decode("utf-8")).splitlines() + lActualStdErr = str(e.stderr.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -497,14 +497,13 @@ def test_summary_output_format_multiple_mixed_jobs_2(self): "File: tests/vsg/entity_architecture.vhd ERROR (200 rules checked) [Error: 11] [Warning: 0]", "File: tests/vsg/entity1.vhd ERROR (200 rules checked) [Error: 1] [Warning: 0]", "File: tests/vsg/entity2.vhd ERROR (200 rules checked) [Error: 1] [Warning: 0]", - "", ] - lExpectedStdOut = ["File: tests/vsg/entity_architecture.fixed.vhd OK (200 rules checked) [Error: 0] [Warning: 0]", ""] + lExpectedStdOut = ["File: tests/vsg/entity_architecture.fixed.vhd OK (200 rules checked) [Error: 0] [Warning: 0]"] try: subprocess.check_output( [ - "bin/vsg", + *utils.vsg_exec(), "-f", "tests/vsg/entity_architecture.vhd", "tests/vsg/entity_architecture.fixed.vhd", @@ -518,8 +517,8 @@ def test_summary_output_format_multiple_mixed_jobs_2(self): ) iExitStatus = 0 except subprocess.CalledProcessError as e: - lActualStdOut = str(e.output.decode("utf-8")).split("\n") - lActualStdErr = str(e.stderr.decode("utf-8")).split("\n") + lActualStdOut = str(e.output.decode("utf-8")).splitlines() + lActualStdErr = str(e.stderr.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(iExitStatus, 1) @@ -530,12 +529,11 @@ def test_summary_output_format_multiple_mixed_jobs_2(self): def test_globbing_filenames_in_configuration_with_file_rules(self): lExpected = [] lExpected.append("ERROR: tests/vsg/entity2.vhd(8)port_008 -- Change number of spaces after *out* to 3.") - lExpected.append("") try: - subprocess.check_output(["bin/vsg", "--configuration", "tests/vsg/config_glob_with_file_rules.yaml", "--output_format", "syntastic"]) + subprocess.check_output([*utils.vsg_exec(), "--configuration", "tests/vsg/config_glob_with_file_rules.yaml", "--output_format", "syntastic"]) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(lActual, lExpected) @@ -543,14 +541,13 @@ def test_globbing_filenames_in_configuration_with_file_rules(self): def test_configuration_with_file_rules_and_no_file_list_entity2(self): lExpected = [] lExpected.append("ERROR: tests/vsg/entity2.vhd(8)port_008 -- Change number of spaces after *out* to 3.") - lExpected.append("") try: subprocess.check_output( - ["bin/vsg", "--configuration", "tests/vsg/config_file_rules.yaml", "-f", "tests/vsg/entity2.vhd", "--output_format", "syntastic"], + [*utils.vsg_exec(), "--configuration", "tests/vsg/config_file_rules.yaml", "-f", "tests/vsg/entity2.vhd", "--output_format", "syntastic"], ) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(lActual, lExpected) @@ -562,10 +559,10 @@ def test_configuration_with_file_rules_and_no_file_list_entity1(self): try: subprocess.check_output( - ["bin/vsg", "--configuration", "tests/vsg/config_file_rules.yaml", "-f", "tests/vsg/entity1.vhd", "--output_format", "syntastic"], + [*utils.vsg_exec(), "--configuration", "tests/vsg/config_file_rules.yaml", "-f", "tests/vsg/entity1.vhd", "--output_format", "syntastic"], ) except subprocess.CalledProcessError as e: - lActual = str(e.output.decode("utf-8")).split("\n") + lActual = str(e.output.decode("utf-8")).splitlines() iExitStatus = e.returncode self.assertEqual(lActual, lExpected) @@ -573,8 +570,10 @@ def test_configuration_with_file_rules_and_no_file_list_entity1(self): def test_file_as_stdin(self): with open("tests/vsg/entity1.vhd") as file1: lExpected = [] - lExpected.append("") - lActual = subprocess.check_output(["bin/vsg", "--configuration", "tests/vsg/config_3.yaml", "--output_format", "syntastic", "--stdin"], stdin=file1) - lActual = str(lActual.decode("utf-8")).split("\n") + lActual = subprocess.check_output( + [*utils.vsg_exec(), "--configuration", "tests/vsg/config_3.yaml", "--output_format", "syntastic", "--stdin"], + stdin=file1, + ) + lActual = str(lActual.decode("utf-8")).splitlines() self.assertEqual(lActual, lExpected) diff --git a/vsg/apply_rules.py b/vsg/apply_rules.py index 25fac1ddd..b6c0c4193 100644 --- a/vsg/apply_rules.py +++ b/vsg/apply_rules.py @@ -94,7 +94,7 @@ def apply_rules(commandLineArguments, oConfig, tIndexFileName): try: oRules = rule_list.rule_list(oVhdlFile, oConfig.severity_list, commandLineArguments.local_rules) except OSError as e: - sOutputStd = f"ERROR: encountered {e.__class__.__name__}, {e.args[1]} " + commandLineArguments.local_rules + " when trying to open local rules file." + sOutputStd = f"ERROR: encountered {e.__class__.__name__}, No such file or directory " + commandLineArguments.local_rules + " when trying to open local rules file." sOutputErr = None return 1, None, dJsonEntry, sOutputStd, sOutputErr, bStopProcessingFiles diff --git a/vsg/config.py b/vsg/config.py index 121fecaad..6ea94b130 100644 --- a/vsg/config.py +++ b/vsg/config.py @@ -142,11 +142,11 @@ def process_file_list_key(dConfig, tempConfiguration, sKey, sConfigFilename): for iIndex, sFilename in enumerate(tempConfiguration["file_list"]): validate_file_exists(sFilename, sConfigFilename) try: - for sGlobbedFilename in glob.glob(utils.expand_filename(sFilename), recursive=True): + for sGlobbedFilename in glob_filenames(sFilename): dReturn["file_list"].append(sGlobbedFilename) except TypeError: sKey = list(sFilename.keys())[0] - for sGlobbedFilename in glob.glob(utils.expand_filename(sKey), recursive=True): + for sGlobbedFilename in glob_filenames(sKey): dTemp = {} dTemp[sGlobbedFilename] = {} dTemp[sGlobbedFilename].update(tempConfiguration["file_list"][iIndex][sKey]) @@ -154,6 +154,16 @@ def process_file_list_key(dConfig, tempConfiguration, sKey, sConfigFilename): return dReturn +def glob_filenames(sFilename): + files = glob.glob(utils.expand_filename(sFilename), recursive=True) + return replace_backslash_with_forward_slash(files) + return(temp) + + +def replace_backslash_with_forward_slash(lStrings): + return [f.replace("\\", "/") for f in lStrings] + + def write_invalid_configuration_junit_file(sFileName, sJUnitFileName): if sJUnitFileName: oJunitFile = junit.xmlfile(sJUnitFileName)