ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license |
| 6 | # that can be found in the LICENSE file in the root of the source |
| 7 | # tree. An additional intellectual property rights grant can be found |
| 8 | # in the file PATENTS. All contributing project authors may |
| 9 | # be found in the AUTHORS file in the root of the source tree. |
| 10 | |
| 11 | import ast |
| 12 | import os |
| 13 | import unittest |
| 14 | |
| 15 | from check_package_boundaries import CheckPackageBoundaries |
| 16 | |
| 17 | |
| 18 | MSG_FORMAT = 'ERROR:check_package_boundaries.py: Unexpected %s.' |
| 19 | TESTDATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), |
| 20 | 'testdata') |
| 21 | |
| 22 | |
| 23 | def ReadPylFile(file_path): |
| 24 | with open(file_path) as f: |
| 25 | return ast.literal_eval(f.read()) |
| 26 | |
| 27 | |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 28 | class UnitTest(unittest.TestCase): |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 29 | def _RunTest(self, test_dir, check_all_build_files=False): |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 30 | build_files = [os.path.join(test_dir, 'BUILD.gn')] |
| 31 | if check_all_build_files: |
| 32 | build_files = None |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 33 | |
| 34 | messages = [] |
| 35 | for violation in CheckPackageBoundaries(test_dir, build_files): |
| 36 | build_file_path = os.path.relpath(violation.build_file_path, test_dir) |
| 37 | build_file_path = build_file_path.replace(os.path.sep, '/') |
| 38 | messages.append(violation._replace(build_file_path=build_file_path)) |
| 39 | |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 40 | expected_messages = ReadPylFile(os.path.join(test_dir, 'expected.pyl')) |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 41 | self.assertListEqual(sorted(expected_messages), sorted(messages)) |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 42 | |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 43 | def testNoErrors(self): |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 44 | self._RunTest(os.path.join(TESTDATA_DIR, 'no_errors')) |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 45 | |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 46 | def testMultipleErrorsSingleTarget(self): |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 47 | self._RunTest(os.path.join(TESTDATA_DIR, 'multiple_errors_single_target')) |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 48 | |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 49 | def testMultipleErrorsMultipleTargets(self): |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 50 | self._RunTest(os.path.join(TESTDATA_DIR, |
| 51 | 'multiple_errors_multiple_targets')) |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 52 | |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 53 | def testCommonPrefix(self): |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 54 | self._RunTest(os.path.join(TESTDATA_DIR, 'common_prefix')) |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 55 | |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 56 | def testAllBuildFiles(self): |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 57 | self._RunTest(os.path.join(TESTDATA_DIR, 'all_build_files'), True) |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 58 | |
Oleh Prypin | afe0165 | 2017-10-04 15:56:08 +0200 | [diff] [blame] | 59 | def testSanitizeFilename(self): |
| 60 | # The `dangerous_filename` test case contains a directory with '++' in its |
| 61 | # name. If it's not properly escaped, a regex error would be raised. |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 62 | self._RunTest(os.path.join(TESTDATA_DIR, 'dangerous_filename'), True) |
Oleh Prypin | afe0165 | 2017-10-04 15:56:08 +0200 | [diff] [blame] | 63 | |
| 64 | def testRelativeFilename(self): |
| 65 | test_dir = os.path.join(TESTDATA_DIR, 'all_build_files') |
Oleh Prypin | afe0165 | 2017-10-04 15:56:08 +0200 | [diff] [blame] | 66 | with self.assertRaises(AssertionError): |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 67 | CheckPackageBoundaries(test_dir, ["BUILD.gn"]) |
Oleh Prypin | afe0165 | 2017-10-04 15:56:08 +0200 | [diff] [blame] | 68 | |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 69 | |
| 70 | if __name__ == '__main__': |
| 71 | unittest.main() |