1#!/usr/bin/env vpython 2# pylint: disable=relative-import,protected-access,unused-argument 3 4# Copyright 2017 The WebRTC project authors. All Rights Reserved. 5# 6# Use of this source code is governed by a BSD-style license 7# that can be found in the LICENSE file in the root of the source 8# tree. An additional intellectual property rights grant can be found 9# in the file PATENTS. All contributing project authors may 10# be found in the AUTHORS file in the root of the source tree. 11 12import unittest 13import mock 14 15from generate_licenses import LicenseBuilder 16 17 18class TestLicenseBuilder(unittest.TestCase): 19 20 @staticmethod 21 def _FakeRunGN(buildfile_dir, target): 22 return """ 23 { 24 "target1": { 25 "deps": [ 26 "//a/b/third_party/libname1:c", 27 "//a/b/third_party/libname2:c(//d/e/f:g)", 28 "//a/b/third_party/libname3/c:d(//e/f/g:h)", 29 "//a/b/not_third_party/c" 30 ] 31 } 32 } 33 """ 34 35 def testParseLibraryName(self): 36 self.assertEquals( 37 LicenseBuilder._ParseLibraryName('//a/b/third_party/libname1:c'), 38 'libname1') 39 self.assertEquals( 40 LicenseBuilder._ParseLibraryName('//a/b/third_party/libname2:c(d)'), 41 'libname2') 42 self.assertEquals( 43 LicenseBuilder._ParseLibraryName('//a/b/third_party/libname3/c:d(e)'), 44 'libname3') 45 self.assertEquals( 46 LicenseBuilder._ParseLibraryName('//a/b/not_third_party/c'), None) 47 48 def testParseLibrarySimpleMatch(self): 49 builder = LicenseBuilder([], [], {}, {}) 50 self.assertEquals( 51 builder._ParseLibrary('//a/b/third_party/libname:c'), 'libname') 52 53 def testParseLibraryRegExNoMatchFallbacksToDefaultLibname(self): 54 lib_dict = { 55 'libname:foo.*': ['path/to/LICENSE'], 56 } 57 builder = LicenseBuilder([], [], lib_dict, {}) 58 self.assertEquals( 59 builder._ParseLibrary('//a/b/third_party/libname:bar_java'), 'libname') 60 61 def testParseLibraryRegExMatch(self): 62 lib_regex_dict = { 63 'libname:foo.*': ['path/to/LICENSE'], 64 } 65 builder = LicenseBuilder([], [], {}, lib_regex_dict) 66 self.assertEquals( 67 builder._ParseLibrary('//a/b/third_party/libname:foo_bar_java'), 68 'libname:foo.*') 69 70 def testParseLibraryRegExMatchWithSubDirectory(self): 71 lib_regex_dict = { 72 'libname/foo:bar.*': ['path/to/LICENSE'], 73 } 74 builder = LicenseBuilder([], [], {}, lib_regex_dict) 75 self.assertEquals( 76 builder._ParseLibrary('//a/b/third_party/libname/foo:bar_java'), 77 'libname/foo:bar.*') 78 79 def testParseLibraryRegExMatchWithStarInside(self): 80 lib_regex_dict = { 81 'libname/foo.*bar.*': ['path/to/LICENSE'], 82 } 83 builder = LicenseBuilder([], [], {}, lib_regex_dict) 84 self.assertEquals( 85 builder._ParseLibrary('//a/b/third_party/libname/fooHAHA:bar_java'), 86 'libname/foo.*bar.*') 87 88 @mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN) 89 def testGetThirdPartyLibrariesWithoutRegex(self): 90 builder = LicenseBuilder([], [], {}, {}) 91 self.assertEquals( 92 builder._GetThirdPartyLibraries('out/arm', 'target1'), 93 set(['libname1', 'libname2', 'libname3'])) 94 95 @mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN) 96 def testGetThirdPartyLibrariesWithRegex(self): 97 lib_regex_dict = { 98 'libname2:c.*': ['path/to/LICENSE'], 99 } 100 builder = LicenseBuilder([], [], {}, lib_regex_dict) 101 self.assertEquals( 102 builder._GetThirdPartyLibraries('out/arm', 'target1'), 103 set(['libname1', 'libname2:c.*', 'libname3'])) 104 105 @mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN) 106 def testGenerateLicenseTextFailIfUnknownLibrary(self): 107 lib_dict = { 108 'simple_library': ['path/to/LICENSE'], 109 } 110 builder = LicenseBuilder(['dummy_dir'], ['dummy_target'], lib_dict, {}) 111 112 with self.assertRaises(Exception) as context: 113 builder.GenerateLicenseText('dummy/dir') 114 115 self.assertEquals( 116 context.exception.message, 117 'Missing licenses for following third_party targets: ' 118 'libname1, libname2, libname3') 119 120 121if __name__ == '__main__': 122 unittest.main() 123