Class: PmdTester::Runner

Inherits:
Object
  • Object
show all
Includes:
PmdTesterUtils
Defined in:
lib/pmdtester/runner.rb

Overview

The Runner is a class responsible of organizing all PmdTester modules and running the PmdTester

Constant Summary

Constants included from PmdTester

BASE, PATCH, PR_NUM_ENV_VAR, VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PmdTesterUtils

#build_cpd_report_diff, #build_html_reports, #build_report_diff, #compute_project_diffs, #parse_cpd_report, #parse_pmd_report

Methods included from PmdTester

#logger, logger

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



9
10
11
# File 'lib/pmdtester/runner.rb', line 9

def initialize(argv)
  @options = Options.new(argv)
end

Class Method Details

.create_message(base_branch, summary) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/pmdtester/runner.rb', line 186

def self.create_message(base_branch, summary)
  return 'No relevant source code has been changed, pmdtester skipped.' if summary[:skipped]

  "Compared to #{base_branch}:\n" \
    'This changeset ' \
    "changes #{summary[:pmd_violations][:changed]} violations,\n" \
    "introduces #{summary[:pmd_violations][:new]} new violations, " \
    "#{summary[:pmd_errors][:new]} new errors and " \
    "#{summary[:pmd_configerrors][:new]} new configuration errors,\n" \
    "removes #{summary[:pmd_violations][:removed]} violations, " \
    "#{summary[:pmd_errors][:removed]} errors and " \
    "#{summary[:pmd_configerrors][:removed]} configuration errors.\n" \
    "There are #{summary[:cpd_duplications][:changed]} changed duplications, " \
    "#{summary[:cpd_duplications][:new]} new duplications and " \
    "#{summary[:cpd_duplications][:removed]} removed duplications.\n" \
    "There are #{summary[:cpd_errors][:changed]} changed CPD errors, " \
    "#{summary[:cpd_errors][:new]} new CPD errors and " \
    "#{summary[:cpd_errors][:removed]} removed CPD errors.\n"
end

.determine_conclusion(summary) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/pmdtester/runner.rb', line 206

def self.determine_conclusion(summary)
  return 'skipped' if summary[:skipped]

  total_changes = summary[:pmd_violations][:changed] \
    + summary[:pmd_violations][:new] \
    + summary[:pmd_violations][:removed] \
    + summary[:pmd_errors][:new] \
    + summary[:pmd_configerrors][:new] \
    + summary[:cpd_duplications][:changed] \
    + summary[:cpd_duplications][:new] \
    + summary[:cpd_duplications][:removed] \
    + summary[:cpd_errors][:new]
  if total_changes.positive?
    'neutral'
  else
    'success'
  end
end

Instance Method Details

#cleanObject



35
36
37
38
# File 'lib/pmdtester/runner.rb', line 35

def clean
  clean_target = 'target/reports'
  FileUtils.rm_rf(clean_target)
end

#determine_project_list_for_online_mode(baseline_path) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/pmdtester/runner.rb', line 97

def determine_project_list_for_online_mode(baseline_path)
  # patch branch build pmd report with same list of projects as base branch
  # if not specified otherwise. This allows to use a different project list
  # than used for creating the baseline. Use with care, though
  if @options.project_list == Options::DEFAULT_LIST_PATH
    project_list = "#{baseline_path}/project-list.xml"
  else
    logger.info "Using project list #{@options.project_list} which might differ from baseline"
    project_list = @options.project_list
  end
  project_list
end

#download_baseline(url_prefix, branch_name) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/pmdtester/runner.rb', line 110

def download_baseline(url_prefix, branch_name)
  branch_filename = PmdBranchDetail.branch_filename(branch_name)
  zip_filename = "#{branch_filename}-baseline.zip"
  target_path = 'target/reports'
  FileUtils.mkdir_p(target_path) unless File.directory?(target_path)

  url = get_baseline_url(url_prefix, zip_filename)
  logger.info "Downloading baseline for branch #{branch_name} from #{url}"
  wget_cmd = "wget --no-verbose --timestamping #{url}"
  unzip_cmd = "unzip -qo #{zip_filename}"

  Dir.chdir(target_path) do
    Cmd.execute_successfully(wget_cmd) unless File.exist?(zip_filename)
    Cmd.execute_successfully(unzip_cmd)
  end

  make_baseline_compatible("#{target_path}/#{branch_filename}")

  "#{target_path}/#{branch_filename}"
end

#get_baseline_url(url_prefix, zip_filename) ⇒ Object



131
132
133
# File 'lib/pmdtester/runner.rb', line 131

def get_baseline_url(url_prefix, zip_filename)
  "#{url_prefix}#{zip_filename}"
end

#get_projects(file_path) ⇒ Object



155
156
157
# File 'lib/pmdtester/runner.rb', line 155

def get_projects(file_path)
  @projects = ProjectsParser.new.parse(file_path)
end

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pmdtester/runner.rb', line 13

def run
  clean unless @options.keep_reports

  case @options.mode
  when Options::LOCAL
    run_local_mode
  when Options::ONLINE
    run_online_mode
  when Options::SINGLE
    run_single_mode
  end

  summary = summarize_diffs
  unless @options.html_flag
    FileUtils.mkdir_p('target/reports/diff') unless File.directory?('target/reports/diff')
    File.write('target/reports/diff/summary.txt', Runner.create_message(@options.base_branch, summary))
    File.write('target/reports/diff/conclusion.txt', Runner.determine_conclusion(summary))
  end

  summary
