blob: fa05eaa12f33e7317d34f4a22e35d3645cd7fb96 [file] [log] [blame]
Brian Sheedy8e144aa2020-06-23 16:02:16 -07001#!/usr/bin/env vpython
sakal67e414c2017-09-05 00:16:15 -07002# 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
sakal67e414c2017-09-05 00:16:15 -070012import unittest
Artem Titov5d7a4c62018-07-23 13:58:25 +020013import mock
sakal67e414c2017-09-05 00:16:15 -070014
15from generate_licenses import LicenseBuilder
16
17
18class TestLicenseBuilder(unittest.TestCase):
Artem Titarenkoa9719482018-12-11 17:00:04 +010019
sakal67e414c2017-09-05 00:16:15 -070020 @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
Artem Titarenkoa9719482018-12-11 17:00:04 +010035 def testParseLibraryName(self):
36 self.assertEquals(
37 LicenseBuilder._ParseLibraryName('//a/b/third_party/libname1:c'),
sakal67e414c2017-09-05 00:16:15 -070038 'libname1')
Artem Titarenkoa9719482018-12-11 17:00:04 +010039 self.assertEquals(
40 LicenseBuilder._ParseLibraryName('//a/b/third_party/libname2:c(d)'),
sakal67e414c2017-09-05 00:16:15 -070041 'libname2')
Artem Titarenkoa9719482018-12-11 17:00:04 +010042 self.assertEquals(
43 LicenseBuilder._ParseLibraryName('//a/b/third_party/libname3/c:d(e)'),
sakal67e414c2017-09-05 00:16:15 -070044 'libname3')
Artem Titarenkoa9719482018-12-11 17:00:04 +010045 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.*')
sakal67e414c2017-09-05 00:16:15 -070087
88 @mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
Artem Titarenkoa9719482018-12-11 17:00:04 +010089 def testGetThirdPartyLibrariesWithoutRegex(self):
90 builder = LicenseBuilder([], [], {}, {})
91 self.assertEquals(
92 builder._GetThirdPartyLibraries('out/arm', 'target1'),
sakal67e414c2017-09-05 00:16:15 -070093 set(['libname1', 'libname2', 'libname3']))
94
Artem Titarenkoa9719482018-12-11 17:00:04 +010095 @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
sakal67e414c2017-09-05 00:16:15 -0700120
121if __name__ == '__main__':
122 unittest.main()