blob: ebef78e132e69375e02a05c5bc1e09a7d75171cb [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
Byoungchan Lee5be2aa12021-05-29 13:50:31 +090013try:
14 # python 3.3+
15 from unittest.mock import patch
16except ImportError:
17 # From site-package
18 from mock import patch
sakal67e414c2017-09-05 00:16:15 -070019
20from generate_licenses import LicenseBuilder
21
22
23class TestLicenseBuilder(unittest.TestCase):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010024 @staticmethod
25 def _FakeRunGN(buildfile_dir, target):
26 return """
sakal67e414c2017-09-05 00:16:15 -070027 {
28 "target1": {
29 "deps": [
30 "//a/b/third_party/libname1:c",
31 "//a/b/third_party/libname2:c(//d/e/f:g)",
32 "//a/b/third_party/libname3/c:d(//e/f/g:h)",
33 "//a/b/not_third_party/c"
34 ]
35 }
36 }
37 """
38
Mirko Bonadei8cc66952020-10-30 10:13:45 +010039 def testParseLibraryName(self):
Byoungchan Lee5be2aa12021-05-29 13:50:31 +090040 self.assertEqual(
Mirko Bonadei8cc66952020-10-30 10:13:45 +010041 LicenseBuilder._ParseLibraryName('//a/b/third_party/libname1:c'),
42 'libname1')
Byoungchan Lee5be2aa12021-05-29 13:50:31 +090043 self.assertEqual(
Mirko Bonadei8cc66952020-10-30 10:13:45 +010044 LicenseBuilder._ParseLibraryName(
45 '//a/b/third_party/libname2:c(d)'), 'libname2')
Byoungchan Lee5be2aa12021-05-29 13:50:31 +090046 self.assertEqual(
Mirko Bonadei8cc66952020-10-30 10:13:45 +010047 LicenseBuilder._ParseLibraryName(
48 '//a/b/third_party/libname3/c:d(e)'), 'libname3')
Byoungchan Lee5be2aa12021-05-29 13:50:31 +090049 self.assertEqual(
Mirko Bonadei8cc66952020-10-30 10:13:45 +010050 LicenseBuilder._ParseLibraryName('//a/b/not_third_party/c'), None)
Artem Titarenkoa9719482018-12-11 17:00:04 +010051
Mirko Bonadei8cc66952020-10-30 10:13:45 +010052 def testParseLibrarySimpleMatch(self):
53 builder = LicenseBuilder([], [], {}, {})
Byoungchan Lee5be2aa12021-05-29 13:50:31 +090054 self.assertEqual(builder._ParseLibrary('//a/b/third_party/libname:c'),
Mirko Bonadei8cc66952020-10-30 10:13:45 +010055 'libname')
Artem Titarenkoa9719482018-12-11 17:00:04 +010056
Mirko Bonadei8cc66952020-10-30 10:13:45 +010057 def testParseLibraryRegExNoMatchFallbacksToDefaultLibname(self):
58 lib_dict = {
59 'libname:foo.*': ['path/to/LICENSE'],
60 }
61 builder = LicenseBuilder([], [], lib_dict, {})
Byoungchan Lee5be2aa12021-05-29 13:50:31 +090062 self.assertEqual(
Mirko Bonadei8cc66952020-10-30 10:13:45 +010063 builder._ParseLibrary('//a/b/third_party/libname:bar_java'),
64 'libname')
Artem Titarenkoa9719482018-12-11 17:00:04 +010065
Mirko Bonadei8cc66952020-10-30 10:13:45 +010066 def testParseLibraryRegExMatch(self):
67 lib_regex_dict = {
68 'libname:foo.*': ['path/to/LICENSE'],
69 }
70 builder = LicenseBuilder([], [], {}, lib_regex_dict)
Byoungchan Lee5be2aa12021-05-29 13:50:31 +090071 self.assertEqual(
Mirko Bonadei8cc66952020-10-30 10:13:45 +010072 builder._ParseLibrary('//a/b/third_party/libname:foo_bar_java'),
73 'libname:foo.*')
Artem Titarenkoa9719482018-12-11 17:00:04 +010074
Mirko Bonadei8cc66952020-10-30 10:13:45 +010075 def testParseLibraryRegExMatchWithSubDirectory(self):
76 lib_regex_dict = {
77 'libname/foo:bar.*': ['path/to/LICENSE'],
78 }
79 builder = LicenseBuilder([], [], {}, lib_regex_dict)
Byoungchan Lee5be2aa12021-05-29 13:50:31 +090080 self.assertEqual(
Mirko Bonadei8cc66952020-10-30 10:13:45 +010081 builder._ParseLibrary('//a/b/third_party/libname/foo:bar_java'),
82 'libname/foo:bar.*')
Artem Titarenkoa9719482018-12-11 17:00:04 +010083
Mirko Bonadei8cc66952020-10-30 10:13:45 +010084 def testParseLibraryRegExMatchWithStarInside(self):
85 lib_regex_dict = {
86 'libname/foo.*bar.*': ['path/to/LICENSE'],
87 }
88 builder = LicenseBuilder([], [], {}, lib_regex_dict)
Byoungchan Lee5be2aa12021-05-29 13:50:31 +090089 self.assertEqual(
Mirko Bonadei8cc66952020-10-30 10:13:45 +010090 builder._ParseLibrary(
91 '//a/b/third_party/libname/fooHAHA:bar_java'),
92 'libname/foo.*bar.*')
sakal67e414c2017-09-05 00:16:15 -070093
Byoungchan Lee5be2aa12021-05-29 13:50:31 +090094 @patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
Mirko Bonadei8cc66952020-10-30 10:13:45 +010095 def testGetThirdPartyLibrariesWithoutRegex(self):
96 builder = LicenseBuilder([], [], {}, {})
Byoungchan Lee5be2aa12021-05-29 13:50:31 +090097 self.assertEqual(
Mirko Bonadei8cc66952020-10-30 10:13:45 +010098 builder._GetThirdPartyLibraries('out/arm', 'target1'),
99 set(['libname1', 'libname2', 'libname3']))
sakal67e414c2017-09-05 00:16:15 -0700100
Byoungchan Lee5be2aa12021-05-29 13:50:31 +0900101 @patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100102 def testGetThirdPartyLibrariesWithRegex(self):
103 lib_regex_dict = {
104 'libname2:c.*': ['path/to/LICENSE'],
105 }
106 builder = LicenseBuilder([], [], {}, lib_regex_dict)
Byoungchan Lee5be2aa12021-05-29 13:50:31 +0900107 self.assertEqual(
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100108 builder._GetThirdPartyLibraries('out/arm', 'target1'),
109 set(['libname1', 'libname2:c.*', 'libname3']))
Artem Titarenkoa9719482018-12-11 17:00:04 +0100110
Byoungchan Lee5be2aa12021-05-29 13:50:31 +0900111 @patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100112 def testGenerateLicenseTextFailIfUnknownLibrary(self):
113 lib_dict = {
114 'simple_library': ['path/to/LICENSE'],
115 }
116 builder = LicenseBuilder(['dummy_dir'], ['dummy_target'], lib_dict, {})
Artem Titarenkoa9719482018-12-11 17:00:04 +0100117
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100118 with self.assertRaises(Exception) as context:
119 builder.GenerateLicenseText('dummy/dir')
Artem Titarenkoa9719482018-12-11 17:00:04 +0100120
Byoungchan Lee5be2aa12021-05-29 13:50:31 +0900121 self.assertEqual(
122 context.exception.args[0],
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100123 'Missing licenses for following third_party targets: '
124 'libname1, libname2, libname3')
Artem Titarenkoa9719482018-12-11 17:00:04 +0100125
sakal67e414c2017-09-05 00:16:15 -0700126
127if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100128 unittest.main()