Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame^] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 7 | from unittest.case import TestCase |
| 8 | import oe.license |
| 9 | |
| 10 | class 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 | |
| 18 | class TestSingleLicense(TestCase): |
| 19 | licenses = [ |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 20 | "GPL-2.0-only", |
| 21 | "LGPL-2.0-only", |
| 22 | "Artistic-1.0", |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 23 | "MIT", |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 24 | "GPL-3.0-or-later", |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 25 | "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 | |
| 46 | class 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 | |
| 67 | class 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 Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 72 | "(GPL-2.0-only|Proprietary)&BSD-4-clause&MIT": ["GPL-2.0-only", "BSD-4-clause", "MIT"], |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 73 | } |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 74 | preferred = ["BAR", "OMEGA", "BETA", "GPL-2.0-only"] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 75 | |
| 76 | class 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 Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 92 | ("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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 95 | [True, ["Proprietary"]], |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 96 | ("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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 98 | } |
| 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]) |