Skip to content

Commit 533358d

Browse files
committed
Change JsonSyntax hook to use hook messages
This simplifies this hook by changing it to not have to explicitly check if there were any messages before returning pass/fail. It now simply returns the messages and the framework will do the rest. To support this, the MessageProcessor was updated to support hook messages without line numbers. The theory is that messages without line numbers should apply to the entire file. Change-Id: I20f293cd0b741ad9879da9a738cc083ede1dc088 Reviewed-on: http://gerrit.causes.com/47212 Tested-by: jenkins <jenkins@brigade.com> Reviewed-by: Shane da Silva <shane.dasilva@brigade.com>
1 parent 8a62ead commit 533358d

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

lib/overcommit/hook/pre_commit/json_syntax.rb

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@ module Overcommit::Hook::PreCommit
22
# Checks the syntax of any modified JSON files.
33
class JsonSyntax < Base
44
def run
5-
output = []
5+
messages = []
66

77
applicable_files.each do |file|
88
begin
99
JSON.parse(IO.read(file))
1010
rescue JSON::ParserError => e
11-
output << "#{e.message} parsing #{file}"
11+
error = "#{e.message} parsing #{file}"
12+
messages << Overcommit::Hook::Message.new(:error, file, nil, error)
1213
end
1314
end
1415

15-
return :pass if output.empty?
16-
17-
[:fail, output]
16+
messages
1817
end
1918
end
2019
end

lib/overcommit/message_processor.rb

+3
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ def remove_ignored_messages(messages)
126126
end
127127

128128
def message_on_modified_line?(message)
129+
# Message without line number assumed to apply to entire file
130+
return true unless message.line
131+
129132
@hook.modified_lines_in_file(message.file).include?(message.line)
130133
end
131134
end

spec/overcommit/hook/pre_commit/json_syntax_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'spec_helper'
2+
require 'json'
23

34
describe Overcommit::Hook::PreCommit::JsonSyntax do
45
let(:config) { Overcommit::ConfigurationLoader.default_configuration }

0 commit comments

Comments
 (0)