blob: bc5acaf19e765b0b46bfec2d7d542aed7b067b99 [file] [log] [blame]
Vaclav Brozekcdc7defb2018-03-20 09:54:351#!/usr/bin/env python
Sylvain Defresnefcda19f2017-06-27 10:14:012# Copyright 2017 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import os
7import sys
8import unittest
9
10import PRESUBMIT
11
12sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
13import PRESUBMIT_test_mocks
14
[email protected]0066f732017-12-28 10:33:0815class CheckARCCompilationGuardTest(unittest.TestCase):
16 """Test the _CheckARCCompilationGuard presubmit check."""
17
18 def testGoodImplementationFiles(self):
19 """Test that .m and .mm files with a guard don't raise any errors."""
Gauthier Ambard7fc07bf2018-01-02 09:48:0920 lines = ["foobar"] + PRESUBMIT.ARC_COMPILE_GUARD
[email protected]0066f732017-12-28 10:33:0821 mock_input = PRESUBMIT_test_mocks.MockInputApi()
22 mock_input.files = [
Gauthier Ambard7fc07bf2018-01-02 09:48:0923 PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', lines),
24 PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.m', lines),
[email protected]0066f732017-12-28 10:33:0825 ]
26 mock_output = PRESUBMIT_test_mocks.MockOutputApi()
27 errors = PRESUBMIT._CheckARCCompilationGuard(mock_input, mock_output)
28 self.assertEqual(len(errors), 0)
29
30 def testBadImplementationFiles(self):
31 """Test that .m and .mm files without a guard raise an error."""
Gauthier Ambard7fc07bf2018-01-02 09:48:0932 lines = ["foobar"]
[email protected]0066f732017-12-28 10:33:0833 mock_input = PRESUBMIT_test_mocks.MockInputApi()
34 mock_input.files = [
Gauthier Ambard7fc07bf2018-01-02 09:48:0935 PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', lines),
36 PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.m', lines),
[email protected]0066f732017-12-28 10:33:0837 ]
38 mock_output = PRESUBMIT_test_mocks.MockOutputApi()
39 errors = PRESUBMIT._CheckARCCompilationGuard(mock_input, mock_output)
40 self.assertEqual(len(errors), 1)
41 self.assertEqual('error', errors[0].type)
42 self.assertTrue('ios/path/foo_controller.m' in errors[0].message)
43 self.assertTrue('ios/path/foo_controller.mm' in errors[0].message)
44
45 def testOtherFiles(self):
46 """Test that other files without a guard don't raise errors."""
Gauthier Ambard7fc07bf2018-01-02 09:48:0947 lines = ["foobar"]
[email protected]0066f732017-12-28 10:33:0848 mock_input = PRESUBMIT_test_mocks.MockInputApi()
49 mock_input.files = [
Gauthier Ambard7fc07bf2018-01-02 09:48:0950 PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.h', lines),
51 PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.cc', lines),
52 PRESUBMIT_test_mocks.MockFile('ios/path/BUILD.gn', lines),
[email protected]0066f732017-12-28 10:33:0853 ]
54 mock_output = PRESUBMIT_test_mocks.MockOutputApi()
55 errors = PRESUBMIT._CheckARCCompilationGuard(mock_input, mock_output)
56 self.assertEqual(len(errors), 0)
Sylvain Defresnefcda19f2017-06-27 10:14:0157
58class CheckTODOFormatTest(unittest.TestCase):
59 """Test the _CheckBugInToDo presubmit check."""
60
61 def testTODOs(self):
62 bad_lines = ['TO''DO(ldap): fix this',
63 'TO''DO(ladp): see crbug.com/8675309',
64 'TO''DO(8675309): fix this',
65 'TO''DO(http://crbug.com/8675309): fix this',
66 'TO''DO( crbug.com/8675309): fix this',
67 'TO''DO(crbug/8675309): fix this',
68 'TO''DO(crbug.com): fix this']
69 good_lines = ['TO''DO(crbug.com/8675309): fix this',
70 'TO''DO(crbug.com/8675309): fix this (please)']
71 mock_input = PRESUBMIT_test_mocks.MockInputApi()
72 mock_input.files = [PRESUBMIT_test_mocks.MockFile(
73 'ios/path/foo_controller.mm', bad_lines + good_lines)]
74 mock_output = PRESUBMIT_test_mocks.MockOutputApi()
75 errors = PRESUBMIT._CheckBugInToDo(mock_input, mock_output)
76 self.assertEqual(len(errors), 1)
77 self.assertEqual('error', errors[0].type)
78 self.assertTrue('without bug numbers' in errors[0].message)
79 error_lines = errors[0].message.split('\n')
80 self.assertEqual(len(error_lines), len(bad_lines) + 2)
81
82
83if __name__ == '__main__':
84 unittest.main()