Skip to content

Commit fbff68d

Browse files
committed
Add new option "--error-recovery"
Enabling this option sets PMD_JAVA_OPTS before executing PMD.
1 parent 6875868 commit fbff68d

File tree

8 files changed

+66
-28
lines changed

8 files changed

+66
-28
lines changed

README.rdoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ on a list of standard projects(Spring Framework, Hibernate, Solr, etc.)
3737
-a, --auto-gen-config whether to generate configurations automatically based on branch differences,this option only works in online and local mode
3838
--keep-reports whether to keep old reports and skip running PMD again if possible
3939
-d, --debug whether change log level to DEBUG to see more information
40+
--error-recovery enable error recovery mode when executing PMD. Might help to analyze errors.
4041
-v, --version
4142
-h, --help
4243

lib/pmdtester/builders/pmd_report_builder.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ module PmdTester
88
# projects and branch of pmd source code
99
class PmdReportBuilder
1010
include PmdTester
11-
def initialize(branch_config, projects, local_git_repo, pmd_branch_name, threads = 1)
12-
@branch_config = branch_config
11+
def initialize(projects, options, branch_config, branch_name)
1312
@projects = projects
14-
@local_git_repo = local_git_repo
15-
@pmd_branch_name = pmd_branch_name
16-
@threads = threads
13+
@local_git_repo = options.local_git_repo
14+
@threads = options.threads
15+
@error_recovery = options.error_recovery
16+
@branch_config = branch_config
17+
@pmd_branch_name = branch_name
1718
@pwd = Dir.getwd
1819

19-
@pmd_branch_details = PmdBranchDetail.new(pmd_branch_name)
20-
@project_builder = ProjectBuilder.new(projects)
20+
@pmd_branch_details = PmdBranchDetail.new(@pmd_branch_name)
21+
@project_builder = ProjectBuilder.new(@projects)
2122
end
2223

2324
def get_pmd_binary_file
@@ -82,8 +83,10 @@ def get_last_commit_message
8283
end
8384

8485
def generate_pmd_report(project)
86+
error_recovery_options = @error_recovery ? 'PMD_JAVA_OPTS="-Dpmd.error_recovery -ea" ' : ''
8587
run_path = "target/pmd-bin-#{@pmd_version}/bin/run.sh"
86-
pmd_cmd = "#{run_path} pmd -d #{project.local_source_path} -f xml " \
88+
pmd_cmd = "#{error_recovery_options}" \
89+
"#{run_path} pmd -d #{project.local_source_path} -f xml " \
8790
"-R #{project.get_config_path(@pmd_branch_name)} " \
8891
"-r #{project.get_pmd_report_path(@pmd_branch_name)} " \
8992
"-failOnViolation false -t #{@threads} " \

lib/pmdtester/parsers/options.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Options
3131
attr_reader :debug_flag
3232
attr_accessor :filter_set
3333
attr_reader :keep_reports
34+
attr_reader :error_recovery
3435

3536
def initialize(argv)
3637
options = parse(argv)
@@ -48,6 +49,7 @@ def initialize(argv)
4849
@debug_flag = options[:d]
4950
@filter_set = nil
5051
@keep_reports = options.keep_reports?
52+
@error_recovery = options.error_recovery?
5153

5254
# if the 'config' option is selected then `config` overrides `base_config` and `patch_config`
5355
@base_config = @config if !@config.nil? && @mode == 'local'
@@ -94,6 +96,8 @@ def parse(argv)
9496
'whether to keep old reports and skip running PMD again if possible'
9597
o.bool '-d', '--debug',
9698
'whether change log level to DEBUG to see more information'
99+
o.bool '--error-recovery',
100+
'enable error recovery mode when executing PMD. Might help to analyze errors.'
97101
o.on '-v', '--version' do
98102
puts VERSION
99103
exit

lib/pmdtester/runner.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@ def run_local_mode
3535
return if rule_sets&.empty?
3636

