blob: 7a5874cf2670a854bd976c597ca8fe891865ecc2 [file] [log] [blame]
ehmaldonado4fb97462017-01-30 05:27:22 -08001#!/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
15from check_package_boundaries import CheckPackageBoundaries
16
17
18MSG_FORMAT = 'ERROR:check_package_boundaries.py: Unexpected %s.'
19TESTDATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),
20 'testdata')
21
22
23def ReadPylFile(file_path):
24 with open(file_path) as f:
25 return ast.literal_eval(f.read())
26
27
ehmaldonado4fb97462017-01-30 05:27:22 -080028class UnitTest(unittest.TestCase):
Oleh Prypin2f33a562017-10-04 20:17:54 +020029 def _RunTest(self, test_dir, check_all_build_files=False):
ehmaldonado4fb97462017-01-30 05:27:22 -080030 build_files = [os.path.join(test_dir, 'BUILD.gn')]
31 if check_all_build_files:
32 build_files = None
Oleh Prypin2f33a562017-10-04 20:17:54 +020033
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
ehmaldonado4fb97462017-01-30 05:27:22 -080040 expected_messages = ReadPylFile(os.path.join(test_dir, 'expected.pyl'))
Oleh Prypin2f33a562017-10-04 20:17:54 +020041 self.assertListEqual(sorted(expected_messages), sorted(messages))
ehmaldonado4fb97462017-01-30 05:27:22 -080042
kjellanderc88b5d52017-04-05 06:42:43 -070043 def testNoErrors(self):
Oleh Prypin2f33a562017-10-04 20:17:54 +020044 self._RunTest(os.path.join(TESTDATA_DIR, 'no_errors'))
ehmaldonado4fb97462017-01-30 05:27:22 -080045
kjellanderc88b5d52017-04-05 06:42:43 -070046 def testMultipleErrorsSingleTarget(self):
Oleh Prypin2f33a562017-10-04 20:17:54 +020047 self._RunTest(os.path.join(TESTDATA_DIR, 'multiple_errors_single_target'))
ehmaldonado4fb97462017-01-30 05:27:22 -080048
kjellanderc88b5d52017-04-05 06:42:43 -070049 def testMultipleErrorsMultipleTargets(self):
Oleh Prypin2f33a562017-10-04 20:17:54 +020050 self._RunTest(os.path.join(TESTDATA_DIR,
51 'multiple_errors_multiple_targets'))
ehmaldonado4fb97462017-01-30 05:27:22 -080052
kjellanderc88b5d52017-04-05 06:42:43 -070053 def testCommonPrefix(self):
Oleh Prypin2f33a562017-10-04 20:17:54 +020054 self._RunTest(os.path.join(TESTDATA_DIR, 'common_prefix'))
ehmaldonado4fb97462017-01-30 05:27:22 -080055
kjellanderc88b5d52017-04-05 06:42:43 -070056 def testAllBuildFiles(self):
Oleh Prypin2f33a562017-10-04 20:17:54 +020057 self._RunTest(os.path.join(TESTDATA_DIR, 'all_build_files'), True)
ehmaldonado4fb97462017-01-30 05:27:22 -080058
Oleh Prypinafe01652017-10-04 15:56:08 +020059 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 Prypin2f33a562017-10-04 20:17:54 +020062 self._RunTest(os.path.join(TESTDATA_DIR, 'dangerous_filename'), True)
Oleh Prypinafe01652017-10-04 15:56:08 +020063
64 def testRelativeFilename(self):
65 test_dir = os.path.join(TESTDATA_DIR, 'all_build_files')
Oleh Prypinafe01652017-10-04 15:56:08 +020066 with self.assertRaises(AssertionError):
Oleh Prypin2f33a562017-10-04 20:17:54 +020067 CheckPackageBoundaries(test_dir, ["BUILD.gn"])
Oleh Prypinafe01652017-10-04 15:56:08 +020068
ehmaldonado4fb97462017-01-30 05:27:22 -080069
70if __name__ == '__main__':
71 unittest.main()