Skip to content

Commit

Permalink
JavaPMDBear: Detect appropriate linter executable
Browse files Browse the repository at this point in the history
Describe current executable requirements of pmd or run.sh.

Also remove explicit invocation using `bash`, which was
ineffective as which('run.sh') would only be successful
if the script was executable.

Also add `-f text` arguments, which are mandatory on recent
versions of pmd, but were optional on older supported versions.

Fixes coala#2908
  • Loading branch information
jayvdb committed Aug 6, 2019
1 parent a0f5a9e commit 2034e83
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 35 deletions.
5 changes: 3 additions & 2 deletions bear-metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1209,14 +1209,15 @@ bear_metadata:
filename: JavaPMDBear.py
requirements:
exe:
bash:
pmd:
run.sh:
languages:
- Java
tags:
- bash
- exe
- java
- pmd
- run.sh
Jinja2Bear:
name: Jinja2Bear
subdir: jinja2
Expand Down
30 changes: 17 additions & 13 deletions bears/java/JavaPMDBear.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from shutil import which
from dependency_management.requirements.AnyOneOfRequirements import (
AnyOneOfRequirements)
from dependency_management.requirements.ExecutableRequirement import (
ExecutableRequirement)

from coalib.bearlib.abstractions.Linter import linter
from coalib.bearlib import deprecate_settings
from coala_utils.param_conversion import negate

_executable = which('run.sh') or which('pmd')

@linter('bash', output_format='regex',

@linter(_executable or 'pmd', output_format='regex',
output_regex=r'.+:(?P<line>.+):(?P<message>.*)')
class JavaPMDBear:
"""
Expand All @@ -17,22 +23,19 @@ class JavaPMDBear:
"""

LANGUAGES = {'Java'}
REQUIREMENTS = {
AnyOneOfRequirements(
[ExecutableRequirement('pmd'),
ExecutableRequirement('run.sh'),
]
),
}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Code Simplification', 'Unreachable Code', 'Smell',
'Duplication'}

@classmethod
def check_prerequisites(cls):
if which('bash') is None:
return 'bash is not installed.'
elif which('pmd') is None and which('run.sh') is None:
return ('PMD is missing. Make sure to install it from '
'<https://pmd.github.io/>')
else:
return True

@staticmethod
@deprecate_settings(allow_unnecessary_code=('check_unnecessary', negate),
allow_unused_code=('check_unused', negate))
Expand Down Expand Up @@ -94,5 +97,6 @@ def create_arguments(filename, file, config_file,
'java-unusedcode': not allow_unused_code}
rules = ','.join(key for key in options if options[key])

executable = which('pmd') or which('run.sh') # Mac vs. Unix
return executable, 'pmd', '-R', rules, '-d', filename
executable = tuple(['pmd'] if _executable.endswith('run.sh') else [])
arguments = '-R', rules, '-d', filename, '-f', 'text'
return executable + arguments
20 changes: 0 additions & 20 deletions tests/java/JavaPMDBearTest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from unittest import mock, TestCase

from bears.java.JavaPMDBear import JavaPMDBear
from coalib.testing.LocalBearTestHelper import verify_local_bear

Expand Down Expand Up @@ -31,24 +29,6 @@ class Hello {
"""


class JavaPMDBearPrerequisiteTest(TestCase):
def test_check_prerequisites(self):
with mock.patch('bears.java.JavaPMDBear.which') as mock_which:
mock_which.side_effect = [None, None, None]
self.assertEqual(JavaPMDBear.check_prerequisites(),
'bash is not installed.')

mock_which.side_effect = ['path/to/bash', None, None]
self.assertEqual(JavaPMDBear.check_prerequisites(),
('PMD is missing. Make sure to install it from '
'<https://pmd.github.io/>'))

mock_which.side_effect = ['path/to/bash',
'path/to/pmd',
'path/to/run']
self.assertEqual(JavaPMDBear.check_prerequisites(), True)


JavaPMDBearTest = verify_local_bear(
JavaPMDBear, valid_files=(good_file,), invalid_files=(bad_file,),
tempfile_kwargs={'suffix': '.java'})

0 comments on commit 2034e83

Please sign in to comment.