Skip to content

Commit 3b26e7f

Browse files
committed
Merge pull request #114 from adangel:update-new-pmd7-cli
Support new PMD 7 CLI interface #114
2 parents a69aa25 + dd4ab5c commit 3b26e7f

File tree

6 files changed

+59
-26
lines changed

6 files changed

+59
-26
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ Metrics/BlockLength:
3030

3131
Metrics/ModuleLength:
3232
CountAsOne: ['array', 'hash']
33+
34+
Metrics/ParameterLists:
35+
CountKeywordArgs: false

History.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
## Enhancements
66

7+
* [#114](https://github.com/pmd/pmd-regression-tester/pull/114): Support new PMD 7 CLI interface
8+
79
## Fixed Issues
810

911
## External Contributions

lib/pmdtester/builders/pmd_report_builder.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,10 @@ def get_last_commit_message
9696

9797
def generate_pmd_report(project)
9898
error_recovery_options = @error_recovery ? 'PMD_JAVA_OPTS="-Dpmd.error_recovery -ea" ' : ''
99-
run_path = "#{saved_distro_path(@pmd_branch_details.branch_last_sha)}/bin/run.sh"
100-
fail_on_violation = should_use_long_cli_options ? '--fail-on-violation false' : '-failOnViolation false'
99+
fail_on_violation = should_use_long_cli_options? ? '--fail-on-violation false' : '-failOnViolation false'
101100
auxclasspath_option = create_auxclasspath_option(project)
102101
pmd_cmd = "#{error_recovery_options}" \
103-
"#{run_path} pmd -d #{project.local_source_path} -f xml " \
102+
"#{determine_run_path} -d #{project.local_source_path} -f xml " \
104103
"-R #{project.get_config_path(@pmd_branch_name)} " \
105104
"-r #{project.get_pmd_report_path(@pmd_branch_name)} " \
106105
"#{fail_on_violation} -t #{@threads} " \
@@ -149,7 +148,7 @@ def generate_pmd_reports
149148

150149
PmdReportDetail.create(execution_time: execution_time, timestamp: end_time,
151150
exit_code: exit_code, report_info_path: project.get_report_info_path(@pmd_branch_name))
152-
logger.info "#{project.name}'s PMD report was generated successfully"
151+
logger.info "#{project.name}'s PMD report was generated successfully (exit code: #{exit_code})"
153152
end
154153

155154
@pmd_branch_details.execution_time = sum_time
@@ -200,15 +199,15 @@ def wd_has_dirty_git_changes
200199
!Cmd.execute_successfully('git status --porcelain').empty?
201200
end
202201

203-
def should_use_long_cli_options
202+
def should_use_long_cli_options?
204203
logger.debug "PMD Version: #{@pmd_version}"
205204
Semver.compare(@pmd_version, '6.41.0') >= 0
206205
end
207206

208207
def create_auxclasspath_option(project)
209208
auxclasspath_option = ''
210209
unless project.auxclasspath.empty?
211-
auxclasspath_option = should_use_long_cli_options ? '--aux-classpath ' : '-auxclasspath '
210+
auxclasspath_option = should_use_long_cli_options? ? '--aux-classpath ' : '-auxclasspath '
212211
auxclasspath_option += project.auxclasspath
213212
end
214213
auxclasspath_option
@@ -217,5 +216,16 @@ def create_auxclasspath_option(project)
217216
def pmd7?
218217
Semver.compare(@pmd_version, '7.0.0-SNAPSHOT') >= 0
219218
end
219+
220+
def determine_run_path
221+
run_path = "#{saved_distro_path(@pmd_branch_details.branch_last_sha)}/bin"
222+
run_path = if File.exist?("#{run_path}/pmd")
223+
# New PMD 7 CLI script (pmd/pmd#4059)
224+
"#{run_path}/pmd check"
225+
else
226+
"#{run_path}/run.sh pmd"
227+
end
228+
run_path
229+
end
220230
end
221231
end

lib/pmdtester/pmd_violation.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ class PmdViolation
3030
attr_reader :fname, :info_url, :line, :old_line, :old_message, :rule_name, :ruleset_name, :language
3131
attr_accessor :message
3232

33-
# rubocop:disable Metrics/ParameterLists
34-
# Disable it: how is replacing a long parameter list with a single hash helping?
3533
def initialize(branch:, fname:, info_url:, bline:, rule_name:, ruleset_name:)
3634
@branch = branch
3735
@fname = fname
@@ -49,7 +47,6 @@ def initialize(branch:, fname:, info_url:, bline:, rule_name:, ruleset_name:)
4947
@old_message = nil
5048
@old_line = nil
5149
end
52-
# rubocop:enable Metrics/ParameterLists
5350

5451
def line_move?(other)
5552
message.eql?(other.message) && (line - other.line).abs <= 5

pmdtester.gemspec

Lines changed: 3 additions & 3 deletions
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, "Cl\u00E9ment Fournier".freeze]
14-
s.date = "2022-05-12"
14+
s.date = "2022-10-16"
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, "[email protected]".freeze]
1717
s.executables = ["pmdtester".freeze]
@@ -42,7 +42,7 @@ Gem::Specification.new do |s|
4242
s.add_development_dependency(%q<rubocop>.freeze, ["~> 0.93"])
4343
s.add_development_dependency(%q<test-unit>.freeze, ["~> 3.5"])
4444
s.add_development_dependency(%q<rdoc>.freeze, ["~> 6.4"])
45-
s.add_development_dependency(%q<hoe>.freeze, ["~> 3.23"])
45+
s.add_development_dependency(%q<hoe>.freeze, ["~> 3.25"])
4646
else
4747
s.add_dependency(%q<nokogiri>.freeze, ["~> 1.13"])
4848
s.add_dependency(%q<slop>.freeze, ["~> 4.6"])
@@ -57,7 +57,7 @@ Gem::Specification.new do |s|
5757
s.add_dependency(%q<rubocop>.freeze, ["~> 0.93"])
5858
s.add_dependency(%q<test-unit>.freeze, ["~> 3.5"])
5959
s.add_dependency(%q<rdoc>.freeze, ["~> 6.4"])
60-
s.add_dependency(%q<hoe>.freeze, ["~> 3.23"])
60+
s.add_dependency(%q<hoe>.freeze, ["~> 3.25"])
6161
end
6262
end
6363

