Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | # |
| 4 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 5 | from unittest.case import TestCase |
| 6 | import oe.license |
| 7 | |
| 8 | class 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 | |
| 16 | class TestSingleLicense(TestCase): |
| 17 | licenses = [ |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame^] | 18 | "GPL-2.0-only", |
| 19 | "LGPL-2.0-only", |
| 20 | "Artistic-1.0", |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 21 | "MIT", |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame^] | 22 | "GPL-3.0-or-later", |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 23 | "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 | |
| 44 | class 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 | |
| 65 | class 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 Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame^] | 70 | "(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] | 71 | } |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame^] | 72 | preferred = ["BAR", "OMEGA", "BETA", "GPL-2.0-only"] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 73 | |
| 74 | class 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 Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame^] | 90 | ("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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 93 | [True, ["Proprietary"]], |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame^] | 94 | ("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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 96 | } |
| 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]) |