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): |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 16 | """Test the _CheckARCCompilationGuard presubmit check.""" |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 17 | |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 18 | def testGoodImplementationFiles(self): |
| 19 | """Test that .m and .mm files with a guard don't raise any errors.""" |
| 20 | lines = ["foobar"] + PRESUBMIT.ARC_COMPILE_GUARD |
| 21 | mock_input = PRESUBMIT_test_mocks.MockInputApi() |
| 22 | mock_input.files = [ |
| 23 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', lines), |
| 24 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.m', lines), |
| 25 | ] |
| 26 | mock_output = PRESUBMIT_test_mocks.MockOutputApi() |
| 27 | errors = PRESUBMIT._CheckARCCompilationGuard(mock_input, mock_output) |
| 28 | self.assertEqual(len(errors), 0) |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 29 | |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 30 | def testBadImplementationFiles(self): |
| 31 | """Test that .m and .mm files without a guard raise an error.""" |
| 32 | lines = ["foobar"] |
| 33 | mock_input = PRESUBMIT_test_mocks.MockInputApi() |
| 34 | mock_input.files = [ |
| 35 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', lines), |
| 36 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.m', lines), |
| 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) |
[email protected] | 0066f73 | 2017-12-28 10:33:08 | [diff] [blame] | 44 | |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 45 | def testOtherFiles(self): |
| 46 | """Test that other files without a guard don't raise errors.""" |
| 47 | lines = ["foobar"] |
| 48 | mock_input = PRESUBMIT_test_mocks.MockInputApi() |
| 49 | mock_input.files = [ |
| 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), |
| 53 | ] |
| 54 | mock_output = PRESUBMIT_test_mocks.MockOutputApi() |
| 55 | errors = PRESUBMIT._CheckARCCompilationGuard(mock_input, mock_output) |
| 56 | self.assertEqual(len(errors), 0) |
| 57 | |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 58 | |
| 59 | class CheckTODOFormatTest(unittest.TestCase): |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 60 | """Test the _CheckBugInToDo presubmit check.""" |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 61 | |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 62 | def testTODOs(self): |
| 63 | bad_lines = [ |
| 64 | 'TO' |
| 65 | 'DO(ldap): fix this', 'TO' |
| 66 | 'DO(ladp): see crbug.com/8675309', 'TO' |
| 67 | 'DO(8675309): fix this', 'TO' |
| 68 | 'DO(http://crbug.com/8675309): fix this', 'TO' |
| 69 | 'DO( crbug.com/8675309): fix this', 'TO' |
| 70 | 'DO(crbug/8675309): fix this', 'TO' |
| 71 | 'DO(crbug.com): fix this' |
| 72 | ] |
| 73 | good_lines = [ |
| 74 | 'TO' |
| 75 | 'DO(crbug.com/8675309): fix this', 'TO' |
| 76 | 'DO(crbug.com/8675309): fix this (please)' |
| 77 | ] |
| 78 | mock_input = PRESUBMIT_test_mocks.MockInputApi() |
| 79 | mock_input.files = [ |
| 80 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', |
| 81 | bad_lines + good_lines) |
| 82 | ] |
| 83 | mock_output = PRESUBMIT_test_mocks.MockOutputApi() |
| 84 | errors = PRESUBMIT._CheckBugInToDo(mock_input, mock_output) |
| 85 | self.assertEqual(len(errors), 1) |
| 86 | self.assertEqual('error', errors[0].type) |
| 87 | self.assertTrue('without bug numbers' in errors[0].message) |
| 88 | error_lines = errors[0].message.split('\n') |
| 89 | self.assertEqual(len(error_lines), len(bad_lines) + 2) |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 90 | |
| 91 | |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 92 | class CheckHasNoIncludeDirectivesTest(unittest.TestCase): |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 93 | """Test the _CheckHasNoIncludeDirectives presubmit check.""" |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 94 | |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 95 | def testFindsIncludeDirectives(self): |
| 96 | good_lines = [ |
| 97 | '#import <system>', '#import "my/path/my/header.h"', |
| 98 | '#import "my/path/my/source.mm"', '#import "my/path/my/source.m"' |
| 99 | ] |
| 100 | bad_lines = [ |
| 101 | '#include <system>', '#import <system>', |
| 102 | '#include "my/path/my/header.h"', |
| 103 | '#include "my/path/my/source.mm"', '#import "my/path/my/header.h"' |
| 104 | '#include "my/path/my/source.m"' |
| 105 | ] |
| 106 | mock_input = PRESUBMIT_test_mocks.MockInputApi() |
| 107 | mock_input.files = [ |
| 108 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', |
| 109 | bad_lines), |
| 110 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller_2.mm', |
| 111 | good_lines), |
| 112 | PRESUBMIT_test_mocks.MockFile('ios/path/bar_controller.h', |
| 113 | bad_lines), |
| 114 | PRESUBMIT_test_mocks.MockFile('ios/path/bar_controller.m', |
| 115 | bad_lines), |
| 116 | PRESUBMIT_test_mocks.MockFile('ios/path/bar_controller.cc', |
| 117 | bad_lines), |
| 118 | PRESUBMIT_test_mocks.MockFile('chrome/path/foo_controller.mm', |
| 119 | bad_lines), |
| 120 | ] |
| 121 | mock_output = PRESUBMIT_test_mocks.MockOutputApi() |
| 122 | errors = PRESUBMIT._CheckHasNoIncludeDirectives( |
| 123 | mock_input, mock_output) |
| 124 | self.assertEqual(len(errors), 1) |
| 125 | self.assertEqual('error', errors[0].type) |
| 126 | self.assertTrue('ios/path/foo_controller.mm:1' in errors[0].message) |
| 127 | self.assertTrue('ios/path/foo_controller.mm:3' in errors[0].message) |
| 128 | self.assertTrue('ios/path/foo_controller.mm:4' in errors[0].message) |
| 129 | |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 130 | |
Gauthier Ambard | f85c5f1 | 2022-09-14 11:26:54 | [diff] [blame^] | 131 | class CheckHasNoPipeInCommentTest(unittest.TestCase): |
| 132 | """Test the _CheckHasNoPipeInComment presubmit check.""" |
| 133 | |
| 134 | def testFindsIncludeDirectives(self): |
| 135 | good_lines = [ |
| 136 | '#if !defined(__has_feature) || !__has_feature(objc_arc)', |
| 137 | '// This does A || B', '// `MySymbol` is correct', |
| 138 | 'bitVariable1 | bitVariable2' |
| 139 | ] |
| 140 | bad_lines = [ |
| 141 | '// |MySymbol| is wrong', '// What is wrong is: |MySymbol|' |
| 142 | ] |
| 143 | mock_input = PRESUBMIT_test_mocks.MockInputApi() |
| 144 | mock_input.files = [ |
| 145 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', |
| 146 | good_lines + bad_lines), |
| 147 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.h', |
| 148 | bad_lines + good_lines), |
| 149 | ] |
| 150 | mock_output = PRESUBMIT_test_mocks.MockOutputApi() |
| 151 | errors = PRESUBMIT._CheckHasNoPipeInComment(mock_input, mock_output) |
| 152 | self.assertEqual(len(errors), 1) |
| 153 | self.assertEqual('warning', errors[0].type) |
| 154 | self.assertTrue('ios/path/foo_controller.mm:5' in errors[0].message) |
| 155 | self.assertTrue('ios/path/foo_controller.mm:6' in errors[0].message) |
| 156 | self.assertTrue('ios/path/foo_controller.h:1' in errors[0].message) |
| 157 | self.assertTrue('ios/path/foo_controller.h:2' in errors[0].message) |
| 158 | error_lines = errors[0].message.split('\n') |
| 159 | self.assertEqual(len(error_lines), len(bad_lines) * 2 + 3) |
| 160 | |
| 161 | |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 162 | if __name__ == '__main__': |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 163 | unittest.main() |