license.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from unittest.case import TestCase
  2. import oe.license
  3. class SeenVisitor(oe.license.LicenseVisitor):
  4. def __init__(self):
  5. self.seen = []
  6. oe.license.LicenseVisitor.__init__(self)
  7. def visit_Str(self, node):
  8. self.seen.append(node.s)
  9. class TestSingleLicense(TestCase):
  10. licenses = [
  11. "GPLv2",
  12. "LGPL-2.0",
  13. "Artistic",
  14. "MIT",
  15. "GPLv3+",
  16. "FOO_BAR",
  17. ]
  18. invalid_licenses = ["GPL/BSD"]
  19. @staticmethod
  20. def parse(licensestr):
  21. visitor = SeenVisitor()
  22. visitor.visit_string(licensestr)
  23. return visitor.seen
  24. def test_single_licenses(self):
  25. for license in self.licenses:
  26. licenses = self.parse(license)
  27. self.assertListEqual(licenses, [license])
  28. def test_invalid_licenses(self):
  29. for license in self.invalid_licenses:
  30. with self.assertRaises(oe.license.InvalidLicense) as cm:
  31. self.parse(license)
  32. self.assertEqual(cm.exception.license, license)
  33. class TestSimpleCombinations(TestCase):
  34. tests = {
  35. "FOO&BAR": ["FOO", "BAR"],
  36. "BAZ & MOO": ["BAZ", "MOO"],
  37. "ALPHA|BETA": ["ALPHA"],
  38. "BAZ&MOO|FOO": ["FOO"],
  39. "FOO&BAR|BAZ": ["FOO", "BAR"],
  40. }
  41. preferred = ["ALPHA", "FOO", "BAR"]
  42. def test_tests(self):
  43. def choose(a, b):
  44. if all(lic in self.preferred for lic in b):
  45. return b
  46. else:
  47. return a
  48. for license, expected in self.tests.items():
  49. licenses = oe.license.flattened_licenses(license, choose)
  50. self.assertListEqual(licenses, expected)
  51. class TestComplexCombinations(TestSimpleCombinations):
  52. tests = {
  53. "FOO & (BAR | BAZ)&MOO": ["FOO", "BAR", "MOO"],
  54. "(ALPHA|(BETA&THETA)|OMEGA)&DELTA": ["OMEGA", "DELTA"],
  55. "((ALPHA|BETA)&FOO)|BAZ": ["BETA", "FOO"],
  56. "(GPL-2.0|Proprietary)&BSD-4-clause&MIT": ["GPL-2.0", "BSD-4-clause", "MIT"],
  57. }
  58. preferred = ["BAR", "OMEGA", "BETA", "GPL-2.0"]
  59. class TestIsIncluded(TestCase):
  60. tests = {
  61. ("FOO | BAR", None, None):
  62. [True, ["FOO"]],
  63. ("FOO | BAR", None, "FOO"):
  64. [True, ["BAR"]],
  65. ("FOO | BAR", "BAR", None):
  66. [True, ["BAR"]],
  67. ("FOO | BAR & FOOBAR", "*BAR", None):
  68. [True, ["BAR", "FOOBAR"]],
  69. ("FOO | BAR & FOOBAR", None, "FOO*"):
  70. [False, ["FOOBAR"]],
  71. ("(FOO | BAR) & FOOBAR | BARFOO", None, "FOO"):
  72. [True, ["BAR", "FOOBAR"]],
  73. ("(FOO | BAR) & FOOBAR | BAZ & MOO & BARFOO", None, "FOO"):
  74. [True, ["BAZ", "MOO", "BARFOO"]],
  75. ("GPL-3.0 & GPL-2.0 & LGPL-2.1 | Proprietary", None, None):
  76. [True, ["GPL-3.0", "GPL-2.0", "LGPL-2.1"]],
  77. ("GPL-3.0 & GPL-2.0 & LGPL-2.1 | Proprietary", None, "GPL-3.0"):
  78. [True, ["Proprietary"]],
  79. ("GPL-3.0 & GPL-2.0 & LGPL-2.1 | Proprietary", None, "GPL-3.0 Proprietary"):
  80. [False, ["GPL-3.0"]]
  81. }
  82. def test_tests(self):
  83. for args, expected in self.tests.items():
  84. is_included, licenses = oe.license.is_included(
  85. args[0], (args[1] or '').split(), (args[2] or '').split())
  86. self.assertEqual(is_included, expected[0])
  87. self.assertListEqual(licenses, expected[1])