blob: 03f14459c29f94294fcbfaf101f7834885ba4444 [file] [log] [blame]
sakal67e414c2017-09-05 07:16:151#!/usr/bin/env python
2# 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
12import os
13import sys
14
Artem Titarenkoa9719482018-12-11 16:00:0415SRC = os.path.abspath(
16 os.path.join(os.path.dirname((__file__)), os.pardir, os.pardir))
sakal67e414c2017-09-05 07:16:1517sys.path.append(os.path.join(SRC, 'third_party', 'pymock'))
18
sakal67e414c2017-09-05 07:16:1519import unittest
Artem Titov5d7a4c62018-07-23 11:58:2520import mock
sakal67e414c2017-09-05 07:16:1521
22from generate_licenses import LicenseBuilder
23
24
25class TestLicenseBuilder(unittest.TestCase):
Artem Titarenkoa9719482018-12-11 16:00:0426
sakal67e414c2017-09-05 07:16:1527 @staticmethod
28 def _FakeRunGN(buildfile_dir, target):
29 return """
30 {
31 "target1": {
32 "deps": [
33 "//a/b/third_party/libname1:c",
34 "//a/b/third_party/libname2:c(//d/e/f:g)",
35 "//a/b/third_party/libname3/c:d(//e/f/g:h)",
36 "//a/b/not_third_party/c"
37 ]
38 }
39 }
40 """
41
Artem Titarenkoa9719482018-12-11 16:00:0442 def testParseLibraryName(self):
43 self.assertEquals(
44 LicenseBuilder._ParseLibraryName('//a/b/third_party/libname1:c'),
sakal67e414c2017-09-05 07:16:1545 'libname1')
Artem Titarenkoa9719482018-12-11 16:00:0446 self.assertEquals(
47 LicenseBuilder._ParseLibraryName('//a/b/third_party/libname2:c(d)'),
sakal67e414c2017-09-05 07:16:1548 'libname2')
Artem Titarenkoa9719482018-12-11 16:00:0449 self.assertEquals(
50 LicenseBuilder._ParseLibraryName('//a/b/third_party/libname3/c:d(e)'),
sakal67e414c2017-09-05 07:16:1551 'libname3')
Artem Titarenkoa9719482018-12-11 16:00:0452 self.assertEquals(
53 LicenseBuilder._ParseLibraryName('//a/b/not_third_party/c'), None)
54
55 def testParseLibrarySimpleMatch(self):
56 builder = LicenseBuilder([], [], {}, {})
57 self.assertEquals(
58 builder._ParseLibrary('//a/b/third_party/libname:c'), 'libname')
59
60 def testParseLibraryRegExNoMatchFallbacksToDefaultLibname(self):
61 lib_dict = {
62 'libname:foo.*': ['path/to/LICENSE'],
63 }
64 builder = LicenseBuilder([], [], lib_dict, {})
65 self.assertEquals(
66 builder._ParseLibrary('//a/b/third_party/libname:bar_java'), 'libname')
67
68 def testParseLibraryRegExMatch(self):
69 lib_regex_dict = {
70 'libname:foo.*': ['path/to/LICENSE'],
71 }
72 builder = LicenseBuilder([], [], {}, lib_regex_dict)
73 self.assertEquals(
74 builder._ParseLibrary('//a/b/third_party/libname:foo_bar_java'),
75 'libname:foo.*')
76
77 def testParseLibraryRegExMatchWithSubDirectory(self):
78 lib_regex_dict = {
79 'libname/foo:bar.*': ['path/to/LICENSE'],
80 }
81 builder = LicenseBuilder([], [], {}, lib_regex_dict)
82 self.assertEquals(
83 builder._ParseLibrary('//a/b/third_party/libname/foo:bar_java'),
84 'libname/foo:bar.*')
85
86 def testParseLibraryRegExMatchWithStarInside(self):
87 lib_regex_dict = {
88 'libname/foo.*bar.*': ['path/to/LICENSE'],
89 }
90 builder = LicenseBuilder([], [], {}, lib_regex_dict)
91 self.assertEquals(
92 builder._ParseLibrary('//a/b/third_party/libname/fooHAHA:bar_java'),
93 'libname/foo.*bar.*')
sakal67e414c2017-09-05 07:16:1594
95 @mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
Artem Titarenkoa9719482018-12-11 16:00:0496 def testGetThirdPartyLibrariesWithoutRegex(self):
97 builder = LicenseBuilder([], [], {}, {})
98 self.assertEquals(
99 builder._GetThirdPartyLibraries('out/arm', 'target1'),
sakal67e414c2017-09-05 07:16:15100 set(['libname1', 'libname2', 'libname3']))
101
Artem Titarenkoa9719482018-12-11 16:00:04102 @mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
103 def testGetThirdPartyLibrariesWithRegex(self):
104 lib_regex_dict = {
105 'libname2:c.*': ['path/to/LICENSE'],
106 }
107 builder = LicenseBuilder([], [], {}, lib_regex_dict)
108 self.assertEquals(
109 builder._GetThirdPartyLibraries('out/arm', 'target1'),
110 set(['libname1', 'libname2:c.*', 'libname3']))
111
112 @mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN)
113 def testGenerateLicenseTextFailIfUnknownLibrary(self):
114 lib_dict = {
115 'simple_library': ['path/to/LICENSE'],
116 }
117 builder = LicenseBuilder(['dummy_dir'], ['dummy_target'], lib_dict, {})
118
119 with self.assertRaises(Exception) as context:
120 builder.GenerateLicenseText('dummy/dir')
121
122 self.assertEquals(
123 context.exception.message,
124 'Missing licenses for following third_party targets: '
125 'libname1, libname2, libname3')
126
sakal67e414c2017-09-05 07:16:15127
128if __name__ == '__main__':
129 unittest.main()