Skip to content

Commit 2c536e1

Browse files
committedNov 30, 2014
Switch HamlLint hook to use extract_messages
Simplify hook by using helper provided by Hook::PreCommit::Base. Change-Id: Icbf45532487a02502cd0395ded5798545436ffd5 Reviewed-on: http://gerrit.causes.com/44902 Tested-by: jenkins <jenkins@brigade.com> Reviewed-by: Shane da Silva <shane.dasilva@brigade.com>
1 parent 482d739 commit 2c536e1

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed
 
+9-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
module Overcommit::Hook::PreCommit
22
# Runs `haml-lint` against any modified HAML files.
33
class HamlLint < Base
4+
MESSAGE_TYPE_CATEGORIZER = lambda do |type|
5+
type.include?('W') ? :warning : :error
6+
end
7+
48
def run
59
result = execute([executable] + applicable_files)
610
return :pass if result.success?
711

8-
# Keep lines from the output for files that we actually modified
9-
error_lines, warning_lines = result.stdout.split("\n").partition do |output_line|
10-
if match = output_line.match(/^([^:]+):(\d+)/)
11-
file = match[1]
12-
line = match[2]
13-
end
14-
modified_lines(file).include?(line.to_i)
15-
end
16-
17-
return :fail, error_lines.join("\n") unless error_lines.empty?
18-
19-
[:warn, "Modified files have lints (on lines you didn't modify)\n" <<
20-
warning_lines.join("\n")]
12+
extract_messages(
13+
result.stdout.split("\n"),
14+
/^(?<file>[^:]+):(?<line>\d+)[^ ]* (?<type>[^ ]+)/,
15+
MESSAGE_TYPE_CATEGORIZER,
16+
)
2117
end
2218
end
2319
end

‎spec/overcommit/hook/pre_commit/haml_lint_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,25 @@
2727
subject.stub(:execute).and_return(result)
2828
end
2929

30-
context 'and it reports lines that were not modified by the commit' do
30+
context 'and it reports a warning' do
3131
before do
3232
result.stub(:stdout).and_return([
3333
'file1.haml:1 [W] Prefer single quoted strings',
3434
].join("\n"))
3535

36-
subject.stub(:modified_lines).and_return([2, 3])
36+
subject.stub(:modified_lines_in_file).and_return([2, 3])
3737
end
3838

3939
it { should warn }
4040
end
4141

42-
context 'and it reports lines that were modified by the commit' do
42+
context 'and it reports an error' do
4343
before do
4444
result.stub(:stdout).and_return([
45-
'file1.haml:1 [W] Prefer single quoted strings',
45+
'file1.haml:1 [E] Unbalanced brackets.',
4646
].join("\n"))
4747

48-
subject.stub(:modified_lines).and_return([1, 2])
48+
subject.stub(:modified_lines_in_file).and_return([1, 2])
4949
end
5050

5151
it { should fail_hook }

0 commit comments

Comments
 (0)