-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.py
994 lines (878 loc) · 48.5 KB
/
test.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
# test.py
#
# Copyright (c) 2021 - 2024 Marius Zwicker
# All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from asyncio import subprocess
import datetime
import functools
import os
import pathlib
import shutil
import subprocess
import tempfile
import textwrap
import unittest
import license_tools
BASE = pathlib.Path(__file__).resolve().absolute().parent
# pin the year to 2022 which is the year tests have been
# written to match the output on
os.putenv('LICTOOLS_OVERRIDE_YEAR', '2022')
def _to_unix(input: str):
return input.replace('\r\n', '\n')
def _to_dos(input: str):
return _to_unix(input).replace('\n', '\r\n')
def _render_endings(input: str):
return input.replace('\r', '\\r').replace('\n', '\\n\n')
class TestFileFilter(unittest.TestCase):
def test_recursive_includes(self):
EXPRESSIONS = [
('foo.txt', ['*']),
('foo.txt', ['*.txt']),
('asterisk?.txt', ['asterisk[?].txt']),
('asterisk.txt', ['asterisk.txt']),
('asteris!.txt', ['asteris!.txt']),
('/foo.txt', ['/*.txt']),
('foo.txt', ['**********.txt']),
('foo.txt', ['**/*']),
('bla/foo.txt', ['*/*.txt']),
('bla/foo.txt', ['**/*.txt']),
('bla/da/foo.txt', ['**/*.txt']),
('bla/da/foo.txt', ['**/*[t]']),
('bla/da/foo.txt', ['**/*']),
('bla/da/foo.txt', ['bla/**/*']),
('bla/da/d1u/foo.txt', ['bla/**/*']),
('bla/da/foo.txt', ['**/*.txt']),
('da_foo.txt', ['[de]a_foo.txt']),
('da_foo.txt', ['da_foo.t?t']),
]
for file_rel, includes in EXPRESSIONS:
try:
self.assertTrue(license_tools.FileFilter.is_included(file_rel, includes, []))
except:
print(f"file_rel={file_rel} includes={includes}")
raise
EXPRESSIONS = [
('foo/foo.txt', ['*']),
('asteriskk.txt', ['asterisk[?].txt']),
('foo/foo.txt', ['**.txt']),
('bla/foo.doc', ['*/*.txt']),
('bla/da/foo.txt', ['*/*.txt']),
('da_foo.txt', ['[!de]a_foo.txt']),
('da_foo.txt', ['?.txt']),
('bla/da/foo_txt', ['**/*.txt']),
]
for file_rel, includes in EXPRESSIONS:
try:
self.assertFalse(license_tools.FileFilter.is_included(file_rel, includes, []))
except:
print(f"file_rel={file_rel} includes={includes}")
raise
class TestDateUtils(unittest.TestCase):
def test_git_date_format(self):
EXPRESSIONS = [
('Thu, 07 Apr 2005 22:13:13 +0200', 2005, 4, 7), # RFC 2822
('2005-04-07T22:13:13', 2005, 4, 7), # ISO 8601
('1998.02.01', 1998, 2, 1),
('01/02/1987', 1987, 1, 2),
('@1453791344 +0100', 2016, 1, 26), # git internal format
]
for expression, year, month, day in EXPRESSIONS:
date = license_tools.DateUtils.parse_git_date(expression)
self.assertEqual(date.year, year)
self.assertEqual(date.month, month)
self.assertEqual(date.day, day)
def parser_test(file: pathlib.Path):
def file_wrapper(func):
@functools.wraps(func)
def wrapper(self):
# make sure any kind of line endings can be parsed
for contents in (_to_dos(file.read_text()), _to_unix(file.read_text())):
parsed = license_tools.ParsedHeader(
contents=contents, file=file)
try:
func(self, parsed)
except:
print(f"While trying to parse: {file}\n---\n{_render_endings(contents)}\n---\n")
raise
return wrapper
return file_wrapper
class TestParserCStyle(unittest.TestCase):
@parser_test(BASE / 'test/TestParserCStyle-1author_1year.h')
def test_1author_1year(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2010, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.C_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual("#include <stdio.h>", parsed.remainder)
@parser_test(BASE / 'test/TestParserTaggedCStyle-1author_1year.h')
def test_tagged_1author_1year(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2010, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.C_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual("#include <stdio.h>", parsed.remainder)
@parser_test(BASE / 'test/TestParserCStyle-copyright_caps.h')
def test_copyright_caps(self, parsed):
self.assertEqual(2, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2010, parsed.authors[0].year_to)
self.assertEqual("Dana Dot", parsed.authors[1].name)
self.assertEqual(2011, parsed.authors[1].year_from)
self.assertEqual(2011, parsed.authors[1].year_to)
self.assertEqual(license_tools.Style.C_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual("#include <stdio.h>", parsed.remainder)
@parser_test(BASE / 'test/TestParserCStyle-1author_1year.hpp')
def test_1author_1year_dash(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("AB_CD-Team", parsed.authors[0].name)
self.assertEqual(1984, parsed.authors[0].year_from)
self.assertEqual(1984, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.C_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual("#include <stdio.h>", parsed.remainder)
@parser_test(BASE / 'test/TestParserCStyle-non_greedy.hpp')
def test_non_greedy(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2012, parsed.authors[0].year_from)
self.assertEqual(2018, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.C_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"Permission to use"), parsed.license)
self.assertEqual("#ifndef GEN_NON_", parsed.remainder[:16])
@parser_test(file=BASE / 'test/TestParserTaggedCStyle-non_greedy.hpp')
def test_tagged_non_greedy(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2012, parsed.authors[0].year_from)
self.assertEqual(2018, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.C_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"Permission to use"), parsed.license)
self.assertEqual("#ifndef GEN_NON_", parsed.remainder[:16])
@parser_test(BASE / 'test/TestParserCStyle-1author_2years.cxx')
def test_1author_2years(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2013, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.C_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
@parser_test(BASE / 'test/TestParserTaggedCStyle-1author_2years.cxx')
def test_tagged_1author_2years(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2013, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.C_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
@parser_test(BASE / 'test/TestParserCStyle-2authors_2years.c')
def test_2authors_2years(self, parsed):
self.assertEqual(3, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2021, parsed.authors[0].year_to)
self.assertEqual("Awesome Limited Inc.", parsed.authors[1].name)
self.assertEqual(2012, parsed.authors[1].year_from)
self.assertEqual(2013, parsed.authors[1].year_to)
self.assertEqual("My Company", parsed.authors[2].name)
self.assertEqual(2013, parsed.authors[2].year_from)
self.assertEqual(2020, parsed.authors[2].year_to)
self.assertEqual(license_tools.Style.C_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
@parser_test(BASE / 'test/TestParserTaggedCStyle-2authors_2years.c')
def test_tagged_2authors_2years(self, parsed):
self.assertEqual(3, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2021, parsed.authors[0].year_to)
self.assertEqual("Awesome Limited Inc.", parsed.authors[1].name)
self.assertEqual(2012, parsed.authors[1].year_from)
self.assertEqual(2013, parsed.authors[1].year_to)
self.assertEqual("My Company", parsed.authors[2].name)
self.assertEqual(2013, parsed.authors[2].year_from)
self.assertEqual(2020, parsed.authors[2].year_to)
self.assertEqual(license_tools.Style.C_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
class TestParserPoundStyle(unittest.TestCase):
@parser_test(BASE / 'test/TestParserPoundStyle-no_newline_after_license_extra_pound.cmake')
def test_no_newline_after_license_extra_slash(self, parsed):
self.assertEqual(1, len(parsed.authors), str(parsed.authors))
self.assertEqual("Test Author", parsed.authors[0].name)
self.assertEqual(2022, parsed.authors[0].year_from)
self.assertEqual(2022, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.POUND_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith('SPDX-License-Identifier:'), parsed.license)
self.assertTrue(parsed.remainder.startswith('set(CMAKE_MACOSX_RPATH ON)'), parsed.remainder)
@parser_test(BASE / 'test/TestParserPoundStyle-no_newline_after_license.cmake')
def test_no_newline_after_license(self, parsed):
self.assertEqual(1, len(parsed.authors), str(parsed.authors))
self.assertEqual("Test Author", parsed.authors[0].name)
self.assertEqual(2022, parsed.authors[0].year_from)
self.assertEqual(2022, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.POUND_STYLE, parsed.style)
self.assertEqual(None, parsed.license)
self.assertTrue(parsed.remainder.startswith('set(CMAKE_MACOSX_RPATH ON)'), parsed.remainder)
@parser_test(BASE / 'test/TestParserPoundStyle-1author_1year.cmake')
def test_1author_1year(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2010, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.POUND_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual(
"cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)", parsed.remainder)
@parser_test(BASE / 'test/TestParserPoundStyle-comment_after_license.cmake')
def test_comment_after_license(self, parsed):
self.assertEqual(1, len(parsed.authors), str(parsed.authors))
self.assertEqual("Test Author", parsed.authors[0].name)
self.assertEqual(2022, parsed.authors[0].year_from)
self.assertEqual(2022, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.POUND_STYLE, parsed.style)
self.assertEqual(None, parsed.license)
self.assertTrue(parsed.remainder.startswith('# rpath handling on OSX'), parsed.remainder)
@parser_test(BASE / 'test/TestParserPoundStyle-no_space.py')
def test_no_space(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2010, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.DOCSTRING_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual("import unittest", parsed.remainder)
@parser_test(BASE / 'test/TestParserPoundStyle-no_content.py')
def test_no_content(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2010, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.DOCSTRING_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual("", parsed.remainder)
@parser_test(BASE / 'test/TestParserTaggedPoundStyle-1author_1year.cmake')
def test_tagged_1author_1year(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2010, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.POUND_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual(
"cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)", parsed.remainder)
@parser_test(BASE / 'test/TestParserPoundStyle-1author_2years.sh')
def test_1author_2years(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2013, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.POUND_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
@parser_test(BASE / 'test/TestParserTaggedPoundStyle-1author_2years.sh')
def test_tagged_1author_2years(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2013, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.POUND_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
@parser_test(BASE / 'test/TestParserPoundStyle-2authors_2years')
def test_2authors_2years(self, parsed):
self.assertEqual(3, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2021, parsed.authors[0].year_to)
self.assertEqual("Awesome Limited Inc.", parsed.authors[1].name)
self.assertEqual(2012, parsed.authors[1].year_from)
self.assertEqual(2013, parsed.authors[1].year_to)
self.assertEqual("My Company", parsed.authors[2].name)
self.assertEqual(2013, parsed.authors[2].year_from)
self.assertEqual(2020, parsed.authors[2].year_to)
self.assertEqual(license_tools.Style.POUND_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
@parser_test(BASE / 'test/TestParserTaggedPoundStyle-2authors_2years')
def test_tagged_2authors_2years(self, parsed):
self.assertEqual(3, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2021, parsed.authors[0].year_to)
self.assertEqual("Awesome Limited Inc.", parsed.authors[1].name)
self.assertEqual(2012, parsed.authors[1].year_from)
self.assertEqual(2013, parsed.authors[1].year_to)
self.assertEqual("My Company", parsed.authors[2].name)
self.assertEqual(2013, parsed.authors[2].year_from)
self.assertEqual(2020, parsed.authors[2].year_to)
self.assertEqual(license_tools.Style.POUND_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
class TestParserDocStringStyle(unittest.TestCase):
@parser_test(BASE / 'test/TestParserDocStringStyle-no_newline_after_license_extra_pound.py')
def test_no_newline_after_license_extra_slash(self, parsed):
self.assertEqual(1, len(parsed.authors), str(parsed.authors))
self.assertEqual("Test Author", parsed.authors[0].name)
self.assertEqual(2022, parsed.authors[0].year_from)
self.assertEqual(2022, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.DOCSTRING_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith('SPDX-License-Identifier:'), parsed.license)
self.assertTrue(parsed.remainder.startswith('import sys'), parsed.remainder)
@parser_test(BASE / 'test/TestParserDocStringStyle-no_newline_after_license.py')
def test_no_newline_after_license(self, parsed):
self.assertEqual(1, len(parsed.authors), str(parsed.authors))
self.assertEqual("Test Author", parsed.authors[0].name)
self.assertEqual(2022, parsed.authors[0].year_from)
self.assertEqual(2022, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.DOCSTRING_STYLE, parsed.style)
self.assertEqual(None, parsed.license)
self.assertTrue(parsed.remainder.startswith('import sys'), parsed.remainder)
@parser_test(BASE / 'test/TestParserDocStringStyle-1author_1year.py')
def test_1author_1year(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2010, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.DOCSTRING_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual("import unittest", parsed.remainder)
@parser_test(BASE / 'test/TestParserTaggedDocStringStyle-1author_1year.py')
def test_tagged_1author_1year(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2010, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.DOCSTRING_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual("import unittest", parsed.remainder)
@parser_test(BASE / 'test/TestParserDocStringStyle-1author_2years.py')
def test_1author_2years(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2013, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.DOCSTRING_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
@parser_test(BASE / 'test/TestParserTaggedDocStringStyle-1author_2years.py')
def test_tagged_1author_2years(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2013, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.DOCSTRING_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
@parser_test(BASE / 'test/TestParserDocStringStyle-comment_after_license.py')
def test_comment_after_license(self, parsed):
self.assertEqual(1, len(parsed.authors), str(parsed.authors))
self.assertEqual("Test Author", parsed.authors[0].name)
self.assertEqual(2022, parsed.authors[0].year_from)
self.assertEqual(2022, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.DOCSTRING_STYLE, parsed.style)
self.assertEqual(None, parsed.license)
self.assertTrue(parsed.remainder.startswith('# System'), parsed.remainder)
@parser_test(BASE / 'test/TestParserDocStringStyle-2authors_2years')
def test_2authors_2years(self, parsed):
self.assertEqual(3, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2021, parsed.authors[0].year_to)
self.assertEqual("Awesome Limited Inc.", parsed.authors[1].name)
self.assertEqual(2012, parsed.authors[1].year_from)
self.assertEqual(2013, parsed.authors[1].year_to)
self.assertEqual("My Company", parsed.authors[2].name)
self.assertEqual(2013, parsed.authors[2].year_from)
self.assertEqual(2020, parsed.authors[2].year_to)
self.assertEqual(license_tools.Style.DOCSTRING_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
@parser_test(BASE / 'test/TestParserTaggedDocStringStyle-2authors_2years')
def test_tagged_2authors_2years(self, parsed):
self.assertEqual(3, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2021, parsed.authors[0].year_to)
self.assertEqual("Awesome Limited Inc.", parsed.authors[1].name)
self.assertEqual(2012, parsed.authors[1].year_from)
self.assertEqual(2013, parsed.authors[1].year_to)
self.assertEqual("My Company", parsed.authors[2].name)
self.assertEqual(2013, parsed.authors[2].year_from)
self.assertEqual(2020, parsed.authors[2].year_to)
self.assertEqual(license_tools.Style.DOCSTRING_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
class TestParserXmlStyle(unittest.TestCase):
@parser_test(BASE / 'test/TestParserXmlStyle-1author_1year.xml')
def test_1author_1year(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2010, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.XML_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual("<xml />", parsed.remainder)
@parser_test(BASE / 'test/TestParserXmlStyle-unicode_marker.xml')
def test_unicode_marker(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2020, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.XML_STYLE, parsed.style)
self.assertListEqual(['\uFEFF', '<?xml version="1.0" encoding="utf-8"?>'], parsed.decls)
if '\r' in parsed.remainder:
self.assertEqual("<rcc>\r\n <!-- qrc sample -->\r\n <qresource", parsed.remainder[:46])
else:
self.assertEqual("<rcc>\n <!-- qrc sample -->\n <qresource", parsed.remainder[:44])
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
@parser_test(BASE / 'test/TestParserXmlStyle-1author_2years.htm')
def test_1author_2years(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2013, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.XML_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
@parser_test(BASE / 'test/TestParserXmlStyle-2authors_2years.html')
def test_2authors_2years(self, parsed):
self.assertEqual(3, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2021, parsed.authors[0].year_to)
self.assertEqual("Awesome Limited Inc.", parsed.authors[1].name)
self.assertEqual(2012, parsed.authors[1].year_from)
self.assertEqual(2013, parsed.authors[1].year_to)
self.assertEqual("My Company", parsed.authors[2].name)
self.assertEqual(2013, parsed.authors[2].year_from)
self.assertEqual(2020, parsed.authors[2].year_to)
self.assertEqual(license_tools.Style.XML_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
@parser_test(BASE / 'test/TestParserXmlStyle-no_license.html')
def test_no_license(self, parsed):
self.assertEqual(3, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2021, parsed.authors[0].year_to)
self.assertEqual("Awesome Limited Inc.", parsed.authors[1].name)
self.assertEqual(2012, parsed.authors[1].year_from)
self.assertEqual(2013, parsed.authors[1].year_to)
self.assertEqual("My Company", parsed.authors[2].name)
self.assertEqual(2013, parsed.authors[2].year_from)
self.assertEqual(2020, parsed.authors[2].year_to)
self.assertEqual(license_tools.Style.XML_STYLE, parsed.style)
self.assertEqual(None, parsed.license)
class TestParserBatchStyle(unittest.TestCase):
@parser_test(BASE / 'test/TestParserBatchStyle-1author_1year.bat')
def test_1author_1year(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2010, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.BATCH_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual(
"echo.Hello World", parsed.remainder)
@parser_test(BASE / 'test/TestParserTaggedBatchStyle-1author_1year.bat')
def test_tagged_1author_1year(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2010, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.BATCH_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual(
"echo.Hello World", parsed.remainder)
@parser_test(BASE / 'test/TestParserBatchStyle-1author_2years.bat')
def test_1author_2years(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2013, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.BATCH_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual(
"echo.Hello World", parsed.remainder)
@parser_test(BASE / 'test/TestParserTaggedBatchStyle-1author_2years.bat')
def test_tagged_1author_2years(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2013, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.BATCH_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual(
"echo.Hello World", parsed.remainder)
class TestParserSlashStyle(unittest.TestCase):
@parser_test(BASE / 'test/TestParserSlashStyle-1author_1year.rc')
def test_1author_1year(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2010, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.SLASH_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual(
"#include <stdio.h>", parsed.remainder)
@parser_test(BASE / 'test/TestParserTaggedSlashStyle-1author_1year.rc')
def test_tagged_1author_1year(self, parsed):
self.assertEqual(1, len(parsed.authors))
self.assertEqual("Max Muster", parsed.authors[0].name)
self.assertEqual(2010, parsed.authors[0].year_from)
self.assertEqual(2010, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.SLASH_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual(
"#include <stdio.h>", parsed.remainder)
@parser_test(BASE / 'test/TestParserSlashStyle-multiline_author.rc')
def test_1author_1year(self, parsed):
self.assertEqual(1, len(parsed.authors), str(parsed.authors))
self.assertEqual("Office of code", parsed.authors[0].name)
self.assertEqual(2003, parsed.authors[0].year_from)
self.assertEqual(2023, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.SLASH_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith(
"This library is"), parsed.license)
self.assertEqual(
"#include <stdio.h>", parsed.remainder)
@parser_test(BASE / 'test/TestParserSlashStyle-comment_after_license.c')
def test_comment_after_license(self, parsed):
self.assertEqual(1, len(parsed.authors), str(parsed.authors))
self.assertEqual("Test Author", parsed.authors[0].name)
self.assertEqual(2022, parsed.authors[0].year_from)
self.assertEqual(2022, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.C_STYLE, parsed.style)
self.assertEqual(None, parsed.license)
self.assertTrue(parsed.remainder.startswith('// System'), parsed.remainder)
@parser_test(BASE / 'test/TestParserSlashStyle-brief_spdx.c')
def test_brief_spdx(self, parsed):
self.assertEqual(1, len(parsed.authors), str(parsed.authors))
self.assertEqual("Test Author", parsed.authors[0].name)
self.assertEqual(2022, parsed.authors[0].year_from)
self.assertEqual(2022, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.C_STYLE, parsed.style)
self.assertEqual("SPDX-License-Identifier: Apache-2.0", parsed.license)
self.assertTrue(parsed.remainder.startswith('// System'), parsed.remainder)
@parser_test(BASE / 'test/TestParserSlashStyle-no_newline_after_license.c')
def test_no_newline_after_license(self, parsed):
self.assertEqual(1, len(parsed.authors), str(parsed.authors))
self.assertEqual("Test Author", parsed.authors[0].name)
self.assertEqual(2022, parsed.authors[0].year_from)
self.assertEqual(2022, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.C_STYLE, parsed.style)
self.assertEqual(None, parsed.license)
self.assertTrue(parsed.remainder.startswith('#include'), parsed.remainder)
@parser_test(BASE / 'test/TestParserSlashStyle-no_newline_after_license_extra_slash.c')
def test_no_newline_after_license_extra_slash(self, parsed):
self.assertEqual(1, len(parsed.authors), str(parsed.authors))
self.assertEqual("Test Author", parsed.authors[0].name)
self.assertEqual(2022, parsed.authors[0].year_from)
self.assertEqual(2022, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.C_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith('SPDX-License-Identifier:'), parsed.license)
self.assertTrue(parsed.remainder.startswith('#include'), parsed.remainder)
class TestParserDashStyle(unittest.TestCase):
@parser_test(BASE / 'test/TestParserDashStyle-comment_after_license.lua')
def test_comment_after_license(self, parsed):
self.assertEqual(1, len(parsed.authors), str(parsed.authors))
self.assertEqual("Test Author", parsed.authors[0].name)
self.assertEqual(2022, parsed.authors[0].year_from)
self.assertEqual(2022, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.DASH_STYLE, parsed.style)
self.assertEqual(None, parsed.license)
self.assertTrue(parsed.remainder.startswith('-- defines'), parsed.remainder)
@parser_test(BASE / 'test/TestParserDashStyle-no_newline_after_license.lua')
def test_no_newline_after_license(self, parsed):
self.assertEqual(1, len(parsed.authors), str(parsed.authors))
self.assertEqual("Test Author", parsed.authors[0].name)
self.assertEqual(2022, parsed.authors[0].year_from)
self.assertEqual(2022, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.DASH_STYLE, parsed.style)
self.assertEqual(None, parsed.license)
self.assertTrue(parsed.remainder.startswith('print'), parsed.remainder)
@parser_test(BASE / 'test/TestParserDashStyle-no_newline_after_license_extra_dash.lua')
def test_no_newline_after_license_extra_slash(self, parsed):
self.assertEqual(1, len(parsed.authors), str(parsed.authors))
self.assertEqual("Test Author", parsed.authors[0].name)
self.assertEqual(2022, parsed.authors[0].year_from)
self.assertEqual(2022, parsed.authors[0].year_to)
self.assertEqual(license_tools.Style.DASH_STYLE, parsed.style)
self.assertTrue(parsed.license.startswith('SPDX-License-Identifier:'), parsed.license)
self.assertTrue(parsed.remainder.startswith('print'), parsed.remainder)
class TestHeader(unittest.TestCase):
def test_generator(self):
self.maxDiff = None
author1 = license_tools.Author('Max Muster', 2013, 2020)
author2 = license_tools.Author('Umbrella Inc', 2021, 2021)
authors = [author1, author2]
for l in license_tools.LICENSES:
license = license_tools.License(builtin=l)
header = license_tools.Header(license)
for style in license_tools.Style:
if license_tools.Style.UNKNOWN == style:
continue
candidates = {
license_tools.Style.C_STYLE: f'TestHeader-c_{l}.expected',
license_tools.Style.POUND_STYLE: f'TestHeader-pound_{l}.expected',
license_tools.Style.DOCSTRING_STYLE: f'TestHeader-docstring_{l}.expected',
license_tools.Style.XML_STYLE: f'TestHeader-xml_{l}.expected',
license_tools.Style.BATCH_STYLE: f'TestHeader-batch_{l}.expected',
license_tools.Style.SLASH_STYLE: f'TestHeader-slash_{l}.expected',
license_tools.Style.DASH_STYLE: f'TestHeader-dash_{l}.expected'
}
filename = candidates[style]
output = header.render(
filename, authors, style)
try:
with open(BASE / 'test' / filename, 'r') as expected:
self.assertEqual(expected.read(), output)
parsed = license_tools.ParsedHeader(
contents=output, file=filename)
self.assertListEqual(authors, parsed.authors)
except:
with open(BASE / 'test' / filename, 'w') as expected:
expected.write(output)
raise
filename = f'TestHeader-no_license_{l}.expected'
output = header.render(
filename, authors, license_tools.Style.C_STYLE)
try:
with open(BASE / 'test' / filename, 'r') as expected:
self.assertEqual(expected.read(), output)
parsed = license_tools.ParsedHeader(
contents=output, file=filename)
self.assertListEqual(authors, parsed.authors)
except:
with open(BASE / 'test' / filename, 'w') as expected:
expected.write(output)
raise
header = license_tools.Header(None)
filename = f'TestHeader-title_{l}.expected'
output = header.render(
'My Project', authors, license_tools.Style.C_STYLE)
try:
with open(BASE / 'test' / filename, 'r') as expected:
self.assertEqual(expected.read(), output)
parsed = license_tools.ParsedHeader(
contents=output, file=filename)
self.assertListEqual(authors, parsed.authors)
except:
with open(BASE / 'test' / filename, 'w') as expected:
expected.write(output)
raise
custom_license = """
Lorem Bin Title.
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
"""
license = license_tools.License(custom=custom_license)
header = license_tools.Header(license)
filename = f'TestHeader-c_custom.expected'
output = header.render(
filename, authors, license_tools.Style.C_STYLE)
try:
with open(BASE / 'test' / filename, 'r') as expected:
self.assertEqual(expected.read(), output)
except:
with open(BASE / 'test' / filename, 'w') as expected:
expected.write(output)
raise
class TestTool(unittest.TestCase):
def _to_unix(self, input: str):
return input.replace('\r\n', '\n')
def _to_dos(self, input: str):
return self._to_unix(input).replace('\n', '\r\n')
def _render_endings(self, input: str):
return input.replace('\r', '\\r').replace('\n', '\\n\n')
def test_retain_newline(self):
author = license_tools.Author("Test Guy", year_to=2021)
license = license_tools.License("Apache-2.0")
title = license_tools.Title("filename")
tool = license_tools.Tool(
default_license=license, default_author=author)
input = BASE / 'test/TestTool-bump_old_copyright_year.input.cxx'
expected = BASE / 'test/TestTool-bump_old_copyright_year.expected'
with tempfile.TemporaryDirectory() as wkdir:
dut = pathlib.Path(wkdir) / input.name
# verify unix line endings get retained
input_unix = self._to_unix(input.read_text())
expected_unix = self._to_unix(expected.read_text())
with open(dut, mode='w', newline='', encoding='utf8') as dut_io:
dut_io.write(input_unix)
_, result = tool.bump(dut, keep_license=True, title=title)
self.assertEqual(expected_unix, result,
f"\nACTUAL ---\n{self._render_endings(result)}\nWANT ---\n{self._render_endings(expected_unix)}\n---")
# verify dos line endings get retained
input_dos = self._to_dos(input.read_text())
expected_dos = self._to_dos(expected.read_text())
with open(dut, mode='w', newline='', encoding='utf8') as dut_io:
dut_io.write(input_dos)
_, result = tool.bump(dut, keep_license=True, title=title)
self.assertEqual(expected_dos, result,
f"\nACTUAL ---\n{self._render_endings(result)}\nWANT ---\n{self._render_endings(expected_dos)}\n---")
for file in BASE.glob('test/TestTool-bump*.input.*'):
author = license_tools.Author("Test Guy", year_to=2021)
license = license_tools.License("Apache-2.0")
title = license_tools.Title("filename")
tool = license_tools.Tool(
default_license=license, default_author=author)
name = file.name.split('.')[0]
def create_test_case():
input = file
stem = name
def test_bump(self):
style, result = tool.bump(input, title=title, keep_license=True)
self.assertIsNotNone(result)
try:
with open(BASE / 'test' / (stem + ".expected"), 'r') as expected:
self.assertEqual(expected.read(), result)
except:
print(f"{file}, style={style}\nACTUAL ---\n{result}\nWANT ---")
with open(BASE / 'test' / (stem + ".expected"), 'w') as expected:
expected.write(result)
raise
return test_bump
setattr(TestTool, f'test_{name.replace("TestTool-","")}', create_test_case())
class TestPackage(unittest.TestCase):
def _prepare_repo(self, commit: pathlib.Path, config: pathlib.Path):
# override any external input to the git config so we truly
# use our expected config to ensure tests cannot fail
os.putenv('GIT_CONFIG_COUNT', '0')
wkdir = tempfile.TemporaryDirectory(suffix='lictools')
try:
cwd = pathlib.Path(wkdir.name)
subprocess.check_call('git init --initial-branch=master', cwd=cwd, shell=True, stdout=subprocess.DEVNULL)
(cwd / ".gitignore").write_text("")
subprocess.check_call('git add .gitignore', cwd=cwd, shell=True, stdout=subprocess.DEVNULL)
subprocess.check_call('git commit -a -m "Prepare Repo"', cwd=cwd, shell=True, stdout=subprocess.DEVNULL)
subprocess.check_call('git config --local user.name "Lictools Unittest"',
cwd=cwd, shell=True, stdout=subprocess.DEVNULL)
subprocess.check_call('git config --local user.email "lictools@unittest.local"',
cwd=cwd, shell=True, stdout=subprocess.DEVNULL)
subprocess.check_call(f'git am {commit}', cwd=cwd, shell=True, stdout=subprocess.DEVNULL)
(cwd / '.license-tools-config.json').write_text(config.read_text())
return wkdir
except:
wkdir.cleanup()
raise
def _diff_repo(self, repo: pathlib.Path, expected: pathlib.Path):
diff = subprocess.check_output('git diff', cwd=repo, shell=True, encoding='utf-8')
try:
with open(expected, 'r') as raw:
self.assertEqual(raw.read(), diff)
except:
with open(expected, 'w') as raw:
raw.write(diff)
raise
def test_bad_config(self):
with self._prepare_repo(BASE / 'test/noglob_package_bad_license.patch',
BASE / 'test/noglob_package_bad_license.patch') as repo:
with self.assertRaises(subprocess.CalledProcessError):
subprocess.check_call(f'{BASE}/lictool', cwd=repo)
def test_no_config(self):
with self._prepare_repo(BASE / 'test/noglob_package_bad_license.patch',
BASE / 'test/noglob_package_bad_license.patch') as repo:
(pathlib.Path(repo) / '.license-tools-config.json').unlink()
with self.assertRaises(subprocess.CalledProcessError):
subprocess.check_call(f'{BASE}/lictool', cwd=repo)
def test_bad_license(self):
with self._prepare_repo(BASE / 'test/noglob_package_bad_license.patch',
BASE / 'test/noglob_package_bad_license.json') as repo:
with self.assertRaises(subprocess.CalledProcessError):
subprocess.check_call(f'{BASE}/lictool', cwd=repo)
def test_from_git_no_repo(self):
with self._prepare_repo(BASE / 'test/noglob_package_from_git_no_repo.patch',
BASE / 'test/noglob_package_from_git_no_repo.json') as repo:
shutil.rmtree(pathlib.Path(repo) / '.git')
with self.assertRaises(subprocess.CalledProcessError):
subprocess.check_call(f'{BASE}/lictool', cwd=repo)
def test_new_author(self):
with self._prepare_repo(BASE / 'test/noglob_package_from_git_new_author.patch',
BASE / 'test/noglob_package_from_git_new_author.json') as repo:
code = pathlib.Path(repo) / 'code.cpp'
code.write_text(code.read_text() + "\ninline void unused(){}\n\n")
subprocess.check_call(f'{BASE}/lictool', cwd=repo)
self._diff_repo(repo, BASE / 'test/noglob_package_from_git_new_author.diff')
def test_commit_amend(self):
with self._prepare_repo(BASE / 'test/noglob_package_commit_amend.patch',
BASE / 'test/noglob_package_commit_amend.json') as repo:
# trigger a change
code = pathlib.Path(repo) / 'code.cpp'
code.write_text(code.read_text() + "\ninline void unused(){}\n")
# enable pre-commit
precommit = pathlib.Path(repo) / '.pre-commit-config.yaml'
precommit.write_text(textwrap.dedent(f'''
repos:
- repo: local
hooks:
- id: license-tools
name: Check license headers
entry: {BASE}/lictool
language: python
types: [text]
additional_dependencies: ['jinja2']
verbose: true
'''))
subprocess.check_call(['pre-commit', 'install'], cwd=repo)
# amend the change, pre-commit should run lictool and continue as there is no changes
subprocess.check_call(['git', 'add', '.pre-commit-config.yaml'], cwd=repo)
subprocess.check_call(['git', 'commit', '--amend', '-a', '-m', 'Amended Message'], cwd=repo)
self._diff_repo(repo, BASE / 'test/noglob_package_commit_amend.diff')
# running lictool directly should not register changes either
subprocess.check_call(f'{BASE}/lictool', cwd=repo)
self._diff_repo(repo, BASE / 'test/noglob_package_commit_amend.diff')
for file in BASE.glob('test/package_*.patch'):
def create_test_case():
patch = file
json = patch.with_suffix('.json')
diff = patch.with_suffix('.diff')
def test_glob_case(self):
with self._prepare_repo(patch, json) as repo:
subprocess.check_call(f'{BASE}/lictool', cwd=repo)
self._diff_repo(repo, diff)
return test_glob_case
setattr(TestPackage, f'test_{file.stem.replace("package_", "")}', create_test_case())
if __name__ == '__main__':
unittest.main()