blob: 51acb89881cf12b02de10540cb12cd305aa931df [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):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010019 @staticmethod
20 def _FakeRunGN(buildfile_dir, target):
21 return """
sakal67e414c2017-09-05 00:16:15 -070022 {
23 "target1": {
24 "deps": [
25 "//a/b/third_party/libname1:c",
26 "//a/b/third_party/libname2:c(//d/e/f:g)",
27 "//a/b/third_party/libname3/c:d(//e/f/g:h)",
28 "//a/b/not_third_party/c"
29 ]
30 }
31 }
32 """
33
Mirko Bonadei8cc66952020-10-30 10:13:45 +010034 def testParseLibraryName(self):
35 self.assertEquals(
36 LicenseBuilder._ParseLibraryName('//a/b/third_party/libname1:c'),
37 'libname1')
38 self.assertEquals(
39 LicenseBuilder._ParseLibraryName(
40 '//a/b/third_party/libname2:c(d)'), 'libname2')
41 self.assertEquals(
42 LicenseBuilder._ParseLibraryName(
43 '//a/b/third_party/libname3/c:d(e)'), 'libname3')
44 self.assertEquals(
45 LicenseBuilder._ParseLibraryName('//a/b/not_third_party/c'), None)
Artem Titarenkoa9719482018-12-11 17:00:04 +010046
Mirko Bonadei8cc66952020-10-30 10:13:45 +010047 def testParseLibrarySimpleMatch(self):
48 builder = LicenseBuilder([], [], {}, {})
49 self.assertEquals(builder._ParseLibrary('//a/b/third_party/libname:c'),
50 'libname')
Artem Titarenkoa9719482018-12-11 17:00:04 +010051
Mirko Bonadei8cc66952020-10-30 10:13:45 +010052 def testParseLibraryRegExNoMatchFallbacksToDefaultLibname(self):
53 lib_dict = {
54 'libname:foo.*': ['path/to/LICENSE'],
55 }
56 builder = LicenseBuilder([], [], lib_dict, {})
57 self.assertEquals(
58 builder._ParseLibrary('//a/b/third_party/libname:bar_java'),
59 'libname')
Artem Titarenkoa9719482018-12-11 17:00:04 +010060
Mirko Bonadei8cc66952020-10-30 10:13:45 +010061 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.*')
Artem Titarenkoa9719482018-12-11 17:00:04 +010069
Mirko Bonadei8cc66952020-10-30 10:13:45 +010070 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.*')
Artem Titarenkoa9719482018-12-11 17:00:04 +010078
Mirko Bonadei8cc66952020-10-30 10:13:45 +010079 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(
86 '//a/b/third_party/libname/fooHAHA:bar_java'),
87 'libname/foo.*bar.*')
sakal67e414c2017-09-05 00:16:15 -070088
Mirko Bonadei8cc66952020-10-30 10:13:45 +010089 @mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
90 def testGetThirdPartyLibrariesWithoutRegex(self):
91 builder = LicenseBuilder([], [], {}, {})
92 self.assertEquals(
93 builder._GetThirdPartyLibraries('out/arm', 'target1'),
94 set(['libname1', 'libname2', 'libname3']))
sakal67e414c2017-09-05 00:16:15 -070095
Mirko Bonadei8cc66952020-10-30 10:13:45 +010096 @mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
97 def testGetThirdPartyLibrariesWithRegex(self):
98 lib_regex_dict = {
99 'libname2:c.*': ['path/to/LICENSE'],
100 }
101 builder = LicenseBuilder([], [], {}, lib_regex_dict)
102 self.assertEquals(
103 builder._GetThirdPartyLibraries('out/arm', 'target1'),
104 set(['libname1', 'libname2:c.*', 'libname3']))
Artem Titarenkoa9719482018-12-11 17:00:04 +0100105
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100106 @mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
107 def testGenerateLicenseTextFailIfUnknownLibrary(self):
108 lib_dict = {
109 'simple_library': ['path/to/LICENSE'],
110 }
111 builder = LicenseBuilder(['dummy_dir'], ['dummy_target'], lib_dict, {})
Artem Titarenkoa9719482018-12-11 17:00:04 +0100112
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100113 with self.assertRaises(Exception) as context:
114 builder.GenerateLicenseText('dummy/dir')
Artem Titarenkoa9719482018-12-11 17:00:04 +0100115
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100116 self.assertEquals(
117 context.exception.message,
118 'Missing licenses for following third_party targets: '
119 'libname1, libname2, libname3')
Artem Titarenkoa9719482018-12-11 17:00:04 +0100120
sakal67e414c2017-09-05 00:16:15 -0700121
122if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100123 unittest.main()