3737
PmdReportBuilder
38-
.new(@options.base_config, @projects, @options.local_git_repo, @options.base_branch,
39-
@options.threads)
38+
.new(@projects, @options, @options.base_config, @options.base_branch)
4039
.build
4140
PmdReportBuilder
42-
.new(@options.patch_config, @projects, @options.local_git_repo, @options.patch_branch,
43-
@options.threads)
41+
.new(@projects, @options, @options.patch_config, @options.patch_branch)
4442
.build
4543

4644
build_html_reports
@@ -66,8 +64,7 @@ def run_online_mode
6664
end
6765

6866
PmdReportBuilder
69-
.new(@options.patch_config, @projects,
70-
@options.local_git_repo, @options.patch_branch, @options.threads)
67+
.new(@projects, @options, @options.patch_config, @options.patch_branch)
7168
.build
7269

7370
build_html_reports
@@ -113,9 +110,8 @@ def run_single_mode
113110

114111
get_projects(@options.project_list) unless @options.nil?
115112
branch_details = PmdReportBuilder
116-
.new(@options.patch_config, @projects,
117-
@options.local_git_repo, @options.patch_branch,
118-
@options.threads)
113+
.new(@projects, @options,
114+
@options.patch_config, @options.patch_branch)
119115
.build
120116
# copy list of projects file to the patch baseline
121117
FileUtils.cp(@options.project_list, branch_details.target_branch_project_list_path)

pmdtester.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
1111
s.metadata = { "bug_tracker_uri" => "https://github.com/pmd/pmd-regression-tester/issues", "homepage_uri" => "https://pmd.github.io", "source_code_uri" => "https://github.com/pmd/pmd-regression-tester" } if s.respond_to? :metadata=
1212
s.require_paths = ["lib".freeze]
1313
s.authors = ["Andreas Dangel".freeze, "Binguo Bao".freeze]
14-
s.date = "2020-10-28"
14+
s.date = "2020-11-05"
1515
s.description = "A regression testing tool ensure that new problems and unexpected behaviors will not be introduced to PMD project after fixing an issue , and new rules can work as expected.".freeze
1616
s.email = ["[email protected]".freeze, "[email protected]".freeze]
1717
s.executables = ["pmdtester".freeze]

test/test_options.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ def test_default_value
3838
assert_equal(Options::DEFAULT_CONFIG_PATH, opts.base_config)
3939
assert_equal(Options::DEFAULT_CONFIG_PATH, opts.patch_config)
4040
assert_equal(Options::DEFAULT_LIST_PATH, opts.project_list)
41+
assert_false(opts.error_recovery)
42+
end
43+
44+
def test_enable_error_recovery
45+
command_line = %w[-r /path/to/repo -b pmd_releases/6.2.0 -p master --error-recovery]
46+
opts = Options.new(command_line)
47+
assert_true(opts.error_recovery)
4148
end
4249

4350
def test_single_mode

test/test_pmd_report_builder.rb

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_build_skip
1414
record_expecations_after_build
1515

1616
PmdTester::PmdReportBuilder
17-
.new(options.base_config, projects, options.local_git_repo, options.base_branch)
17+
.new(projects, options, options.base_config, options.base_branch)
1818
.build
1919
end
2020

@@ -33,7 +33,7 @@ def test_build_normal
3333
record_expecations_after_build
3434

3535
PmdTester::PmdReportBuilder
36-
.new(options.base_config, projects, options.local_git_repo, options.base_branch)
36+
.new(projects, options, options.base_config, options.base_branch)
3737
.build
3838
end
3939

@@ -52,24 +52,45 @@ def test_build_with_projects
5252
record_expectations_project_build
5353

5454
PmdTester::PmdReportBuilder
55-
.new(options.base_config, projects, options.local_git_repo, options.base_branch)
55+
.new(projects, options, options.base_config, options.base_branch)
5656
.build
5757

5858
expected = File.read('test/resources/pmd_report_builder/expected-config.xml')
5959
actual = File.read('target/reports/master/checkstyle/config.xml')
6060
assert_equal(expected, actual)
6161
end
6262

