• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env vpython3
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
15import check_package_boundaries
16
17MSG_FORMAT = 'ERROR:check_package_boundaries.py: Unexpected %s.'
18TESTDATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),
19                            'testdata')
20
21
22def ReadPylFile(file_path):
23  with open(file_path) as f:
24    return ast.literal_eval(f.read())
25
26
27class UnitTest(unittest.TestCase):
28  def _RunTest(self, test_dir, check_all_build_files=False):
29    build_files = [os.path.join(test_dir, 'BUILD.gn')]
30    if check_all_build_files:
31      build_files = None
32
33    messages = []
34    for violation in check_package_boundaries.CheckPackageBoundaries(
35        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
40    expected_messages = ReadPylFile(os.path.join(test_dir, 'expected.pyl'))
41    self.assertListEqual(sorted(expected_messages), sorted(messages))
42
43  def testNoErrors(self):
44    self._RunTest(os.path.join(TESTDATA_DIR, 'no_errors'))
45
46  def testMultipleErrorsSingleTarget(self):
47    self._RunTest(os.path.join(TESTDATA_DIR, 'multiple_errors_single_target'))
48
49  def testMultipleErrorsMultipleTargets(self):
50    self._RunTest(os.path.join(TESTDATA_DIR,
51                               'multiple_errors_multiple_targets'))
52
53  def testCommonPrefix(self):
54    self._RunTest(os.path.join(TESTDATA_DIR, 'common_prefix'))
55
56  def testAllBuildFiles(self):
57    self._RunTest(os.path.join(TESTDATA_DIR, 'all_build_files'), True)
58
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.
62    self._RunTest(os.path.join(TESTDATA_DIR, 'dangerous_filename'), True)
63
64  def testRelativeFilename(self):
65    test_dir = os.path.join(TESTDATA_DIR, 'all_build_files')
66    with self.assertRaises(AssertionError):
67      check_package_boundaries.CheckPackageBoundaries(test_dir, ["BUILD.gn"])
68
69
70if __name__ == '__main__':
71  unittest.main()
72