blob: a75b31f3a791040682d4056ce79a917ca42eb0d7 [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
Matt Schulte52e1d5a2024-01-18 15:30:30 -080085 def test_0bsd_licensefile_with_extension(self):
86 self.fs.create_file("LICENSE-0BSD.md")
87 licenses = add3prf.decide_license_type("")
88 self.assertEqual(len(licenses), 1)
89 preferred_license = licenses[0]
90 self.assertEqual(preferred_license.type, add3prf.LicenseType.ZERO_BSD)
91 self.assertEqual(preferred_license.group, add3prf.LicenseGroup.PERMISSIVE)
92 self.assertEqual(preferred_license.filename, "LICENSE-0BSD.md")
93
Matt Schulteb4fa3db2023-12-21 08:06:49 -080094 def test_unlicense_license(self):
95 self.fs.create_file("LICENSE")
96 licenses = add3prf.decide_license_type("Unlicense")
97 self.assertEqual(len(licenses), 1)
98 preferred_license = licenses[0]
99 self.assertEqual(preferred_license.type, add3prf.LicenseType.UNLICENSE)
100 self.assertEqual(preferred_license.group, add3prf.LicenseGroup.PERMISSIVE)
101 self.assertEqual(preferred_license.filename, "LICENSE")
102
103 def test_unlicense_licensefile(self):
104 self.fs.create_file("UNLICENSE")
105 licenses = add3prf.decide_license_type("")
106 self.assertEqual(len(licenses), 1)
107 preferred_license = licenses[0]
108 self.assertEqual(preferred_license.type, add3prf.LicenseType.UNLICENSE)
109 self.assertEqual(preferred_license.group, add3prf.LicenseGroup.PERMISSIVE)
110 self.assertEqual(preferred_license.filename, "UNLICENSE")
111
Matt Schulte52e1d5a2024-01-18 15:30:30 -0800112 def test_unlicense_licensefile_with_extension(self):
113 self.fs.create_file("UNLICENSE.txt")
114 licenses = add3prf.decide_license_type("")
115 self.assertEqual(len(licenses), 1)
116 preferred_license = licenses[0]
117 self.assertEqual(preferred_license.type, add3prf.LicenseType.UNLICENSE)
118 self.assertEqual(preferred_license.group, add3prf.LicenseGroup.PERMISSIVE)
119 self.assertEqual(preferred_license.filename, "UNLICENSE.txt")
Thiébaud Weksteen8da49112021-02-19 11:59:49 +0100120
121class AddModuleLicenseTestCase(fake_filesystem_unittest.TestCase):
122
123 def setUp(self):
124 self.setUpPyfakefs()
125
126 def test_no_file(self):
127 add3prf.add_module_license(add3prf.LicenseType.APACHE2)
128 self.assertTrue(os.path.exists("MODULE_LICENSE_APACHE2"))
129
130 def test_already_exists(self):
131 self.fs.create_file("MODULE_LICENSE_APACHE2")
132 add3prf.add_module_license(add3prf.LicenseType.APACHE2)
133
134 def test_mit_apache(self):
135 self.fs.create_file("MODULE_LICENSE_MIT")
136 with self.assertRaises(Exception):
137 add3prf.add_module_license(add3prf.LicenseType.APACHE2)
138
139
140if __name__ == '__main__':
141 unittest.main(verbosity=2)