1# 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)
33    self.assertEqual(preferred_license.group, add3prf.LicenseGroup.NOTICE)
34    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)
42    self.assertEqual(preferred_license.group, add3prf.LicenseGroup.NOTICE)
43    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)
51    self.assertEqual(preferred_license.group, add3prf.LicenseGroup.NOTICE)
52    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
58  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
67  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
85  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
94  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
112  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")
120
121  def test_zlib_license(self):
122    self.fs.create_file("LICENSE")
123    licenses = add3prf.decide_license_type("Zlib")
124    self.assertEqual(len(licenses), 1)
125    preferred_license = licenses[0]
126    self.assertEqual(preferred_license.type, add3prf.LicenseType.ZLIB)
127    self.assertEqual(preferred_license.group, add3prf.LicenseGroup.NOTICE)
128    self.assertEqual(preferred_license.filename, "LICENSE")
129
130  def test_zlib_licensefile(self):
131    self.fs.create_file("LICENSE-ZLIB")
132    licenses = add3prf.decide_license_type("")
133    self.assertEqual(len(licenses), 1)
134    preferred_license = licenses[0]
135    self.assertEqual(preferred_license.type, add3prf.LicenseType.ZLIB)
136    self.assertEqual(preferred_license.group, add3prf.LicenseGroup.NOTICE)
137    self.assertEqual(preferred_license.filename, "LICENSE-ZLIB")
138
139  def test_boost_license(self):
140    self.fs.create_file("LICENSE")
141    licenses = add3prf.decide_license_type("BSL-1.0")
142    self.assertEqual(len(licenses), 1)
143    preferred_license = licenses[0]
144    self.assertEqual(preferred_license.type, add3prf.LicenseType.BOOST)
145    self.assertEqual(preferred_license.group, add3prf.LicenseGroup.NOTICE)
146    self.assertEqual(preferred_license.filename, "LICENSE")
147
148  def test_boost_licensefile(self):
149    self.fs.create_file("LICENSE-BOOST")
150    licenses = add3prf.decide_license_type("")
151    self.assertEqual(len(licenses), 1)
152    preferred_license = licenses[0]
153    self.assertEqual(preferred_license.type, add3prf.LicenseType.BOOST)
154    self.assertEqual(preferred_license.group, add3prf.LicenseGroup.NOTICE)
155    self.assertEqual(preferred_license.filename, "LICENSE-BOOST")
156
157  def test_bsd_licensefile(self):
158    self.fs.create_file("LICENSE-BSD")
159    licenses = add3prf.decide_license_type("")
160    self.assertEqual(len(licenses), 1)
161    preferred_license = licenses[0]
162    self.assertEqual(preferred_license.type, add3prf.LicenseType.BSD_LIKE)
163    self.assertEqual(preferred_license.group, add3prf.LicenseGroup.NOTICE)
164    self.assertEqual(preferred_license.filename, "LICENSE-BSD")
165
166class AddModuleLicenseTestCase(fake_filesystem_unittest.TestCase):
167
168  def setUp(self):
169    self.setUpPyfakefs()
170
171  def test_no_file(self):
172    add3prf.add_module_license(add3prf.LicenseType.APACHE2)
173    self.assertTrue(os.path.exists("MODULE_LICENSE_APACHE2"))
174
175  def test_already_exists(self):
176    self.fs.create_file("MODULE_LICENSE_APACHE2")
177    add3prf.add_module_license(add3prf.LicenseType.APACHE2)
178
179  def test_mit_apache(self):
180    self.fs.create_file("MODULE_LICENSE_MIT")
181    with self.assertRaises(Exception):
182      add3prf.add_module_license(add3prf.LicenseType.APACHE2)
183
184
185if __name__ == '__main__':
186  unittest.main(verbosity=2)
187