Updates for issue #339:

The configuration of a single rule can be requested:

  1)  Added tests
  2)  Updated documentation
  3)  Updated code
This commit is contained in:
Jeremiah C Leary 2020-02-22 17:53:31 -06:00
parent 8e1fa8b172
commit 4c962c9bc6
6 changed files with 176 additions and 2 deletions

View File

@ -92,6 +92,39 @@ Here are a list of attributes that can be altered for each rule:
.. NOTE:: Some rules have additional attributes. These will be noted in the rule description.
Reporting Single Rule Configuration
-----------------------------------
The configuration for a single rule can be reported using the **-rc** option:
.. code-block:: bash
$ vsg -rc entity_001
{
"rule": {
"entity_001": {
"indentSize": 2,
"phase": 4,
"disable": false,
"fixable": true
}
}
}
VSG will print the configuration for the rule given in a JSON format.
This configuration can be altered and added to a configuration file.
Reporting Configuration for All Rules
-------------------------------------
Every rule configuration can be report and saved to a file using the **-oc** option:
.. code-block:: bash
$ vsg -oc configuration.json
The output file will be in JSON format and can be modified and passed back to VSG using the *-c* option.
Example: Disabling a rule
##########################

View File

@ -77,6 +77,8 @@ The command line tool can be invoked with:
| | used), and how every rule was configured. |
| | This configuration can be fed back into VSG. |
+-------------------------------+-------------------------------------------------+
| --rule_configuration | Displays the configuration of a rule. |
+-------------------------------+-------------------------------------------------+
| --version | Displays the version of VSG. |
+-------------------------------+-------------------------------------------------+

View File

@ -30,8 +30,9 @@ def parse_command_line_arguments():
parser.add_argument('-fp', '--fix_phase', default=10, action='store', help='Fix issues up to and including this phase')
parser.add_argument('-j', '--junit', action='store', help='Extract Junit file')
parser.add_argument('-of', '--output_format', action='store', default='vsg', choices=['vsg', 'syntastic'], help='Sets the output format.')
parser.add_argument('-b', '--backup', default=False, action='store_true', help='Creates copy of input file for comparison with fixed version.')
parser.add_argument('-oc', '--output_configuration', default=None, action='store', help='Output configuration file name')
parser.add_argument('-b', '--backup', default=False, action='store_true', help='Creates a copy of input file for comparison with fixed version.')
parser.add_argument('-oc', '--output_configuration', default=None, action='store', help='Write configuration to file name.')
parser.add_argument('-rc', '--rule_configuration', default=None, action='store', help='Display configuration of a rule')
parser.add_argument('-v', '--version', default=False, action='store_true', help='Displays version information')
if len(sys.argv) == 1:
@ -184,6 +185,39 @@ def generate_output_configuration(commandLineArguments, configuration):
sys.exit(fExitStatus)
def display_rule_configuration(commandLineArguments, configuration):
'''
Displays the configuration of a rule passed on the command line.
Parameters:
commandLineArguments: (argparse object)
configuration: (configuration dictionary)
Returns: Nothing
'''
if commandLineArguments.rule_configuration:
fExitStatus = 0
# Create empty file so it can be used to create the rule list
oVhdlFile = vhdlFile.vhdlFile([''])
oRules = rule_list.rule_list(oVhdlFile, commandLineArguments.local_rules)
oRules.configure(configuration)
dOutputConfiguration = {}
if commandLineArguments.local_rules:
dOutputConfiguration['local_rules'] = commandLineArguments.local_rules
dFullConfiguration = oRules.get_configuration()
if commandLineArguments.rule_configuration in dFullConfiguration:
dOutputConfiguration['rule'] = {}
dOutputConfiguration['rule'][commandLineArguments.rule_configuration] = dFullConfiguration[commandLineArguments.rule_configuration]
# Format the data for displaying
print(json.dumps(dOutputConfiguration, indent=2))
else:
print('ERROR: rule ' + commandLineArguments.rule_configuration + ' was not found.')
fExitStatus = 1
sys.exit(fExitStatus)
def main():
'''Main routine of the VHDL Style Guide (VSG) program.'''
@ -207,6 +241,8 @@ def main():
generate_output_configuration(commandLineArguments, configuration)
display_rule_configuration(commandLineArguments, configuration)
for sFileName in commandLineArguments.filename:
oVhdlFile = vhdlFile.vhdlFile(read_vhdlfile(sFileName))
oVhdlFile.filename = sFileName

View File

@ -0,0 +1,8 @@
{
"rule": {
"after_002": {
"fixable": false,
"indentSize": 4
}
}
}

View File

@ -0,0 +1,7 @@
{
"rule": {
"global": {
"indentSize": 3
}
}
}

88
vsg/tests/vsg/test_rc.py Normal file
View File

@ -0,0 +1,88 @@
import unittest
from unittest import mock
import subprocess
import os
from tempfile import TemporaryFile
from vsg.tests import utils
from vsg import version
class command_line_args():
''' This is used as an input into the version command.'''
def __init__(self, version=False):
self.version = version
class testVsg(unittest.TestCase):
def test_rc_command_line_argument(self):
lExpected = []
lExpected.append('{')
lExpected.append(' "rule": {')
lExpected.append(' "after_002": {')
lExpected.append(' "phase": 5, ')
lExpected.append(' "fixable": true, ')
lExpected.append(' "disable": true, ')
lExpected.append(' "indentSize": 2')
lExpected.append(' }')
lExpected.append(' }')
lExpected.append('}')
lExpected.append('')
lActual = subprocess.check_output(['bin/vsg','-rc','after_002'])
lActual = str(lActual.decode('utf-8')).split('\n')
self.assertEqual(lActual, lExpected)
def test_rc_command_line_argument_w_configuration(self):
lExpected = []
lExpected.append('{')
lExpected.append(' "rule": {')
lExpected.append(' "after_002": {')
lExpected.append(' "phase": 5, ')
lExpected.append(' "fixable": false, ')
lExpected.append(' "disable": true, ')
lExpected.append(' "indentSize": 4')
lExpected.append(' }')
lExpected.append(' }')
lExpected.append('}')
lExpected.append('')
lActual = subprocess.check_output(['bin/vsg','-rc','after_002', '-c', 'vsg/tests/vsg/rc_config.json'])
lActual = str(lActual.decode('utf-8')).split('\n')
self.assertEqual(lActual, lExpected)
def test_rc_command_line_argument_w_global(self):
lExpected = []
lExpected.append('{')
lExpected.append(' "rule": {')
lExpected.append(' "after_002": {')
lExpected.append(' "phase": 5, ')
lExpected.append(' "fixable": true, ')
lExpected.append(' "disable": true, ')
lExpected.append(' "indentSize": 3')
lExpected.append(' }')
lExpected.append(' }')
lExpected.append('}')
lExpected.append('')
lActual = subprocess.check_output(['bin/vsg','-rc','after_002', '-c', 'vsg/tests/vsg/rc_global_config.json'])
lActual = str(lActual.decode('utf-8')).split('\n')
self.assertEqual(lActual, lExpected)
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'])
except subprocess.CalledProcessError as e:
lActual = str(e.output.decode('utf-8')).split('\n')
iExitStatus = e.returncode
self.assertEqual(iExitStatus,1)
self.assertEqual(lActual, lExpected)