Skip to content

Commit 4f21520

Browse files
committed
Merge branch 'pr-21'
2 parents 84d4086 + ceed6b8 commit 4f21520

37 files changed

+354
-50
lines changed

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
inherit_from: .rubocop_todo.yml
22

33
AllCops:
4+
TargetRubyVersion: 2.4
45
Exclude:
6+
- 'target/**/*'
57
- 'test/resources/**/*'
68

79
Metrics/LineLength:

.rubocop_todo.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2018-06-11 10:04:30 +0200 using RuboCop version 0.56.0.
3+
# on 2018-07-04 23:39:08 +0800 using RuboCop version 0.56.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 10
9+
# Offense count: 18
1010
Metrics/AbcSize:
1111
Max: 47
1212

13-
# Offense count: 1
13+
# Offense count: 3
1414
# Configuration parameters: CountComments.
1515
Metrics/ClassLength:
16-
Max: 200
16+
Max: 173
1717

18-
# Offense count: 18
18+
# Offense count: 33
1919
# Configuration parameters: CountComments.
2020
Metrics/MethodLength:
21-
Max: 30
21+
Max: 32
2222

23-
# Offense count: 6
23+
# Offense count: 5
2424
Naming/AccessorMethodName:
2525
Exclude:
26-
- 'lib/pmdtester/builders/diff_builder.rb'
2726
- 'lib/pmdtester/builders/pmd_report_builder.rb'
2827
- 'lib/pmdtester/parsers/projects_parser.rb'
2928

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525
* [#18](https://github.com/pmd/pmd-regression-tester/pull/18): Add SummaryReportBuilder to PmdTester - [BBG](https://github.com/djydewang)
2626
* [#19](https://github.com/pmd/pmd-regression-tester/pull/19): Add online mode for PmdTester - [BBG](https://github.com/djydewang)
2727
* [#20](https://github.com/pmd/pmd-regression-tester/pull/20): Change the way of parsing xml file from DOM to SAX - [BBG](https://github.com/djydewang)
28+
* [#21](https://github.com/pmd/pmd-regression-tester/pull/21): Add auto-gen-config option for PmdTester - [BBG](https://github.com/djydewang)

lib/pmdtester/builders/diff_builder.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'nokogiri'
24
require_relative '../pmd_error'
35
require_relative '../pmd_violation'
@@ -10,9 +12,9 @@ module PmdTester
1012
class DiffBuilder
1113
# The schema of pmd xml report refers to
1214
# http://pmd.sourceforge.net/report_2_0_0.xsd
13-
def build(base_report_filename, patch_report_filename, base_info, patch_info)
15+
def build(base_report_filename, patch_report_filename, base_info, patch_info, filter_set = nil)
1416
report_diffs = ReportDiff.new
15-
base_violations, base_errors = parse_pmd_report(base_report_filename, 'base')
17+
base_violations, base_errors = parse_pmd_report(base_report_filename, 'base', filter_set)
1618
patch_violations, patch_errors = parse_pmd_report(patch_report_filename, 'patch')
1719
report_diffs.calculate_violations(base_violations, patch_violations)
1820
report_diffs.calculate_errors(base_errors, patch_errors)
@@ -21,8 +23,8 @@ def build(base_report_filename, patch_report_filename, base_info, patch_info)
2123
report_diffs
2224
end
2325

24-
def parse_pmd_report(report_filename, branch)
25-
doc = PmdReportDocument.new(branch)
26+
def parse_pmd_report(report_filename, branch, filter_set = nil)
27+
doc = PmdReportDocument.new(branch, filter_set)
2628
parser = Nokogiri::XML::SAX::Parser.new(doc)
2729
parser.parse_file(report_filename) unless report_filename.nil?
2830
[doc.violations, doc.errors]

lib/pmdtester/builders/diff_report_builder.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
# frozen_string_literal: true
2+
13
require 'nokogiri'
24
require_relative './html_report_builder'
35

46
module PmdTester
57
# Building diff report for a single project
68
class DiffReportBuilder < HtmlReportBuilder
7-
NO_DIFFERENCES_MESSAGE = 'No differences found!'.freeze
9+
NO_DIFFERENCES_MESSAGE = 'No differences found!'
810

911
def build(project)
1012
@project = project

lib/pmdtester/builders/html_report_builder.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
# frozen_string_literal: true
2+
13
module PmdTester
24
# This class is the parent of all classes which is used to build html report
35
class HtmlReportBuilder
4-
CSS_SRC_DIR = 'resources/css'.freeze
6+
CSS_SRC_DIR = 'resources/css'
57

68
def build_html_report(title_name)
79
html_builder = Nokogiri::HTML::Builder.new do |doc|

lib/pmdtester/builders/pmd_report_builder.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'fileutils'
24
require_relative '../cmd'
35
require_relative '../project'
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# frozen_string_literal: true
2+
3+
require 'nokogiri'
4+
require 'set'
5+
require_relative '../cmd'
6+
module PmdTester
7+
# This class is responsible for generation dynamic configuration
8+
# according to the difference between base and patch branch of Pmd.
9+
# Attention: we only consider java rulesets now.
10+
class RuleSetBuilder
11+
ALL_RULE_SETS = Set['bestpractices', 'codestyle', 'design', 'documentation',
12+
'errorprone', 'multithreading', 'performance', 'security'].freeze
13+
PATH_TO_PMD_JAVA_BASED_RULES =
14+
'pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule'
15+
PATH_TO_PMD_XPATH_BASED_RULES = 'pmd-java/src/main/resources/category/java'
16+
PATH_TO_ALL_JAVA_RULES = 'config/all-java.xml'
17+
PATH_TO_DYNAMIC_CONFIG = 'target/dynamic-config.xml'
18+
NO_JAVA_RULES_CHANGED_MESSAGE = 'No java rules have been changed!'
19+
20+
def initialize(options)
21+
@options = options
22+
end
23+
24+
def build
25+
filenames = diff_filenames
26+
rule_sets = get_rule_sets(filenames)
27+
output_filter_set(rule_sets)
28+
build_config_file(rule_sets)
29+
end
30+
31+
def output_filter_set(rule_sets)
32+
if rule_sets == ALL_RULE_SETS
33+
# if `rule_sets` contains all rule sets, than no need to filter the baseline
34+
@options.filter_set = nil
35+
else
36+
@options.filter_set = rule_sets
37+
end
38+
end
39+
40+
def diff_filenames
41+
filenames = nil
42+
Dir.chdir(@options.local_git_repo) do
43+
base = @options.base_branch
44+
patch = @options.patch_branch
45+
# We only need to support git here, since PMD's repo is using git.
46+
diff_cmd = "git diff --name-only #{base}..#{patch} -- pmd/core pmd/java"
47+
filenames = Cmd.execute(diff_cmd)
48+
end
49+
filenames.split("\n")
50+
end
51+
52+
def get_rule_sets(filenames)
53+
rule_sets = Set[]
54+
filenames.each do |filename|
55+
match_data = %r{#{PATH_TO_PMD_JAVA_BASED_RULES}/([^/]+)/[^/]+Rule.java}.match(filename)
56+
if match_data.nil?
57+
match_data = %r{#{PATH_TO_PMD_XPATH_BASED_RULES}/([^/]+).xml}.match(filename)
58+
end
59+
60+
category = if match_data.nil?
61+
nil
62+
else
63+
match_data[1]
64+
end
65+
66+
if category.nil?
67+
rule_sets = ALL_RULE_SETS
68+
break
69+
else
70+
rule_sets.add(category)
71+
end
72+
end
73+
rule_sets
74+
end
75+
76+
def build_config_file(rule_sets)
77+
if rule_sets.empty?
78+
puts NO_JAVA_RULES_CHANGED_MESSAGE
79+
exit 0
80+
end
81+
82+
doc = Nokogiri::XML(File.read(PATH_TO_ALL_JAVA_RULES))
83+
doc.search('rule').each do |rule|
84+
rule.remove unless match_ref?(rule, rule_sets)
85+
end
86+
87+
description = doc.at_css('description')
88+
description.content = 'The ruleset generated by PmdTester dynamically'
89+
90+
write_dynamic_file(doc)
91+
end
92+
93+
def match_ref?(rule_node, rule_sets)
94+
rule_sets.each do |rule_set|
95+
return true unless rule_node['ref'].index(rule_set).nil?
96+
end
97+
98+
false
99+
end
100+
101+
def write_dynamic_file(doc)
102+
File.open(PATH_TO_DYNAMIC_CONFIG, 'w') do |x|
103+
x << doc.to_s.gsub(/\n\s+\n/, "\n")
104+
end
105+
@options.base_config = PATH_TO_DYNAMIC_CONFIG
106+
@options.patch_config = PATH_TO_DYNAMIC_CONFIG
107+
end
108+
end
109+
end

lib/pmdtester/builders/summary_report_builder.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
# frozen_string_literal: true
2+
13
require_relative './html_report_builder'
24
require_relative '../pmd_branch_detail'
35

46
module PmdTester
57
# Building summary report to show the details about projects and pmd branchs
68
class SummaryReportBuilder < HtmlReportBuilder
7-
REPORT_DIR = 'target/reports/diff'.freeze
8-
BASE_CONFIG_PATH = 'target/reports/diff/base_config.xml'.freeze
9-
PATCH_CONFIG_PATH = 'target/reports/diff/patch_config.xml'.freeze
10-
INDEX_PATH = 'target/reports/diff/index.html'.freeze
9+
REPORT_DIR = 'target/reports/diff'
10+
BASE_CONFIG_PATH = 'target/reports/diff/base_config.xml'
11+
PATCH_CONFIG_PATH = 'target/reports/diff/patch_config.xml'
12+
INDEX_PATH = 'target/reports/diff/index.html'
1113

1214
def build(projects, base_name, patch_name)
1315
@projects = projects

lib/pmdtester/cmd.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'open3'
24

35
module PmdTester
@@ -14,7 +16,7 @@ def self.execute(cmd)
1416
exit(status.exitstatus)
1517
end
1618

17-
stdout.chomp! unless stdout.nil?
19+
stdout&.chomp!
1820

1921
stdout
2022
end

0 commit comments

Comments
 (0)