11from __future__ import absolute_import
22from __future__ import unicode_literals
33
4+ import io
45import mock
56import os .path
67import pytest
@@ -25,12 +26,20 @@ def test_install_python_repo_in_env(tmpdir_factory, store):
2526 assert os .path .exists (os .path .join (store .directory , repo .sha , 'py_env' ))
2627
2728
28- def _test_hook_repo (tmpdir_factory , store , repo_path , hook_id , args , expected ):
29+ def _test_hook_repo (
30+ tmpdir_factory ,
31+ store ,
32+ repo_path ,
33+ hook_id ,
34+ args ,
35+ expected ,
36+ expected_return_code = 0 ,
37+ ):
2938 path = make_repo (tmpdir_factory , repo_path )
3039 config = make_config_from_repo (path )
3140 repo = Repository .create (config , store )
3241 ret = repo .run_hook (hook_id , args )
33- assert ret [0 ] == 0
42+ assert ret [0 ] == expected_return_code
3443 assert ret [1 ] == expected
3544
3645
@@ -102,6 +111,69 @@ def test_run_a_script_hook(tmpdir_factory, store):
102111 )
103112
104113
114+ @pytest .mark .integration
115+ def test_pcre_hook_no_match (tmpdir_factory , store ):
116+ path = git_dir (tmpdir_factory )
117+ with local .cwd (path ):
118+ with io .open ('herp' , 'w' ) as herp :
119+ herp .write ('foo' )
120+
121+ with io .open ('derp' , 'w' ) as derp :
122+ derp .write ('bar' )
123+
124+ _test_hook_repo (
125+ tmpdir_factory , store , 'pcre_hooks_repo' ,
126+ 'regex-with-quotes' , ['herp' , 'derp' ], '' ,
127+ )
128+
129+ _test_hook_repo (
130+ tmpdir_factory , store , 'pcre_hooks_repo' ,
131+ 'other-regex' , ['herp' , 'derp' ], '' ,
132+ )
133+
134+
135+ @pytest .mark .integration
136+ def test_pcre_hook_matching (tmpdir_factory , store ):
137+ path = git_dir (tmpdir_factory )
138+ with local .cwd (path ):
139+ with io .open ('herp' , 'w' ) as herp :
140+ herp .write ("\n herpfoo'bard\n " )
141+
142+ with io .open ('derp' , 'w' ) as derp :
143+ derp .write ('[INFO] information yo\n ' )
144+
145+ _test_hook_repo (
146+ tmpdir_factory , store , 'pcre_hooks_repo' ,
147+ 'regex-with-quotes' , ['herp' , 'derp' ], "herp:2:herpfoo'bard\n " ,
148+ expected_return_code = 123 ,
149+ )
150+
151+ _test_hook_repo (
152+ tmpdir_factory , store , 'pcre_hooks_repo' ,
153+ 'other-regex' , ['herp' , 'derp' ], 'derp:1:[INFO] information yo\n ' ,
154+ expected_return_code = 123 ,
155+ )
156+
157+
158+ @pytest .mark .integration
159+ def test_pcre_many_files (tmpdir_factory , store ):
160+ # This is intended to simulate lots of passing files and one failing file
161+ # to make sure it still fails. This is not the case when naively using
162+ # a system hook with `grep -H -n '...'` and expected_return_code=123.
163+ path = git_dir (tmpdir_factory )
164+ with local .cwd (path ):
165+ with io .open ('herp' , 'w' ) as herp :
166+ herp .write ('[INFO] info\n ' )
167+
168+ _test_hook_repo (
169+ tmpdir_factory , store , 'pcre_hooks_repo' ,
170+ 'other-regex' ,
171+ ['/dev/null' ] * 15000 + ['herp' ],
172+ 'herp:1:[INFO] info\n ' ,
173+ expected_return_code = 123 ,
174+ )
175+
176+
105177@pytest .mark .integration
106178def test_cwd_of_hook (tmpdir_factory , store ):
107179 # Note: this doubles as a test for `system` hooks
0 commit comments