-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_license_expression.py
More file actions
1626 lines (1393 loc) · 68 KB
/
Copy pathtest_license_expression.py
File metadata and controls
1626 lines (1393 loc) · 68 KB
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
995
996
997
998
999
1000
# license-expression is a free software tool from nexB Inc. and others.
# Visit https://github.com/nexB/license-expression for support and download.
#
# Copyright (c) 2017 nexB Inc. and others. All rights reserved.
# http://nexb.com and http://aboutcode.org
#
# This software is licensed under the Apache License version 2.0.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://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 __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from collections import OrderedDict
from unittest import TestCase
import sys
from boolean.boolean import PARSE_UNBALANCED_CLOSING_PARENS
from boolean.boolean import PARSE_INVALID_SYMBOL_SEQUENCE
from license_expression import PARSE_INVALID_EXPRESSION
from license_expression import PARSE_INVALID_NESTING
from license_expression import PARSE_INVALID_EXCEPTION
from license_expression import PARSE_INVALID_SYMBOL_AS_EXCEPTION
from license_expression import ExpressionError
from license_expression import Keyword
from license_expression import Licensing
from license_expression import LicenseExpression
from license_expression import LicenseSymbol
from license_expression import LicenseWithExceptionSymbol
from license_expression import ParseError
from license_expression import Result
from license_expression import Output
from license_expression import group_results_for_with_subexpression
from license_expression import splitter
from license_expression import strip_and_skip_spaces
from license_expression import validate_symbols
from license_expression import TOKEN_AND
from license_expression import TOKEN_LPAR
from license_expression import TOKEN_OR
from license_expression import TOKEN_RPAR
from license_expression import TOKEN_SYMBOL
from license_expression import TOKEN_WITH
def _parse_error_as_dict(pe):
"""
Return a dict for a ParseError.
"""
return dict(
token_type=pe.token_type,
token_string=pe.token_string,
position=pe.position,
error_code=pe.error_code,
)
class LicenseSymbolTest(TestCase):
def test_LicenseSymbol(self):
sym1 = LicenseSymbol('MIT', ['MIT license'])
assert sym1 == sym1
assert 'MIT' == sym1.key
assert ('MIT license',) == sym1.aliases
sym2 = LicenseSymbol('mit', ['MIT license'])
assert 'mit' == sym2.key
assert ('MIT license',) == sym2.aliases
assert not sym2.is_exception
assert sym1 != sym2
assert sym1 is not sym2
sym3 = LicenseSymbol('mit', ['MIT license'], is_exception=True)
assert 'mit' == sym3.key
assert ('MIT license',) == sym3.aliases
assert sym3.is_exception
assert sym2 != sym3
sym4 = LicenseSymbol('mit', ['MIT license'])
assert 'mit' == sym4.key
assert ('MIT license',) == sym4.aliases
# symbol equality is based ONLY on the key
assert sym2 == sym4
assert sym1 != sym4
sym5 = LicenseWithExceptionSymbol(sym2, sym3)
assert sym2 == sym5.license_symbol
assert sym3 == sym5.exception_symbol
sym6 = LicenseWithExceptionSymbol(sym4, sym3)
# symbol euqality is based ONLY on the key
assert sym5 == sym6
class LicensingTest(TestCase):
def test_Licensing_create(self):
Licensing()
Licensing(None)
Licensing(list())
class LicensingTokenizeWithoutSymbolsTest(TestCase):
def test_tokenize_plain1(self):
licensing = Licensing()
expected = [
(TOKEN_LPAR, '(', 1),
(LicenseSymbol(key='mit'), 'mit', 3),
(TOKEN_RPAR, ')', 7),
(TOKEN_AND, 'and', 9),
(LicenseSymbol(key='gpl'), 'gpl', 13)
]
assert expected == list(licensing.tokenize(' ( mit ) and gpl'))
def test_tokenize_plain2(self):
licensing = Licensing()
expected = [
(TOKEN_LPAR, '(', 0),
(LicenseSymbol(key='mit'), 'mit', 1),
(TOKEN_AND, 'and', 5),
(LicenseSymbol(key='gpl'), 'gpl', 9),
(TOKEN_RPAR, ')', 12)
]
assert expected == list(licensing.tokenize('(mit and gpl)'))
def test_tokenize_plain3(self):
licensing = Licensing()
expected = [
(LicenseSymbol(key='mit'), 'mit', 0),
(TOKEN_AND, 'AND', 4),
(LicenseSymbol(key='gpl'), 'gpl', 8),
(TOKEN_OR, 'or', 12),
(LicenseSymbol(key='gpl'), 'gpl', 15)
]
assert expected == list(licensing.tokenize('mit AND gpl or gpl'))
def test_tokenize_plain4(self):
licensing = Licensing()
expected = [
(TOKEN_LPAR, '(', 0),
(TOKEN_LPAR, '(', 1),
(LicenseSymbol(key=u'l-a+'), u'l-a+', 2),
(TOKEN_AND, 'AND', 7),
(LicenseSymbol(key=u'l-b'), u'l-b', 11),
(TOKEN_RPAR, ')', 14),
(TOKEN_OR, 'OR', 16),
(TOKEN_LPAR, '(', 19),
(LicenseSymbol(key='l-c+'), 'l-c+', 20),
(TOKEN_RPAR, ')', 24),
(TOKEN_RPAR, ')', 25)
]
assert expected == list(licensing.tokenize('((l-a+ AND l-b) OR (l-c+))'))
def test_tokenize_plain5(self):
licensing = Licensing()
expected = [
(TOKEN_LPAR, '(', 0),
(TOKEN_LPAR, '(', 1),
(LicenseSymbol(key='l-a+'), 'l-a+', 2),
(TOKEN_AND, 'AND', 7),
(LicenseSymbol(key='l-b'), 'l-b', 11),
(TOKEN_RPAR, ')', 14),
(TOKEN_OR, 'OR', 16),
(TOKEN_LPAR, '(', 19),
(LicenseSymbol(key='l-c+'), 'l-c+', 20),
(TOKEN_RPAR, ')', 24),
(TOKEN_RPAR, ')', 25),
(TOKEN_AND, 'and', 27),
(LicenseWithExceptionSymbol(
license_symbol=LicenseSymbol(key='gpl'),
exception_symbol=LicenseSymbol(key='classpath')),
'gpl with classpath', 31
)
]
assert expected == list(licensing.tokenize('((l-a+ AND l-b) OR (l-c+)) and gpl with classpath'))
class LicensingTokenizeWithSymbolsTest(TestCase):
def get_symbols_and_licensing(self):
gpl_20 = LicenseSymbol('GPL-2.0', ['The GNU GPL 20'])
gpl_20_plus = LicenseSymbol('gpl-2.0+',
['The GNU GPL 20 or later', 'GPL-2.0 or later', 'GPL v2.0 or later'])
lgpl_21 = LicenseSymbol('LGPL-2.1', ['LGPL v2.1'])
mit = LicenseSymbol('MIT', ['MIT license'])
symbols = [gpl_20, gpl_20_plus, lgpl_21, mit]
licensing = Licensing(symbols)
return gpl_20, gpl_20_plus, lgpl_21, mit, licensing
def test_tokenize_1(self):
gpl_20, _gpl_20_plus, lgpl_21, mit, licensing = self.get_symbols_and_licensing()
result = licensing.tokenize('The GNU GPL 20 or LGPL-2.1 and mit')
expected = [
(gpl_20, 'The GNU GPL 20', 0),
(TOKEN_OR, ' or ', 14),
(lgpl_21, 'LGPL-2.1', 18),
(TOKEN_AND, ' and ', 26),
(mit, 'mit', 31)]
assert expected == list(result)
def test_tokenize_with_trailing_unknown(self):
gpl_20, _gpl_20_plus, lgpl_21, mit, licensing = self.get_symbols_and_licensing()
result = licensing.tokenize('The GNU GPL 20 or LGPL-2.1 and mit2')
expected = [
(gpl_20, 'The GNU GPL 20', 0),
(TOKEN_OR, ' or ', 14),
(lgpl_21, 'LGPL-2.1', 18),
(TOKEN_AND, ' and ', 26),
(mit, 'mit', 31),
(LicenseSymbol(key='2'), '2', 34)
]
assert expected == list(result)
def test_tokenize_3(self):
gpl_20, gpl_20_plus, lgpl_21, mit, licensing = self.get_symbols_and_licensing()
result = licensing.tokenize('The GNU GPL 20 or later or (LGPL-2.1 and mit) or The GNU GPL 20 or mit')
expected = [
(gpl_20_plus, 'The GNU GPL 20 or later', 0),
(TOKEN_OR, ' or ', 23),
(TOKEN_LPAR, '(', 27),
(lgpl_21, 'LGPL-2.1', 28),
(TOKEN_AND, ' and ', 36),
(mit, 'mit', 41),
(TOKEN_RPAR, ')', 44),
(TOKEN_OR, ' or ', 45),
(gpl_20, 'The GNU GPL 20', 49), (2, ' or ', 63),
(mit, 'mit', 67)
]
assert expected == list(result)
def test_tokenize_unknown_as_trailing_single_attached_character(self):
symbols = [LicenseSymbol('MIT', ['MIT license'])]
l = Licensing(symbols)
result = list(l.tokenize('mit2'))
expected = [
(LicenseSymbol(key='MIT', aliases=('MIT license',)), 'mit', 0),
(LicenseSymbol(key='2'), '2', 3),
]
assert expected == result
class LicensingParseTest(TestCase):
def test_parse_does_not_raise_error_for_empty_expression(self):
licensing = Licensing()
assert None == licensing.parse('')
def test_parse(self):
expression = ' ( (( gpl and bsd ) or lgpl) and gpl-exception) '
expected = '((gpl AND bsd) OR lgpl) AND gpl-exception'
licensing = Licensing()
self.assertEqual(expected, str(licensing.parse(expression)))
def test_parse_raise_ParseError(self):
expression = ' ( (( gpl and bsd ) or lgpl) and gpl-exception)) '
licensing = Licensing()
try:
licensing.parse(expression)
self.fail('ParseError should be raised')
except ParseError as pe:
expected = {'error_code': PARSE_UNBALANCED_CLOSING_PARENS, 'position': 48, 'token_string': ')', 'token_type': TOKEN_RPAR}
assert expected == _parse_error_as_dict(pe)
def test_parse_raise_ExpressionError_when_validating(self):
expression = 'gpl and bsd or lgpl with exception'
licensing = Licensing()
try:
licensing.parse(expression, validate=True)
except ExpressionError as ee:
assert 'Unknown license key(s): gpl, bsd, lgpl, exception' == str(ee)
def test_parse_raise_ExpressionError_when_validating_strict(self):
expression = 'gpl and bsd or lgpl with exception'
licensing = Licensing()
try:
licensing.parse(expression, validate=True, strict=True)
except ExpressionError as ee:
assert str(ee).startswith('exception_symbol must be an exception with "is_exception" set to True:')
def test_parse_in_strict_mode_for_solo_symbol(self):
expression = 'lgpl'
licensing = Licensing()
licensing.parse(expression, strict=True)
def test_parse_invalid_expression_raise_expression(self):
licensing = Licensing()
expr = 'wrong'
licensing.parse(expr)
expr = 'l-a AND none'
licensing.parse(expr)
expr = '(l-a + AND l-b'
try:
licensing.parse(expr)
self.fail("Exception not raised when validating '%s'" % expr)
except ParseError:
pass
expr = '(l-a + AND l-b))'
try:
licensing.parse(expr)
self.fail("Exception not raised when validating '%s'" % expr)
except ParseError:
pass
expr = 'l-a AND'
try:
licensing.parse(expr)
self.fail("Exception not raised when validating '%s'" % expr)
except ParseError:
pass
expr = 'OR l-a'
try:
licensing.parse(expr)
self.fail("Exception not raised when validating '%s'" % expr)
except ParseError:
pass
expr = '+l-a'
licensing.parse(expr)
def test_parse_can_parse(self):
licensing = Licensing()
expr = ' GPL-2.0 or LGPL2.1 and mit '
parsed = licensing.parse(expr)
gpl2 = LicenseSymbol('GPL-2.0')
lgpl = LicenseSymbol('LGPL2.1')
mit = LicenseSymbol('mit')
expected = [gpl2, lgpl, mit]
self.assertEqual(expected, licensing.license_symbols(parsed))
self.assertEqual(expected, licensing.license_symbols(expr))
self.assertEqual('GPL-2.0 OR (LGPL2.1 AND mit)', str(parsed))
expected = licensing.OR(gpl2, licensing.AND(lgpl, mit))
assert expected == parsed
def test_parse_errors_catch_invalid_nesting(self):
licensing = Licensing()
try:
licensing.parse('mit (and LGPL 2.1)')
self.fail('Exception not raised')
except ParseError as pe:
expected = {'error_code': PARSE_INVALID_NESTING, 'position': 4, 'token_string': '(', 'token_type': TOKEN_LPAR}
assert expected == _parse_error_as_dict(pe)
def test_parse_errors_catch_invalid_expression_with_bare_and(self):
licensing = Licensing()
try:
licensing.parse('and')
self.fail('Exception not raised')
except ParseError as pe:
expected = {'error_code': PARSE_INVALID_EXPRESSION, 'position':-1, 'token_string': '', 'token_type': None}
assert expected == _parse_error_as_dict(pe)
def test_parse_errors_catch_invalid_expression_with_or_and_no_other(self):
licensing = Licensing()
try:
licensing.parse('or that')
self.fail('Exception not raised')
except ParseError as pe:
expected = {'error_code': PARSE_INVALID_EXPRESSION, 'position':-1, 'token_string': '', 'token_type': None}
assert expected == _parse_error_as_dict(pe)
def test_parse_errors_catch_invalid_expression_with_empty_parens(self):
licensing = Licensing()
try:
licensing.parse('with ( )this')
self.fail('Exception not raised')
except ParseError as pe:
expected = {'error_code': PARSE_INVALID_EXPRESSION, 'position': 0, 'token_string': 'with', 'token_type': TOKEN_WITH}
assert expected == _parse_error_as_dict(pe)
def test_parse_errors_catch_invalid_non_unicode_byte_strings_on_python3(self):
py2 = sys.version_info[0] == 2
py3 = sys.version_info[0] == 3
licensing = Licensing()
if py2:
extra_bytes = bytes(chr(0) + chr(12) + chr(255))
try:
licensing.parse('mit (and LGPL 2.1)'.encode('utf-8') + extra_bytes)
self.fail('Exception not raised')
except ExpressionError as ee:
assert str(ee).startswith('expression must be a string and')
if py3:
extra_bytes = bytes(chr(0) + chr(12) + chr(255), encoding='utf-8')
try:
licensing.parse('mit (and LGPL 2.1)'.encode('utf-8') + extra_bytes)
self.fail('Exception not raised')
except ExpressionError as ee:
assert str(ee).startswith('Invalid license key')
def test_parse_errors_does_not_raise_error_on_plain_non_unicode_raw_string(self):
# plain non-unicode string does not raise error
licensing = Licensing()
x = licensing.parse(r'mit and (LGPL-2.1)')
self.assertTrue(isinstance(x, LicenseExpression))
def test_parse_simplify_and_contain_and_equal(self):
licensing = Licensing()
expr = licensing.parse(' GPL-2.0 or LGPL2.1 and mit ')
expr2 = licensing.parse(' (mit and LGPL2.1) or GPL-2.0 ')
self.assertEqual(expr2.simplify(), expr.simplify())
self.assertEqual(expr2, expr)
expr3 = licensing.parse('mit and LGPL2.1')
self.assertTrue(expr3 in expr2)
def test_license_expression_is_equivalent(self):
lic = Licensing()
is_equiv = lic.is_equivalent
self.assertTrue(is_equiv(lic.parse('mit AND gpl'), lic.parse('mit AND gpl')))
self.assertTrue(is_equiv(lic.parse('mit AND gpl'), lic.parse('gpl AND mit')))
self.assertTrue(is_equiv(lic.parse('mit AND gpl and apache'), lic.parse('apache and gpl AND mit')))
self.assertTrue(is_equiv(lic.parse('mit AND (gpl AND apache)'), lic.parse('(mit AND gpl) AND apache')))
# same but without parsing:
self.assertTrue(is_equiv('mit AND gpl', 'mit AND gpl'))
self.assertTrue(is_equiv('mit AND gpl', 'gpl AND mit'))
self.assertTrue(is_equiv('mit AND gpl and apache', 'apache and gpl AND mit'))
self.assertTrue(is_equiv('mit AND (gpl AND apache)', '(mit AND gpl) AND apache'))
# Real-case example of generated expression vs. stored expression:
ex1 = '''Commercial
AND apache-1.1 AND apache-2.0 AND aslr AND bsd-new
AND cpl-1.0 AND epl-1.0
AND ibm-icu AND ijg AND jdom AND lgpl-2.1
AND mit-open-group AND mpl-1.1 AND sax-pd AND unicode AND w3c AND
w3c-documentation'''
ex2 = '''
apache-1.1 AND apache-2.0 AND aslr AND bsd-new
AND cpl-1.0 AND epl-1.0
AND lgpl-2.1 AND ibm-icu AND ijg
AND jdom AND mit-open-group
AND mpl-1.1 AND Commercial AND sax-pd AND unicode
AND w3c-documentation AND w3c'''
self.assertTrue(is_equiv(lic.parse(ex1), lic.parse(ex2)))
self.assertFalse(is_equiv(lic.parse('mit AND gpl'), lic.parse('mit OR gpl')))
self.assertFalse(is_equiv(lic.parse('mit AND gpl'), lic.parse('gpl OR mit')))
def test_license_expression_license_keys(self):
licensing = Licensing()
assert ['mit', 'gpl'] == licensing.license_keys(licensing.parse(' ( mit ) and gpl'))
assert ['mit', 'gpl'] == licensing.license_keys(licensing.parse('(mit and gpl)'))
# these two are surprising for now: this is because the expression is a
# logical expression so the order may be different on more complex expressions
assert ['mit', 'gpl'] == licensing.license_keys(licensing.parse('mit AND gpl or gpl'))
assert ['l-a+', 'l-b', '+l-c'] == licensing.license_keys(licensing.parse('((l-a+ AND l-b) OR (+l-c))'))
# same without parsing
assert ['mit', 'gpl'] == licensing.license_keys('mit AND gpl or gpl')
assert ['l-a+', 'l-b', 'l-c+'] == licensing.license_keys('((l-a+ AND l-b) OR (l-c+))')
def test_end_to_end(self):
# these were formerly doctest ported to actual real code tests here
l = Licensing()
expr = l.parse(' GPL-2.0 or LGPL-2.1 and mit ')
expected = 'GPL-2.0 OR (LGPL-2.1 AND mit)'
assert expected == str(expr)
expected = [
LicenseSymbol('GPL-2.0'),
LicenseSymbol('LGPL-2.1'),
LicenseSymbol('mit'),
]
assert expected == l.license_symbols(expr)
def test_pretty(self):
l = Licensing()
expr = l.parse(' GPL-2.0 or LGPL2.1 and mit ')
expected = '''OR(
LicenseSymbol('GPL-2.0'),
AND(
LicenseSymbol('LGPL2.1'),
LicenseSymbol('mit')
)
)'''
assert expected == expr.pretty()
def test_simplify_and_contains(self):
l = Licensing()
expr = l.parse(' GPL-2.0 or LGPL2.1 and mit ')
expr2 = l.parse(' GPL-2.0 or (mit and LGPL2.1) ')
assert expr2.simplify() == expr.simplify()
expr3 = l.parse('mit and LGPL2.1')
assert expr3 in expr2
def test_simplify_and_equivalent_and_contains(self):
l = Licensing()
expr2 = l.parse(' GPL-2.0 or (mit and LGPL-2.1) or bsd Or GPL-2.0 or (mit and LGPL-2.1)')
# note thats simplification does SORT the symbols such that they can
# eventually be compared sequence-wise. This sorting is based on license key
expected = 'GPL-2.0 OR bsd OR (LGPL-2.1 AND mit)'
assert expected == str(expr2.simplify())
# Two expressions can be compared for equivalence:
expr1 = l.parse(' GPL-2.0 or (LGPL-2.1 and mit) ')
assert 'GPL-2.0 OR (LGPL-2.1 AND mit)' == str(expr1)
expr2 = l.parse(' (mit and LGPL-2.1) or GPL-2.0 ')
assert '(mit AND LGPL-2.1) OR GPL-2.0' == str(expr2)
assert l.is_equivalent(expr1, expr2)
assert 'GPL-2.0 OR (LGPL-2.1 AND mit)' == str(expr1.simplify())
assert 'GPL-2.0 OR (LGPL-2.1 AND mit)' == str(expr2.simplify())
assert expr1.simplify() == expr2.simplify()
expr3 = l.parse(' GPL-2.0 or mit or LGPL-2.1')
assert not l.is_equivalent(expr2, expr3)
expr4 = l.parse('mit and LGPL-2.1')
assert expr4.simplify() in expr2.simplify()
assert l.contains(expr2, expr4)
def test_contains_works_with_plain_symbol(self):
l = Licensing()
assert not l.contains('mit', 'mit and LGPL-2.1')
assert l.contains('mit and LGPL-2.1', 'mit')
assert l.contains('mit', 'mit')
assert not l.contains(l.parse('mit'), l.parse('mit and LGPL-2.1'))
assert l.contains(l.parse('mit and LGPL-2.1'), l.parse('mit'))
assert l.contains('mit with GPL', 'GPL')
assert l.contains('mit with GPL', 'mit')
assert l.contains('mit with GPL', 'mit with GPL')
assert not l.contains('mit with GPL', 'GPL with mit')
assert not l.contains('mit with GPL', 'GPL and mit')
assert not l.contains('GPL', 'mit with GPL')
assert l.contains('mit with GPL and GPL and BSD', 'GPL and BSD')
def test_create_from_python(self):
# Expressions can be built from Python expressions, using bitwise operators
# between Licensing objects, but use with caution. The behavior is not as
# well specified that using text expression and parse
licensing = Licensing()
expr1 = (licensing.LicenseSymbol('GPL-2.0')
| (licensing.LicenseSymbol('mit')
& licensing.LicenseSymbol('LGPL-2.1')))
expr2 = licensing.parse(' GPL-2.0 or (mit and LGPL-2.1) ')
assert 'GPL-2.0 OR (LGPL-2.1 AND mit)' == str(expr1.simplify())
assert 'GPL-2.0 OR (LGPL-2.1 AND mit)' == str(expr2.simplify())
assert licensing.is_equivalent(expr1, expr2)
a = licensing.OR(
LicenseSymbol(key='gpl-2.0'),
licensing.AND(LicenseSymbol(key='mit'),
LicenseSymbol(key='lgpl-2.1')
)
)
b = licensing.OR(
LicenseSymbol(key='gpl-2.0'),
licensing.AND(LicenseSymbol(key='mit'),
LicenseSymbol(key='lgpl-2.1')
)
)
assert a == b
def test_parse_with_repeated_or_later_raise_parse_error(self):
l = Licensing()
expr = 'LGPL2.1+ + and mit'
try:
l.parse(expr)
self.fail('Exception not raised')
except ParseError as ee:
expected = 'Invalid symbols sequence such as (A B) for token: "+" at position: 9'
assert expected == str(ee)
def test_render_complex(self):
licensing = Licensing()
expression = '''
EPL-1.0 AND Apache-1.1 AND Apache-2.0 AND BSD-Modified AND CPL-1.0 AND
ICU-Composite-License AND JPEG-License AND JDOM-License AND LGPL-2.0 AND
MIT-Open-Group AND MPL-1.1 AND SAX-PD AND Unicode-Inc-License-Agreement
AND W3C-Software-Notice and License AND W3C-Documentation-License'''
result = licensing.parse(expression)
expected = ('EPL-1.0 AND Apache-1.1 AND Apache-2.0 AND BSD-Modified '
'AND CPL-1.0 AND ICU-Composite-License AND JPEG-License '
'AND JDOM-License AND LGPL-2.0 AND MIT-Open-Group AND MPL-1.1 '
'AND SAX-PD AND Unicode-Inc-License-Agreement '
'AND W3C-Software-Notice AND License AND W3C-Documentation-License')
assert expected == result.render('{symbol.key}')
expectedkey = ('EPL-1.0 AND Apache-1.1 AND Apache-2.0 AND BSD-Modified AND '
'CPL-1.0 AND ICU-Composite-License AND JPEG-License AND JDOM-License AND '
'LGPL-2.0 AND MIT-Open-Group AND MPL-1.1 AND SAX-PD AND '
'Unicode-Inc-License-Agreement AND W3C-Software-Notice AND License AND'
' W3C-Documentation-License')
assert expectedkey == result.render('{symbol.key}')
def test_render_with(self):
licensing = Licensing()
expression = 'GPL-2.0 with Classpath-2.0 OR BSD-new'
result = licensing.parse(expression)
expected = 'GPL-2.0 WITH Classpath-2.0 OR BSD-new'
assert expected == result.render('{symbol.key}')
expected_html = (
'<a href="path/GPL-2.0">GPL-2.0</a> WITH '
'<a href="path/Classpath-2.0">Classpath-2.0</a> '
'OR <a href="path/BSD-new">BSD-new</a>')
assert expected_html == result.render('<a href="path/{symbol.key}">{symbol.key}</a>')
expected = 'GPL-2.0 WITH Classpath-2.0 OR BSD-new'
assert expected == result.render('{symbol.key}')
def test_parse_complex(self):
licensing = Licensing()
expression = ' GPL-2.0 or later with classpath-Exception and mit or LPL-2.1 and mit or later '
result = licensing.parse(expression)
# this may look weird, but we did not provide symbols hence in "or later",
# "later" is treated as if it were a license
expected = 'GPL-2.0 OR (later WITH classpath-Exception AND mit) OR (LPL-2.1 AND mit) OR later'
assert expected == result.render('{symbol.key}')
def test_parse_complex2(self):
licensing = Licensing()
expr = licensing.parse(" GPL-2.0 or LGPL-2.1 and mit ")
expected = [
LicenseSymbol('GPL-2.0'),
LicenseSymbol('LGPL-2.1'),
LicenseSymbol('mit')
]
assert expected == sorted(licensing.license_symbols(expr))
expected = 'GPL-2.0 OR (LGPL-2.1 AND mit)'
assert expected == expr.render('{symbol.key}')
def test_Licensing_can_scan_valid_expressions_with_symbols_that_contain_and_with_or(self):
licensing = Licensing()
expression = 'orgpl or withbsd with orclasspath and andmit or andlgpl and ormit or withme'
result = [r.string for r in licensing.get_scanner().scan(expression)]
expected = [
'orgpl', ' or ', 'withbsd', ' with ', 'orclasspath',
' and ', 'andmit', ' or ', 'andlgpl', ' and ', 'ormit',
' or ', 'withme'
]
assert expected == result
def test_Licensing_can_tokenize_valid_expressions_with_symbols_that_contain_and_with_or(self):
licensing = Licensing()
expression = 'orgpl or withbsd with orclasspath and andmit or anlgpl and ormit or withme'
result = list(licensing.tokenize(expression))
expected = [
(LicenseSymbol(key='orgpl'), 'orgpl', 0),
(2, 'or', 6),
(LicenseWithExceptionSymbol(
license_symbol=LicenseSymbol(key='withbsd'),
exception_symbol=LicenseSymbol(key='orclasspath')),
'withbsd with orclasspath', 9),
(1, 'and', 34),
(LicenseSymbol(key='andmit'), 'andmit', 38),
(2, 'or', 45),
(LicenseSymbol(key='anlgpl'), 'anlgpl', 48),
(1, 'and', 55),
(LicenseSymbol(key='ormit'), 'ormit', 59),
(2, 'or', 65),
(LicenseSymbol(key='withme'), 'withme', 68)
]
assert expected == result
def test_Licensing_can_parse_valid_expressions_with_symbols_that_contain_and_with_or(self):
licensing = Licensing()
expression = 'orgpl or withbsd with orclasspath and andmit or anlgpl and ormit or withme'
result = licensing.parse(expression)
expected = 'orgpl OR (withbsd WITH orclasspath AND andmit) OR (anlgpl AND ormit) OR withme'
assert expected == result.render('{symbol.key}')
class LicensingParseWithSymbolsSimpleTest(TestCase):
def test_Licensing_with_illegal_symbols_raise_Exception(self):
try:
Licensing([
'GPL-2.0 or LATER',
'classpath Exception',
'something with else+',
'mit',
'LGPL 2.1',
'mit or later'
])
except ExpressionError as ee:
expected = ('Invalid license key: "or later" words are reserved and '
'cannot be used in a key: "GPL-2.0 or LATER"')
assert expected == str(ee)
def get_syms_and_licensing(self):
a = LicenseSymbol('l-a')
ap = LicenseSymbol('L-a+', ['l-a +'])
b = LicenseSymbol('l-b')
c = LicenseSymbol('l-c')
symbols = [a, ap, b, c]
return a, ap, b, c, Licensing(symbols)
def test_parse_license_expression1(self):
a, _ap, _b, _c, licensing = self.get_syms_and_licensing()
express_string = 'l-a'
result = licensing.parse(express_string)
assert express_string == str(result)
expected = a
assert expected == result
assert [] == licensing.unknown_license_keys(result)
def test_parse_license_expression_with_alias(self):
_a, ap, _b, _c, licensing = self.get_syms_and_licensing()
express_string = 'l-a +'
result = licensing.parse(express_string)
assert 'L-a+' == str(result)
expected = ap
assert expected == result
assert [] == licensing.unknown_license_keys(result)
def test_parse_license_expression3(self):
_a, ap, _b, _c, licensing = self.get_syms_and_licensing()
express_string = 'l-a+'
result = licensing.parse(express_string)
assert 'L-a+' == str(result)
expected = ap
assert expected == result
assert [] == licensing.unknown_license_keys(result)
def test_parse_license_expression4(self):
_a, _ap, _b, _c, licensing = self.get_syms_and_licensing()
express_string = '(l-a)'
result = licensing.parse(express_string)
assert 'l-a' == str(result)
expected = LicenseSymbol(key='l-a', aliases=())
assert expected == result
assert [] == licensing.unknown_license_keys(result)
def test_parse_license_expression5(self):
_a, ap, b, c, licensing = self.get_syms_and_licensing()
express_string = '((l-a+ AND l-b) OR (l-c))'
result = licensing.parse(express_string)
assert '(L-a+ AND l-b) OR l-c' == str(result)
expected = licensing.OR(licensing.AND(ap, b), c)
assert expected == result
assert [] == licensing.unknown_license_keys(result)
def test_parse_license_expression6(self):
a, _ap, b, _c, licensing = self.get_syms_and_licensing()
express_string = 'l-a and l-b'
result = licensing.parse(express_string)
assert 'l-a AND l-b' == str(result)
expected = licensing.AND(a, b)
assert expected == result
assert [] == licensing.unknown_license_keys(result)
def test_parse_license_expression7(self):
a, _ap, b, _c, licensing = self.get_syms_and_licensing()
express_string = 'l-a or l-b'
result = licensing.parse(express_string)
assert 'l-a OR l-b' == str(result)
expected = licensing.OR(a, b)
assert expected == result
assert [] == licensing.unknown_license_keys(result)
def test_parse_license_expression8(self):
a, _ap, b, c, licensing = self.get_syms_and_licensing()
express_string = 'l-a and l-b OR l-c'
result = licensing.parse(express_string)
assert '(l-a AND l-b) OR l-c' == str(result)
expected = licensing.OR(licensing.AND(a, b), c)
assert expected == result
assert [] == licensing.unknown_license_keys(result)
def test_parse_license_expression8_twice(self):
_a, _ap, _b, _c, licensing = self.get_syms_and_licensing()
express_string = 'l-a and l-b OR l-c'
result = licensing.parse(express_string)
assert '(l-a AND l-b) OR l-c' == str(result)
# there was some issues with reusing a Licensing
result = licensing.parse(express_string)
assert '(l-a AND l-b) OR l-c' == str(result)
def test_parse_license_expression_with_trailing_space_plus(self):
symbols = [
LicenseSymbol('l-a'),
LicenseSymbol('L-a+', ['l-a +']),
LicenseSymbol('l-b'),
LicenseSymbol('l-c'),
]
licensing = Licensing(symbols)
expresssion_str = 'l-a'
result = licensing.parse(expresssion_str)
assert expresssion_str == str(result)
assert [] == licensing.unknown_license_keys(result)
# plus sign is not attached to the symbol, but an alias
expresssion_str = 'l-a +'
result = licensing.parse(expresssion_str)
assert 'l-a+' == str(result).lower()
assert [] == licensing.unknown_license_keys(result)
expresssion_str = '(l-a)'
result = licensing.parse(expresssion_str)
assert 'l-a' == str(result).lower()
assert [] == licensing.unknown_license_keys(result)
expresssion_str = '((l-a+ AND l-b) OR (l-c))'
result = licensing.parse(expresssion_str)
assert '(L-a+ AND l-b) OR l-c' == str(result)
assert [] == licensing.unknown_license_keys(result)
expresssion_str = 'l-a and l-b'
result = licensing.parse(expresssion_str)
assert 'l-a AND l-b' == str(result)
assert [] == licensing.unknown_license_keys(result)
expresssion_str = 'l-a or l-b'
result = licensing.parse(expresssion_str)
assert 'l-a OR l-b' == str(result)
assert [] == licensing.unknown_license_keys(result)
expresssion_str = 'l-a and l-b OR l-c'
result = licensing.parse(expresssion_str)
assert '(l-a AND l-b) OR l-c' == str(result)
assert [] == licensing.unknown_license_keys(result)
def test_parse_of_side_by_side_symbols_raise_exception(self):
gpl2 = LicenseSymbol('gpl')
l = Licensing([gpl2])
try:
l.parse('gpl mit')
self.fail('ParseError not raised')
except ParseError:
pass
def test_validate_symbols(self):
symbols = [
LicenseSymbol('l-a', is_exception=True),
LicenseSymbol('l-a'),
LicenseSymbol('l-b'),
LicenseSymbol('l-c'),
]
warnings, errors = validate_symbols(symbols)
expectedw = []
assert expectedw == warnings
expectede = [
'Invalid duplicated license key: l-a.',
]
assert expectede == errors
class LicensingParseWithSymbolsTest(TestCase):
def test_parse_raise_ParseError_when_validating_strict_with_non_exception_symbols(self):
licensing = Licensing(['gpl', 'bsd', 'lgpl', 'exception'])
expression = 'gpl and bsd or lgpl with exception'
try:
licensing.parse(expression, validate=True, strict=True)
except ParseError as pe:
expected = {
'error_code': PARSE_INVALID_SYMBOL_AS_EXCEPTION,
'position': 25,
'token_string': 'exception',
'token_type': TOKEN_SYMBOL}
assert expected == _parse_error_as_dict(pe)
def test_parse_raise_ParseError_when_validating_strict_with_exception_symbols_in_incorrect_spot(self):
licensing = Licensing([LicenseSymbol('gpl', is_exception=False),
LicenseSymbol('exception', is_exception=True)])
licensing.parse('gpl with exception', validate=True, strict=True)
try:
licensing.parse('exception with gpl', validate=True, strict=True)
except ParseError as pe:
expected = {
'error_code': PARSE_INVALID_EXCEPTION,
'position': 0,
'token_string': 'exception',
'token_type': TOKEN_SYMBOL}
assert expected == _parse_error_as_dict(pe)
try:
licensing.parse('gpl with gpl', validate=True, strict=True)
except ParseError as pe:
expected = {
'error_code': PARSE_INVALID_SYMBOL_AS_EXCEPTION,
'position': 9,
'token_string': 'gpl',
'token_type': TOKEN_SYMBOL}
assert expected == _parse_error_as_dict(pe)
class LicensingSymbolsReplacement(TestCase):
def get_symbols_and_licensing(self):
gpl2 = LicenseSymbol('gpl-2.0', ['The GNU GPL 20', 'GPL-2.0', 'GPL v2.0'])
gpl2plus = LicenseSymbol('gpl-2.0+', ['The GNU GPL 20 or later', 'GPL-2.0 or later', 'GPL v2.0 or later'])
lgpl = LicenseSymbol('LGPL-2.1', ['LGPL v2.1'])
mit = LicenseSymbol('MIT', ['MIT license'])
mitand2 = LicenseSymbol('mitand2', ['mitand2', 'mitand2 license'])
symbols = [gpl2, gpl2plus, lgpl, mit, mitand2]
licensing = Licensing(symbols)
return gpl2, gpl2plus, lgpl, mit, mitand2, licensing
def test_simple_substitution(self):
gpl2, gpl2plus, _lgpl, _mit, _mitand2, licensing = self.get_symbols_and_licensing()
subs = {gpl2plus: gpl2}
expr = licensing.parse('gpl-2.0 or gpl-2.0+')
result = expr.subs(subs)
assert 'gpl-2.0 OR gpl-2.0' == result.render()
def test_advanced_substitution(self):
_gpl2, _gpl2plus, lgpl, _mit, _mitand2, licensing = self.get_symbols_and_licensing()
source = licensing.parse('gpl-2.0+ and mit')
target = lgpl
subs = {source: target}
expr = licensing.parse('gpl-2.0 or gpl-2.0+ and mit')
result = expr.subs(subs)
assert 'gpl-2.0 OR LGPL-2.1' == result.render()
def test_multiple_substitutions(self):
gpl2, gpl2plus, lgpl, mit, _mitand2, licensing = self.get_symbols_and_licensing()
source1 = licensing.parse('gpl-2.0+ and mit')
target1 = lgpl
source2 = licensing.parse('mitand2')
target2 = mit
source3 = gpl2
target3 = gpl2plus
subs = OrderedDict([
(source1, target1),
(source2, target2),
(source3, target3),
])
expr = licensing.parse('gpl-2.0 or gpl-2.0+ and mit')
# step 1: yields 'gpl-2.0 or lgpl'
# step 2: yields 'gpl-2.0+ or LGPL-2.1'
result = expr.subs(subs)
assert 'gpl-2.0+ OR LGPL-2.1' == result.render()
def test_multiple_substitutions_complex(self):
gpl2, gpl2plus, lgpl, mit, _mitand2, licensing = self.get_symbols_and_licensing()
source1 = licensing.parse('gpl-2.0+ and mit')
target1 = lgpl
source2 = licensing.parse('mitand2')
target2 = mit
source3 = gpl2
target3 = gpl2plus
subs = OrderedDict([
(source1, target1),
(source2, target2),
(source3, target3),
])
expr = licensing.parse('(gpl-2.0 or gpl-2.0+ and mit) and (gpl-2.0 or gpl-2.0+ and mit)')
# step 1: yields 'gpl-2.0 or lgpl'
# step 2: yields 'gpl-2.0+ or LGPL-2.1'
result = expr.subs(subs)
assert '(gpl-2.0+ OR LGPL-2.1) AND (gpl-2.0+ OR LGPL-2.1)' == result.render()
expr = licensing.parse('(gpl-2.0 or mit and gpl-2.0+) and (gpl-2.0 or gpl-2.0+ and mit)')
# step 1: yields 'gpl-2.0 or lgpl'
# step 2: yields 'gpl-2.0+ or LGPL-2.1'
result = expr.subs(subs)
assert '(gpl-2.0+ OR LGPL-2.1) AND (gpl-2.0+ OR LGPL-2.1)' == result.render()