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.filename, "LICENSE-APACHE") 34 35 def test_mit_license(self): 36 self.fs.create_file("LICENSE") 37 licenses = add3prf.decide_license_type("MIT") 38 self.assertEqual(len(licenses), 1) 39 preferred_license = licenses[0] 40 self.assertEqual(preferred_license.type, add3prf.LicenseType.MIT) 41 self.assertEqual(preferred_license.filename, "LICENSE") 42 43 def test_misc_license(self): 44 self.fs.create_file("LICENSE.txt") 45 licenses = add3prf.decide_license_type("") 46 self.assertEqual(len(licenses), 1) 47 preferred_license = licenses[0] 48 self.assertEqual(preferred_license.type, add3prf.LicenseType.BSD_LIKE) 49 self.assertEqual(preferred_license.filename, "LICENSE.txt") 50 51 def test_missing_license_file(self): 52 with self.assertRaises(FileNotFoundError): 53 add3prf.decide_license_type("MIT OR Apache-2.0") 54 55 56class AddModuleLicenseTestCase(fake_filesystem_unittest.TestCase): 57 58 def setUp(self): 59 self.setUpPyfakefs() 60 61 def test_no_file(self): 62 add3prf.add_module_license(add3prf.LicenseType.APACHE2) 63 self.assertTrue(os.path.exists("MODULE_LICENSE_APACHE2")) 64 65 def test_already_exists(self): 66 self.fs.create_file("MODULE_LICENSE_APACHE2") 67 add3prf.add_module_license(add3prf.LicenseType.APACHE2) 68 69 def test_mit_apache(self): 70 self.fs.create_file("MODULE_LICENSE_MIT") 71 with self.assertRaises(Exception): 72 add3prf.add_module_license(add3prf.LicenseType.APACHE2) 73 74 75if __name__ == '__main__': 76 unittest.main(verbosity=2) 77