Skip to content

Commit

Permalink
PycodestyleBear: Migrate bear to aspects
Browse files Browse the repository at this point in the history
This migrates `PycodestyleBear` to its respective
aspects and tastes.

Closes #2656
  • Loading branch information
pareksha committed Aug 7, 2018
1 parent e46d352 commit c9e8df0
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
35 changes: 31 additions & 4 deletions bears/python/PycodestyleBear.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import sys
import re

from coalib.bearlib.abstractions.Linter import linter
from coalib.settings.Setting import typed_list
from coalib.results.Result import Result
from coalib.bearlib.aspects import map_setting_to_aspect
from coalib.bearlib.aspects.Formatting import LineLength

from dependency_management.requirements.PipRequirement import PipRequirement


OUTPUT_REGEX = (r'(?P<line>\d+) (?P<column>\d+) '
r'(?P<message>(?P<origin>\S+).*)')


@linter(executable='pycodestyle',
output_format='regex',
output_regex=r'(?P<line>\d+) (?P<column>\d+) '
r'(?P<message>(?P<origin>\S+).*)')
)
class PycodestyleBear:
"""
A wrapper for the tool ``pycodestyle`` formerly known as ``pep8``.
Expand All @@ -21,8 +27,11 @@ class PycodestyleBear:
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Formatting'}

@staticmethod
@map_setting_to_aspect(
max_line_length=LineLength.max_line_length,
)
def create_arguments(
self,
filename, file, config_file,
pycodestyle_ignore: typed_list(str) = (
'E121', 'E123', 'E126', 'E133', 'E226',
Expand Down Expand Up @@ -61,3 +70,21 @@ def create_arguments(
arguments.append(filename)

return arguments

def process_output(self, output, filename, file):
result = re.match(OUTPUT_REGEX, output)
if not result:
return
line, column, message, rule = result.groups()
if rule == 'E501':
aspect = LineLength('py')
else:
aspect = None
yield Result.from_values(
origin='{} ({})'.format(self.name, rule),
message=message,
file=filename,
line=int(line),
column=int(column),
aspect=aspect,
)
53 changes: 52 additions & 1 deletion tests/python/PycodestyleBearTest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import unittest

from bears.python.PycodestyleBear import PycodestyleBear
from coalib.testing.LocalBearTestHelper import verify_local_bear
from coalib.testing.LocalBearTestHelper import verify_local_bear, execute_bear
from coalib.settings.Section import Section
from coala_utils.ContextManagers import prepare_file
from queue import Queue
from coalib.bearlib.aspects.Formatting import LineLength
from coalib.bearlib.aspects import (
AspectList,
get as get_aspect,
)


good_file = '''
Expand Down Expand Up @@ -45,6 +55,8 @@ def hello():

long_line = 'a = "{0}"'.format('a' * 100)

small_line = 'Small line.'

PycodestyleBearLineLengthTest = verify_local_bear(
PycodestyleBear,
valid_files=(),
Expand All @@ -63,3 +75,42 @@ def hello():
valid_files=(file_with_very_long_line,),
invalid_files=(),
settings={'max_line_length': 0})

PycodestyleBearAspectsTest = verify_local_bear(
PycodestyleBear,
valid_files=(small_line,),
invalid_files=(long_line,),
aspects=AspectList([
get_aspect('LineLength')('Python', max_line_length=30),
]),
)

PycodestyleBearSettingsOverAspectsTest = verify_local_bear(
PycodestyleBear,
valid_files=(small_line,),
invalid_files=(long_line,),
aspects=AspectList([
get_aspect('LineLength')('Python', max_line_length=2),
]),
settings={'max_line_length': 30},
)


class PycodestyleBearTest(unittest.TestCase):

def setUp(self):
self.section = Section('')
self.section.aspects = AspectList([
get_aspect('LineLength')('Python', max_line_length=30),
])
self.uut = PycodestyleBear(self.section, Queue())

def test_line_length(self):
content = long_line.splitlines()
with prepare_file(content, None) as (file, fname):
with execute_bear(self.uut, fname, file) as results:
result = results[0]
self.assertEqual(result.message,
'E501 line too long (106 > 30 characters)')
self.assertEqual(result.origin, 'PycodestyleBear (E501)')
self.assertEqual(result.aspect, LineLength('py'))

0 comments on commit c9e8df0

Please sign in to comment.