test/test_pmd_report_builder.rb

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def test_build_with_projects
9797
projects[0].auxclasspath = 'extra:dirs'
9898
record_expectations('sha1abc', 'sha1abc', true)
9999
record_expectations_after_build
100-
record_expectations_project_build('sha1abc')
100+
record_expectations_project_build(sha1: 'sha1abc')
101101

102102
PmdTester::PmdReportBuilder
103103
.new(projects, options, options.base_config, options.base_branch)
@@ -120,7 +120,7 @@ def test_build_error_recovery
120120
projects[0].auxclasspath = 'extra:dirs'
121121
record_expectations('sha1abc', 'sha1abc', true)
122122
record_expectations_after_build
123-
record_expectations_project_build('sha1abc', true)
123+
record_expectations_project_build(sha1: 'sha1abc', error: true)
124124

125125
PmdTester::PmdReportBuilder
126126
.new(projects, options, options.base_config, options.base_branch)
@@ -140,7 +140,7 @@ def test_build_long_cli_options
140140
projects[0].auxclasspath = 'extra:dirs'
141141
record_expectations('sha1abc', 'sha1abc', true)
142142
record_expectations_after_build
143-
record_expectations_project_build('sha1abc', true, true)
143+
record_expectations_project_build(sha1: 'sha1abc', error: true, long_cli_options: true)
144144

145145
PmdTester::PmdReportBuilder
146146
.new(projects, options, options.base_config, options.base_branch)
@@ -149,6 +149,7 @@ def test_build_long_cli_options
149149

150150
def test_build_pmd7
151151
@pmd_version = '7.0.0-SNAPSHOT'
152+
sha1 = 'sha1abc'
152153
project_list = 'test/resources/pmd_report_builder/project-list.xml'
153154
projects = PmdTester::ProjectsParser.new.parse(project_list)
154155
assert_equal(1, projects.size)
@@ -158,13 +159,19 @@ def test_build_pmd7
158159
options = PmdTester::Options.new(argv)
159160

