sakal | 67e414c | 2017-09-05 07:16:15 | [diff] [blame] | 1 | #!/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 | |
| 12 | import os |
| 13 | import sys |
| 14 | |
Artem Titarenko | a971948 | 2018-12-11 16:00:04 | [diff] [blame] | 15 | SRC = os.path.abspath( |
| 16 | os.path.join(os.path.dirname((__file__)), os.pardir, os.pardir)) |
sakal | 67e414c | 2017-09-05 07:16:15 | [diff] [blame] | 17 | sys.path.append(os.path.join(SRC, 'third_party', 'pymock')) |
| 18 | |
sakal | 67e414c | 2017-09-05 07:16:15 | [diff] [blame] | 19 | import unittest |
Artem Titov | 5d7a4c6 | 2018-07-23 11:58:25 | [diff] [blame] | 20 | import mock |
sakal | 67e414c | 2017-09-05 07:16:15 | [diff] [blame] | 21 | |
| 22 | from generate_licenses import LicenseBuilder |
| 23 | |
| 24 | |
| 25 | class TestLicenseBuilder(unittest.TestCase): |
Artem Titarenko | a971948 | 2018-12-11 16:00:04 | [diff] [blame] | 26 | |
sakal | 67e414c | 2017-09-05 07:16:15 | [diff] [blame] | 27 | @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 Titarenko | a971948 | 2018-12-11 16:00:04 | [diff] [blame] | 42 | def testParseLibraryName(self): |
| 43 | self.assertEquals( |
| 44 | LicenseBuilder._ParseLibraryName('//a/b/third_party/libname1:c'), |
sakal | 67e414c | 2017-09-05 07:16:15 | [diff] [blame] | 45 | 'libname1') |
Artem Titarenko | a971948 | 2018-12-11 16:00:04 | [diff] [blame] | 46 | self.assertEquals( |
| 47 | LicenseBuilder._ParseLibraryName('//a/b/third_party/libname2:c(d)'), |
sakal | 67e414c | 2017-09-05 07:16:15 | [diff] [blame] | 48 | 'libname2') |
Artem Titarenko | a971948 | 2018-12-11 16:00:04 | [diff] [blame] | 49 | self.assertEquals( |
| 50 | LicenseBuilder._ParseLibraryName('//a/b/third_party/libname3/c:d(e)'), |
sakal | 67e414c | 2017-09-05 07:16:15 | [diff] [blame] | 51 | 'libname3') |
Artem Titarenko | a971948 | 2018-12-11 16:00:04 | [diff] [blame] | 52 | 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.*') |
sakal | 67e414c | 2017-09-05 07:16:15 | [diff] [blame] | 94 | |
| 95 | @mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN) |
Artem Titarenko | a971948 | 2018-12-11 16:00:04 | [diff] [blame] | 96 | def testGetThirdPartyLibrariesWithoutRegex(self): |
| 97 | builder = LicenseBuilder([], [], {}, {}) |
| 98 | self.assertEquals( |
| 99 | builder._GetThirdPartyLibraries('out/arm', 'target1'), |
sakal | 67e414c | 2017-09-05 07:16:15 | [diff] [blame] | 100 | set(['libname1', 'libname2', 'libname3'])) |
| 101 | |
Artem Titarenko | a971948 | 2018-12-11 16:00:04 | [diff] [blame] | 102 | @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 | |
sakal | 67e414c | 2017-09-05 07:16:15 | [diff] [blame] | 127 | |
| 128 | if __name__ == '__main__': |
| 129 | unittest.main() |