Brian Sheedy | 8e144aa | 2020-06-23 16:02:16 -0700 | [diff] [blame] | 1 | #!/usr/bin/env vpython |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 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 | |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 12 | import unittest |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 13 | try: |
| 14 | # python 3.3+ |
| 15 | from unittest.mock import patch |
| 16 | except ImportError: |
| 17 | # From site-package |
| 18 | from mock import patch |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 19 | |
| 20 | from generate_licenses import LicenseBuilder |
| 21 | |
| 22 | |
| 23 | class TestLicenseBuilder(unittest.TestCase): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 24 | @staticmethod |
| 25 | def _FakeRunGN(buildfile_dir, target): |
| 26 | return """ |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 27 | { |
| 28 | "target1": { |
| 29 | "deps": [ |
| 30 | "//a/b/third_party/libname1:c", |
| 31 | "//a/b/third_party/libname2:c(//d/e/f:g)", |
| 32 | "//a/b/third_party/libname3/c:d(//e/f/g:h)", |
| 33 | "//a/b/not_third_party/c" |
| 34 | ] |
| 35 | } |
| 36 | } |
| 37 | """ |
| 38 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 39 | def testParseLibraryName(self): |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 40 | self.assertEqual( |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 41 | LicenseBuilder._ParseLibraryName('//a/b/third_party/libname1:c'), |
| 42 | 'libname1') |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 43 | self.assertEqual( |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 44 | LicenseBuilder._ParseLibraryName( |
| 45 | '//a/b/third_party/libname2:c(d)'), 'libname2') |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 46 | self.assertEqual( |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 47 | LicenseBuilder._ParseLibraryName( |
| 48 | '//a/b/third_party/libname3/c:d(e)'), 'libname3') |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 49 | self.assertEqual( |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 50 | LicenseBuilder._ParseLibraryName('//a/b/not_third_party/c'), None) |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 51 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 52 | def testParseLibrarySimpleMatch(self): |
| 53 | builder = LicenseBuilder([], [], {}, {}) |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 54 | self.assertEqual(builder._ParseLibrary('//a/b/third_party/libname:c'), |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 55 | 'libname') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 56 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 57 | def testParseLibraryRegExNoMatchFallbacksToDefaultLibname(self): |
| 58 | lib_dict = { |
| 59 | 'libname:foo.*': ['path/to/LICENSE'], |
| 60 | } |
| 61 | builder = LicenseBuilder([], [], lib_dict, {}) |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 62 | self.assertEqual( |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 63 | builder._ParseLibrary('//a/b/third_party/libname:bar_java'), |
| 64 | 'libname') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 65 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 66 | def testParseLibraryRegExMatch(self): |
| 67 | lib_regex_dict = { |
| 68 | 'libname:foo.*': ['path/to/LICENSE'], |
| 69 | } |
| 70 | builder = LicenseBuilder([], [], {}, lib_regex_dict) |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 71 | self.assertEqual( |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 72 | builder._ParseLibrary('//a/b/third_party/libname:foo_bar_java'), |
| 73 | 'libname:foo.*') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 74 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 75 | def testParseLibraryRegExMatchWithSubDirectory(self): |
| 76 | lib_regex_dict = { |
| 77 | 'libname/foo:bar.*': ['path/to/LICENSE'], |
| 78 | } |
| 79 | builder = LicenseBuilder([], [], {}, lib_regex_dict) |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 80 | self.assertEqual( |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 81 | builder._ParseLibrary('//a/b/third_party/libname/foo:bar_java'), |
| 82 | 'libname/foo:bar.*') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 83 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 84 | def testParseLibraryRegExMatchWithStarInside(self): |
| 85 | lib_regex_dict = { |
| 86 | 'libname/foo.*bar.*': ['path/to/LICENSE'], |
| 87 | } |
| 88 | builder = LicenseBuilder([], [], {}, lib_regex_dict) |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 89 | self.assertEqual( |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 90 | builder._ParseLibrary( |
| 91 | '//a/b/third_party/libname/fooHAHA:bar_java'), |
| 92 | 'libname/foo.*bar.*') |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 93 | |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 94 | @patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN) |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 95 | def testGetThirdPartyLibrariesWithoutRegex(self): |
| 96 | builder = LicenseBuilder([], [], {}, {}) |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 97 | self.assertEqual( |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 98 | builder._GetThirdPartyLibraries('out/arm', 'target1'), |
| 99 | set(['libname1', 'libname2', 'libname3'])) |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 100 | |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 101 | @patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN) |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 102 | def testGetThirdPartyLibrariesWithRegex(self): |
| 103 | lib_regex_dict = { |
| 104 | 'libname2:c.*': ['path/to/LICENSE'], |
| 105 | } |
| 106 | builder = LicenseBuilder([], [], {}, lib_regex_dict) |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 107 | self.assertEqual( |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 108 | builder._GetThirdPartyLibraries('out/arm', 'target1'), |
| 109 | set(['libname1', 'libname2:c.*', 'libname3'])) |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 110 | |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 111 | @patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN) |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 112 | def testGenerateLicenseTextFailIfUnknownLibrary(self): |
| 113 | lib_dict = { |
| 114 | 'simple_library': ['path/to/LICENSE'], |
| 115 | } |
| 116 | builder = LicenseBuilder(['dummy_dir'], ['dummy_target'], lib_dict, {}) |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 117 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 118 | with self.assertRaises(Exception) as context: |
| 119 | builder.GenerateLicenseText('dummy/dir') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 120 | |
Byoungchan Lee | 5be2aa1 | 2021-05-29 13:50:31 +0900 | [diff] [blame] | 121 | self.assertEqual( |
| 122 | context.exception.args[0], |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 123 | 'Missing licenses for following third_party targets: ' |
| 124 | 'libname1, libname2, libname3') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 125 | |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 126 | |
| 127 | if __name__ == '__main__': |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 128 | unittest.main() |