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
This commit is contained in:
Jeremiah Leary 2020-12-08 10:23:26 -06:00
parent 1cc326e628
commit d4f08e36eb
10 changed files with 147 additions and 23 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -8,6 +8,8 @@ length_001
This rule checks the length of the line.
Refer to the section `Configuring Length Rules <configuring_length_rules.html>`_ 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 <configuring_line_length.html>`_ for information on changing the default.
length_002
##########
This rule checks the length of a file.
Refer to the section `Configuring Length Rules <configuring_length_rules.html>`_ for configuring this option.

View File

@ -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

38
vsg/rules/file_length.py Normal file
View File

@ -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)

View File

@ -1,2 +1,3 @@
from .rule_001 import rule_001
from .rule_002 import rule_002

View File

@ -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')

View File

@ -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;

View File

@ -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))