Skip to content

Commit d6422f7

Browse files
Issue#1358: Adding user_error_message option. (#1387)
1 parent cc678cb commit d6422f7

File tree

9 files changed

+134
-2
lines changed

9 files changed

+134
-2
lines changed

docs/configuring.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Configuring
3636
configuring_type_of_instantiations.rst
3737
configuring_uppercase_and_lowercase_rules.rst
3838
configuring_use_clause_indenting.rst
39+
configuring_user_error_messages.rst
3940
configuring_whitespace_rules.rst
4041
configuring_whitespace_after_comment_rules.rst
4142
configuring_vhdl_reserved_words.rst
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
.. _configuring-user-error-messages:
2+
3+
Configuring User Error Messages
4+
-------------------------------
5+
6+
There is an option to provide additional information to the user when a violation is found.
7+
This is accomplished by using the :code:`user_error_message` option.
8+
Setting this option for a rule will append the message to the normal output.
9+
10+
Example without :code:`user_error_message` defined
11+
##################################################
12+
13+
Applying a custom regex does not provide additional information to the user.
14+
15+
Given the following configuration file which defines a custom regex:
16+
17+
.. code-block:: yaml
18+
19+
rule:
20+
group:
21+
case::name:
22+
case: regex
23+
regex: (?![a-z||0-9]\u005F)(?!.*\u005F[a-z||0-9]$)([a-z||0-9]+(?:\u005F[a-z||0-9]+)*)
24+
25+
The following error report is generated:
26+
27+
.. code-block:: bash
28+
29+
$vsg -f original.vhd -c config.yaml
30+
================================================================================
31+
File: original.vhd
32+
================================================================================
33+
Phase 6 of 7... Reporting
34+
Total Rules Checked: 857
35+
Total Violations: 2
36+
Error : 2
37+
Warning : 0
38+
-------------+------------+------------+--------------------------------------
39+
Rule | severity | line(s) | Solution
40+
-------------+------------+------------+--------------------------------------
41+
entity_008 | Error | 2 | Format FIFO to match defined regex
42+
entity_012 | Error | 3 | Format FIFO to match defined regex
43+
-------------+------------+------------+--------------------------------------
44+
NOTE: Refer to online documentation at https://vhdl-style-guide.readthedocs.io/en/latest/index.html for more information.
45+
46+
Example with :code:`user_error_message` defined
47+
###############################################
48+
49+
Adding a custom error message can provide additional information to the user:
50+
51+
.. code-block:: yaml
52+
53+
rule:
54+
group:
55+
case::name:
56+
case: regex
57+
regex: (?![a-z||0-9]\u005F)(?!.*\u005F[a-z||0-9]$)([a-z||0-9]+(?:\u005F[a-z||0-9]+)*)
58+
user_error_message: "UPPERCASE and single letter prefix/suffix not allowed"
59+
60+
The following error report is generated:
61+
62+
.. code-block:: bash
63+
64+
$vsg -f original.vhd -c config.yaml
65+
================================================================================
66+
File: original.vhd
67+
================================================================================
68+
Phase 6 of 7... Reporting
69+
Total Rules Checked: 857
70+
Total Violations: 2
71+
Error : 2
72+
Warning : 0
73+
-------------+------------+------------+--------------------------------------
74+
Rule | severity | line(s) | Solution
75+
-------------+------------+------------+--------------------------------------
76+
entity_008 | Error | 2 | Format FIFO to match defined regex [user_error_message: UPPERCASE and single letter prefix/suffix not allowed]
77+
entity_012 | Error | 3 | Format FIFO to match defined regex [user_error_message: UPPERCASE and single letter prefix/suffix not allowed]
78+
-------------+------------+------------+--------------------------------------
79+
NOTE: Refer to online documentation at https://vhdl-style-guide.readthedocs.io/en/latest/index.html for more information.

