File tree 2 files changed +45
-5
lines changed
lib/overcommit/hook/pre_commit
spec/overcommit/hook/pre_commit
2 files changed +45
-5
lines changed Original file line number Diff line number Diff line change @@ -3,18 +3,22 @@ module Overcommit::Hook::PreCommit
3
3
#
4
4
# @see http://checkstyle.sourceforge.net/
5
5
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
7
11
8
12
def run
9
13
result = execute ( command , args : applicable_files )
10
14
output = result . stdout . chomp
11
- return :pass if result . success?
12
15
13
16
# example message:
14
17
# path/to/file.java:3:5: Error message
15
18
extract_messages (
16
19
output . split ( "\n " ) . grep ( MESSAGE_REGEX ) ,
17
- MESSAGE_REGEX
20
+ MESSAGE_REGEX ,
21
+ MESSAGE_TYPE_CATEGORIZER
18
22
)
19
23
end
20
24
end
Original file line number Diff line number Diff line change 29
29
end
30
30
end
31
31
32
- context 'when checkstyle exits unsucessfully ' do
32
+ context 'when checkstyle exits unsuccessfully ' do
33
33
let ( :result ) { double ( 'result' ) }
34
34
35
35
before do
36
36
result . stub ( :success? ) . and_return ( false )
37
37
subject . stub ( :execute ) . and_return ( result )
38
38
end
39
39
40
- context 'and it reports an error ' do
40
+ context 'and it reports a message with no severity tag ' do
41
41
before do
42
42
result . stub ( :stdout ) . and_return ( [
43
43
'Starting audit...' ,
48
48
49
49
it { should fail_hook }
50
50
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
51
87
end
52
88
end
You can’t perform that action at this time.
0 commit comments