Skip to content

Commit 34aa1d0

Browse files
committed
Merge branch 'pr-74' into master
Merge violations that have just changed messages #74
2 parents 6875868 + 7c20c78 commit 34aa1d0

22 files changed

+265
-123
lines changed

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Metrics/MethodLength:
2020
Exclude:
2121
- 'lib/pmdtester/parsers/options.rb'
2222

23+
Metrics/ClassLength:
24+
Exclude:
25+
- '**/*'
26+
2327
Metrics/BlockLength:
2428
Exclude:
2529
- 'lib/pmdtester/parsers/options.rb'

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Typeresolution is now supported by two new tags in the project-list.xml file:
1414
* [#68](https://github.com/pmd/pmd-regression-tester/pull/68): Don't generate a dynamic ruleset if not needed
1515
* [#69](https://github.com/pmd/pmd-regression-tester/pull/69): Detect single rules with auto-gen-config
1616
* [#70](https://github.com/pmd/pmd-regression-tester/pull/70): Add link to PR on github in HTML report
17+
* [#74](https://github.com/pmd/pmd-regression-tester/pull/74): Merge violations that have just changed messages
1718

1819
## External Contributions
1920

lib/pmdtester/builders/diff_report/violations.rb

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require 'differ'
4+
35
# Contains methods to write out html for the violations.
46
# This mixin is used by DiffReportBuilder.
57
module DiffReportBuilderViolations
@@ -25,7 +27,7 @@ def build_violation_table(doc, key, value)
2527
end
2628

2729
def build_violation_table_head(doc)
28-
build_table_head(doc, '', 'Priority', 'Rule', 'Message', 'Line')
30+
build_table_head(doc, '', 'Rule', 'Message', 'Line')
2931
end
3032

3133
def build_violation_table_body(doc, key, value)
@@ -36,42 +38,61 @@ def build_violation_table_body(doc, key, value)
3638
end
3739
end
3840

39-
def build_violation_table_row(doc, key, pmd_violation)
40-
doc.tr(class: pmd_violation.branch == PmdTester::BASE ? 'b' : 'a') do
41+
def build_violation_table_row(doc, key, violation)
42+
doc.tr(class: get_css_class(violation)) do
4143
build_table_anchor_column(doc, 'A', increment_violation_index)
4244

43-
violation = pmd_violation.attrs
44-
45-
# The priority of the rule
46-
doc.td violation['priority']
47-
4845
# The rule that trigger the violation
4946
doc.td do
50-
doc.a(href: (violation['externalInfoUrl']).to_s) { doc.text violation['rule'] }
47+
doc.a(href: violation.info_url.to_s) { doc.text violation.rule_name }
5148
end
5249

5350
# The violation message
54-
doc.td "\n" + pmd_violation.text + "\n"
55-
56-
# The begin line of the violation
57-
line = violation['beginline']
51+
if violation.changed && violation.message != violation.old_message
52+
doc.td { diff_fragments(doc, violation) }
53+
else
54+
doc.td violation.text
55+
end
5856

5957
# The link to the source file
6058
doc.td do
6159
link = get_link_to_source(violation, key)
62-
doc.a(href: link.to_s) { doc.text line }
60+
doc.a(href: link.to_s) { doc.text display_line(violation) }
6361
end
6462
end
6563
end
6664

65+
def diff_fragments(doc, violation)
66+
diff = Differ.diff_by_word(violation.old_message, violation.message)
67+
doc << diff.format_as(:html)
68+
end
69+
70+
def display_line(violation)
71+
if violation.changed && violation.old_line && violation.old_line != violation.line
72+
"#{violation.old_line} => #{violation.line}"
73+
else
74+
violation.line
75+
end
76+
end
77+
6778
def get_link_to_source(violation, key)
6879
l_str = @project.type == 'git' ? 'L' : 'l'
69-
line_str = "##{l_str}#{violation['beginline']}"
80+
line_str = "##{l_str}#{violation.line}"
7081
@project.get_webview_url(key) + line_str
7182
end
7283

7384
def increment_violation_index
7485
@violation_index ||= 0 # init with 0
7586
@violation_index += 1
7687
end
88+
89+
def get_css_class(pmd_violation)
90+
if pmd_violation.changed
91+
'd'
92+
elsif pmd_violation.branch == PmdTester::BASE
93+
'b'
94+
else
95+
'a'
96+
end
97+
end
7798
end

lib/pmdtester/builders/diff_report_builder.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ def build_violations_summary_row(doc)
8282
base: @report_diff.base_violations_size,
8383
patch: @report_diff.patch_violations_size,
8484
removed: @report_diff.removed_violations_size,
85-
added: @report_diff.new_violations_size
85+
added: @report_diff.new_violations_size,
86+
changed: @report_diff.changed_violations_size
8687
})
8788
end
8889

@@ -127,7 +128,7 @@ def build_summary_row(doc, row_data)
127128
doc.td(class: 'a') { doc.text row_data[:patch] }
128129
doc.td(class: 'c') do
129130
if row_data.key?(:removed) && row_data.key?(:added)
130-
build_table_content_for(doc, row_data[:removed], row_data[:added])
131+
build_table_content_for(doc, row_data[:removed], row_data[:added], row_data[:changed])
131132
elsif row_data.key?(:diff_execution_time)
132133
doc.text row_data[:diff_execution_time]
133134
end

lib/pmdtester/builders/html_report_builder.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ def copy_css(report_dir)
4747
FileUtils.copy_entry(CSS_SRC_DIR, css_dest_dir)
4848
end
4949

50-
def build_table_content_for(doc, removed_size, new_size)
51-
doc.font(color: 'red') { doc.text "-#{removed_size}" }
50+
def build_table_content_for(doc, removed_size, new_size, changed_size = nil)
51+
doc.span(class: 'removed') { doc.text "-#{removed_size}" }
52+
if changed_size
53+
doc.text ' | '
54+
doc.span(class: 'changed') { doc.text "~#{changed_size}" }
55+
end
5256
doc.text ' | '
53-
doc.font(color: 'green') { doc.text "+#{new_size}" }
57+
doc.span(class: 'added') { doc.text "+#{new_size}" }
5458
end
5559
end
5660
end

lib/pmdtester/builders/summary_report_builder.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def build_projects_table_head(doc)
127127
doc.th 'project name'
128128
doc.th 'project branch/tag'
129129
doc.th 'removed|new errors size'
130-
doc.th 'removed|new violations size'
130+
doc.th 'removed|changed|new violations size'
131131
doc.th 'removed|new configerrors size'
132132
end
133133
end
@@ -142,12 +142,12 @@ def build_projects_table_body(doc)
142142
end
143143
doc.td project.tag
144144
doc.td do
145-
build_table_content_for(doc, project.removed_errors_size,
146-
project.new_errors_size)
145+
build_table_content_for(doc, project.removed_errors_size, project.new_errors_size)
147146
end
148147
doc.td do
149148
build_table_content_for(doc, project.removed_violations_size,
150-
project.new_violations_size)
149+
project.new_violations_size,
150+
project.changed_violations_size)
151151
end
152152
doc.td do
153153
build_table_content_for(doc, project.removed_configerrors_size,

lib/pmdtester/parsers/pmd_report_document.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def start_element(name, attrs = [])
3131
when 'file'
3232
remove_work_dir!(attrs['name'])
3333
@current_violations = []
34-
@current_filename = attrs['name']
34+
@current_filename = attrs['name'].freeze
3535
when 'violation'
36-
@current_violation = PmdViolation.new(attrs, @branch_name)
36+
@current_violation = PmdViolation.new(attrs, @branch_name, @current_filename)
3737
when 'error'
3838
remove_work_dir!(attrs['filename'])
3939
remove_work_dir!(attrs['msg'])
@@ -81,7 +81,7 @@ def match_filter_set?(violation)
8181

8282
@filter_set.each do |filter_rule_ref|
8383
ruleset_attr = violation.attrs['ruleset'].delete(' ').downcase + '.xml'
84-
rule = violation.attrs['rule']
84+
rule = violation.rule_name
8585
rule_ref = "#{ruleset_attr}/#{rule}"
8686
return true if filter_rule_ref.eql?(ruleset_attr)
8787
return true if filter_rule_ref.eql?(rule_ref)

lib/pmdtester/pmd_configerror.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def msg
5252
@attrs['msg']
5353
end
5454

55+
def changed
56+
false
57+
end
58+
5559
def eql?(other)
5660
rulename.eql?(other.rulename) && msg.eql?(other.msg)
5761
end

lib/pmdtester/pmd_error.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def msg
5555
@attrs['msg']
5656
end
5757

58+
def changed
59+
false
60+
end
61+
5862
def eql?(other)
5963
filename.eql?(other.filename) && msg.eql?(other.msg) &&
6064
@text.eql?(other.text)

lib/pmdtester/pmd_violation.rb

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,70 @@ class PmdViolation
4747
# </xs:complexType>
4848

4949
attr_reader :attrs
50+
attr_reader :fname
5051
attr_accessor :text
5152

52-
def initialize(attrs, branch)
53+
# means it was in both branches but changed messages
54+
attr_accessor :changed
55+
56+
def initialize(attrs, branch, fname)
5357
@attrs = attrs
5458
@branch = branch
59+
@changed = false
60+
@fname = fname
5561
@text = ''
5662
end
5763

64+
def line_move?(other)
65+
message.eql?(other.message) && (line - other.line).abs <= 5
66+
end
67+
68+
def try_merge?(other)
69+
if branch != BASE && branch != other.branch && rule_name == other.rule_name &&
70+
!changed && # not already changed
71+
(line == other.line || line_move?(other))
72+
@changed = true
73+
@attrs['oldMessage'] = other.text
74+
@attrs['oldLine'] = other.line
75+
true
76+
else
77+
false
78+
end
79+
end
80+
81+
def old_message
82+
@attrs['oldMessage']
83+
end
84+
85+
def old_line
86+
@attrs['oldLine']&.to_i
87+
end
88+
89+
def info_url
90+
@attrs['externalInfoUrl']
91+
end
92+
93+
def line
94+
@attrs['beginline'].to_i
95+
end
96+
97+
def rule_name
98+
@attrs['rule']
99+
end
100+
101+
def message
102+
@text
103+
end
104+
58105
def eql?(other)
59-
@attrs['beginline'].eql?(other.attrs['beginline']) &&
60-
@attrs['rule'].eql?(other.attrs['rule']) &&
61-
@text.eql?(other.text)
106+
rule_name.eql?(other.rule_name) &&
107+
line.eql?(other.line) &&
108+
fname.eql?(other.fname) &&
109+
message.eql?(other.message)
62110
end
63111

64112
def hash
65-
[@attrs['beginline'], @attrs['rule'], @text].hash
113+
[line, rule_name, message].hash
66114
end
67115
end
68116
end

0 commit comments

Comments
 (0)