160161
projects[0].auxclasspath = 'extra:dirs'
161-
record_expectations('sha1abc', 'sha1abc', true)
162+
record_expectations(sha1, sha1, true)
162163
record_expectations_after_build
163-
record_expectations_project_build('sha1abc', true, true, true)
164-
165-
PmdTester::PmdReportBuilder
166-
.new(projects, options, options.base_config, options.base_branch)
167-
.build
164+
record_expectations_project_build(sha1: sha1, error: true, long_cli_options: true,
165+
no_progress_bar: true, base_cmd: 'pmd check')
166+
167+
pmd_cli_cmd = prepare_pmd_dist_dir(version: @pmd_version, sha1: sha1)
168+
begin
169+
PmdTester::PmdReportBuilder
170+
.new(projects, options, options.base_config, options.base_branch)
171+
.build
172+
ensure
173+
cleanup_pmd_dist_dir(base_dir: pmd_cli_cmd)
174+
end
168175
end
169176

170177
#
@@ -183,7 +190,7 @@ def test_build_failing
183190
projects[0].auxclasspath = 'extra:dirs'
184191
record_expectations('sha1abc', 'sha1abc', true)
185192
record_expectations_after_build
186-
record_expectations_project_build('sha1abc', true, false, false, 1)
193+
record_expectations_project_build(sha1: 'sha1abc', error: true, exit_status: 1)
187194

188195
PmdTester::PmdReportBuilder
189196
.new(projects, options, options.base_config, options.base_branch)
@@ -192,8 +199,8 @@ def test_build_failing
192199

193200
private
194201

195-
def record_expectations_project_build(sha1, error = false, long_cli_options = false,
196-
no_progress_bar = false, exit_status = 0)
202+
def record_expectations_project_build(sha1:, error: false, long_cli_options: false,
203+
no_progress_bar: false, exit_status: 0, base_cmd: 'run.sh pmd')
197204
PmdTester::ProjectBuilder.any_instance.stubs(:clone_projects).once
198205
PmdTester::ProjectBuilder.any_instance.stubs(:build_projects).once
199206
PmdTester::SimpleProgressLogger.any_instance.stubs(:start).once
@@ -204,8 +211,8 @@ def record_expectations_project_build(sha1, error = false, long_cli_options = fa
204211
process_status.expects(:exitstatus).returns(exit_status).once
205212
PmdTester::Cmd.stubs(:execute)
206213
.with("#{error_prefix}" \
207-
"#{distro_path}/bin/run.sh " \
208-
'pmd -d target/repositories/checkstyle -f xml ' \
214+
"#{distro_path}/bin/#{base_cmd} " \
215+
'-d target/repositories/checkstyle -f xml ' \
209216
'-R target/reports/master/checkstyle/config.xml ' \
210217
'-r target/reports/master/checkstyle/pmd_report.xml ' \
211218
"#{long_cli_options ? '--fail-on-violation false' : '-failOnViolation false'} -t 1 " \
@@ -244,4 +251,18 @@ def record_expectations_after_build
244251
PmdTester::PmdBranchDetail.any_instance.stubs(:save).once
245252
FileUtils.stubs(:cp).with('config/design.xml', 'target/reports/master/config.xml').once
246253
end
254+
255+
# Creates a fake pmd script file as .../bin/pmd.
256+
# This is used in the new PMD 7 CLI interface
257+
def prepare_pmd_dist_dir(version:, sha1:)
258+
pmd_cli_cmd = "#{Dir.getwd}/target/pmd-bin-#{version}-master-#{sha1}/bin"
259+
FileUtils.mkdir_p(pmd_cli_cmd)
260+
File.new("#{pmd_cli_cmd}/pmd", 'w')
261+
pmd_cli_cmd
262+
end
263+
264+
def cleanup_pmd_dist_dir(base_dir:)
265+
File.unlink("#{base_dir}/pmd")
266+
Dir.rmdir(base_dir)
267+
end
247268
end

0 commit comments

Comments
 (0)