11from __future__ import absolute_import
22from __future__ import unicode_literals
33
4+ import collections
45import io
56import os .path
67import re
3637from testing .util import xfailif_windows_no_ruby
3738
3839
40+ def _norm_out (b ):
41+ return b .replace (b'\r \n ' , b'\n ' )
42+
43+
3944def _test_hook_repo (
4045 tempdir_factory ,
4146 store ,
@@ -54,7 +59,7 @@ def _test_hook_repo(
5459 ]
5560 ret = repo .run_hook (hook_dict , args )
5661 assert ret [0 ] == expected_return_code
57- assert ret [1 ]. replace ( b' \r \n ' , b' \n ' ) == expected
62+ assert _norm_out ( ret [1 ]) == expected
5863
5964
6065@pytest .mark .integration
@@ -114,7 +119,7 @@ def run_on_version(version, expected_output):
114119 ]
115120 ret = repo .run_hook (hook_dict , [])
116121 assert ret [0 ] == 0
117- assert ret [1 ]. replace ( b' \r \n ' , b' \n ' ) == expected_output
122+ assert _norm_out ( ret [1 ]) == expected_output
118123
119124 run_on_version ('python3.4' , b'3.4\n []\n Hello World\n ' )
120125 run_on_version ('python3.5' , b'3.5\n []\n Hello World\n ' )
@@ -277,25 +282,6 @@ def test_missing_executable(tempdir_factory, store):
277282 )
278283
279284
280- @pytest .mark .integration
281- def test_missing_pcre_support (tempdir_factory , store ):
282- orig_find_executable = parse_shebang .find_executable
283-
284- def no_grep (exe , ** kwargs ):
285- if exe == pcre .GREP :
286- return None
287- else :
288- return orig_find_executable (exe , ** kwargs )
289-
290- with mock .patch .object (parse_shebang , 'find_executable' , no_grep ):
291- _test_hook_repo (
292- tempdir_factory , store , 'pcre_hooks_repo' ,
293- 'regex-with-quotes' , ['/dev/null' ],
294- 'Executable `{}` not found' .format (pcre .GREP ).encode ('UTF-8' ),
295- expected_return_code = 1 ,
296- )
297-
298-
299285@pytest .mark .integration
300286def test_run_a_script_hook (tempdir_factory , store ):
301287 _test_hook_repo (
@@ -330,85 +316,88 @@ def test_run_hook_with_curly_braced_arguments(tempdir_factory, store):
330316 )
331317
332318
333- @xfailif_no_pcre_support
334- @pytest .mark .integration
335- def test_pcre_hook_no_match (tempdir_factory , store ):
336- path = git_dir (tempdir_factory )
337- with cwd (path ):
338- with io .open ('herp' , 'w' ) as herp :
339- herp .write ('foo' )
340-
341- with io .open ('derp' , 'w' ) as derp :
342- derp .write ('bar' )
343-
344- _test_hook_repo (
345- tempdir_factory , store , 'pcre_hooks_repo' ,
346- 'regex-with-quotes' , ['herp' , 'derp' ], b'' ,
347- )
348-
349- _test_hook_repo (
350- tempdir_factory , store , 'pcre_hooks_repo' ,
351- 'other-regex' , ['herp' , 'derp' ], b'' ,
352- )
353-
319+ def _make_grep_repo (language , entry , store , args = ()):
320+ config = collections .OrderedDict ((
321+ ('repo' , 'local' ),
322+ (
323+ 'hooks' , [
324+ collections .OrderedDict ((
325+ ('id' , 'grep-hook' ),
326+ ('name' , 'grep-hook' ),
327+ ('language' , language ),
328+ ('entry' , entry ),
329+ ('args' , args ),
330+ ('types' , ['text' ]),
331+ )),
332+ ],
333+ ),
334+ ))
335+ repo = Repository .create (config , store )
336+ (_ , hook ), = repo .hooks
337+ return repo , hook
354338
355- @xfailif_no_pcre_support
356- @pytest .mark .integration
357- def test_pcre_hook_matching (tempdir_factory , store ):
358- path = git_dir (tempdir_factory )
359- with cwd (path ):
360- with io .open ('herp' , 'w' ) as herp :
361- herp .write ("\n herpfoo'bard\n " )
362339
363- with io .open ('derp' , 'w' ) as derp :
364- derp .write ('[INFO] information yo\n ' )
340+ @pytest .fixture
341+ def greppable_files (tmpdir ):
342+ with tmpdir .as_cwd ():
343+ cmd_output ('git' , 'init' , '.' )
344+ tmpdir .join ('f1' ).write_binary (b"hello'hi\n world\n " )
345+ tmpdir .join ('f2' ).write_binary (b'foo\n bar\n baz\n ' )
346+ tmpdir .join ('f3' ).write_binary (b'[WARN] hi\n ' )
347+ yield tmpdir
365348
366- _test_hook_repo (
367- tempdir_factory , store , 'pcre_hooks_repo' ,
368- 'regex-with-quotes' , ['herp' , 'derp' ], b"herp:2:herpfoo'bard\n " ,
369- expected_return_code = 1 ,
370- )
371349
372- _test_hook_repo (
373- tempdir_factory , store , 'pcre_hooks_repo' ,
374- 'other-regex' , ['herp' , 'derp' ], b'derp:1:[INFO] information yo\n ' ,
375- expected_return_code = 1 ,
376- )
350+ class TestPygrep (object ):
351+ language = 'pygrep'
377352
353+ def test_grep_hook_matching (self , greppable_files , store ):
354+ repo , hook = _make_grep_repo (self .language , 'ello' , store )
355+ ret , out , _ = repo .run_hook (hook , ('f1' , 'f2' , 'f3' ))
356+ assert ret == 1
357+ assert _norm_out (out ) == b"f1:1:hello'hi\n "
378358
379- @xfailif_no_pcre_support
380- @pytest .mark .integration
381- def test_pcre_hook_case_insensitive_option (tempdir_factory , store ):
382- path = git_dir (tempdir_factory )
383- with cwd (path ):
384- with io .open ('herp' , 'w' ) as herp :
385- herp .write ('FoOoOoObar\n ' )
359+ def test_grep_hook_case_insensitive (self , greppable_files , store ):
360+ repo , hook = _make_grep_repo (self .language , 'ELLO' , store , args = ['-i' ])
361+ ret , out , _ = repo .run_hook (hook , ('f1' , 'f2' , 'f3' ))
362+ assert ret == 1
363+ assert _norm_out (out ) == b"f1:1:hello'hi\n "
386364
387- _test_hook_repo (
388- tempdir_factory , store , 'pcre_hooks_repo' ,
389- ' regex-with-grep-args' , [ 'herp' ], b'herp:1:FoOoOoObar \n ' ,
390- expected_return_code = 1 ,
391- )
365+ @ pytest . mark . parametrize ( 'regex' , ( 'nope' , "foo'bar" , r'^\[INFO\]' ))
366+ def test_grep_hook_not_matching ( self , regex , greppable_files , store ):
367+ repo , hook = _make_grep_repo ( self . language , regex , store )
368+ ret , out , _ = repo . run_hook ( hook , ( 'f1' , 'f2' , 'f3' ))
369+ assert ( ret , out ) == ( 0 , b'' )
392370
393371
394372@xfailif_no_pcre_support
395- @pytest .mark .integration
396- def test_pcre_many_files (tempdir_factory , store ):
397- # This is intended to simulate lots of passing files and one failing file
398- # to make sure it still fails. This is not the case when naively using
399- # a system hook with `grep -H -n '...'` and expected_return_code=1.
400- path = git_dir (tempdir_factory )
401- with cwd (path ):
402- with io .open ('herp' , 'w' ) as herp :
403- herp .write ('[INFO] info\n ' )
404-
405- _test_hook_repo (
406- tempdir_factory , store , 'pcre_hooks_repo' ,
407- 'other-regex' ,
408- ['/dev/null' ] * 15000 + ['herp' ],
409- b'herp:1:[INFO] info\n ' ,
410- expected_return_code = 1 ,
411- )
373+ class TestPCRE (TestPygrep ):
374+ """organized as a class for xfailing pcre"""
375+ language = 'pcre'
376+
377+ def test_pcre_hook_many_files (self , greppable_files , store ):
378+ # This is intended to simulate lots of passing files and one failing
379+ # file to make sure it still fails. This is not the case when naively
380+ # using a system hook with `grep -H -n '...'`
381+ repo , hook = _make_grep_repo ('pcre' , 'ello' , store )
382+ ret , out , _ = repo .run_hook (hook , (os .devnull ,) * 15000 + ('f1' ,))
383+ assert ret == 1
384+ assert _norm_out (out ) == b"f1:1:hello'hi\n "
385+
386+ def test_missing_pcre_support (self , greppable_files , store ):
387+ orig_find_executable = parse_shebang .find_executable
388+
389+ def no_grep (exe , ** kwargs ):
390+ if exe == pcre .GREP :
391+ return None
392+ else :
393+ return orig_find_executable (exe , ** kwargs )
394+
395+ with mock .patch .object (parse_shebang , 'find_executable' , no_grep ):
396+ repo , hook = _make_grep_repo ('pcre' , 'ello' , store )
397+ ret , out , _ = repo .run_hook (hook , ('f1' , 'f2' , 'f3' ))
398+ assert ret == 1
399+ expected = 'Executable `{}` not found' .format (pcre .GREP ).encode ()
400+ assert out == expected
412401
413402
414403def _norm_pwd (path ):
@@ -703,7 +692,7 @@ def test_local_python_repo(store):
703692 (_ , hook ), = repo .hooks
704693 ret = repo .run_hook (hook , ('filename' ,))
705694 assert ret [0 ] == 0
706- assert ret [1 ]. replace ( b' \r \n ' , b' \n ' ) == b"['filename']\n Hello World\n "
695+ assert _norm_out ( ret [1 ]) == b"['filename']\n Hello World\n "
707696
708697
709698def test_hook_id_not_present (tempdir_factory , store , fake_log_handler ):
0 commit comments