Skip to content

Commit 49a6ceb

Browse files
committed
Merge pull request #112 from adangel:exitcode
Make stdout/stderr and exit code available #112
2 parents f3f9308 + 427b6cb commit 49a6ceb

31 files changed

+251
-83
lines changed

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## Enhancements
66

77
* [#108](https://github.com/pmd/pmd-regression-tester/issues/108): Disable progress bar for PMD 7
8+
* [#109](https://github.com/pmd/pmd-regression-tester/issues/109): Make stdout/stderr and exit code available
89

910
## Fixed Issues
1011

lib/pmdtester/builders/liquid_renderer.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ def write_project_index(project, root)
6060
# copy original pmd reports
6161
copy_file("#{root}/base_pmd_report.xml", project.report_diff.base_report.file)
6262
copy_file("#{root}/patch_pmd_report.xml", project.report_diff.patch_report.file)
63+
# copy stdout and stderr outputs
64+
copy_file("#{root}/base_stdout.txt", "#{project.report_diff.base_report.report_folder}/stdout.txt")
65+
copy_file("#{root}/base_stderr.txt", "#{project.report_diff.base_report.report_folder}/stderr.txt")
66+
copy_file("#{root}/patch_stdout.txt", "#{project.report_diff.patch_report.report_folder}/stdout.txt")
67+
copy_file("#{root}/patch_stderr.txt", "#{project.report_diff.patch_report.report_folder}/stderr.txt")
6368
# render full pmd reports
6469
write_file("#{root}/base_pmd_report.html",
6570
render_liquid('project_pmd_report.html', pmd_report_liquid_env(project, BASE)))
@@ -120,6 +125,7 @@ def report_to_h(project, report)
120125

121126
'execution_time' => PmdReportDetail.convert_seconds(report.exec_time),
122127
'timestamp' => report.timestamp,
128+
'exit_code' => report.exit_code,
123129

124130
'rules' => report.rule_summaries,
125131
'errors' => report.errors_by_file.all_values.map { |e| error_to_hash(e, project) },

lib/pmdtester/builders/pmd_report_builder.rb

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def initialize(projects, options, branch_config, branch_name)
2525
def get_pmd_binary_file
2626
logger.info "#{@pmd_branch_name}: Start packaging PMD"
2727
Dir.chdir(@local_git_repo) do
28-
build_branch_sha = Cmd.execute("git rev-parse #{@pmd_branch_name}^{commit}")
28+
build_branch_sha = Cmd.execute_successfully("git rev-parse #{@pmd_branch_name}^{commit}")
2929

3030
checkout_build_branch # needs a clean working tree, otherwise fails
3131

@@ -70,28 +70,28 @@ def build_pmd(into_dir:)
7070
' -Dcheckstyle.skip=true' \
7171
' -Dpmd.skip=true' \
7272
' -T1C -B'
73-
Cmd.execute(package_cmd)
73+
Cmd.execute_successfully(package_cmd)
7474
end
7575

7676
logger.info "#{@pmd_branch_name}: Extracting the zip"
77-
Cmd.execute("unzip -qo #{pmd_dist_target} -d pmd-dist/target/exploded")
78-
Cmd.execute("mv pmd-dist/target/exploded/pmd-bin-#{@pmd_version} #{into_dir}")
77+
Cmd.execute_successfully("unzip -qo #{pmd_dist_target} -d pmd-dist/target/exploded")
78+
Cmd.execute_successfully("mv pmd-dist/target/exploded/pmd-bin-#{@pmd_version} #{into_dir}")
7979
end
8080

8181
def determine_pmd_version
8282
version_cmd = "./mvnw -q -Dexec.executable=\"echo\" -Dexec.args='${project.version}' " \
8383
'--non-recursive org.codehaus.mojo:exec-maven-plugin:1.5.0:exec'
84-
Cmd.execute(version_cmd)
84+
Cmd.execute_successfully(version_cmd)
8585
end
8686

8787
def get_last_commit_sha
8888
get_last_commit_sha_cmd = 'git rev-parse HEAD^{commit}'
89-
Cmd.execute(get_last_commit_sha_cmd)
89+
Cmd.execute_successfully(get_last_commit_sha_cmd)
9090
end
9191

9292
def get_last_commit_message
9393
get_last_commit_message_cmd = 'git log -1 --pretty=%B'
94-
Cmd.execute(get_last_commit_message_cmd)
94+
Cmd.execute_successfully(get_last_commit_message_cmd)
9595
end
9696

9797
def generate_pmd_report(project)
@@ -107,14 +107,16 @@ def generate_pmd_report(project)
107107
"#{auxclasspath_option}" \
108108
"#{pmd7? ? ' --no-progress' : ''}"
109109
start_time = Time.now
110+
exit_code = nil
110111
if File.exist?(project.get_pmd_report_path(@pmd_branch_name))
111112
logger.warn "#{@pmd_branch_name}: Skipping PMD run - report " \
112113
"#{project.get_pmd_report_path(@pmd_branch_name)} already exists"
113114
else
114-
Cmd.execute(pmd_cmd)
115+
status = Cmd.execute(pmd_cmd, project.get_project_target_dir(@pmd_branch_name))
116+
exit_code = status.exitstatus
115117
end
116118
end_time = Time.now
117-
[end_time - start_time, end_time]
119+
[end_time - start_time, end_time, exit_code]
118120
end
119121

120122
def generate_config_for(project)
@@ -141,12 +143,12 @@ def generate_pmd_reports
141143
progress_logger = SimpleProgressLogger.new("generating #{project.name}'s PMD report")
142144
progress_logger.start
143145
generate_config_for(project)
144-
execution_time, end_time = generate_pmd_report(project)
146+
execution_time, end_time, exit_code = generate_pmd_report(project)
145147
progress_logger.stop
146148
sum_time += execution_time
147149

148-
report_details = PmdReportDetail.new(execution_time: execution_time, timestamp: end_time)
149-
report_details.save(project.get_report_info_path(@pmd_branch_name))
150+
PmdReportDetail.create(execution_time: execution_time, timestamp: end_time,
151+
exit_code: exit_code, report_info_path: project.get_report_info_path(@pmd_branch_name))
150152
logger.info "#{project.name}'s PMD report was generated successfully"
151153
end
152154

@@ -169,7 +171,7 @@ def build
169171
def checkout_build_branch
170172
logger.info "#{@pmd_branch_name}: Checking out the branch"
171173
# note that this would fail if the tree is dirty
172-
Cmd.execute("git checkout #{@pmd_branch_name}")
174+
Cmd.execute_successfully("git checkout #{@pmd_branch_name}")
173175

174176
# determine the version
175177
@pmd_version = determine_pmd_version
@@ -195,7 +197,7 @@ def saved_distro_path(build_sha)
195197
end
196198

197199
def wd_has_dirty_git_changes
198-
!Cmd.execute('git status --porcelain').empty?
200+
!Cmd.execute_successfully('git status --porcelain').empty?
199201
end
200202

201203
def should_use_long_cli_options

lib/pmdtester/builders/project_builder.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def clone_projects
3030
# once but may be used with several tags.
3131
clone_cmd = "git clone --no-single-branch --depth 1 #{project.connection} #{path}"
3232

33-
Cmd.execute(clone_cmd)
33+
Cmd.execute_successfully(clone_cmd)
3434
end
3535

3636
Dir.chdir(path) do
@@ -87,7 +87,7 @@ def run_as_script(path, command)
8787
if command.start_with?('#!')
8888
shell = command.lines[0].chomp[2..] # remove leading "#!"
8989
end
90-
stdout = Cmd.execute("#{shell} #{script.path}")
90+
stdout = Cmd.execute_successfully("#{shell} #{script.path}")
9191
ensure
9292
script.unlink
9393
end
@@ -99,7 +99,7 @@ def execute_reset_cmd(type, tag)
9999

100100
reset_cmd = "git checkout #{tag}; git reset --hard #{tag}"
101101

102-
Cmd.execute(reset_cmd)
102+
Cmd.execute_successfully(reset_cmd)
103103
end
104104
end
105105
end

lib/pmdtester/builders/project_hasher.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ def report_diff_to_h(rdiff)
2121
'base_timestamp' => rdiff.base_report.timestamp,
2222
'patch_timestamp' => rdiff.patch_report.timestamp,
2323

24+
'base_exit_code' => rdiff.base_report.exit_code,
25+
'patch_exit_code' => rdiff.patch_report.exit_code,
26+
2427
'rule_diffs' => rdiff.rule_summaries
2528
}
2629
end

lib/pmdtester/builders/rule_set_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def diff_filenames(languages)
176176

177177
# We only need to support git here, since PMD's repo is using git.
178178
diff_cmd = "git diff --name-only #{base}..#{patch} #{filepath_filter}"
179-
filenames = Cmd.execute(diff_cmd)
179+
filenames = Cmd.execute_successfully(diff_cmd)
180180
end
181181
filenames.split("\n")
182182
end

lib/pmdtester/cmd.rb

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,35 @@ module PmdTester
66
# Containing the common method for executing shell command
77
class Cmd
88
extend PmdTester
9-
def self.execute(cmd)
10-
stdout, _stderr, _status = internal_execute(cmd)
11-
stdout&.chomp!
9+
10+
#
11+
# Executes the given command and returns the process status.
12+
# stdout and stderr are written to the files "stdout.txt" and "stderr.txt"
13+
# in path.
14+
#
15+
def self.execute(cmd, path)
16+
stdout, stderr, status = internal_execute(cmd)
17+
18+
file = File.new("#{path}/stdout.txt", 'w')
19+
file.puts stdout
20+
file.close
21+
22+
file = File.new("#{path}/stderr.txt", 'w')
23+
file.puts stderr
24+
file.close
25+
26+
status
27+
end
28+
29+
def self.execute_successfully(cmd)
30+
stdout, stderr, status = internal_execute(cmd)
31+
32+
unless status.success?
33+
logger.error stdout
34+
logger.error stderr
35+
raise CmdException.new(cmd, stdout, stderr, status)
36+
end
37+
1238
stdout
1339
end
1440

@@ -22,14 +48,12 @@ def self.internal_execute(cmd)
2248

2349
stdout, stderr, status = Open3.capture3("#{cmd};")
2450

25-
logger.debug stdout
26-
unless status.success?
27-
logger.error stdout
28-
logger.error stderr
29-
raise CmdException.new(cmd, stderr)
30-
end
51+
logger.debug "status: #{status}"
52+
logger.debug "stdout: #{stdout}"
53+
logger.debug "stderr: #{stderr}"
3154

3255
stdout&.chomp!
56+
stderr&.chomp!
3357

3458
[stdout, stderr, status]
3559
end
@@ -40,15 +64,19 @@ def self.internal_execute(cmd)
4064
# The exception should be raised when the shell command failed.
4165
class CmdException < StandardError
4266
attr_reader :cmd
67+
attr_reader :stdout
4368
attr_reader :error
69+
attr_reader :status
4470
attr_reader :message
4571

4672
COMMON_MSG = 'An error occurred while executing the shell command'
4773

48-
def initialize(cmd, error)
74+
def initialize(cmd, stdout, error, status)
4975
@cmd = cmd
76+
@stdout = stdout
5077
@error = error
51-
@message = "#{COMMON_MSG} '#{cmd}'"
78+
@status = status
79+
@message = "#{COMMON_MSG} '#{cmd}' #{status}"
5280
end
5381
end
5482
end

lib/pmdtester/pmd_report_detail.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,22 @@ class PmdReportDetail
88
attr_accessor :execution_time
99
attr_accessor :timestamp
1010
attr_accessor :working_dir
11+
attr_accessor :exit_code
1112

12-
def initialize(execution_time: 0, timestamp: '', working_dir: Dir.getwd)
13+
def initialize(execution_time: 0, timestamp: '', working_dir: Dir.getwd, exit_code: nil)
1314
@execution_time = execution_time
1415
@timestamp = timestamp
1516
@working_dir = working_dir
17+
@exit_code = exit_code.nil? ? '?' : exit_code.to_s
1618
end
1719

1820
def save(report_info_path)
19-
hash = { execution_time: @execution_time, timestamp: @timestamp, working_dir: @working_dir }
21+
hash = {
22+
execution_time: @execution_time,
23+
timestamp: @timestamp,
24+
working_dir: @working_dir,
25+
exit_code: @exit_code
26+
}
2027
file = File.new(report_info_path, 'w')
2128
file.puts JSON.generate(hash)
2229
file.close
@@ -36,6 +43,13 @@ def format_execution_time
3643
self.class.convert_seconds(@execution_time)
3744
end
3845

46+
def self.create(execution_time: 0, timestamp: '', working_dir: Dir.getwd, exit_code: nil, report_info_path:)
47+
detail = PmdReportDetail.new(execution_time: execution_time, timestamp: timestamp,
48+
working_dir: working_dir, exit_code: exit_code)
49+
detail.save(report_info_path)
50+
detail
51+
end
52+
3953
# convert seconds into HH::MM::SS
4054
def self.convert_seconds(seconds)
4155
Time.at(seconds.abs).utc.strftime('%H:%M:%S')

lib/pmdtester/pmd_tester_utils.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def parse_pmd_report(report_file, branch, report_details, filter_set = nil)
3232
file: report_file,
3333

3434
timestamp: report_details.timestamp,
35-
exec_time: report_details.execution_time
35+
exec_time: report_details.execution_time,
36+
exit_code: report_details.exit_code
3637
)
3738
end
3839

lib/pmdtester/project.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ def compute_report_diff(base_branch, patch_branch, filter_set)
123123
get_report_info_path(base_branch),
124124
get_report_info_path(patch_branch),
125125
filter_set)
126+
127+
report_diff.base_report.report_folder = get_project_target_dir(base_branch)
128+
report_diff.patch_report.report_folder = get_project_target_dir(patch_branch)
126129
end
127130
end
128131
end

0 commit comments

Comments
 (0)