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