Skip to content

Commit

Permalink
LineCountBear.py: Add exclude_blank_lines option
Browse files Browse the repository at this point in the history
Add option to exclude blank lines. The setting is disabled by default.

Closes coala#1478
  • Loading branch information
yashtrivedi96 committed Mar 17, 2017
1 parent f04f8bc commit 34f1be4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
14 changes: 13 additions & 1 deletion bears/general/LineCountBear.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

from coalib.bears.LocalBear import LocalBear
from coalib.results.Result import Result
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
Expand All @@ -10,14 +12,24 @@ class LineCountBear(LocalBear):
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Formatting'}

def run(self, filename, file, max_lines_per_file: int):
def _get_blank_line_count(self, file):
num_blank_lines = len(
list(filter(lambda x: re.match(r'^\s*$', x), file)))
return num_blank_lines

def run(self, filename, file, max_lines_per_file: int,
exclude_blank_lines: bool=False):
"""
Count the number of lines in a file and ensure that they are
smaller than a given size.
:param max_lines_per_file: Number of lines allowed per file.
:param exclude_blank_lines: "True" if blank lines are to be excluded.
"""
file_length = len(file)
if exclude_blank_lines:
num_blank_lines = self._get_blank_line_count(file)
file_length = file_length - num_blank_lines
if file_length > max_lines_per_file:
yield Result.from_values(
origin=self,
Expand Down
15 changes: 15 additions & 0 deletions tests/general/LineCountBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,18 @@ def test_run(self):
filename='default')
self.check_validity(self.uut, ['1 line'])
self.check_validity(self.uut, []) # Empty file

def test_exclude_blank_lines(self):
self.section.append(Setting('exclude_blank_lines', True))
self.section.append(Setting('max_lines_per_file', 2))
self.check_results(
self.uut, ['line 1', ' ', 'line 2',
'line 3', '\n', '\t', ' line 4',
'line 5 ', ' line 6 ', '\t\tline 7',
'', '\t \n ', ' \t\n '],
[Result.from_values('LineCountBear',
'This file had 7 lines, which is 5 lines more '
'than the maximum limit specified.',
severity=RESULT_SEVERITY.NORMAL,
file='default')],
filename='default')

0 comments on commit 34f1be4

Please sign in to comment.