11from contextlib import contextmanager
22from datetime import datetime
33from itertools import product
4+ import io
45import os
56import shutil
7+ import sys
68import pytest
79
810from pre_commit_hooks .insert_license import main as insert_license , LicenseInfo
1214
1315
1416@pytest .mark .parametrize (
15- ('license_file_path' , 'src_file_path' , 'comment_prefix' , 'new_src_file_expected' , 'fail_check' , 'extra_args' ),
16- map (lambda a : a [:1 ] + a [1 ], product ( # combine license files with other args
17- ('LICENSE_with_trailing_newline.txt' , 'LICENSE_without_trailing_newline.txt' ),
18- (
19- ('module_without_license.py' , '#' , 'module_with_license.py' , True , None ),
20- ('module_without_license_skip.py' , '#' , None , False , None ),
21- ('module_with_license.py' , '#' , None , False , None ),
22- ('module_with_license_todo.py' , '#' , None , True , None ),
23-
24- ('module_without_license.jinja' , '{#||#}' , 'module_with_license.jinja' , True , None ),
25- ('module_without_license_skip.jinja' , '{#||#}' , None , False , None ),
26- ('module_with_license.jinja' , '{#||#}' , None , False , None ),
27- ('module_with_license_todo.jinja' , '{#||#}' , None , True , None ),
28-
29- ('module_without_license_and_shebang.py' , '#' , 'module_with_license_and_shebang.py' , True , None ),
30- ('module_without_license_and_shebang_skip.py' , '#' , None , False , None ),
31- ('module_with_license_and_shebang.py' , '#' , None , False , None ),
32- ('module_with_license_and_shebang_todo.py' , '#' , None , True , None ),
33-
34- ('module_without_license.groovy' , '//' , 'module_with_license.groovy' , True , None ),
35- ('module_without_license_skip.groovy' , '//' , None , False , None ),
36- ('module_with_license.groovy' , '//' , None , False , None ),
37- ('module_with_license_todo.groovy' , '//' , None , True , None ),
38-
39- ('module_without_license.css' , '/*| *| */' , 'module_with_license.css' , True , None ),
40- ('module_without_license_and_few_words.css' , '/*| *| */' ,
41- 'module_with_license_and_few_words.css' , True , None ), # Test fuzzy match does not match greedily
42- ('module_without_license_skip.css' , '/*| *| */' , None , False , None ),
43- ('module_with_license.css' , '/*| *| */' , None , False , None ),
44- ('module_with_license_todo.css' , '/*| *| */' , None , True , None ),
45-
46- ('main_without_license.cpp' , '/*|\t | */' , 'main_with_license.cpp' , True , None ),
47- ('main_iso8859_without_license.cpp' , '/*|\t | */' , 'main_iso8859_with_license.cpp' , True , None ),
48- ('module_without_license.txt' , '' , 'module_with_license_noprefix.txt' , True , None ),
49- ('module_without_license.py' , '#' , 'module_with_license_nospace.py' , True , ['--no-space-in-comment-prefix' ]),
50- ('module_without_license.php' , '/*| *| */' , 'module_with_license.php' , True , ['--insert-license-after-regex' , '^<\\ ?php$' ]),
51- ('module_without_license.py' , '#' , 'module_with_license_noeol.py' , True , ['--no-extra-eol' ]),
52-
53- ('module_without_license.groovy' , '//' , 'module_with_license.groovy' , True , ['--use-current-year' ]),
54- ('module_with_stale_year_in_license.py' , '#' , 'module_with_year_range_in_license.py' , True , ['--use-current-year' ]),
55- ('module_with_stale_year_range_in_license.py' , '#' , 'module_with_year_range_in_license.py' , True , ['--use-current-year' ]),
56- ('module_with_badly_formatted_stale_year_range_in_license.py' , '#' , 'module_with_badly_formatted_stale_year_range_in_license.py' , False ,
57- ['--use-current-year' ]),
17+ ("license_file_path" , "src_file_path" , "comment_prefix" , "new_src_file_expected" , "message_expected" , "fail_check" , "extra_args" ),
18+ map (
19+ lambda a : a [:1 ] + a [1 ],
20+ product ( # combine license files with other args
21+ ("LICENSE_with_trailing_newline.txt" , "LICENSE_without_trailing_newline.txt" ),
22+ (
23+ ("module_without_license.py" , "#" , "module_with_license.py" , "" , True , None ),
24+ ("module_without_license_skip.py" , "#" , None , "" , False , None ),
25+ ("module_with_license.py" , "#" , None , "" , False , None ),
26+ ("module_with_license_todo.py" , "#" , None , "" , True , None ),
27+ ("module_without_license.jinja" , "{#||#}" , "module_with_license.jinja" , "" , True , None ),
28+ ("module_without_license_skip.jinja" , "{#||#}" , None , "" , False , None ),
29+ ("module_with_license.jinja" , "{#||#}" , None , "" , False , None ),
30+ ("module_with_license_todo.jinja" , "{#||#}" , None , "" , True , None ),
31+ ("module_without_license_and_shebang.py" , "#" , "module_with_license_and_shebang.py" , "" , True , None ),
32+ ("module_without_license_and_shebang_skip.py" , "#" , None , "" , False , None ),
33+ ("module_with_license_and_shebang.py" , "#" , None , "" , False , None ),
34+ ("module_with_license_and_shebang_todo.py" , "#" , None , "" , True , None ),
35+ ("module_without_license.groovy" , "//" , "module_with_license.groovy" , "" , True , None ),
36+ ("module_without_license_skip.groovy" , "//" , None , "" , False , None ),
37+ ("module_with_license.groovy" , "//" , None , "" , False , None ),
38+ ("module_with_license_todo.groovy" , "//" , None , "" , True , None ),
39+ ("module_without_license.css" , "/*| *| */" , "module_with_license.css" , "" , True , None ),
40+ (
41+ "module_without_license_and_few_words.css" ,
42+ "/*| *| */" ,
43+ "module_with_license_and_few_words.css" ,
44+ "" ,
45+ True ,
46+ None ,
47+ ), # Test fuzzy match does not match greedily
48+ ("module_without_license_skip.css" , "/*| *| */" , None , "" , False , None ),
49+ ("module_with_license.css" , "/*| *| */" , None , "" , False , None ),
50+ ("module_with_license_todo.css" , "/*| *| */" , None , "" , True , None ),
51+ ("main_without_license.cpp" , "/*|\t | */" , "main_with_license.cpp" , "" , True , None ),
52+ ("main_iso8859_without_license.cpp" , "/*|\t | */" , "main_iso8859_with_license.cpp" , "" , True , None ),
53+ ("module_without_license.txt" , "" , "module_with_license_noprefix.txt" , "" , True , None ),
54+ ("module_without_license.py" , "#" , "module_with_license_nospace.py" , "" , True , ["--no-space-in-comment-prefix" ]),
55+ ("module_without_license.php" , "/*| *| */" , "module_with_license.php" , "" , True , ["--insert-license-after-regex" , "^<\\ ?php$" ]),
56+ ("module_without_license.py" , "#" , "module_with_license_noeol.py" , "" , True , ["--no-extra-eol" ]),
57+ ("module_without_license.groovy" , "//" , "module_with_license.groovy" , "" , True , ["--use-current-year" ]),
58+ ("module_with_stale_year_in_license.py" , "#" , "module_with_year_range_in_license.py" , "" , True , ["--use-current-year" ]),
59+ ("module_with_stale_year_range_in_license.py" , "#" , "module_with_year_range_in_license.py" , "" , True , ["--use-current-year" ]),
60+ (
61+ "module_with_badly_formatted_stale_year_range_in_license.py" ,
62+ "#" ,
63+ "module_with_badly_formatted_stale_year_range_in_license.py" ,
64+ "module_with_badly_formatted_stale_year_range_in_license.py" ,
65+ True ,
66+ ["--use-current-year" ],
67+ ),
68+ ),
5869 ),
59- )) ,
70+ ),
6071)
6172def test_insert_license (license_file_path ,
6273 src_file_path ,
6374 comment_prefix ,
6475 new_src_file_expected ,
76+ message_expected ,
6577 fail_check ,
6678 extra_args ,
6779 tmpdir ):
6880 encoding = 'ISO-8859-1' if 'iso8859' in src_file_path else 'utf-8'
6981 with chdir_to_test_resources ():
70- path = tmpdir .join (' src_file_path' )
82+ path = tmpdir .join (src_file_path )
7183 shutil .copy (src_file_path , path .strpath )
7284 args = ['--license-filepath' , license_file_path , '--comment-style' , comment_prefix , path .strpath ]
7385 if extra_args is not None :
7486 args .extend (extra_args )
75- assert insert_license (args ) == (1 if fail_check else 0 )
87+
88+ with capture_stdout () as stdout :
89+ assert insert_license (args ) == (1 if fail_check else 0 )
90+ assert message_expected in stdout .getvalue ()
91+
7692 if new_src_file_expected :
7793 with open (new_src_file_expected , encoding = encoding ) as expected_content_file :
7894 expected_content = expected_content_file .read ()
@@ -82,6 +98,33 @@ def test_insert_license(license_file_path,
8298 assert new_file_content == expected_content
8399
84100
101+ @pytest .mark .parametrize (
102+ ('license_file_path' , 'src_file_path' , 'comment_prefix' ),
103+ map (lambda a : a [:1 ] + a [1 ], product ( # combine license files with other args
104+ ('LICENSE_with_trailing_newline.txt' , 'LICENSE_without_trailing_newline.txt' ),
105+ (
106+ ('module_with_license.groovy' , '//' ),
107+ ('module_with_license_and_numbers.py' , '#' ),
108+ ('module_with_year_range_in_license.py' , '#' ),
109+ ('module_with_spaced_year_range_in_license.py' , '#' ),
110+ ),
111+ )),
112+ )
113+ def test_insert_license_current_year_already_there (license_file_path ,
114+ src_file_path ,
115+ comment_prefix ,
116+ tmpdir ):
117+ with chdir_to_test_resources ():
118+ with open (src_file_path , encoding = "utf-8" ) as src_file :
119+ input_contents = src_file .read ().replace ("2017" , str (datetime .now ().year ))
120+ path = tmpdir .join ('src_file_path' )
121+ with open (path .strpath , "w" , encoding = "utf-8" ) as input_file :
122+ input_file .write (input_contents )
123+
124+ args = ['--license-filepath' , license_file_path , '--comment-style' , comment_prefix , "--use-current-year" , path .strpath ]
125+ assert insert_license (args ) == 0
126+
127+
85128@pytest .mark .parametrize (
86129 ('license_file_path' , 'src_file_path' , 'comment_style' , 'new_src_file_expected' , 'fail_check' ),
87130 map (lambda a : a [:1 ] + a [1 ], product ( # combine license files with other args
@@ -229,8 +272,18 @@ def test_remove_license(license_file_path,
229272def chdir_to_test_resources ():
230273 prev_dir = os .getcwd ()
231274 try :
232- res_dir = os .path .dirname (os .path .realpath (__file__ )) + '/resources'
275+ res_dir = os .path .dirname (os .path .realpath (__file__ )) + '/resources'
233276 os .chdir (res_dir )
234277 yield
235278 finally :
236279 os .chdir (prev_dir )
280+
281+
282+ @contextmanager
283+ def capture_stdout ():
284+ try :
285+ captured = io .StringIO ()
286+ sys .stdout = captured
287+ yield captured
288+ finally :
289+ sys .stdout = sys .__stdout__
0 commit comments