Skip to content

Commit

Permalink
Pycheckbear.py: Add new PyCheckBear
Browse files Browse the repository at this point in the history
New Bear is added to find
bugs in python file.

Closes #2151
  • Loading branch information
AkshJain99 committed Mar 15, 2019
1 parent fd5a5a7 commit 6d4ae18
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions .ci/deps.apt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set -x
export DEBIAN_FRONTEND=noninteractive

deps="libclang1-3.4 astyle indent mono-mcs chktex r-base julia golang-go luarocks verilator cppcheck flawfinder devscripts mercurial"
deps="libclang1-3.4 indent mono-mcs chktex r-base julia golang-go luarocks verilator cppcheck flawfinder devscripts pychecker"
deps_infer="m4 opam"

case $CIRCLE_BUILD_IMAGE in
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ addons:
- php-codesniffer
- r-base
- verilator
- pychecker

cache:
pip: true
Expand Down
40 changes: 40 additions & 0 deletions bears/python/PyCheckerBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import shlex

from coalib.bearlib.abstractions.Linter import linter
from dependency_management.requirements.DistributionRequirement import (
DistributionRequirement)


@linter(executable='pychecker',
output_format='regex',
output_regex=r'(?P<filename>\w+\.py):(?P<line>\d+): '
r'(?P<message>.*)')
class PyCheckerBear:
"""
Find bugs in your Python source code.
The code for each function, class, and method is checked for possible
problems. Checks for unused globals and locals(module or variable), unused
method arguments and using a variable before setting it or if you are
redefining a function/class/method in the same scope.
"""
LANGUAGES = {'Python', 'Python 2', 'Python 3'}
REQUIREMENTS = {DistributionRequirement(apt_get='pychecker')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Unused method/function arguments',
'No doc string', 'Redefining'}
SEE_MORE = 'http://pychecker.sourceforge.net/'

@staticmethod
def create_arguments(filename, file, config_file,
pychecker_cli_options: str = ''):
"""
:param pychecker_cli_options: Command line options you wish to be
passed to pychecker.
"""
args = ()
if pychecker_cli_options:
args += tuple(shlex.split(pychecker_cli_options))

return args + (filename,)
51 changes: 51 additions & 0 deletions tests/python/PycheckerBearTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from queue import Queue
import os.path

from coalib.settings.Section import Section
from coalib.results.Result import Result
from coalib.testing.LocalBearTestHelper import LocalBearTestHelper
from coalib.testing.BearTestHelper import generate_skip_decorator

from bears.python.PyCheckerBear import PyCheckerBear


def get_testfile_path(file):
return os.path.join(os.path.dirname(__file__),
'pychecker_test_files', file)


def load_testfiles(name):
with open(get_testfile_path(name)) as f1:
contents = f1.read().splitlines(True)

return contents


@generate_skip_decorator(PyCheckerBear)
class PyCheckerBearTest(LocalBearTestHelper):

def setUp(self):
self.uut = PyCheckerBear(Section('name'), Queue())
self.good_file = get_testfile_path('good_file.py')
self.bad_file = get_testfile_path('bad_file.py')
self.maxDiff = None

def test_good_file(self):
self.check_results(self.uut, load_testfiles('good_file.py'),
[],
self.good_file)

def test_bad_file(self):
self.check_results(self.uut, load_testfiles('bad_file.py'),
[Result.from_values('PyCheckerBear',
'Parameter (c) not used',
self.bad_file,
line=1)],
self.bad_file)

def test_cli_options(self):
self.check_results(self.uut, load_testfiles('bad_file.py'),
[],
self.bad_file,
settings={'pychecker_cli_options': '--argsused=off'}
)
Empty file.
5 changes: 5 additions & 0 deletions tests/python/pychecker_test_files/bad_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def multiply(a, b, c):
return a * b


result = multiply(1, 2, 3)
5 changes: 5 additions & 0 deletions tests/python/pychecker_test_files/good_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def multiply(a, b, c):
return a * b * c


result = multiply(1, 2, 3)

0 comments on commit 6d4ae18

Please sign in to comment.