forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.rb
120 lines (97 loc) · 2.66 KB
/
hook.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# General spec matcher logic for checking hook status and output.
class HookMatcher
def initialize(status, args)
options = args.empty? ? {} : { message: args.first }
@expected_status = status
@expected_message = options[:message]
end
def matches?(check)
result = [check.run].flatten
if result.is_a?(Array) &&
(result.first.is_a?(Overcommit::Hook::Message) || result.empty?)
messages_match?(result)
else
actual_status, actual_message = result
status_matches?(actual_status) && message_matches?(actual_message)
end
end
def messages_match?(messages)
case @expected_status
when :fail
messages.any? { |message| message.type == :error }
when :warn
messages.any? { |message| message.type == :warning }
else
messages.empty?
end
end
def status_matches?(actual_status)
@expected_status.nil? || actual_status == @expected_status
end
def message_matches?(actual_message)
return true if @expected_message.nil?
if @expected_message.is_a?(Regexp)
actual_message =~ @expected_message
else
actual_message == @expected_message
end
end
def failure_message(actual, error_message)
actual_status, actual_message = [actual].flatten
if status_matches?(actual_status)
error_message <<
" with message matching #{@expected_message.inspect}," \
" but was #{actual_message.inspect}"
end
error_message
end
end
# Can't use 'fail' as it is a reserved word.
RSpec::Matchers.define :fail_hook do |*args|
check_matcher = HookMatcher.new(:fail, args)
match do
check_matcher.matches?(actual)
end
failure_message do
check_matcher.failure_message(
actual,
'expected that the hook would fail'
)
end
failure_message_when_negated do
'expected that the hook would not fail'
end
description { 'fail' }
end
RSpec::Matchers.define :pass do |*args|
check_matcher = HookMatcher.new(:pass, args)
match do
check_matcher.matches?(actual)
end
failure_message do
check_matcher.failure_message(
actual,
'expected that the check would pass'
)
end
failure_message_when_negated do
'expected that the check would not pass'
end
description { 'pass the check' }
end
RSpec::Matchers.define :warn do |*args|
check_matcher = HookMatcher.new(:warn, args)
match do |check|
check_matcher.matches?(check)
end
failure_message do
check_matcher.failure_message(
actual,
'expected that the check would report a warning'
)
end
failure_message_when_negated do
'expected that the check would not report a warning'
end
description { 'report a warning' }
end