1
1
from contextlib import contextmanager
2
+ from datetime import datetime
2
3
from itertools import product
3
4
import os
4
5
import shutil
48
49
('module_without_license.py' , '#' , 'module_with_license_nospace.py' , True , ['--no-space-in-comment-prefix' ]),
49
50
('module_without_license.php' , '/*| *| */' , 'module_with_license.php' , True , ['--insert-license-after-regex' , '^<\\ ?php$' ]),
50
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' ]),
51
58
),
52
59
)),
53
60
)
@@ -69,6 +76,8 @@ def test_insert_license(license_file_path,
69
76
if new_src_file_expected :
70
77
with open (new_src_file_expected , encoding = encoding ) as expected_content_file :
71
78
expected_content = expected_content_file .read ()
79
+ if '--use-current-year' in args :
80
+ expected_content = expected_content .replace ("2017" , str (datetime .now ().year ))
72
81
new_file_content = path .open (encoding = encoding ).read ()
73
82
assert new_file_content == expected_content
74
83
@@ -127,23 +136,29 @@ def test_fuzzy_match_license(license_file_path,
127
136
128
137
129
138
@pytest .mark .parametrize (
130
- ('src_file_content' , 'expected_index' ),
139
+ ('src_file_content' , 'expected_index' , 'match_years_strictly' ),
131
140
(
132
- (['foo\n ' , 'bar\n ' ], None ),
133
- (['# License line 1\n ' , '# License line 2\n ' , '\n ' , 'foo\n ' , 'bar\n ' ], 0 ),
134
- (['\n ' , '# License line 1\n ' , '# License line 2\n ' , 'foo\n ' , 'bar\n ' ], 1 ),
141
+ (['foo\n ' , 'bar\n ' ], None , True ),
142
+ (['# License line 1\n ' , '# Copyright 2017\n ' , '\n ' , 'foo\n ' , 'bar\n ' ], 0 , True ),
143
+ (['\n ' , '# License line 1\n ' , '# Copyright 2017\n ' , 'foo\n ' , 'bar\n ' ], 1 , True ),
144
+ (['\n ' , '# License line 1\n ' , '# Copyright 2017\n ' , 'foo\n ' , 'bar\n ' ], 1 , False ),
145
+ (['# License line 1\n ' , '# Copyright 1984\n ' , '\n ' , 'foo\n ' , 'bar\n ' ], None , True ),
146
+ (['# License line 1\n ' , '# Copyright 1984\n ' , '\n ' , 'foo\n ' , 'bar\n ' ], 0 , False ),
147
+ (['\n ' , '# License line 1\n ' , '# Copyright 2013,2015-2016\n ' , 'foo\n ' , 'bar\n ' ], 1 , False ),
135
148
),
136
149
)
137
- def test_is_license_present (src_file_content , expected_index ):
150
+ def test_is_license_present (src_file_content , expected_index , match_years_strictly ):
138
151
license_info = LicenseInfo (
139
152
plain_license = "" ,
140
153
eol = "\n " ,
141
154
comment_start = "" ,
142
155
comment_prefix = "#" ,
143
156
comment_end = "" ,
144
157
num_extra_lines = 0 ,
145
- prefixed_license = ['# License line 1\n ' , '# License line 2\n ' ])
146
- assert expected_index == find_license_header_index (src_file_content , license_info , 5 )
158
+ prefixed_license = ['# License line 1\n ' , '# Copyright 2017\n ' ])
159
+ assert expected_index == find_license_header_index (
160
+ src_file_content , license_info , 5 , match_years_strictly = match_years_strictly
161
+ )
147
162
148
163
149
164
@pytest .mark .parametrize (
@@ -152,27 +167,35 @@ def test_is_license_present(src_file_content, expected_index):
152
167
'comment_style' ,
153
168
'fuzzy_match' ,
154
169
'new_src_file_expected' ,
155
- 'fail_check' ),
170
+ 'fail_check' ,
171
+ 'use_current_year' ),
156
172
map (lambda a : a [:1 ] + a [1 ], product ( # combine license files with other args
157
173
('LICENSE_with_trailing_newline.txt' , 'LICENSE_without_trailing_newline.txt' ),
158
174
(
159
- ('module_with_license.css' , '/*| *| */' , False , 'module_without_license.css' , True ),
175
+ ('module_with_license.css' , '/*| *| */' , False , 'module_without_license.css' , True , False ),
160
176
('module_with_license_and_few_words.css' , '/*| *| */' , False ,
161
- 'module_without_license_and_few_words.css' , True ),
162
- ('module_with_license_todo.css' , '/*| *| */' , False , None , True ),
163
- ('module_with_fuzzy_matched_license.css' , '/*| *| */' , False , None , False ),
164
- ('module_without_license.css' , '/*| *| */' , False , None , False ),
165
-
166
- ('module_with_license.py' , '#' , False , 'module_without_license.py' , True ),
167
- ('module_with_license_and_shebang.py' , '#' , False , 'module_without_license_and_shebang.py' , True ),
168
- ('init_with_license.py' , '#' , False , 'init_without_license.py' , True ),
169
- ('init_with_license_and_newline.py' , '#' , False , 'init_without_license.py' , True ),
177
+ 'module_without_license_and_few_words.css' , True , False ),
178
+ ('module_with_license_todo.css' , '/*| *| */' , False , None , True , False ),
179
+ ('module_with_fuzzy_matched_license.css' , '/*| *| */' , False , None , False , False ),
180
+ ('module_without_license.css' , '/*| *| */' , False , None , False , False ),
181
+
182
+ ('module_with_license.py' , '#' , False , 'module_without_license.py' , True , False ),
183
+ ('module_with_license_and_shebang.py' , '#' , False , 'module_without_license_and_shebang.py' , True , False ),
184
+ ('init_with_license.py' , '#' , False , 'init_without_license.py' , True , False ),
185
+ ('init_with_license_and_newline.py' , '#' , False , 'init_without_license.py' , True , False ),
170
186
# Fuzzy match
171
- ('module_with_license.css' , '/*| *| */' , True , 'module_without_license.css' , True ),
172
- ('module_with_license_todo.css' , '/*| *| */' , True , None , True ),
173
- ('module_with_fuzzy_matched_license.css' , '/*| *| */' , True , 'module_with_license_todo.css' , True ),
174
- ('module_without_license.css' , '/*| *| */' , True , None , False ),
175
- ('module_with_license_and_shebang.py' , '#' , True , 'module_without_license_and_shebang.py' , True ),
187
+ ('module_with_license.css' , '/*| *| */' , True , 'module_without_license.css' , True , False ),
188
+ ('module_with_license_todo.css' , '/*| *| */' , True , None , True , False ),
189
+ ('module_with_fuzzy_matched_license.css' , '/*| *| */' , True , 'module_with_license_todo.css' , True , False ),
190
+ ('module_without_license.css' , '/*| *| */' , True , None , False , False ),
191
+ ('module_with_license_and_shebang.py' , '#' , True , 'module_without_license_and_shebang.py' , True , False ),
192
+ # Strict and flexible years
193
+ ('module_with_stale_year_in_license.py' , '#' , False , None , False , False ),
194
+ ('module_with_stale_year_range_in_license.py' , '#' , False , None , False , False ),
195
+ ('module_with_license.py' , '#' , False , 'module_without_license.py' , True , True ),
196
+ ('module_with_stale_year_in_license.py' , '#' , False , 'module_without_license.py' , True , True ),
197
+ ('module_with_stale_year_range_in_license.py' , '#' , False , 'module_without_license.py' , True , True ),
198
+ ('module_with_badly_formatted_stale_year_range_in_license.py' , '#' , False , 'module_without_license.py' , True , True ),
176
199
),
177
200
)),
178
201
)
@@ -182,6 +205,7 @@ def test_remove_license(license_file_path,
182
205
fuzzy_match ,
183
206
new_src_file_expected ,
184
207
fail_check ,
208
+ use_current_year ,
185
209
tmpdir ):
186
210
with chdir_to_test_resources ():
187
211
path = tmpdir .join ('src_file_path' )
@@ -191,6 +215,8 @@ def test_remove_license(license_file_path,
191
215
'--comment-style' , comment_style ]
192
216
if fuzzy_match :
193
217
argv = ['--fuzzy-match-generates-todo' ] + argv
218
+ if use_current_year :
219
+ argv = ['--use-current-year' ] + argv
194
220
assert insert_license (argv ) == (1 if fail_check else 0 )
195
221
if new_src_file_expected :
196
222
with open (new_src_file_expected , encoding = 'utf-8' ) as expected_content_file :
0 commit comments