From d4f08e36ebe01a1fda5aa3ba480c60cd6b7e86f8 Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Tue, 8 Dec 2020 10:23:26 -0600 Subject: [PATCH] Updates for issue #408 Adding check for length of file. 1) Documentation updates a) Added documentation for rule length_002 b) Added section in configuring length rules to cover length_002 2) Added tests 3) Added rule length_002 --- docs/configuring.rst | 2 +- docs/configuring_length_rules.rst | 35 ++++++++++++++++++++++ docs/configuring_line_length.rst | 22 -------------- docs/length_rules.rst | 9 ++++++ vsg/rules/__init__.py | 1 + vsg/rules/file_length.py | 38 ++++++++++++++++++++++++ vsg/rules/length/__init__.py | 1 + vsg/rules/length/rule_002.py | 11 +++++++ vsg/tests/length/rule_002_test_input.vhd | 13 ++++++++ vsg/tests/length/test_rule_002.py | 38 ++++++++++++++++++++++++ 10 files changed, 147 insertions(+), 23 deletions(-) create mode 100644 docs/configuring_length_rules.rst delete mode 100644 docs/configuring_line_length.rst create mode 100644 vsg/rules/file_length.py create mode 100644 vsg/rules/length/rule_002.py create mode 100644 vsg/tests/length/rule_002_test_input.vhd create mode 100644 vsg/tests/length/test_rule_002.py diff --git a/docs/configuring.rst b/docs/configuring.rst index da7812e1..e7641d17 100644 --- a/docs/configuring.rst +++ b/docs/configuring.rst @@ -227,7 +227,7 @@ Configure the indent size for all rules by setting the **global** attribute. .. include:: configuring_case.rst .. include:: configuring_prefix_suffix.rst .. include:: configuring_number_of_signals.rst -.. include:: configuring_line_length.rst +.. include:: configuring_length_rules.rst .. include:: configuring_keyword_alignment.rst .. include:: configuring_declaration_identifier_alignment.rst .. include:: multiple_configurations.rst diff --git a/docs/configuring_length_rules.rst b/docs/configuring_length_rules.rst new file mode 100644 index 00000000..2e1594ca --- /dev/null +++ b/docs/configuring_length_rules.rst @@ -0,0 +1,35 @@ +Configuring Length Rules +------------------------ + +VSG includes several rules enforcing maximum lengths of code structures. +These rules are set as warnings. + +Overriding Line Length +###################### + +Limiting the line length of the VHDL code can improve readability. +Code that exceeds the editor window is more difficult to read. +The default line length is 120, and can be changed by configuring rule **length_001**. + +Use the following configuration to change the line length to 180. + +.. code-block:: yaml + + rule : + length_001 : + length : 180 + +Overridding File Line Length +############################ + +Limiting the length of a VHDL file can improve readability. +Excessively long files can indicate the file can be broken into smaller modules. +The default line length is 2000, and can be changed by configuring rule **length_002**. + +Use the following configuration to change the file length to 5000. + +.. code-block:: yaml + + rule : + length_002 : + length : 5000 diff --git a/docs/configuring_line_length.rst b/docs/configuring_line_length.rst deleted file mode 100644 index 02f5c225..00000000 --- a/docs/configuring_line_length.rst +++ /dev/null @@ -1,22 +0,0 @@ -Configuring the Maximum Line Length ------------------------------------ - -Limiting the line length of the VHDL code can improve readability. -Code that exceeds the editor window is more difficult to read. - -The default line length is 120, and can be set by configuring rule **length_001**. - -Overriding Line Length -###################### - -The default setting can be changed using a configuration. -We can use the following configuration to change the line length to 180. - -.. code-block:: yaml - - --- - - rule : - length_001 : - length : 180 - diff --git a/docs/length_rules.rst b/docs/length_rules.rst index 5824953b..32dde0d8 100644 --- a/docs/length_rules.rst +++ b/docs/length_rules.rst @@ -8,6 +8,8 @@ length_001 This rule checks the length of the line. +Refer to the section `Configuring Length Rules `_ for configuring this option. + **Violation** .. code-block:: vhdl @@ -18,3 +20,10 @@ This rule checks the length of the line. .. NOTE:: The user must fix this violation. Refer to the section `Configuring Line Length `_ for information on changing the default. + +length_002 +########## + +This rule checks the length of a file. + +Refer to the section `Configuring Length Rules `_ for configuring this option. diff --git a/vsg/rules/__init__.py b/vsg/rules/__init__.py index d94bc60e..5f500363 100644 --- a/vsg/rules/__init__.py +++ b/vsg/rules/__init__.py @@ -10,6 +10,7 @@ from .insert_token_right_of_token_if_it_does_not_exist import insert_token_right from .single_space_between_tokens import single_space_between_tokens from .line_length import line_length +from .file_length import file_length from .token_case import token_case from .token_case_in_range_bounded_by_tokens import token_case_in_range_bounded_by_tokens from .blank_line_below_line_ending_with_token import blank_line_below_line_ending_with_token diff --git a/vsg/rules/file_length.py b/vsg/rules/file_length.py new file mode 100644 index 00000000..2acd7ebd --- /dev/null +++ b/vsg/rules/file_length.py @@ -0,0 +1,38 @@ + +from vsg import parser +from vsg import rule +from vsg import severity +from vsg import violation + + +class file_length(rule.Rule): + ''' + Checks the length of the file. + + Parameters + ---------- + + name : string + The group the rule belongs to. + + identifier : string + unique identifier. Usually in the form of 00N. + + ''' + + def __init__(self, name, identifier): + rule.Rule.__init__(self, name=name, identifier=identifier) + self.phase = 7 + self.fixable = False # The user will have to fix line length violations + self.severity = severity.set_warning_severity + self.length = 2000 + self.configuration.append('length') + + def _get_tokens_of_interest(self, oFile): + return oFile.get_tokens_matching([parser.carriage_return]) + + def _analyze(self, lToi): + if len(lToi) > self.length: + sSolution = 'Reduce file length to less than ' + str(self.length) + ' lines' + oViolation = violation.New(self.length, None, sSolution) + self.violations.append(oViolation) diff --git a/vsg/rules/length/__init__.py b/vsg/rules/length/__init__.py index c422df02..d92a54c4 100644 --- a/vsg/rules/length/__init__.py +++ b/vsg/rules/length/__init__.py @@ -1,2 +1,3 @@ from .rule_001 import rule_001 +from .rule_002 import rule_002 diff --git a/vsg/rules/length/rule_002.py b/vsg/rules/length/rule_002.py new file mode 100644 index 00000000..ce1b9f2c --- /dev/null +++ b/vsg/rules/length/rule_002.py @@ -0,0 +1,11 @@ + +from vsg.rules import file_length + + +class rule_002(file_length): + ''' + Checks for length of the file. + ''' + + def __init__(self): + file_length.__init__(self, 'length', '002') diff --git a/vsg/tests/length/rule_002_test_input.vhd b/vsg/tests/length/rule_002_test_input.vhd new file mode 100644 index 00000000..45463726 --- /dev/null +++ b/vsg/tests/length/rule_002_test_input.vhd @@ -0,0 +1,13 @@ +------------------------------------------------------------------------------- +-- Next line is a failure +------------------------------------------------------------------------------- + +architecture RTL of ENT is + + signal a : std_logic; + + signal a : std_logic_vector(15 downto 0); + +begin + +end architecture RTL; diff --git a/vsg/tests/length/test_rule_002.py b/vsg/tests/length/test_rule_002.py new file mode 100644 index 00000000..d66d26f9 --- /dev/null +++ b/vsg/tests/length/test_rule_002.py @@ -0,0 +1,38 @@ + +import os +import unittest + +from vsg.rules import length +from vsg import vhdlFile +from vsg.tests import utils + +sTestDir = os.path.dirname(__file__) + +lFile = utils.read_vhdlfile(os.path.join(sTestDir,'rule_002_test_input.vhd')) + + +class test_length_rule(unittest.TestCase): + + def setUp(self): + self.oFile = vhdlFile.vhdlFile(lFile) + + def test_rule_002_default(self): + oRule = length.rule_002() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'length') + self.assertEqual(oRule.identifier, '002') + + lExpected = [] + + oRule.analyze(self.oFile) + self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations)) + + def test_rule_002_with_125_characters(self): + oRule = length.rule_002() + oRule.length = 10 + + lExpected = [10] + + oRule.analyze(self.oFile) + self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations)) +