Skip to content

Commit

Permalink
Remove duplicated function and unify it in compat
Browse files Browse the repository at this point in the history
JSComplexityBear.py and JSONFormatBear.py had a common function
get_available_decodeerror, which can lead to differences in future,
hence, it is unified in the compat module, from where the error is
imported.
  • Loading branch information
meetmangukiya committed Sep 19, 2016
1 parent 72f1bac commit c48383d
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 48 deletions.
11 changes: 2 additions & 9 deletions bears/js/JSComplexityBear.py
Expand Up @@ -3,24 +3,17 @@

from coalib.bearlib.abstractions.Linter import linter
from coalib.bears.requirements.NpmRequirement import NpmRequirement
from coalib.misc.Compatibility import JSONDecodeError
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
from coalib.results.Result import Result


def get_available_decodeerror():
try:
return json.decoder.JSONDecodeError
except AttributeError: # Python 3.4 does not have JSONDecodeError there
return ValueError


@linter(executable='cr')
class JSComplexityBear:
"""
Calculates cyclomatic complexity using ``cr``, the command line utility
provided by the NodeJS module ``complexity-report``.
"""
DecodeError = get_available_decodeerror()

LANGUAGES = {"JavaScript"}
REQUIREMENTS = {NpmRequirement('complexity-report', '2.0.0-alpha')}
Expand All @@ -42,7 +35,7 @@ def process_output(self, output, filename, file, cc_threshold: int=10):
if output:
try:
output = json.loads(output)
except self.DecodeError:
except JSONDecodeError:
output_regex = (r'Fatal error \[getReports\]: .+: '
r'Line (?P<line>\d+): (?P<message>.*)')
for match in re.finditer(output_regex, output):
Expand Down
11 changes: 2 additions & 9 deletions bears/js/JSONFormatBear.py
Expand Up @@ -4,20 +4,13 @@
from coalib.bearlib import deprecate_settings
from coalib.bearlib.spacing.SpacingHelper import SpacingHelper
from coalib.bears.LocalBear import LocalBear
from coalib.misc.Compatibility import JSONDecodeError
from coalib.results.Diff import Diff
from coalib.results.Result import Result
from coala_utils.param_convertion import negate


def get_available_decodeerror():
try:
return json.decoder.JSONDecodeError
except AttributeError: # Python 3.4 does not have JSONDecodeError there
return ValueError


class JSONFormatBear(LocalBear):
DecodeError = get_available_decodeerror()

LANGUAGES = {"JSON"}
AUTHORS = {'The coala developers'}
Expand All @@ -42,7 +35,7 @@ def run(self, filename, file,
try:
json_content = json.loads(''.join(file),
object_pairs_hook=OrderedDict)
except self.DecodeError as err:
except JSONDecodeError as err:
yield Result.from_values(
self,
"This file does not contain parsable JSON. " + repr(str(err)),
Expand Down
15 changes: 0 additions & 15 deletions tests/js/JSComplexityBearTest.py
Expand Up @@ -59,18 +59,3 @@
valid_files=(complexity_4, complexity_12),
invalid_files=(),
tempfile_kwargs={"suffix": ".not_js"})


def get_available_decodeerror_test(monkeypatch):
class TestError(Exception):
pass
monkeypatch.setattr(
json.decoder, 'JSONDecodeError', TestError, raising=False)
result = JSComplexityBear.get_available_decodeerror()
assert result == TestError


def get_available_decodeerror_py34_test(monkeypatch):
monkeypatch.delattr('json.decoder.JSONDecodeError', raising=False)
result = JSComplexityBear.get_available_decodeerror()
assert result == ValueError
15 changes: 0 additions & 15 deletions tests/js/JSONFormatBearTest.py
Expand Up @@ -54,18 +54,3 @@
invalid_files=(),
settings={'escape_unicode':
'false'})


def get_available_decodeerror_test(monkeypatch):
class TestError(Exception):
pass
monkeypatch.setattr(
json.decoder, 'JSONDecodeError', TestError, raising=False)
result = JSONFormatBear.get_available_decodeerror()
assert result == TestError


def get_available_decodeerror_py34_test(monkeypatch):
monkeypatch.delattr('json.decoder.JSONDecodeError', raising=False)
result = JSONFormatBear.get_available_decodeerror()
assert result == ValueError

0 comments on commit c48383d

Please sign in to comment.