Skip to content

Commit 2c9effa

Browse files
committed
Merge changed violations
1 parent 6875868 commit 2c9effa

File tree

8 files changed

+93
-22
lines changed

8 files changed

+93
-22
lines changed

lib/pmdtester/builders/diff_report/violations.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ def build_violation_table_body(doc, key, value)
3737
end
3838

3939
def build_violation_table_row(doc, key, pmd_violation)
40-
doc.tr(class: pmd_violation.branch == PmdTester::BASE ? 'b' : 'a') do
40+
klass = if pmd_violation.changed
41+
'd'
42+
elsif pmd_violation.branch == PmdTester::BASE
43+
'b'
44+
else
45+
'a'
46+
end
47+
48+
doc.tr(class: klass) do
4149
build_table_anchor_column(doc, 'A', increment_violation_index)
4250

4351
violation = pmd_violation.attrs

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/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: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,33 @@ class PmdViolation
4949
attr_reader :attrs
5050
attr_accessor :text
5151

52+
# means it was in both branches but changed messages
53+
attr_accessor :changed
54+
5255
def initialize(attrs, branch)
5356
@attrs = attrs
5457
@branch = branch
58+
@changed = false
5559
@text = ''
5660
end
5761

58-
def eql?(other)
62+
def is_same_modulo_message?(other)
5963
@attrs['beginline'].eql?(other.attrs['beginline']) &&
60-
@attrs['rule'].eql?(other.attrs['rule']) &&
64+
@attrs['rule'].eql?(other.attrs['rule'])
65+
end
66+
67+
def try_merge?(other)
68+
if branch != BASE && @branch != other.branch && is_same_modulo_message?(other)
69+
@changed = true
70+
@attrs['oldMessage'] = other.text
71+
true
72+
else
73+
false
74+
end
75+
end
76+
77+
def eql?(other)
78+
is_same_modulo_message?(other) &&
6179
@text.eql?(other.text)
6280
end
6381

lib/pmdtester/report_diff.rb

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class ReportDiff
1010
attr_accessor :base_violations_size
1111
attr_accessor :patch_violations_size
1212
attr_accessor :new_violations_size
13+
attr_accessor :changed_violations_size
1314
attr_accessor :removed_violations_size
1415
attr_accessor :violation_diffs_size
1516

@@ -88,28 +89,28 @@ def diffs_exist?
8889
def calculate_violations(base_violations, patch_violations)
8990
@base_violations_size = base_violations.violations_size
9091
@patch_violations_size = patch_violations.violations_size
91-
violation_diffs = build_diffs(base_violations.violations, patch_violations.violations)
92-
@violation_diffs = violation_diffs
93-
@new_violations_size, @removed_violations_size = get_diffs_size(violation_diffs)
94-
@violation_diffs_size = @new_violations_size + @removed_violations_size
92+
93+
@violation_diffs = build_diffs(base_violations.violations, patch_violations.violations)
94+
@violation_diffs = merge_changed_violations(@violation_diffs)
95+
96+
@new_violations_size, @changed_violations_size, @removed_violations_size = get_diffs_size(@violation_diffs)
97+
@violation_diffs_size = @violation_diffs.size
9598
end
9699

97100
def calculate_errors(base_errors, patch_errors)
98101
@base_errors_size = base_errors.errors_size
99102
@patch_errors_size = patch_errors.errors_size
100-
error_diffs = build_diffs(base_errors.errors, patch_errors.errors)
101-
@error_diffs = error_diffs
102-
@new_errors_size, @removed_errors_size = get_diffs_size(error_diffs)
103-
@error_diffs_size = @new_errors_size + @removed_errors_size
103+
@error_diffs = build_diffs(base_errors.errors, patch_errors.errors)
104+
@new_errors_size, _, @removed_errors_size = get_diffs_size(@error_diffs)
105+
@error_diffs_size = @error_diffs.size
104106
end
105107

106108
def calculate_configerrors(base_configerrors, patch_configerrors)
107109
@base_configerrors_size = base_configerrors.size
108110
@patch_configerrors_size = patch_configerrors.size
109-
configerrors_diffs = build_diffs(base_configerrors.errors, patch_configerrors.errors)
110-
@configerrors_diffs = configerrors_diffs
111-
@new_configerrors_size, @removed_configerrors_size = get_diffs_size(configerrors_diffs)
112-
@configerrors_diffs_size = @new_configerrors_size + @removed_configerrors_size
111+
@configerrors_diffs = build_diffs(base_configerrors.errors, patch_configerrors.errors)
112+
@new_configerrors_size, _, @removed_configerrors_size = get_diffs_size(@configerrors_diffs)
113+
@configerrors_diffs_size = @configerrors_diffs.size
113114
end
114115

115116
def calculate_details(base_info, patch_info)
@@ -130,7 +131,10 @@ def calculate_details(base_info, patch_info)
130131
end
131132

132133
def build_diffs(base_hash, patch_hash)
134+
# Keys are filenames
135+
# Values are lists of violations/errors
133136
diffs = base_hash.merge(patch_hash) do |_key, base_value, patch_value|
137+
# make the difference of values
134138
(base_value | patch_value) - (base_value & patch_value)
135139
end
136140

@@ -139,15 +143,33 @@ def build_diffs(base_hash, patch_hash)
139143
end
140144
end
141145

146+
# @param diff_violations a hash { filename => list[violation]}, containing those that changed
147+
def merge_changed_violations(diff_violations)
148+
diff_violations.each do |fname, different|
149+
diff_violations[fname] = different.dup.delete_if { |v|
150+
v.branch == BASE &&
151+
# try_merge will set v2.changed = true if it succeeds
152+
different.any? { |v2| v2.try_merge?(v) }
153+
}
154+
end
155+
end
156+
142157
def get_diffs_size(diffs_hash)
143158
new_size = 0
159+
changed_size = 0
144160
removed_size = 0
145161
diffs_hash.each_value do |value|
146162
value.each do |item|
147-
item.branch.eql?(BASE) ? removed_size += 1 : new_size += 1
163+
if item.changed
164+
changed_size += 1
165+
elsif item.branch.eql?(BASE)
166+
removed_size += 1
167+
else
168+
new_size += 1
169+
end
148170
end
149171
end
150-
[new_size, removed_size]
172+
[new_size, changed_size, removed_size]
151173
end
152174

153175
def introduce_new_errors?

resources/css/maven-theme.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ table.bodyTable th, table.bodyTable td {
9494
font-size: 1em;
9595
}
9696

97+
span.added {
98+
color: green;
99+
}
100+
span.removed {
101+
color: red;
102+
}
103+
span.changed {
104+
color: darkorange;
105+
}
106+
97107
table.bodyTable tr.a {
98108
background-color: #af8;
99109
}

0 commit comments

Comments
 (0)