Vaclav Brozek | cdc7defb | 2018-03-20 09:54:35 | [diff] [blame] | 1 | #!/usr/bin/env python |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 2 | # 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 | |
| 6 | import os |
| 7 | import sys |
| 8 | import unittest |
| 9 | |
| 10 | import PRESUBMIT |
| 11 | |
| 12 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 13 | import PRESUBMIT_test_mocks |
| 14 | |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 15 | class 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 Ambard | 7fc07bf | 2018-01-02 09:48:09 | [diff] [blame] | 20 | lines = ["foobar"] + PRESUBMIT.ARC_COMPILE_GUARD |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 21 | mock_input = PRESUBMIT_test_mocks.MockInputApi() |
| 22 | mock_input.files = [ |
Gauthier Ambard | 7fc07bf | 2018-01-02 09:48:09 | [diff] [blame] | 23 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', lines), |
| 24 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.m', lines), |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 25 | ] |
| 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 Ambard | 7fc07bf | 2018-01-02 09:48:09 | [diff] [blame] | 32 | lines = ["foobar"] |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 33 | mock_input = PRESUBMIT_test_mocks.MockInputApi() |
| 34 | mock_input.files = [ |
Gauthier Ambard | 7fc07bf | 2018-01-02 09:48:09 | [diff] [blame] | 35 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', lines), |
| 36 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.m', lines), |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 37 | ] |
| 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 Ambard | 7fc07bf | 2018-01-02 09:48:09 | [diff] [blame] | 47 | lines = ["foobar"] |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 48 | mock_input = PRESUBMIT_test_mocks.MockInputApi() |
| 49 | mock_input.files = [ |
Gauthier Ambard | 7fc07bf | 2018-01-02 09:48:09 | [diff] [blame] | 50 | 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] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 53 | ] |
| 54 | mock_output = PRESUBMIT_test_mocks.MockOutputApi() |
| 55 | errors = PRESUBMIT._CheckARCCompilationGuard(mock_input, mock_output) |
| 56 | self.assertEqual(len(errors), 0) |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 57 | |
| 58 | class 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 | |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 83 | class CheckHasNoIncludeDirectivesTest(unittest.TestCase): |
| 84 | """Test the _CheckHasNoIncludeDirectives presubmit check.""" |
| 85 | |
| 86 | def testFindsIncludeDirectives(self): |
| 87 | good_lines = ['#import <system>', |
| 88 | '#import "my/path/my/header.h"', |
| 89 | '#import "my/path/my/source.mm"', |
| 90 | '#import "my/path/my/source.m"'] |
| 91 | bad_lines = ['#include <system>', |
| 92 | '#import <system>', |
| 93 | '#include "my/path/my/header.h"', |
| 94 | '#include "my/path/my/source.mm"', |
| 95 | '#import "my/path/my/header.h"' |
| 96 | '#include "my/path/my/source.m"'] |
| 97 | mock_input = PRESUBMIT_test_mocks.MockInputApi() |
| 98 | mock_input.files = [ |
| 99 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', bad_lines), |
| 100 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller_2.mm', good_lines), |
| 101 | PRESUBMIT_test_mocks.MockFile('ios/path/bar_controller.h', bad_lines), |
| 102 | PRESUBMIT_test_mocks.MockFile('ios/path/bar_controller.m', bad_lines), |
| 103 | PRESUBMIT_test_mocks.MockFile('ios/path/bar_controller.cc', bad_lines), |
| 104 | PRESUBMIT_test_mocks.MockFile('chrome/path/foo_controller.mm', bad_lines), |
| 105 | ] |
| 106 | mock_output = PRESUBMIT_test_mocks.MockOutputApi() |
| 107 | errors = PRESUBMIT._CheckHasNoIncludeDirectives(mock_input, mock_output) |
| 108 | self.assertEqual(len(errors), 1) |
| 109 | self.assertEqual('error', errors[0].type) |
| 110 | self.assertTrue('ios/path/foo_controller.mm:1' in errors[0].message) |
| 111 | self.assertTrue('ios/path/foo_controller.mm:3' in errors[0].message) |
| 112 | self.assertTrue('ios/path/foo_controller.mm:4' in errors[0].message) |
| 113 | |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 114 | if __name__ == '__main__': |
| 115 | unittest.main() |