blob: 80bcfa4c18c9387778c4afa8cf13ed6dc95fe400 [file] [log] [blame]
Thiébaud Weksteen8da49112021-02-19 11:59:49 +01001# Copyright 2021 Google Inc. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14import os.path
15import unittest
16
17from pyfakefs import fake_filesystem_unittest
18
19import add3prf
20
21class LicenseDetectionTestCase(fake_filesystem_unittest.TestCase):
22
23 def setUp(self):
24 self.setUpPyfakefs()
25
26 def test_dual_license(self):
27 self.fs.create_file("LICENSE-APACHE")
28 self.fs.create_file("LICENSE-MIT")
29 licenses = add3prf.decide_license_type("MIT OR Apache-2.0")
30 self.assertEqual(len(licenses), 2)
31 preferred_license = licenses[0]
32 self.assertEqual(preferred_license.type, add3prf.LicenseType.APACHE2)
Matt Schulte055ccb32023-10-30 14:07:27 -070033 self.assertEqual(preferred_license.group, add3prf.LicenseGroup.NOTICE)
Thiébaud Weksteen8da49112021-02-19 11:59:49 +010034 self.assertEqual(preferred_license.filename, "LICENSE-APACHE")
35
36 def test_mit_license(self):
37 self.fs.create_file("LICENSE")
38 licenses = add3prf.decide_license_type("MIT")
39 self.assertEqual(len(licenses), 1)
40 preferred_license = licenses[0]
41 self.assertEqual(preferred_license.type, add3prf.LicenseType.MIT)
Matt Schulte055ccb32023-10-30 14:07:27 -070042 self.assertEqual(preferred_license.group, add3prf.LicenseGroup.NOTICE)
Thiébaud Weksteen8da49112021-02-19 11:59:49 +010043 self.assertEqual(preferred_license.filename, "LICENSE")
44
45 def test_misc_license(self):
46 self.fs.create_file("LICENSE.txt")
47 licenses = add3prf.decide_license_type("")
48 self.assertEqual(len(licenses), 1)
49 preferred_license = licenses[0]
50 self.assertEqual(preferred_license.type, add3prf.LicenseType.BSD_LIKE)
Matt Schulte055ccb32023-10-30 14:07:27 -070051 self.assertEqual(preferred_license.group, add3prf.LicenseGroup.NOTICE)
Thiébaud Weksteen8da49112021-02-19 11:59:49 +010052 self.assertEqual(preferred_license.filename, "LICENSE.txt")
53
54 def test_missing_license_file(self):
55 with self.assertRaises(FileNotFoundError):
56 add3prf.decide_license_type("MIT OR Apache-2.0")
57
Matt Schulte055ccb32023-10-30 14:07:27 -070058 def test_mpl_license(self):
59 self.fs.create_file("LICENSE")
60 licenses = add3prf.decide_license_type("MPL")
61 self.assertEqual(len(licenses), 1)
62 preferred_license = licenses[0]
63 self.assertEqual(preferred_license.type, add3prf.LicenseType.MPL)
64 self.assertEqual(preferred_license.group, add3prf.LicenseGroup.RECIPROCAL)
65 self.assertEqual(preferred_license.filename, "LICENSE")
66
Matt Schulte38d199e2023-12-20 10:05:57 -080067 def test_0bsd_license(self):
68 self.fs.create_file("LICENSE")
69 licenses = add3prf.decide_license_type("0BSD")
70 self.assertEqual(len(licenses), 1)
71 preferred_license = licenses[0]
72 self.assertEqual(preferred_license.type, add3prf.LicenseType.ZERO_BSD)
73 self.assertEqual(preferred_license.group, add3prf.LicenseGroup.PERMISSIVE)
74 self.assertEqual(preferred_license.filename, "LICENSE")
75
76 def test_0bsd_licensefile(self):
77 self.fs.create_file("LICENSE-0BSD")
78 licenses = add3prf.decide_license_type("")
79 self.assertEqual(len(licenses), 1)
80 preferred_license = licenses[0]
81 self.assertEqual(preferred_license.type, add3prf.LicenseType.ZERO_BSD)
82 self.assertEqual(preferred_license.group, add3prf.LicenseGroup.PERMISSIVE)
83 self.assertEqual(preferred_license.filename, "LICENSE-0BSD")
84
Thiébaud Weksteen8da49112021-02-19 11:59:49 +010085
86class AddModuleLicenseTestCase(fake_filesystem_unittest.TestCase):
87
88 def setUp(self):
89 self.setUpPyfakefs()
90
91 def test_no_file(self):
92 add3prf.add_module_license(add3prf.LicenseType.APACHE2)
93 self.assertTrue(os.path.exists("MODULE_LICENSE_APACHE2"))
94
95 def test_already_exists(self):
96 self.fs.create_file("MODULE_LICENSE_APACHE2")
97 add3prf.add_module_license(add3prf.LicenseType.APACHE2)
98
99 def test_mit_apache(self):
100 self.fs.create_file("MODULE_LICENSE_MIT")
101 with self.assertRaises(Exception):
102 add3prf.add_module_license(add3prf.LicenseType.APACHE2)
103
104
105if __name__ == '__main__':
106 unittest.main(verbosity=2)