xref: /aosp_15_r20/external/webrtc/tools_webrtc/libs/generate_licenses_test.py (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1#!/usr/bin/env vpython3
2
3# pylint: disable=protected-access,unused-argument
4
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
13import unittest
14from mock import patch
15
16from generate_licenses import LicenseBuilder
17
18
19class TestLicenseBuilder(unittest.TestCase):
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.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)
47
48  def testParseLibrarySimpleMatch(self):
49    builder = LicenseBuilder([], [], {}, {})
50    self.assertEqual(builder._ParseLibrary('//a/b/third_party/libname:c'),
51                     'libname')
52
53  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')
60
61  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.*')
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.assertEqual(
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.assertEqual(
85        builder._ParseLibrary('//a/b/third_party/libname/fooHAHA:bar_java'),
86        'libname/foo.*bar.*')
87
88  @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']))
93
94  @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']))
102
103  @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, {})
109
110    with self.assertRaises(Exception) as context:
111      builder.GenerateLicenseText('dummy/dir')
112
113    self.assertEqual(
114        context.exception.args[0],
115        'Missing licenses for following third_party targets: '
116        'libname1, libname2, libname3')
117
118
119if __name__ == '__main__':
120  unittest.main()
121