tests/constant/test_rule_400.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_rule_400_options(self):
2222
self.assertTrue(oRule)
2323
self.assertEqual(oRule.name, "constant")
2424
self.assertEqual(oRule.identifier, "400")
25-
self.assertEqual(len(oRule.configuration), 11)
25+
self.assertEqual(len(oRule.configuration), 12)
2626

2727
def test_rule_400_yes_no_no_no_no(self):
2828
oRule = constant.rule_400()

tests/option/test_option.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def test_option_reports_in_rc_command(self):
4646
dExpected["fixable"] = True
4747
dExpected["severity"] = "Error"
4848
dExpected["option_name"] = "yes"
49+
dExpected["user_error_message"] = ""
4950

5051
dActual = self.oRule.get_configuration()
5152

tests/rule/test_rule.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def test_get_configuration(self):
8787
dExpected["indent_style"] = "spaces"
8888
dExpected["phase"] = 3
8989
dExpected["severity"] = "Error"
90+
dExpected["user_error_message"] = ""
9091
dActual = oRule.get_configuration()
9192
for sKey in dExpected.keys():
9293
self.assertEqual(dActual[sKey], dExpected[sKey])

tests/user_error_messages/__init__.py

Whitespace-only changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import os
4+
import unittest
5+
6+
from tests import utils
7+
from vsg import vhdlFile
8+
from vsg.rules import entity
9+
10+
sTestDir = os.path.dirname(__file__)
11+
12+
lFile, eError = vhdlFile.utils.read_vhdlfile(os.path.join(sTestDir, "user_error_messages_input.vhd"))
13+
14+
15+
class test_option(unittest.TestCase):
16+
def setUp(self):
17+
self.oFile = vhdlFile.vhdlFile(lFile)
18+
self.assertIsNone(eError)
19+
20+
def test_rule_entity_008_without_user_message(self):
21+
oRule = entity.rule_008()
22+
self.assertTrue(oRule)
23+
self.assertEqual(oRule.name, "entity")
24+
self.assertEqual(oRule.identifier, "008")
25+
26+
lExpected = [2]
27+
28+
oRule.analyze(self.oFile)
29+
self.assertEqual(utils.extract_violation_lines_from_violation_object(oRule.violations), lExpected)
30+
self.assertEqual(1, len(oRule.violations))
31+
self.assertEqual('Change "FIFO" to "fifo"', oRule.violations[0].sSolution)
32+
33+
def test_rule_entity_008_with_user_message(self):
34+
oRule = entity.rule_008()
35+
self.assertTrue(oRule)
36+
self.assertEqual(oRule.name, "entity")
37+
self.assertEqual(oRule.identifier, "008")
38+
oRule.user_error_message = "This is a user error message."
39+
40+
lExpected = [2]
41+
42+
oRule.analyze(self.oFile)
43+
self.assertEqual(utils.extract_violation_lines_from_violation_object(oRule.violations), lExpected)
44+
self.assertEqual(1, len(oRule.violations))
45+
self.assertEqual('Change "FIFO" to "fifo" [user_error_message: This is a user error message.]', oRule.violations[0].sSolution)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
entity FIFO is end entity;

vsg/rule.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ def __init__(self):
1818
self.disable = False
1919
self.fixable = True
2020
self.severity = severity.error("Error")
21+
self.user_error_message = ""
2122
self.debug = False
2223
self.dFix = {}
2324
self.dFix["violations"] = {}
24-
self.configuration = ["indent_style", "indent_size", "phase", "disable", "fixable", "severity"]
25+
self.configuration = ["indent_style", "indent_size", "phase", "disable", "fixable", "severity", "user_error_message"]
2526
self.deprecated = False
2627
self.proposed = False
2728
self.groups = []
@@ -132,6 +133,8 @@ def add_violation(self, violation):
132133
Adds a linenumber to a violations list.
133134
"""
134135
if not violation.has_code_tag(self.unique_id):
136+
if self.user_error_message != "":
137+
violation.sSolution = violation.sSolution + " [user_error_message: " + self.user_error_message + "]"
135138
self.violations.append(violation)
136139

137140
def analyze(self, oFile):

0 commit comments

Comments
 (0)