Skip to content

Commit

Permalink
JavaPMDBear.py: Allow usage of custom rule set
Browse files Browse the repository at this point in the history
PMD allows user to supply a custom rule set for it to use when linting.
This change introduces a parameter that enables its usage,
through a custom rule set file.

Closes coala#1700
  • Loading branch information
tupaschoal committed May 3, 2017
1 parent 197747f commit 8e00aee
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
36 changes: 21 additions & 15 deletions bears/java/JavaPMDBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def check_prerequisites(cls): # pragma: no cover
@deprecate_settings(allow_unnecessary_code=('check_unnecessary', negate),
allow_unused_code=('check_unused', negate))
def create_arguments(filename, file, config_file,
rule_set_file: str = None,
check_best_practices: bool = True,
check_braces: bool = True,
check_clone_implementation: bool = True,
Expand All @@ -50,6 +51,8 @@ def create_arguments(filename, file, config_file,
allow_unnecessary_code: bool = False,
allow_unused_code: bool = False):
"""
:param rule_set_file:
Specify custom rule set file path.
:param check_best_practices:
Checks for best practices.
:param check_braces:
Expand Down Expand Up @@ -78,21 +81,24 @@ def create_arguments(filename, file, config_file,
:param allow_unused_code:
Allows unused code.
"""
options = {
'java-basic': check_best_practices,
'java-braces': check_braces,
'java-clone': check_clone_implementation,
'java-codesize': check_code_size,
'java-comments': check_comments,
'java-controversial': check_controversial,
'java-design': check_design,
'java-imports': check_imports,
'java-naming': check_naming,
'java-optimizations': check_optimizations,
'java-strings': check_strings,
'java-unnecessary': not allow_unnecessary_code,
'java-unusedcode': not allow_unused_code}
rules = ','.join(key for key in options if options[key])
if rule_set_file:
rules = rule_set_file
else:
options = {
'java-basic': check_best_practices,
'java-braces': check_braces,
'java-clone': check_clone_implementation,
'java-codesize': check_code_size,
'java-comments': check_comments,
'java-controversial': check_controversial,
'java-design': check_design,
'java-imports': check_imports,
'java-naming': check_naming,
'java-optimizations': check_optimizations,
'java-strings': check_strings,
'java-unnecessary': not allow_unnecessary_code,
'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
10 changes: 10 additions & 0 deletions tests/java/JavaPMDBearTest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from bears.java.JavaPMDBear import JavaPMDBear
from coalib.testing.LocalBearTestHelper import verify_local_bear

Expand All @@ -23,6 +24,7 @@ class Hello {
class Hello {
int test() {
String s = null;
String bla = new String();
return s.length();
}
}
Expand All @@ -32,3 +34,11 @@ class Hello {
JavaPMDBearTest = verify_local_bear(
JavaPMDBear, valid_files=(good_file,), invalid_files=(bad_file,),
tempfile_kwargs={'suffix': '.java'})

test_files = os.path.join(os.path.dirname(__file__), 'test_files')
custom_rule_set = os.path.join(test_files, 'custom_rule_set.xml')

JavaPMDBearTest2 = verify_local_bear(
JavaPMDBear, valid_files=(good_file,), invalid_files=(bad_file,),
tempfile_kwargs={'suffix': '.java'},
settings={'rule_set_file': custom_rule_set})
12 changes: 12 additions & 0 deletions tests/java/test_files/custom_rule_set.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>

<ruleset name="Custom"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
This is a custom ruleset to validate JavaPMDBear rule set option
</description>
<rule ref="rulesets/java/strings.xml"/>
</ruleset>

0 comments on commit 8e00aee

Please sign in to comment.