Skip to content

Commit fc1bc2a

Browse files
gautamjainsds
authored andcommitted
Update JavaCheckstyle to categorize messages depending on type
Checkstyle generates messages with `ERROR`, `WARN`, or `INFO` tags depending on their severity. Prior to this, all messages (irrespective of their tag) were treated as an error. With this change, messages with `WARN` and `INFO` tags will be handled as Overcommit warnings.
1 parent 5409330 commit fc1bc2a

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

lib/overcommit/hook/pre_commit/java_checkstyle.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@ module Overcommit::Hook::PreCommit
33
#
44
# @see http://checkstyle.sourceforge.net/
55
class JavaCheckstyle < Base
6-
MESSAGE_REGEX = /^(\[[^\]]+\]\s+)?(?<file>(?:\w:)?[^:]+):(?<line>\d+)/
6+
MESSAGE_REGEX = /^(\[(?<type>[^\]]+)\]\s+)?(?<file>(?:\w:)?[^:]+):(?<line>\d+)/
7+
8+
MESSAGE_TYPE_CATEGORIZER = lambda do |type|
9+
%w[WARN INFO].include?(type.to_s) ? :warning : :error
10+
end
711

812
def run
913
result = execute(command, args: applicable_files)
1014
output = result.stdout.chomp
11-
return :pass if result.success?
1215

1316
# example message:
1417
# path/to/file.java:3:5: Error message
1518
extract_messages(
1619
output.split("\n").grep(MESSAGE_REGEX),
17-
MESSAGE_REGEX
20+
MESSAGE_REGEX,
21+
MESSAGE_TYPE_CATEGORIZER
1822
)
1923
end
2024
end

spec/overcommit/hook/pre_commit/java_checkstyle_spec.rb

+38-2
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929
end
3030
end
3131

32-
context 'when checkstyle exits unsucessfully' do
32+
context 'when checkstyle exits unsuccessfully' do
3333
let(:result) { double('result') }
3434

3535
before do
3636
result.stub(:success?).and_return(false)
3737
subject.stub(:execute).and_return(result)
3838
end
3939

40-
context 'and it reports an error' do
40+
context 'and it reports a message with no severity tag' do
4141
before do
4242
result.stub(:stdout).and_return([
4343
'Starting audit...',
@@ -48,5 +48,41 @@
4848

4949
it { should fail_hook }
5050
end
51+
52+
context 'and it reports an error' do
53+
before do
54+
result.stub(:stdout).and_return([
55+
'Starting audit...',
56+
'[ERROR] file1.java:1: Missing a Javadoc comment.',
57+
'Audit done.'
58+
].join("\n"))
59+
end
60+
61+
it { should fail_hook }
62+
end
63+
64+
context 'and it reports an warning' do
65+
before do
66+
result.stub(:stdout).and_return([
67+
'Starting audit...',
68+
'[WARN] file1.java:1: Missing a Javadoc comment.',
69+
'Audit done.'
70+
].join("\n"))
71+
end
72+
73+
it { should warn }
74+
end
75+
76+
context 'and it reports an info message' do
77+
before do
78+
result.stub(:stdout).and_return([
79+
'Starting audit...',
80+
'[INFO] file1.java:1: Missing a Javadoc comment.',
81+
'Audit done.'
82+
].join("\n"))
83+
end
84+
85+
it { should warn }
86+
end
5187
end
5288
end

0 commit comments

Comments
 (0)