From 25b21cc9953d8853a56cdf5894910377e0cf3aea Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Fri, 8 Oct 2021 07:06:25 -0500 Subject: [PATCH 1/3] Updates for issue #619 Adding validation for --backup command line argument. 1) Updated documentation to say --backup is only valid with --fix 2) Added tests a) Testing --backup works with --fix b) Testing --backup without --fix returns an error 3) Updated checking of --backup after parsing command line arguments --- docs/usage.rst | 1 + vsg/__main__.py | 6 ---- vsg/apply_rules.py | 8 ++++++ vsg/cmd_line_args.py | 9 ++++++ vsg/tests/vsg/test_main.py | 59 +++++++++++++++++++++++++++++++++++++- 5 files changed, 76 insertions(+), 7 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index 0d50f263..3b17317f 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -82,6 +82,7 @@ The command line tool can be invoked with: | --backup | Creates a copy of the input file before | | | applying any fixes. This can be used to | | | compare the fixed file against the original. | +| | NOTE: This is only valid when using --fix. | +-------------------------------+-------------------------------------------------+ | --output_configuration | Writes a JSON configuration file of the current | | | run. It includes a file_list, local_rules (if | diff --git a/vsg/__main__.py b/vsg/__main__.py index 493a6e0d..577fc27f 100644 --- a/vsg/__main__.py +++ b/vsg/__main__.py @@ -3,7 +3,6 @@ import sys import os import json -import shutil import glob import yaml import functools @@ -21,11 +20,6 @@ from . import version from . import vhdlFile -def create_backup_file(sFileName): - '''Copies existing file and adds .bak to the end.''' - shutil.copy2(sFileName, sFileName + '.bak') - - def generate_output_configuration(commandLineArguments, oConfig): ''' Creates a configuration based on parameters passed on the command line. diff --git a/vsg/apply_rules.py b/vsg/apply_rules.py index 5ce5cc5c..70af54e8 100644 --- a/vsg/apply_rules.py +++ b/vsg/apply_rules.py @@ -1,9 +1,17 @@ + +import shutil + from . import config from . import rule_list from . import utils from . import vhdlFile +def create_backup_file(sFileName): + '''Copies existing file and adds .bak to the end.''' + shutil.copy2(sFileName, sFileName + '.bak') + + def configure_rules(oConfig, oRules, configuration, iIndex, sFileName): oRules.configure(oConfig) diff --git a/vsg/cmd_line_args.py b/vsg/cmd_line_args.py index 044083d2..546a3d46 100644 --- a/vsg/cmd_line_args.py +++ b/vsg/cmd_line_args.py @@ -58,6 +58,8 @@ def parse_command_line_arguments(): args_ = parser.parse_args() + validate_backup_argument(args_) + if sys.platform == "win32": # Work around https://bugs.python.org/issue26903 args_.jobs = min(args_.jobs, 60) @@ -87,4 +89,11 @@ def get_predefined_styles(): lReturn.append(tempConfiguration['name']) return lReturn +def validate_backup_argument(args_): + ''' + The function validates the backup option is only present when the fix option is also present. + ''' + if args_.backup and not args_.fix: + print('ERROR: --backup argument requires --fix argument') + sys.exit(1) diff --git a/vsg/tests/vsg/test_main.py b/vsg/tests/vsg/test_main.py index f92dd857..469b9c70 100644 --- a/vsg/tests/vsg/test_main.py +++ b/vsg/tests/vsg/test_main.py @@ -1,8 +1,10 @@ +import filecmp import pathlib import unittest from unittest import mock -import subprocess import os +import subprocess +import shutil import sys import contextlib @@ -368,3 +370,58 @@ class testVsg(unittest.TestCase): pass mock_stdout.write.assert_has_calls(lExpected) + + @mock.patch('sys.stdout') + def test_backup_file(self, mock_stdout): + + if os.path.isfile('vsg/tests/vsg/deleteme.vhd'): + os.remove('vsg/tests/vsg/deleteme.vhd') + + if os.path.isfile('vsg/tests/vsg/deleteme.vhd.bak'): + os.remove('vsg/tests/vsg/deleteme.vhd.bak') + + shutil.copyfile('vsg/tests/vsg/entity1.vhd', 'vsg/tests/vsg/deleteme.vhd') + + lExpected = [] + + sys.argv = ['vsg'] + sys.argv.extend(['--output_format', 'syntastic']) + sys.argv.extend(['-f', 'vsg/tests/vsg/deleteme.vhd']) + sys.argv.extend(['--fix']) + sys.argv.extend(['--backup']) + + try: + __main__.main() + except SystemExit: + pass + + self.assertTrue(os.path.isfile('vsg/tests/vsg/deleteme.vhd.bak')) + + self.assertTrue(filecmp.cmp('vsg/tests/vsg/entity1.vhd', 'vsg/tests/vsg/deleteme.vhd.bak')) + + mock_stdout.write.assert_has_calls(lExpected) + + if os.path.isfile('vsg/tests/vsg/deleteme.vhd'): + os.remove('vsg/tests/vsg/deleteme.vhd') + + if os.path.isfile('vsg/tests/vsg/deleteme.vhd.bak'): + os.remove('vsg/tests/vsg/deleteme.vhd.bak') + + @mock.patch('sys.stdout') + def test_backup_file_without_fix(self, mock_stdout): + + lExpected = [] + lExpected.append(mock.call('ERROR: --backup argument requires --fix argument')) + lExpected.append(mock.call('\n')) + + sys.argv = ['vsg'] + sys.argv.extend(['--output_format', 'syntastic']) + sys.argv.extend(['-f', 'vsg/tests/vsg/entity1.vhd']) + sys.argv.extend(['--backup']) + + with self.assertRaises(SystemExit) as cm: + __main__.main() + + mock_stdout.write.assert_has_calls(lExpected) + self.assertEqual(cm.exception.code, 1) + self.assertFalse(os.path.isfile('vsg/tests/vsg/entity1.vhd.bak')) From 7af8652d8df9aaba36e14f96cbca698cf98e5f6f Mon Sep 17 00:00:00 2001 From: Hector Fernandez Date: Sun, 10 Oct 2021 13:25:24 +0200 Subject: [PATCH 2/3] Ignored Pycharm and VSCode internal files. --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index b2283299..17055570 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,7 @@ _build # This version file should never be committed vsg/version_info.py + +# IDE files +.idea/ +.vscode/ From 10548a1e81954a1496d2cbadb7ec3140e006c442 Mon Sep 17 00:00:00 2001 From: Hector Fernandez Date: Mon, 11 Oct 2021 15:12:50 +0200 Subject: [PATCH 3/3] Several fixes in port and port_map rules documentation. --- docs/port_map_rules.rst | 12 ++++++------ docs/port_rules.rst | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/port_map_rules.rst b/docs/port_map_rules.rst index 8f819ad8..d5fdfdde 100644 --- a/docs/port_map_rules.rst +++ b/docs/port_map_rules.rst @@ -49,10 +49,10 @@ Refer to the section `Configuring Uppercase and Lowercase Rules wr_en, - RD_EN => rd_en, - OVERFLOW => overflow, - UNDERFLOW(c_index) => underflow + wr_en => wr_en, + rd_en => rd_en, + overflow => overflow, + underflow(c_index) => underflow ); port_map_003 @@ -60,7 +60,7 @@ port_map_003 |phase_1| |error| -This rule checks the ( is on the same line as the **port map** keywords. +This rule checks the "(" character is on the same line as the **port map** keywords. **Violation** @@ -90,7 +90,7 @@ port_map_004 |phase_1| |error| -This rule checks the closing ) for the port map is on it's own line. +This rule checks the closing ")" character for the port map is on it's own line. **Violation** diff --git a/docs/port_rules.rst b/docs/port_rules.rst index de2804d8..868974c5 100644 --- a/docs/port_rules.rst +++ b/docs/port_rules.rst @@ -219,7 +219,7 @@ port_010 |phase_6| |error| -This rule checks port names are uppercase. +This rule checks the port names have proper case. Refer to the section `Configuring Uppercase and Lowercase Rules `_ for information on changing the default case. @@ -239,10 +239,10 @@ Refer to the section `Configuring Uppercase and Lowercase Rules