blob: c59c4850b3dbf3616e8f4a3cad0bebcb7f3e3fde [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
28class Logger(object):
29 def __init__(self, test_dir):
30 self.messages = []
31 self.test_dir = test_dir
32
kjellander94f4d9e2017-03-09 06:09:33 -080033 def log(self, build_file_path, line_number, target_name, source_file,
ehmaldonado4fb97462017-01-30 05:27:22 -080034 subpackage):
35 build_file_path = os.path.relpath(build_file_path, self.test_dir)
ehmaldonadof6ddbe72017-02-13 05:18:02 -080036 build_file_path = build_file_path.replace(os.path.sep, '/')
ehmaldonado4fb97462017-01-30 05:27:22 -080037 self.messages.append([build_file_path, line_number, target_name,
38 source_file, subpackage])
39
40
41class UnitTest(unittest.TestCase):
42 def RunTest(self, test_dir, check_all_build_files=False):
43 logger = Logger(test_dir)
44 build_files = [os.path.join(test_dir, 'BUILD.gn')]
45 if check_all_build_files:
46 build_files = None
47 CheckPackageBoundaries(test_dir, logger, build_files)
48 expected_messages = ReadPylFile(os.path.join(test_dir, 'expected.pyl'))
49 self.assertListEqual(sorted(expected_messages), sorted(logger.messages))
50
kjellander94f4d9e2017-03-09 06:09:33 -080051 def test_no_errors(self):
ehmaldonado4fb97462017-01-30 05:27:22 -080052 self.RunTest(os.path.join(TESTDATA_DIR, 'no_errors'))
53
kjellander94f4d9e2017-03-09 06:09:33 -080054 def test_multiple_errors_single_target(self):
ehmaldonado4fb97462017-01-30 05:27:22 -080055 self.RunTest(os.path.join(TESTDATA_DIR, 'multiple_errors_single_target'))
56
kjellander94f4d9e2017-03-09 06:09:33 -080057 def test_multiple_errors_multiple_targets(self):
ehmaldonado4fb97462017-01-30 05:27:22 -080058 self.RunTest(os.path.join(TESTDATA_DIR, 'multiple_errors_multiple_targets'))
59
kjellander94f4d9e2017-03-09 06:09:33 -080060 def test_common_prefix(self):
ehmaldonado4fb97462017-01-30 05:27:22 -080061 self.RunTest(os.path.join(TESTDATA_DIR, 'common_prefix'))
62
kjellander94f4d9e2017-03-09 06:09:33 -080063 def test_all_build_files(self):
ehmaldonado4fb97462017-01-30 05:27:22 -080064 self.RunTest(os.path.join(TESTDATA_DIR, 'all_build_files'), True)
65
66
67if __name__ == '__main__':
68 unittest.main()