end

#run_local_modeObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pmdtester/runner.rb', line 40

def run_local_mode
  logger.info "Mode: #{@options.mode}"
  get_projects(@options.project_list) unless @options.nil?

  rules_changed = true
  rules_changed = RuleSetBuilder.new(@options).build? if @options.auto_config_flag
  impl_changed = determine_impl_changed?

  base_branch_details = create_pmd_report(config: @options.base_config, branch: @options.base_branch,
                                          rules_changed: rules_changed, impl_changed: impl_changed)
  # copy list of projects file to the base baseline
  base_branch_dir = File.dirname(base_branch_details.target_branch_project_list_path)
  FileUtils.mkdir_p(base_branch_dir) unless File.directory?(base_branch_dir)
  FileUtils.cp(@options.project_list, base_branch_details.target_branch_project_list_path)

  patch_branch_details = create_pmd_report(config: @options.patch_config, branch: @options.patch_branch,
                                           rules_changed: rules_changed, impl_changed: impl_changed)
  # copy list of projects file to the patch baseline
  patch_branch_dir = File.dirname(patch_branch_details.target_branch_project_list_path)
  FileUtils.mkdir_p(patch_branch_dir) unless File.directory?(patch_branch_dir)
  FileUtils.cp(@options.project_list, patch_branch_details.target_branch_project_list_path)

  build_html_reports(@projects, base_branch_details, patch_branch_details, nil,
                     rules_changed: rules_changed, impl_changed: impl_changed)
end

#run_online_modeObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pmdtester/runner.rb', line 66

def run_online_mode
  logger.info "Mode: #{@options.mode}"

  baseline_path = download_baseline(@options.baseline_download_url_prefix, @options.base_branch)

  project_list = determine_project_list_for_online_mode(baseline_path)
  get_projects(project_list)

  rules_changed = determine_rule_changed_in_online_mode?(baseline_path)
  impl_changed = determine_impl_changed?

  # When neither PMD nor CPD is executed, we can abort directly
  # No report will be generated then
  if !rules_changed && !impl_changed
    @skipped = true
    logger.info 'No relevant source code has been changed, pmdtester skipped.'
    return
  end

  patch_branch_details = create_pmd_report(config: @options.patch_config, branch: @options.patch_branch,
                                           rules_changed: rules_changed, impl_changed: impl_changed)
  # copy list of projects file to the patch baseline
  patch_branch_dir = File.dirname(patch_branch_details.target_branch_project_list_path)
  FileUtils.mkdir_p(patch_branch_dir) unless File.directory?(patch_branch_dir)
  FileUtils.cp(project_list, patch_branch_details.target_branch_project_list_path)

  base_branch_details = PmdBranchDetail.load(@options.base_branch, logger)
  build_html_reports(@projects, base_branch_details, patch_branch_details, @options.filter_set,
                     rules_changed: rules_changed, impl_changed: impl_changed)
end

#run_single_modeObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/pmdtester/runner.rb', line 135

def run_single_mode
  logger.info "Mode: #{@options.mode}"

  get_projects(@options.project_list) unless @options.nil?
  patch_branch_details = create_pmd_report(config: @options.patch_config, branch: @options.patch_branch,
                                           rules_changed: true, impl_changed: true)
  # copy list of projects file to the patch baseline
  patch_branch_dir = File.dirname(patch_branch_details.target_branch_project_list_path)
  FileUtils.mkdir_p(patch_branch_dir) unless File.directory?(patch_branch_dir)
  FileUtils.cp(@options.project_list, patch_branch_details.target_branch_project_list_path)

  # for creating a baseline, no html report is needed
  return if @options.html_flag

  # in single mode, we don't have a base branch, only a patch branch...
  empty_base_branch_details = PmdBranchDetail.load('single-mode', logger)
  build_html_reports(@projects, empty_base_branch_details, patch_branch_details, nil,
                     rules_changed: true, impl_changed: true)
end

#summarize_diffsObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/pmdtester/runner.rb', line 159

def summarize_diffs
  pmd_error_total = RunningDiffCounters.new(0)
  pmd_violations_total = RunningDiffCounters.new(0)
  pmd_configerrors_total = RunningDiffCounters.new(0)
  cpd_error_total = RunningDiffCounters.new(0)
  cpd_duplications_total = RunningDiffCounters.new(0)

  @projects.each do |project|
    diff = project.report_diff
    cpd_diff = project.cpd_report_diff

    # in case we are in single mode, there might be no diffs (only the patch branch is available)
    # or if only pmd or only cpd is has been run
    sum_pmd_counters(diff, pmd_error_total, pmd_violations_total, pmd_configerrors_total) unless diff.nil?
    sum_cpd_counters(cpd_diff, cpd_error_total, cpd_duplications_total) unless cpd_diff.nil?
  end

  {
    pmd_errors: pmd_error_total.to_h,
    pmd_violations: pmd_violations_total.to_h,
    pmd_configerrors: pmd_configerrors_total.to_h,
    cpd_errors: cpd_error_total.to_h,
    cpd_duplications: cpd_duplications_total.to_h,
    skipped: @skipped
  }
end