class PmdTester::ReportDiff

This class represents all the diff report information, including the summary information of the original pmd reports, as well as the specific information of the diff report.

Attributes

base_report[RW]
configerror_counts[R]
configerror_diffs_by_rule[RW]
error_counts[R]
error_diffs_by_file[RW]
patch_report[RW]
rule_infos_union[RW]
violation_counts[R]
violation_diffs_by_file[RW]

Public Class Methods

new(base_report:, patch_report:) click to toggle source
# File lib/pmdtester/report_diff.rb, line 132
def initialize(base_report:, patch_report:)
  @violation_counts = RunningDiffCounters.new(base_report.violations_by_file.total_size)
  @error_counts = RunningDiffCounters.new(base_report.errors_by_file.total_size)
  @configerror_counts = RunningDiffCounters.new(base_report.configerrors_by_rule.values.flatten.length)

  @violation_diffs_by_file = {}
  @error_diffs_by_file = {}
  @configerror_diffs_by_rule = {}

  @rule_infos_union = {}
  @base_report = base_report
  @patch_report = patch_report

  @violation_diffs_by_rule = {}
  diff_with(patch_report)
end

Public Instance Methods

rule_summaries() click to toggle source
# File lib/pmdtester/report_diff.rb, line 149
def rule_summaries
  @violation_diffs_by_rule.map do |(rule, counters)|
    {
      'name' => rule,
      'info_url' => @rule_infos_union[rule].info_url,
      **counters.to_h.transform_keys(&:to_s)
    }
  end
end

Private Instance Methods

build_diffs(counters, &getter) click to toggle source
# File lib/pmdtester/report_diff.rb, line 201
def build_diffs(counters, &getter)
  base_hash = getter.yield(@base_report)
  patch_hash = getter.yield(@patch_report)
  # Keys are filenames
  # Values are lists of violations/errors
  diffs = base_hash.to_h.merge(patch_hash.to_h) do |_key, base_value, patch_value|
    # make the difference of values
    (base_value | patch_value) - (base_value & patch_value)
  end

  diffs.delete_if do |_key, value|
    value.empty?
  end

  merge_changed_items(diffs)
  count(diffs) { |_| counters }
  diffs
end
count(item_h) { |item| ... } click to toggle source
# File lib/pmdtester/report_diff.rb, line 233
def count(item_h)
  item_h = { '' => item_h } if item_h.is_a?(Array)

  item_h.each do |_k, items|
    items.each do |item|
      counter = yield item

      if item.changed?
        counter.changed += 1
      elsif item.branch.eql?(BASE)
        counter.removed += 1
      else
        counter.new += 1
      end
    end
  end
end
count_by_rule(violations_h, base:) click to toggle source
# File lib/pmdtester/report_diff.rb, line 189
def count_by_rule(violations_h, base:)
  violations_h.each_value do |v|
    record_rule_info(v)
    rule_diff = getvdiff(v.rule_name)
    if base
      rule_diff.base_total += 1
    else
      rule_diff.patch_total += 1
    end
  end
end
diff_with(patch_report) click to toggle source
# File lib/pmdtester/report_diff.rb, line 161
def diff_with(patch_report)
  @violation_counts.patch_total = patch_report.violations_by_file.total_size
  @error_counts.patch_total = patch_report.errors_by_file.total_size
  @configerror_counts.patch_total = patch_report.configerrors_by_rule.values.flatten.length

  @violation_diffs_by_file = build_diffs(@violation_counts, &:violations_by_file)
  count(@violation_diffs_by_file) { |v| getvdiff(v.rule_name) } # record the diffs in the rule counter

  @error_diffs_by_file = build_diffs(@error_counts, &:errors_by_file)
  @configerror_diffs_by_rule = build_diffs(@configerror_counts, &:configerrors_by_rule)

  count_by_rule(@base_report.violations_by_file, base: true)
  count_by_rule(@patch_report.violations_by_file, base: false)
  self
end
getvdiff(rule_name) click to toggle source
# File lib/pmdtester/report_diff.rb, line 183
def getvdiff(rule_name)
  @violation_diffs_by_rule.fetch(rule_name) do |_|
    @violation_diffs_by_rule[rule_name] = RunningDiffCounters.new(0)
  end
end
merge_changed_items(diff_h) click to toggle source

@param diff_h a hash { filename => list}, containing those that changed in case of config errors it's a hash { rulename => list }

# File lib/pmdtester/report_diff.rb, line 222
def merge_changed_items(diff_h)
  diff_h.each do |fname, different|
    different.sort_by!(&:sort_key)
    diff_h[fname] = different.delete_if do |v|
      v.branch == BASE &&
        # try_merge will set v2.changed = true if it succeeds
        different.any? { |v2| v2.try_merge?(v) }
    end
  end
end
record_rule_info(violation) click to toggle source
# File lib/pmdtester/report_diff.rb, line 177
def record_rule_info(violation)
  return if @rule_infos_union.key?(violation.rule_name)

  @rule_infos_union[violation.rule_name] = RuleInfo.new(violation.rule_name, violation.info_url)
end