-
Notifications
You must be signed in to change notification settings - Fork 3
/
hooktests.py
291 lines (242 loc) · 10.4 KB
/
hooktests.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import unittest
import time
from mock import MagicMock
import hooklib_input
from hooklib import hookrunner, basehook, parallelhookrunner
from hooklib_input import inputparser
from hooklib_git import *
from hooklib_hg import *
import os
import sys
ERROR_MSG = "ERROR ABC"
ERROR_MSG2 = "ERROR XYZ"
class passinghook(basehook):
def check(self, log, revdata):
return True
class failinghook(basehook):
def check(self, log, revdata):
log.write(ERROR_MSG)
return False
class failinghook2(basehook):
def check(self, log, revdata):
log.write(ERROR_MSG2)
return False
class slowfailinghook(basehook):
def check(self, log, revdata):
time.sleep(0.1)
log.write(ERROR_MSG)
return False
class testhookrunner(unittest.TestCase):
def test_passing_hook(self):
"""Passing hook works"""
runner = hookrunner()
runner.register(passinghook)
assert(runner.evaluate()) == True
def test_failing_hook(self):
"""Failing hook fails with error message recorded"""
runner = hookrunner()
runner.register(failinghook)
assert(runner.evaluate()) == False
assert(runner.log.read()) == [ERROR_MSG]
def test_passing_failing_hook(self):
"""Passing than failing hook, fails overall"""
runner = hookrunner()
runner.register(passinghook)
runner.register(failinghook)
assert(runner.evaluate()) == False
def test_failing_stop_hook(self):
"""Two failing hook, second one should not run"""
runner = hookrunner()
runner.register(failinghook)
runner.register(failinghook2)
assert(runner.evaluate()) == False
assert(runner.log.read()) == [ERROR_MSG]
def test_failing_non_blocking_hook(self):
"""Two failing hook, non blocking, all evaluated"""
runner = hookrunner()
runner.register(failinghook, blocking=False)
runner.register(failinghook2)
assert(runner.evaluate()) == False
assert(runner.log.read()) == [ERROR_MSG, ERROR_MSG2]
class testparallelhookrunner(unittest.TestCase):
def test_speed(self):
"""parallel hook runner should run hooks really in parallel"""
runner = parallelhookrunner()
for i in range(100):
runner.register(slowfailinghook)
t1 = time.time()
assert(runner.evaluate()) == False
t2 = time.time()
# 100 * 0.1 = 10s if the run was not parallel
# here we expect less than 0.5s
assert (t2-t1) < 0.5
def test_aggregation(self):
"""parallel hook runner should aggregate log of all the failures"""
runner = parallelhookrunner()
for i in range(3):
runner.register(slowfailinghook)
runner.register(failinghook2)
runner.evaluate()
assert len(runner.log.read()) == 4
assert ERROR_MSG2 in runner.log.read()
def test_correctness(self):
"""parallel hook runner with failing + passing hook
should return failure"""
runner = parallelhookrunner()
for i in range(3):
runner.register(slowfailinghook)
for i in range(3):
runner.register(passinghook)
assert(runner.evaluate() == False)
class testscmresolution(unittest.TestCase):
"""Checking that we get the right SCM parser for different hook type"""
def setUp(self):
self.origargv = list(sys.argv)
self.origenv = os.environ.copy()
def tearDown(self):
os.environ = self.origenv
sys.argv = self.origargv
def test_git_postupdate(self):
os.environ["GIT_DIR"] = "."
sys.argv = ["program.name", "a"*40]
revdata = inputparser.fromphase('post-update').parse()
assert(revdata.revs == ["a"*40])
def test_hg_postupdate(self):
os.environ["HG_NODE"] = "."
with self.assertRaises(NotImplementedError):
revdata = inputparser.fromphase('post-update')
def test_git_update(self):
os.environ["GIT_DIR"] = "."
sys.argv = ["program.name", "a"*40, "0"*40, "1"*40]
revdata = inputparser.fromphase('update').parse()
assert(revdata.refname == "a"*40)
assert(revdata.old == "0"*40)
assert(revdata.new == "1"*40)
def test_hg_update(self):
os.environ["HG_NODE"] = "a"*40
revdata = inputparser.fromphase('update').parse()
assert(revdata.revs == ["a"*40])
def test_git_precommit(self):
os.environ["GIT_DIR"] = "."
sys.argv = ["program.name"]
revdata = inputparser.fromphase('pre-commit').parse()
assert(isinstance(revdata, gitinforesolver))
def test_hg_precommit(self):
os.environ["HG_NODE"] = "."
with self.assertRaises(NotImplementedError):
revdata = inputparser.fromphase('pre-commit')
def test_unknown_hookname(self):
with self.assertRaises(NotImplementedError):
revdata = inputparser.fromphase('unknown-phase')
def test_gitapplypatchmsg(self):
sys.argv = ['program.name', 'messagefile']
revdata = inputparser.fromphase('applypatch-msg').parse()
assert(revdata.messagefile == 'messagefile')
def test_gitpreapplypatch(self):
parser = inputparser.fromphase('pre-applypatch')
assert(isinstance(parser, gitpreapplypatchinputparser))
assert(isinstance(parser.parse(), gitinforesolver))
def test_gitpostapplypatch(self):
parser = inputparser.fromphase('post-applypatch')
assert(isinstance(parser, gitpostapplypatchinputparser))
assert(isinstance(parser.parse(), gitinforesolver))
def test_cascade_hook_type(self):
"""If you write a hook for hg and git and they don't
have the same hook phase available, you can specify what
phase you want for each SCM
The following hook will run at the update phase for
hg repos and post-applypatch for git repos
A hg repo. If both are available the order of the tuple
is honored.
"""
os.environ["HG_NODE"] = "a"*40
parser = inputparser.fromphases((('hg', 'update'),
('git', 'post-applypatch')))
assert(isinstance(parser, hgupdateinputparser))
del os.environ["HG_NODE"]
parser = inputparser.fromphases((('hg', 'update'),
('git', 'post-applypatch')))
assert(isinstance(parser, gitpostapplypatchinputparser))
def test_cascade_hook_notfound(self):
os.environ["HG_NODE"] = "a"*40
with self.assertRaises(NotImplementedError):
parser = inputparser.fromphases((('hg', 'post-applypatch'),
('git', 'blah')))
def test_gitpreparecommitmsg(self):
# possible options
# message, template, merge, squash, commit
cases = (
# valid arg, list of args
(True, 'commitlogmsg', 'message'),
(False, 'commitlogmsg', 'message', 'a'*40),
(True, 'commitlogmsg', 'template'),
(False, 'commitlogmsg', 'template', 'a'*40),
(True, 'commitlogmsg', 'merge'),
(False, 'commitlogmsg', 'merge', 'a'*40),
(True, 'commitlogmsg', 'squash'),
(False, 'commitlogmsg', 'squash', 'a'*40),
(False, 'commitlogmsg', 'commit'),
(True, 'commitlogmsg', 'commit', 'a'*40),
(False, 'commitlogmsg', 'illegal'),
)
for case in cases:
valid = case[0]
args = case[1:]
sys.argv = ['program.name'] + list(args)
parser = inputparser.fromphase('prepare-commit-msg')
assert(isinstance(parser, gitpreparecommitmsginputparser))
if valid:
parser.parse() # not exception
else:
with self.assertRaises(ValueError):
parser.parse()
def test_gitcommitmsg(self):
sys.argv = ['program.name', 'messagefile']
revdata = inputparser.fromphase('commit-msg').parse()
assert(revdata.messagefile == 'messagefile')
def test_gitpostcommit(self):
parser = inputparser.fromphase('post-commit')
assert(isinstance(parser, gitpostcommitinputparser))
def test_gitprerebase(self):
sys.argv = ['program.name', 'upstream', 'rebased']
parser = inputparser.fromphase('pre-rebase')
assert(isinstance(parser, gitprerebaseinputparser))
revdata = parser.parse()
assert(revdata.upstream == 'upstream')
assert(revdata.rebased == 'rebased')
def test_gitprerebasecurrentbranch(self):
sys.argv = ['program.name', 'upstream']
parser = inputparser.fromphase('pre-rebase')
assert(isinstance(parser, gitprerebaseinputparser))
revdata = parser.parse()
assert(revdata.upstream == 'upstream')
assert(revdata.rebased is None)
def test_gitpreautogc(self):
parser = inputparser.fromphase('pre-auto-gc')
assert(isinstance(parser, gitpreautogcinputparser))
def test_gitprereceive(self):
revs = (('a'*40, 'b'*40, 'refs/heads/master'),
('c'*40, 'd'*40, 'refs/heads/stable'))
dummyinput = [' '.join(r)+'\n' for r in revs]
hooklib_input.readlines = MagicMock(return_value=dummyinput)
revdata = inputparser.fromphase('pre-receive').parse()
assert(revdata.receivedrevs == revs)
def test_gitpostreceive(self):
revs = (('a'*40, 'b'*40, 'refs/heads/master'),
('c'*40, 'd'*40, 'refs/heads/stable'))
dummyinput = [' '.join(r)+'\n' for r in revs]
hooklib_input.readlines = MagicMock(return_value=dummyinput)
revdata = inputparser.fromphase('post-receive').parse()
assert(revdata.receivedrevs == revs)
def test_gitprepush(self):
revs = (('refs/heads/master', 'a'*40, 'refs/heads/foreign', 'b'*40),
('refs/heads/master', 'a'*40, 'refs/heads/foreign', '0'*40))
dummyinput = [' '.join(r)+'\n' for r in revs]
hooklib_input.readlines = MagicMock(return_value=dummyinput)
revdata = inputparser.fromphase('pre-push').parse()
assert(revdata.revstobepushed == revs)
# TODO post-checkout, post-merge, push-to-checkout, post-rewrite
# TODO add documentation for what is available for each kind of hooks
# see https://git-scm.com/docs/githooks
if __name__ == '__main__':
unittest.main()