Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
| 2 | |
| 3 | # pylint: disable=protected-access,unused-argument |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 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 | |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 13 | import unittest |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 14 | from mock import patch |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 15 | |
| 16 | from generate_licenses import LicenseBuilder |
| 17 | |
| 18 | |
| 19 | class TestLicenseBuilder(unittest.TestCase): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 20 | @staticmethod |
| 21 | def _FakeRunGN(buildfile_dir, target): |
| 22 | return """ |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 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 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 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) |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 47 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 48 | def testParseLibrarySimpleMatch(self): |
| 49 | builder = LicenseBuilder([], [], {}, {}) |
| 50 | self.assertEqual(builder._ParseLibrary('//a/b/third_party/libname:c'), |
| 51 | 'libname') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 52 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 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') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 60 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 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.*') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 69 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 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.*') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 78 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 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.*') |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 87 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 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'])) |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 93 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 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'])) |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 102 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 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, {}) |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 109 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 110 | with self.assertRaises(Exception) as context: |
| 111 | builder.GenerateLicenseText('dummy/dir') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 112 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 113 | self.assertEqual( |
| 114 | context.exception.args[0], |
| 115 | 'Missing licenses for following third_party targets: ' |
| 116 | 'libname1, libname2, libname3') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 117 | |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 118 | |
| 119 | if __name__ == '__main__': |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 120 | unittest.main() |