Skip to content

Commit af31d46

Browse files
committed
Merge pull request #80 from adangel:reuse-pmd-builds
Cache and reuse pmd builds #80
2 parents ab54e34 + 52b58c9 commit af31d46

File tree

9 files changed

+179
-80
lines changed

9 files changed

+179
-80
lines changed

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ puts summary
2727
* [#75](https://github.com/pmd/pmd-regression-tester/pull/75): Add new option "--error-recovery"
2828
* [#76](https://github.com/pmd/pmd-regression-tester/pull/76): Speedup XML parsing
2929
* [#79](https://github.com/pmd/pmd-regression-tester/pull/79): Add new configuration option "--baseline-download-url"
30+
* [#80](https://github.com/pmd/pmd-regression-tester/pull/80): Cache and reuse pmd builds
3031

3132
## External Contributions
3233

README.rdoc

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,21 @@ The tool creates the following folders:
6565
│ ├── PROJECT_NAME_1
6666
│ ├── ......
6767
│ └── PROJECT_NAME_n
68-
└── reports
69-
├── BASE_BRANCH_NAME <- the base baseline is placed here
70-
├── PATCH_BRANCH_NAME <- the patch baseline is placed here
71-
└── diff
72-
├── index.xml <- the summary report of diff reports
73-
├── base_config.xml <- the resources of the summary report
74-
├── patch_config.xml <- the resources fo the summary report
75-
├── css <- css reources are placed here
76-
├── PROJECT_NAME_1
77-
└── index.xml <- the diff report of PROJECT_1
78-
├── .......
79-
└── PROJECT_NAME_n
80-
└── index.xml <- the diff report of PROJECT_N
68+
├── reports
69+
│ ├── BASE_BRANCH_NAME <- the base baseline is placed here
70+
│ ├── PATCH_BRANCH_NAME <- the patch baseline is placed here
71+
│ └── diff
72+
│ ├── index.xml <- the summary report of diff reports
73+
│ ├── base_config.xml <- the resources of the summary report
74+
│ ├── patch_config.xml <- the resources fo the summary report
75+
│ ├── css <- css reources are placed here
76+
│ ├── PROJECT_NAME_1
77+
│ │ └── index.xml <- the diff report of PROJECT_1
78+
│ ├── .......
79+
│ └── PROJECT_NAME_n
80+
│ └── index.xml <- the diff report of PROJECT_N
81+
├── pmd-bin-<version>-<branch_name>-<sha1> <- cached pmd builds that are reused
82+
└── ....
8183

8284
==== The baseline format
8385
branch_name

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ hoe = Hoe.spec 'pmdtester' do
1919

2020
self.clean_globs = %w[target/reports/**/* target/test/**/* target/dynamic-config.xml Gemfile.lock]
2121
self.extra_deps += [['nokogiri', '~> 1.8'], ['slop', '~> 4.6'], ['differ', '~> 0.1'],
22-
['rufus-scheduler', '~> 3.5']]
22+
['rufus-scheduler', '~> 3.5'], ['logger-colors', '~> 1.0']]
2323
self.extra_dev_deps += [
2424
['hoe-bundler', '~> 1.5'],
2525
['hoe-git', '~> 1.6'],

lib/pmdtester.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'logger'
4+
require 'logger/colors'
45

56
require_relative 'pmdtester/cmd'
67
require_relative 'pmdtester/pmd_branch_detail'

lib/pmdtester/builders/pmd_report_builder.rb

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,52 @@ def initialize(projects, options, branch_config, branch_name)
2424
def get_pmd_binary_file
2525
logger.info "#{@pmd_branch_name}: Start packaging PMD"
2626
Dir.chdir(@local_git_repo) do
27-
current_head_sha = Cmd.execute('git rev-parse HEAD')
28-
current_branch_sha = Cmd.execute("git rev-parse #{@pmd_branch_name}")
27+
build_branch_sha = Cmd.execute("git rev-parse #{@pmd_branch_name}^{commit}")
2928

30-
@pmd_version = determine_pmd_version
29+
checkout_build_branch # needs a clean working tree, otherwise fails
3130

32-
# in case we are already on the correct branch
33-
# and a binary zip already exists...
34-
if current_head_sha == current_branch_sha &&
35-
File.exist?("pmd-dist/target/pmd-bin-#{@pmd_version}.zip")
36-
logger.warn "#{@pmd_branch_name}: Skipping packaging - zip for " \
37-
"#{@pmd_version} already exists"
31+
raise "Wrong branch #{get_last_commit_sha}" unless build_branch_sha == get_last_commit_sha
32+
33+
logger.debug "#{@pmd_branch_name}: PMD Version is #{@pmd_version} " \
34+
"(sha=#{build_branch_sha})"
35+
distro_path = saved_distro_path(build_branch_sha)
36+
if File.directory?(distro_path)
37+
logger.info "#{@pmd_branch_name}: Skipping packaging - saved version exists " \
38+
" in #{distro_path}"
3839
else
39-
build_pmd
40+
build_pmd(into_dir: distro_path)
4041
end
4142

42-
@pmd_branch_details.branch_last_sha = get_last_commit_sha
43+
# we're still on the build branch
44+
@pmd_branch_details.branch_last_sha = build_branch_sha
4345
@pmd_branch_details.branch_last_message = get_last_commit_message
44-
45-
logger.info "#{@pmd_branch_name}: Extracting the zip"
46-
unzip_cmd = "unzip -qo pmd-dist/target/pmd-bin-#{@pmd_version}.zip -d #{@pwd}/target"
47-
Cmd.execute(unzip_cmd)
4846
end
4947
logger.info "#{@pmd_branch_name}: Packaging PMD completed"
5048
end
5149

52-
def build_pmd
53-
logger.info "#{@pmd_branch_name}: Checking out the branch"
54-
checkout_cmd = "git checkout #{@pmd_branch_name}"
55-
Cmd.execute(checkout_cmd)
56-
57-
# determine the version again - it might be different in the other branch
58-
@pmd_version = determine_pmd_version
50+
# builds pmd on currently checked out branch
51+
def build_pmd(into_dir:)
52+
# in CI there might have been a build performed already. In that case
53+
# we reuse the build result, otherwise we build PMD freshly
54+
pmd_dist_target = "pmd-dist/target/pmd-bin-#{@pmd_version}.zip"
55+
if File.exist?("#{@local_git_repo}/#{pmd_dist_target}")
56+
# that's a warning, because we don't know, whether this build really
57+
# belongs to the current branch or whether it's from a previous branch.
58+
# In CI, that's not a problem, because the workspace is always fresh.
59+
logger.warn "#{@pmd_branch_name}: Reusing already existing #{pmd_dist_target}"
60+
else
61+
logger.info "#{@pmd_branch_name}: Building PMD #{@pmd_version}..."
62+
package_cmd = './mvnw clean package' \
63+
' -Dmaven.test.skip=true' \
64+
' -Dmaven.javadoc.skip=true' \
65+
' -Dmaven.source.skip=true' \
66+
' -Dcheckstyle.skip=true'
67+
Cmd.execute(package_cmd)
68+
end
5969

60-
logger.info "#{@pmd_branch_name}: Building PMD #{@pmd_version}..."
61-
package_cmd = './mvnw clean package' \
62-
' -Dmaven.test.skip=true' \
63-
' -Dmaven.javadoc.skip=true' \
64-
' -Dmaven.source.skip=true' \
65-
' -Dcheckstyle.skip=true'
66-
Cmd.execute(package_cmd)
70+
logger.info "#{@pmd_branch_name}: Extracting the zip"
71+
Cmd.execute("unzip -qo #{pmd_dist_target} -d pmd-dist/target/exploded")
72+
Cmd.execute("mv pmd-dist/target/exploded/pmd-bin-#{@pmd_version} #{into_dir}")
6773
end
6874

6975
def determine_pmd_version
@@ -73,7 +79,7 @@ def determine_pmd_version
7379
end
7480

7581
def get_last_commit_sha
76-
get_last_commit_sha_cmd = 'git rev-parse HEAD'
82+
get_last_commit_sha_cmd = 'git rev-parse HEAD^{commit}'
7783
Cmd.execute(get_last_commit_sha_cmd)
7884
end
7985

@@ -84,7 +90,7 @@ def get_last_commit_message
8490

8591
def generate_pmd_report(project)
8692
error_recovery_options = @error_recovery ? 'PMD_JAVA_OPTS="-Dpmd.error_recovery -ea" ' : ''
87-
run_path = "target/pmd-bin-#{@pmd_version}/bin/run.sh"
93+
run_path = "#{saved_distro_path(@pmd_branch_details.branch_last_sha)}/bin/run.sh"
8894
pmd_cmd = "#{error_recovery_options}" \
8995
"#{run_path} pmd -d #{project.local_source_path} -f xml " \
9096
"-R #{project.get_config_path(@pmd_branch_name)} " \
@@ -149,5 +155,39 @@ def build
149155
get_pmd_binary_file
150156
generate_pmd_reports
151157
end
158+
159+
private
160+
161+
def checkout_build_branch
162+
logger.info "#{@pmd_branch_name}: Checking out the branch"
163+
# note that this would fail if the tree is dirty
164+
Cmd.execute("git checkout #{@pmd_branch_name}")
165+
166+
# determine the version
167+
@pmd_version = determine_pmd_version
168+
169+
return unless wd_has_dirty_git_changes
170+
171+
# working dir is dirty....
172+
# we don't allow this because we need the SHA to address the zip file
173+
logger.error "#{@pmd_branch_name}: Won\'t build without a clean working tree, " \
174+
'commit your changes'
175+
end
176+
177+
def work_dir
178+
"#{@pwd}/target"
179+
end
180+
181+
# path to the unzipped distribution
182+
# e.g. <cwd>/pmd-bin-<version>-<branch>-<sha>
183+
def saved_distro_path(build_sha)
184+
"#{work_dir}/pmd-bin-#{@pmd_version}" \
185+
"-#{PmdBranchDetail.branch_filename(@pmd_branch_name)}" \
186+
"-#{build_sha}"
187+
end
188+
189+
def wd_has_dirty_git_changes
190+
!Cmd.execute('git status --porcelain').empty?
191+
end
152192
end
153193
end

pmdtester.gemspec

Lines changed: 3 additions & 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-11-25"
14+
s.date = "2020-11-28"
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]
@@ -33,6 +33,7 @@ Gem::Specification.new do |s|
3333
s.add_runtime_dependency(%q<slop>.freeze, ["~> 4.6"])
3434
s.add_runtime_dependency(%q<differ>.freeze, ["~> 0.1"])
3535
s.add_runtime_dependency(%q<rufus-scheduler>.freeze, ["~> 3.5"])
36+
s.add_runtime_dependency(%q<logger-colors>.freeze, ["~> 1.0"])
3637
s.add_development_dependency(%q<hoe-bundler>.freeze, ["~> 1.5"])
3738
s.add_development_dependency(%q<hoe-git>.freeze, ["~> 1.6"])
3839
s.add_development_dependency(%q<minitest>.freeze, ["~> 5.10"])
@@ -46,6 +47,7 @@ Gem::Specification.new do |s|
4647
s.add_dependency(%q<slop>.freeze, ["~> 4.6"])
4748
s.add_dependency(%q<differ>.freeze, ["~> 0.1"])
4849
s.add_dependency(%q<rufus-scheduler>.freeze, ["~> 3.5"])
50+
s.add_dependency(%q<logger-colors>.freeze, ["~> 1.0"])
4951
s.add_dependency(%q<hoe-bundler>.freeze, ["~> 1.5"])
5052
s.add_dependency(%q<hoe-git>.freeze, ["~> 1.6"])
5153
s.add_dependency(%q<minitest>.freeze, ["~> 5.10"])

test/integration_test_runner.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ def test_single_mode_with_html_flag_option
5656
end
5757

5858
def test_online_mode
59-
# This test depends on the file test_branch_2-baseline.zip being available on sourceforge:
60-
# https://sourceforge.net/projects/pmd/files/pmd-regression-tester/test_branch_2-baseline.zip/download
59+
# This test depends on the file test_branch_2-baseline.zip being available at:
60+
# https://pmd-code.org/pmd-regression-tester/test_branch_2-baseline.zip
6161
base_branch = 'test_branch_2'
62-
argv = "-r target/repositories/pmd -m online -b #{base_branch} -p pmd_releases/6.7.0"
62+
argv = "-r target/repositories/pmd -m online -b #{base_branch} -p pmd_releases/6.7.0 " \
63+
'--baseline-download-url https://pmd-code.org/pmd-regression-tester/'
6364

6465
system("bundle exec bin/pmdtester #{argv}")
6566

@@ -76,15 +77,16 @@ def test_online_mode
7677
end
7778

7879
def test_online_mode_different_project_list_and_config
79-
# This test depends on the file pmd_releases_6.6.0-baseline.zip being available on sourceforge:
80-
# https://sourceforge.net/projects/pmd/files/pmd-regression-tester/pmd_releases_6.6.0-baseline.zip/download
80+
# This test depends on the file pmd_releases_6.6.0-baseline.zip being available at:
81+
# https://pmd-code.org/pmd-regression-tester/pmd_releases_6.6.0-baseline.zip
8182
argv = '--local-git-repo target/repositories/pmd '\
8283
'--mode online '\
8384
'--base-branch pmd_releases/6.6.0 '\
8485
'--patch-branch pmd_releases/6.7.0 '\
8586
'--patch-config test/resources/integration_test_runner/patch-config.xml '\
8687
'--list-of-project test/resources/integration_test_runner/project-list.xml '\
87-
'--auto-gen-config'
88+
'--auto-gen-config ' \
89+
'--baseline-download-url https://pmd-code.org/pmd-regression-tester/'
8890

8991
system("bundle exec bin/pmdtester #{argv}")
9092

test/resources/project-test.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ xsi:noNamespaceSchemaLocation="projectlist_1_1_0.xsd">
88
<name>checkstyle</name>
99
<type>git</type>
1010
<connection>https://github.com/checkstyle/checkstyle</connection>
11-
<tag>checkstyle-8.0</tag>
11+
<tag>checkstyle-8.10</tag>
1212
<exclude-pattern>.*/target/test-classes/com/puppycrawl/tools/checkstyle/.*</exclude-pattern>
1313
<exclude-pattern>.*/target/generated-sources/.*</exclude-pattern>
1414

@@ -17,7 +17,9 @@ if test -e classpath.txt; then
1717
exit
1818
fi
1919
20-
mvn test-compile
20+
set -e
21+
22+
mvn clean test-compile
2123
mvn dependency:build-classpath -DincludeScope=test -Dmdep.outputFile=classpath.txt
2224
]]></build-command>
2325
<auxclasspath-command>echo -n "$(pwd)/target/classes:$(pwd)/target/test-classes:"; cat classpath.txt</auxclasspath-command>

0 commit comments

Comments
 (0)