63+
def test_build_error_recovery
64+
project_list = 'test/resources/pmd_report_builder/project-list.xml'
65+
projects = PmdTester::ProjectsParser.new.parse(project_list)
66+
assert_equal(1, projects.size)
67+
argv = %w[-r target/repositories/pmd -b master -p pmd_releases/6.1.0
68+
-c config/design.xml --debug --error-recovery -l]
69+
argv.push project_list
70+
options = PmdTester::Options.new(argv)
71+
72+
projects[0].auxclasspath = '-auxclasspath extra:dirs'
73+
record_expectations('sha1abc', 'sha1abc', true)
74+
record_expecations_after_build
75+
record_expectations_project_build(true)
76+
77+
PmdTester::PmdReportBuilder
78+
.new(projects, options, options.base_config, options.base_branch)
79+
.build
80+
end
81+
6382
private
6483

65-
def record_expectations_project_build
84+
def record_expectations_project_build(error = false)
6685
PmdTester::ProjectBuilder.any_instance.stubs(:clone_projects).once
6786
PmdTester::ProjectBuilder.any_instance.stubs(:build_projects).once
6887
PmdTester::SimpleProgressLogger.any_instance.stubs(:start).once
6988
PmdTester::SimpleProgressLogger.any_instance.stubs(:stop).once
7089
File.stubs(:exist?).with('target/reports/master/checkstyle/pmd_report.xml').returns(false).once
90+
error_prefix = error ? 'PMD_JAVA_OPTS="-Dpmd.error_recovery -ea" ' : ''
7191
PmdTester::Cmd.stubs(:execute)
72-
.with('target/pmd-bin-6.10.0-SNAPSHOT/bin/run.sh ' \
92+
.with("#{error_prefix}" \
93+
'target/pmd-bin-6.10.0-SNAPSHOT/bin/run.sh ' \
7394
'pmd -d target/repositories/checkstyle -f xml ' \
7495
'-R target/reports/master/checkstyle/config.xml ' \
7596
'-r target/reports/master/checkstyle/pmd_report.xml ' \

test/test_runner.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ def test_single_mode
3434
def test_single_mode_multithreading
3535
report_builder_mock = mock
3636
PmdReportBuilder.stubs(:new)
37-
.with(anything, anything, anything, anything, 4)
37+
.with do |_, options, _, _|
38+
options.threads == 4
39+
end
3840
.returns(report_builder_mock)
3941
.once
4042
report_builder_mock.stubs(:build).returns(PmdBranchDetail.new('test_branch')).once
@@ -62,7 +64,9 @@ def test_local_mode
6264
def test_local_mode_multithreading
6365
report_builder_mock = mock
6466
PmdReportBuilder.stubs(:new)
65-
.with(anything, anything, anything, anything, 4)
67+
.with do |_, options, _, _|
68+
options.threads == 5
69+
end
6670
.returns(report_builder_mock)
6771
.twice
6872
report_builder_mock.stubs(:build).twice
@@ -71,7 +75,7 @@ def test_local_mode_multithreading
7175
SummaryReportBuilder.any_instance.stubs(:build).once
7276

7377
argv = %w[-r target/repositories/pmd -b master -bc config/design.xml -p pmd_releases/6.1.0
74-
-pc config/design.xml -l test/resources/project-test.xml -t 4]
78+
-pc config/design.xml -l test/resources/project-test.xml -t 5]
7579
run_runner(argv)
7680
end
7781

@@ -101,13 +105,15 @@ def test_online_mode_multithreading
101105

102106
report_builder_mock = mock
103107
PmdReportBuilder.stubs(:new)
104-
.with(anything, anything, anything, anything, 4)
108+
.with do |_, options, _, _|
109+
options.threads == 3
110+
end
105111
.returns(report_builder_mock)
106112
.once
107113
report_builder_mock.stubs(:build).once
108114
SummaryReportBuilder.any_instance.stubs(:build).once
109115

110-
argv = %w[-r target/repositories/pmd -m online -b master -p pmd_releases/6.7.0 -t 4]
116+
argv = %w[-r target/repositories/pmd -m online -b master -p pmd_releases/6.7.0 -t 3]
111117
run_runner(argv)
112118
end
113119
end

0 commit comments

Comments
 (0)