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 |
Artem Titov | 5d7a4c6 | 2018-07-23 13:58:25 +0200 | [diff] [blame] | 13 | import mock |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 14 | |
| 15 | from generate_licenses import LicenseBuilder |
| 16 | |
| 17 | |
| 18 | class TestLicenseBuilder(unittest.TestCase): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 19 | @staticmethod |
| 20 | def _FakeRunGN(buildfile_dir, target): |
| 21 | return """ |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 22 | { |
| 23 | "target1": { |
| 24 | "deps": [ |
| 25 | "//a/b/third_party/libname1:c", |
| 26 | "//a/b/third_party/libname2:c(//d/e/f:g)", |
| 27 | "//a/b/third_party/libname3/c:d(//e/f/g:h)", |
| 28 | "//a/b/not_third_party/c" |
| 29 | ] |
| 30 | } |
| 31 | } |
| 32 | """ |
| 33 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 34 | def testParseLibraryName(self): |
| 35 | self.assertEquals( |
| 36 | LicenseBuilder._ParseLibraryName('//a/b/third_party/libname1:c'), |
| 37 | 'libname1') |
| 38 | self.assertEquals( |
| 39 | LicenseBuilder._ParseLibraryName( |
| 40 | '//a/b/third_party/libname2:c(d)'), 'libname2') |
| 41 | self.assertEquals( |
| 42 | LicenseBuilder._ParseLibraryName( |
| 43 | '//a/b/third_party/libname3/c:d(e)'), 'libname3') |
| 44 | self.assertEquals( |
| 45 | LicenseBuilder._ParseLibraryName('//a/b/not_third_party/c'), None) |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 46 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 47 | def testParseLibrarySimpleMatch(self): |
| 48 | builder = LicenseBuilder([], [], {}, {}) |
| 49 | self.assertEquals(builder._ParseLibrary('//a/b/third_party/libname:c'), |
| 50 | 'libname') |
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 testParseLibraryRegExNoMatchFallbacksToDefaultLibname(self): |
| 53 | lib_dict = { |
| 54 | 'libname:foo.*': ['path/to/LICENSE'], |
| 55 | } |
| 56 | builder = LicenseBuilder([], [], lib_dict, {}) |
| 57 | self.assertEquals( |
| 58 | builder._ParseLibrary('//a/b/third_party/libname:bar_java'), |
| 59 | 'libname') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 60 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +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.assertEquals( |
| 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 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +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.assertEquals( |
| 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 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +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.assertEquals( |
| 85 | builder._ParseLibrary( |
| 86 | '//a/b/third_party/libname/fooHAHA:bar_java'), |
| 87 | 'libname/foo.*bar.*') |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 88 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 89 | @mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN) |
| 90 | def testGetThirdPartyLibrariesWithoutRegex(self): |
| 91 | builder = LicenseBuilder([], [], {}, {}) |
| 92 | self.assertEquals( |
| 93 | builder._GetThirdPartyLibraries('out/arm', 'target1'), |
| 94 | set(['libname1', 'libname2', 'libname3'])) |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 95 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 96 | @mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN) |
| 97 | def testGetThirdPartyLibrariesWithRegex(self): |
| 98 | lib_regex_dict = { |
| 99 | 'libname2:c.*': ['path/to/LICENSE'], |
| 100 | } |
| 101 | builder = LicenseBuilder([], [], {}, lib_regex_dict) |
| 102 | self.assertEquals( |
| 103 | builder._GetThirdPartyLibraries('out/arm', 'target1'), |
| 104 | set(['libname1', 'libname2:c.*', 'libname3'])) |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 105 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 106 | @mock.patch('generate_licenses.LicenseBuilder._RunGN', _FakeRunGN) |
| 107 | def testGenerateLicenseTextFailIfUnknownLibrary(self): |
| 108 | lib_dict = { |
| 109 | 'simple_library': ['path/to/LICENSE'], |
| 110 | } |
| 111 | builder = LicenseBuilder(['dummy_dir'], ['dummy_target'], lib_dict, {}) |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 112 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 113 | with self.assertRaises(Exception) as context: |
| 114 | builder.GenerateLicenseText('dummy/dir') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 115 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 116 | self.assertEquals( |
| 117 | context.exception.message, |
| 118 | 'Missing licenses for following third_party targets: ' |
| 119 | 'libname1, libname2, libname3') |
Artem Titarenko | a971948 | 2018-12-11 17:00:04 +0100 | [diff] [blame] | 120 | |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 121 | |
| 122 | if __name__ == '__main__': |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 123 | unittest.main() |