blob: 3b359396b67dca331b3551c4bd44fb8dc2802582 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: MIT
3#
4
Brad Bishopd7bf8c12018-02-25 22:55:05 -05005from unittest.case import TestCase
6import oe.license
7
8class SeenVisitor(oe.license.LicenseVisitor):
9 def __init__(self):
10 self.seen = []
11 oe.license.LicenseVisitor.__init__(self)
12
13 def visit_Str(self, node):
14 self.seen.append(node.s)
15
16class TestSingleLicense(TestCase):
17 licenses = [
Andrew Geissler9aee5002022-03-30 16:27:02 +000018 "GPL-2.0-only",
19 "LGPL-2.0-only",
20 "Artistic-1.0",
Brad Bishopd7bf8c12018-02-25 22:55:05 -050021 "MIT",
Andrew Geissler9aee5002022-03-30 16:27:02 +000022 "GPL-3.0-or-later",
Brad Bishopd7bf8c12018-02-25 22:55:05 -050023 "FOO_BAR",
24 ]
25 invalid_licenses = ["GPL/BSD"]
26
27 @staticmethod
28 def parse(licensestr):
29 visitor = SeenVisitor()
30 visitor.visit_string(licensestr)
31 return visitor.seen
32
33 def test_single_licenses(self):
34 for license in self.licenses:
35 licenses = self.parse(license)
36 self.assertListEqual(licenses, [license])
37
38 def test_invalid_licenses(self):
39 for license in self.invalid_licenses:
40 with self.assertRaises(oe.license.InvalidLicense) as cm:
41 self.parse(license)
42 self.assertEqual(cm.exception.license, license)
43
44class TestSimpleCombinations(TestCase):
45 tests = {
46 "FOO&BAR": ["FOO", "BAR"],
47 "BAZ & MOO": ["BAZ", "MOO"],
48 "ALPHA|BETA": ["ALPHA"],
49 "BAZ&MOO|FOO": ["FOO"],
50 "FOO&BAR|BAZ": ["FOO", "BAR"],
51 }
52 preferred = ["ALPHA", "FOO", "BAR"]
53
54 def test_tests(self):
55 def choose(a, b):
56 if all(lic in self.preferred for lic in b):
57 return b
58 else:
59 return a
60
61 for license, expected in self.tests.items():
62 licenses = oe.license.flattened_licenses(license, choose)
63 self.assertListEqual(licenses, expected)
64
65class TestComplexCombinations(TestSimpleCombinations):
66 tests = {
67 "FOO & (BAR | BAZ)&MOO": ["FOO", "BAR", "MOO"],
68 "(ALPHA|(BETA&THETA)|OMEGA)&DELTA": ["OMEGA", "DELTA"],
69 "((ALPHA|BETA)&FOO)|BAZ": ["BETA", "FOO"],
Andrew Geissler9aee5002022-03-30 16:27:02 +000070 "(GPL-2.0-only|Proprietary)&BSD-4-clause&MIT": ["GPL-2.0-only", "BSD-4-clause", "MIT"],
Brad Bishopd7bf8c12018-02-25 22:55:05 -050071 }
Andrew Geissler9aee5002022-03-30 16:27:02 +000072 preferred = ["BAR", "OMEGA", "BETA", "GPL-2.0-only"]
Brad Bishopd7bf8c12018-02-25 22:55:05 -050073
74class TestIsIncluded(TestCase):
75 tests = {
76 ("FOO | BAR", None, None):
77 [True, ["FOO"]],
78 ("FOO | BAR", None, "FOO"):
79 [True, ["BAR"]],
80 ("FOO | BAR", "BAR", None):
81 [True, ["BAR"]],
82 ("FOO | BAR & FOOBAR", "*BAR", None):
83 [True, ["BAR", "FOOBAR"]],
84 ("FOO | BAR & FOOBAR", None, "FOO*"):
85 [False, ["FOOBAR"]],
86 ("(FOO | BAR) & FOOBAR | BARFOO", None, "FOO"):
87 [True, ["BAR", "FOOBAR"]],
88 ("(FOO | BAR) & FOOBAR | BAZ & MOO & BARFOO", None, "FOO"):
89 [True, ["BAZ", "MOO", "BARFOO"]],
Andrew Geissler9aee5002022-03-30 16:27:02 +000090 ("GPL-3.0-or-later & GPL-2.0-only & LGPL-2.1-only | Proprietary", None, None):
91 [True, ["GPL-3.0-or-later", "GPL-2.0-only", "LGPL-2.1-only"]],
92 ("GPL-3.0-or-later & GPL-2.0-only & LGPL-2.1-only | Proprietary", None, "GPL-3.0-or-later"):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050093 [True, ["Proprietary"]],
Andrew Geissler9aee5002022-03-30 16:27:02 +000094 ("GPL-3.0-or-later & GPL-2.0-only & LGPL-2.1-only | Proprietary", None, "GPL-3.0-or-later Proprietary"):
95 [False, ["GPL-3.0-or-later"]]
Brad Bishopd7bf8c12018-02-25 22:55:05 -050096 }
97
98 def test_tests(self):
99 for args, expected in self.tests.items():
100 is_included, licenses = oe.license.is_included(
101 args[0], (args[1] or '').split(), (args[2] or '').split())
102 self.assertEqual(is_included, expected[0])
103 self.assertListEqual(licenses, expected[1])