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 11import ast 12import os 13import unittest 14 15#pylint: disable=relative-import 16from check_package_boundaries import CheckPackageBoundaries 17 18 19MSG_FORMAT = 'ERROR:check_package_boundaries.py: Unexpected %s.' 20TESTDATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 21 'testdata') 22 23 24def ReadPylFile(file_path): 25 with open(file_path) as f: 26 return ast.literal_eval(f.read()) 27 28 29class UnitTest(unittest.TestCase): 30 def _RunTest(self, test_dir, check_all_build_files=False): 31 build_files = [os.path.join(test_dir, 'BUILD.gn')] 32 if check_all_build_files: 33 build_files = None 34 35 messages = [] 36 for violation in CheckPackageBoundaries(test_dir, build_files): 37 build_file_path = os.path.relpath(violation.build_file_path, test_dir) 38 build_file_path = build_file_path.replace(os.path.sep, '/') 39 messages.append(violation._replace(build_file_path=build_file_path)) 40 41 expected_messages = ReadPylFile(os.path.join(test_dir, 'expected.pyl')) 42 self.assertListEqual(sorted(expected_messages), sorted(messages)) 43 44 def testNoErrors(self): 45 self._RunTest(os.path.join(TESTDATA_DIR, 'no_errors')) 46 47 def testMultipleErrorsSingleTarget(self): 48 self._RunTest(os.path.join(TESTDATA_DIR, 'multiple_errors_single_target')) 49 50 def testMultipleErrorsMultipleTargets(self): 51 self._RunTest(os.path.join(TESTDATA_DIR, 52 'multiple_errors_multiple_targets')) 53 54 def testCommonPrefix(self): 55 self._RunTest(os.path.join(TESTDATA_DIR, 'common_prefix')) 56 57 def testAllBuildFiles(self): 58 self._RunTest(os.path.join(TESTDATA_DIR, 'all_build_files'), True) 59 60 def testSanitizeFilename(self): 61 # The `dangerous_filename` test case contains a directory with '++' in its 62 # name. If it's not properly escaped, a regex error would be raised. 63 self._RunTest(os.path.join(TESTDATA_DIR, 'dangerous_filename'), True) 64 65 def testRelativeFilename(self): 66 test_dir = os.path.join(TESTDATA_DIR, 'all_build_files') 67 with self.assertRaises(AssertionError): 68 CheckPackageBoundaries(test_dir, ["BUILD.gn"]) 69 70 71if __name__ == '__main__': 72 unittest.main() 73