blob: 6dfd8f3e229c713f8aeeb3fa7be701d44496bc37 [file] [log] [blame]
Christoffer Jansson4e8a7732022-02-08 09:01:12 +01001#!/usr/bin/env vpython3
2
3# pylint: disable=protected-access,unused-argument
sakal67e414c2017-09-05 00:16:15 -07004
5# Copyright 2017 The WebRTC project authors. All Rights Reserved.
6#
7# Use of this source code is governed by a BSD-style license
8# that can be found in the LICENSE file in the root of the source
9# tree. An additional intellectual property rights grant can be found
10# in the file PATENTS. All contributing project authors may
11# be found in the AUTHORS file in the root of the source tree.
12
sakal67e414c2017-09-05 00:16:15 -070013import unittest
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010014from mock import patch
sakal67e414c2017-09-05 00:16:15 -070015
16from generate_licenses import LicenseBuilder
17
18
19class TestLicenseBuilder(unittest.TestCase):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010020 @staticmethod
21 def _FakeRunGN(buildfile_dir, target):
22 return """
sakal67e414c2017-09-05 00:16:15 -070023 {
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
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010035 def testParseLibraryName(self):
36 self.assertEqual(
37 LicenseBuilder._ParseLibraryName('//a/b/third_party/libname1:c'),
38 'libname1')
39 self.assertEqual(
40 LicenseBuilder._ParseLibraryName('//a/b/third_party/libname2:c(d)'),
41 'libname2')
42 self.assertEqual(
43 LicenseBuilder._ParseLibraryName('//a/b/third_party/libname3/c:d(e)'),
44 'libname3')
45 self.assertEqual(
46 LicenseBuilder._ParseLibraryName('//a/b/not_third_party/c'), None)
Artem Titarenkoa9719482018-12-11 17:00:04 +010047
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010048 def testParseLibrarySimpleMatch(self):
49 builder = LicenseBuilder([], [], {}, {})
50 self.assertEqual(builder._ParseLibrary('//a/b/third_party/libname:c'),
51 'libname')
Artem Titarenkoa9719482018-12-11 17:00:04 +010052
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010053 def testParseLibraryRegExNoMatchFallbacksToDefaultLibname(self):
54 lib_dict = {
55 'libname:foo.*': ['path/to/LICENSE'],
56 }
57 builder = LicenseBuilder([], [], lib_dict, {})
58 self.assertEqual(
59 builder._ParseLibrary('//a/b/third_party/libname:bar_java'), 'libname')
Artem Titarenkoa9719482018-12-11 17:00:04 +010060
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010061 def testParseLibraryRegExMatch(self):
62 lib_regex_dict = {
63 'libname:foo.*': ['path/to/LICENSE'],
64 }
65 builder = LicenseBuilder([], [], {}, lib_regex_dict)
66 self.assertEqual(
67 builder._ParseLibrary('//a/b/third_party/libname:foo_bar_java'),
68 'libname:foo.*')
Artem Titarenkoa9719482018-12-11 17:00:04 +010069
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010070 def testParseLibraryRegExMatchWithSubDirectory(self):
71 lib_regex_dict = {
72 'libname/foo:bar.*': ['path/to/LICENSE'],
73 }
74 builder = LicenseBuilder([], [], {}, lib_regex_dict)
75 self.assertEqual(
76 builder._ParseLibrary('//a/b/third_party/libname/foo:bar_java'),
77 'libname/foo:bar.*')
Artem Titarenkoa9719482018-12-11 17:00:04 +010078
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010079 def testParseLibraryRegExMatchWithStarInside(self):
80 lib_regex_dict = {
81 'libname/foo.*bar.*': ['path/to/LICENSE'],
82 }
83 builder = LicenseBuilder([], [], {}, lib_regex_dict)
84 self.assertEqual(
85 builder._ParseLibrary('//a/b/third_party/libname/fooHAHA:bar_java'),
86 'libname/foo.*bar.*')
sakal67e414c2017-09-05 00:16:15 -070087
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010088 @patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
89 def testGetThirdPartyLibrariesWithoutRegex(self):
90 builder = LicenseBuilder([], [], {}, {})
91 self.assertEqual(builder._GetThirdPartyLibraries('out/arm', 'target1'),
92 set(['libname1', 'libname2', 'libname3']))
sakal67e414c2017-09-05 00:16:15 -070093
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010094 @patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
95 def testGetThirdPartyLibrariesWithRegex(self):
96 lib_regex_dict = {
97 'libname2:c.*': ['path/to/LICENSE'],
98 }
99 builder = LicenseBuilder([], [], {}, lib_regex_dict)
100 self.assertEqual(builder._GetThirdPartyLibraries('out/arm', 'target1'),
101 set(['libname1', 'libname2:c.*', 'libname3']))
Artem Titarenkoa9719482018-12-11 17:00:04 +0100102
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100103 @patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
104 def testGenerateLicenseTextFailIfUnknownLibrary(self):
105 lib_dict = {
106 'simple_library': ['path/to/LICENSE'],
107 }
108 builder = LicenseBuilder(['dummy_dir'], ['dummy_target'], lib_dict, {})
Artem Titarenkoa9719482018-12-11 17:00:04 +0100109
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100110 with self.assertRaises(Exception) as context:
111 builder.GenerateLicenseText('dummy/dir')
Artem Titarenkoa9719482018-12-11 17:00:04 +0100112
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100113 self.assertEqual(
114 context.exception.args[0],
115 'Missing licenses for following third_party targets: '
116 'libname1, libname2, libname3')
Artem Titarenkoa9719482018-12-11 17:00:04 +0100117
sakal67e414c2017-09-05 00:16:15 -0700118
119if __name__ == '__main__':
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100120 unittest.main()