Vaclav Brozek | cdc7defb | 2018-03-20 09:54:35 | [diff] [blame] | 1 | #!/usr/bin/env python |
Avi Drissman | ea1be23 | 2022-09-14 23:29:06 | [diff] [blame] | 2 | # Copyright 2017 The Chromium Authors |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 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 | |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 15 | class CheckTODOFormatTest(unittest.TestCase): |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 16 | """Test the _CheckBugInToDo presubmit check.""" |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 17 | |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 18 | def testTODOs(self): |
Mark Cogan | cc4c1ea | 2024-03-07 09:31:29 | [diff] [blame] | 19 | # All instances of the "TO DO" string in the following test cases are |
| 20 | # broken by line breaks, because this file is run through the PRESUBIT |
| 21 | # that it tests, so incorrectly formatted items in the test fixture |
| 22 | # will trigger errors. |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 23 | bad_lines = [ |
| 24 | 'TO' |
| 25 | 'DO(ldap): fix this', 'TO' |
| 26 | 'DO(ladp): see crbug.com/8675309', 'TO' |
| 27 | 'DO(8675309): fix this', 'TO' |
| 28 | 'DO(http://crbug.com/8675309): fix this', 'TO' |
| 29 | 'DO( crbug.com/8675309): fix this', 'TO' |
| 30 | 'DO(crbug/8675309): fix this', 'TO' |
Mark Cogan | cc4c1ea | 2024-03-07 09:31:29 | [diff] [blame] | 31 | 'DO(crbug.com): fix this', 'TO' |
| 32 | 'DO(inccrbug.com): fix this', |
| 33 | ] |
| 34 | deprecated_lines = [ |
| 35 | 'TO' |
| 36 | 'DO(b/12345): fix this' |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 37 | ] |
| 38 | good_lines = [ |
| 39 | 'TO' |
| 40 | 'DO(crbug.com/8675309): fix this', 'TO' |
Mark Cogan | cc4c1ea | 2024-03-07 09:31:29 | [diff] [blame] | 41 | 'DO(crbug.com/8675309): fix this (please)' |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 42 | ] |
| 43 | mock_input = PRESUBMIT_test_mocks.MockInputApi() |
Mark Cogan | cc4c1ea | 2024-03-07 09:31:29 | [diff] [blame] | 44 | lines = bad_lines + deprecated_lines + good_lines |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 45 | mock_input.files = [ |
Mark Cogan | cc4c1ea | 2024-03-07 09:31:29 | [diff] [blame] | 46 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', lines) |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 47 | ] |
| 48 | mock_output = PRESUBMIT_test_mocks.MockOutputApi() |
Mark Cogan | cc4c1ea | 2024-03-07 09:31:29 | [diff] [blame] | 49 | results = PRESUBMIT._CheckBugInToDo(mock_input, mock_output) |
| 50 | # Expect one error result and one warning result. |
| 51 | self.assertEqual(len(results), 2) |
| 52 | self.assertEqual('error', results[0].type) |
| 53 | self.assertEqual('warning', results[1].type) |
| 54 | self.assertTrue('without bug numbers' in results[0].message) |
| 55 | self.assertTrue('with a deprecated bug link' in results[1].message) |
| 56 | error_lines = results[0].message.split('\n') |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 57 | self.assertEqual(len(error_lines), len(bad_lines) + 2) |
Mark Cogan | cc4c1ea | 2024-03-07 09:31:29 | [diff] [blame] | 58 | warning_lines = results[1].message.split('\n') |
| 59 | self.assertEqual(len(warning_lines), len(deprecated_lines) + 2) |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 60 | |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 61 | class CheckHasNoIncludeDirectivesTest(unittest.TestCase): |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 62 | """Test the _CheckHasNoIncludeDirectives presubmit check.""" |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 63 | |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 64 | def testFindsIncludeDirectives(self): |
| 65 | good_lines = [ |
| 66 | '#import <system>', '#import "my/path/my/header.h"', |
| 67 | '#import "my/path/my/source.mm"', '#import "my/path/my/source.m"' |
| 68 | ] |
| 69 | bad_lines = [ |
| 70 | '#include <system>', '#import <system>', |
| 71 | '#include "my/path/my/header.h"', |
| 72 | '#include "my/path/my/source.mm"', '#import "my/path/my/header.h"' |
| 73 | '#include "my/path/my/source.m"' |
| 74 | ] |
| 75 | mock_input = PRESUBMIT_test_mocks.MockInputApi() |
| 76 | mock_input.files = [ |
| 77 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', |
| 78 | bad_lines), |
| 79 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller_2.mm', |
| 80 | good_lines), |
| 81 | PRESUBMIT_test_mocks.MockFile('ios/path/bar_controller.h', |
| 82 | bad_lines), |
| 83 | PRESUBMIT_test_mocks.MockFile('ios/path/bar_controller.m', |
| 84 | bad_lines), |
| 85 | PRESUBMIT_test_mocks.MockFile('ios/path/bar_controller.cc', |
| 86 | bad_lines), |
| 87 | PRESUBMIT_test_mocks.MockFile('chrome/path/foo_controller.mm', |
| 88 | bad_lines), |
| 89 | ] |
| 90 | mock_output = PRESUBMIT_test_mocks.MockOutputApi() |
| 91 | errors = PRESUBMIT._CheckHasNoIncludeDirectives( |
| 92 | mock_input, mock_output) |
| 93 | self.assertEqual(len(errors), 1) |
| 94 | self.assertEqual('error', errors[0].type) |
| 95 | self.assertTrue('ios/path/foo_controller.mm:1' in errors[0].message) |
| 96 | self.assertTrue('ios/path/foo_controller.mm:3' in errors[0].message) |
| 97 | self.assertTrue('ios/path/foo_controller.mm:4' in errors[0].message) |
| 98 | |
Petro Akzhygitov | db9b35162 | 2022-07-01 08:21:50 | [diff] [blame] | 99 | |
Gauthier Ambard | f85c5f1 | 2022-09-14 11:26:54 | [diff] [blame] | 100 | class CheckHasNoPipeInCommentTest(unittest.TestCase): |
| 101 | """Test the _CheckHasNoPipeInComment presubmit check.""" |
| 102 | |
| 103 | def testFindsIncludeDirectives(self): |
| 104 | good_lines = [ |
Peter Kasting | 06d4831 | 2023-12-20 22:26:14 | [diff] [blame] | 105 | '#if !defined(a) || !defined(b)', |
Gauthier Ambard | f85c5f1 | 2022-09-14 11:26:54 | [diff] [blame] | 106 | '// This does A || B', '// `MySymbol` is correct', |
| 107 | 'bitVariable1 | bitVariable2' |
| 108 | ] |
| 109 | bad_lines = [ |
| 110 | '// |MySymbol| is wrong', '// What is wrong is: |MySymbol|' |
| 111 | ] |
| 112 | mock_input = PRESUBMIT_test_mocks.MockInputApi() |
| 113 | mock_input.files = [ |
| 114 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', |
| 115 | good_lines + bad_lines), |
| 116 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.h', |
| 117 | bad_lines + good_lines), |
| 118 | ] |
| 119 | mock_output = PRESUBMIT_test_mocks.MockOutputApi() |
| 120 | errors = PRESUBMIT._CheckHasNoPipeInComment(mock_input, mock_output) |
| 121 | self.assertEqual(len(errors), 1) |
| 122 | self.assertEqual('warning', errors[0].type) |
| 123 | self.assertTrue('ios/path/foo_controller.mm:5' in errors[0].message) |
| 124 | self.assertTrue('ios/path/foo_controller.mm:6' in errors[0].message) |
| 125 | self.assertTrue('ios/path/foo_controller.h:1' in errors[0].message) |
| 126 | self.assertTrue('ios/path/foo_controller.h:2' in errors[0].message) |
| 127 | error_lines = errors[0].message.split('\n') |
| 128 | self.assertEqual(len(error_lines), len(bad_lines) * 2 + 3) |
| 129 | |
Federica Germinario | 6aeebd4 | 2024-09-17 11:59:15 | [diff] [blame] | 130 | class _CheckCanImproveTestUsingExpectNSEQ(unittest.TestCase): |
| 131 | """Test the _CheckCanImproveTestUsingExpectNSEQ presubmit. """ |
| 132 | |
| 133 | def testFindImprovableTestUsingExpectNSEQ(self): |
| 134 | good_lines = [ |
| 135 | 'EXPECT_TRUE(a == b);', |
| 136 | 'if (a isEqualToString:b) {', |
| 137 | 'if (a isEqualToData:b) {' |
| 138 | ] |
| 139 | bad_lines = [ |
| 140 | 'EXPECT_TRUE(a ', |
| 141 | ' isEqualToString:b);', |
| 142 | 'EXPECT_TRUE(@"example" isEqualToString:@"example");', |
| 143 | 'EXPECT_FALSE(@"example" isEqualToData:@"example");', |
| 144 | 'EXPECT_TRUE(@"example" isEqualToArray:@"example");' |
| 145 | ] |
| 146 | mock_input = PRESUBMIT_test_mocks.MockInputApi() |
| 147 | mock_input.files = [ |
| 148 | PRESUBMIT_test_mocks.MockFile('ios/path/foo_unittest.mm', |
| 149 | good_lines + bad_lines), |
| 150 | ] |
| 151 | mock_output = PRESUBMIT_test_mocks.MockOutputApi() |
| 152 | errors = PRESUBMIT._CheckCanImproveTestUsingExpectNSEQ( |
| 153 | mock_input, mock_output) |
| 154 | self.assertEqual(len(errors), 1) |
| 155 | self.assertEqual('warning', errors[0].type) |
| 156 | self.assertFalse('ios/path/foo_unittest.mm:1' in errors[0].message) |
| 157 | self.assertFalse('ios/path/foo_unittest.mm:2' in errors[0].message) |
| 158 | self.assertFalse('ios/path/foo_unittest.mm:3' in errors[0].message) |
| 159 | self.assertTrue('ios/path/foo_unittest.mm:4' in errors[0].message) |
| 160 | self.assertFalse('ios/path/foo_unittest.mm:5' in errors[0].message) |
| 161 | self.assertTrue('ios/path/foo_unittest.mm:6' in errors[0].message) |
| 162 | self.assertTrue('ios/path/foo_unittest.mm:7' in errors[0].message) |
| 163 | self.assertTrue('ios/path/foo_unittest.mm:8' in errors[0].message) |
| 164 | |
Federica Germinario | 2ce51a8 | 2025-01-27 14:44:10 | [diff] [blame^] | 165 | class _CheckNotUsingNSUserDefaults(unittest.TestCase): |
| 166 | """Test the _CheckNotUsingNSUserDefaults presubmit. """ |
| 167 | |
| 168 | def testFindImprovableTestUsingExpectNSEQ(self): |
| 169 | good_lines = [ |
| 170 | '[defaults dictionaryForKey:key_name];', |
| 171 | ] |
| 172 | bad_lines = [ |
| 173 | '[[NSUserDefaults standardUserDefaults]', |
| 174 | 'NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];', |
| 175 | '[[NSUserDefaults standardUserDefaults] setObject:object_name', |
| 176 | ] |
| 177 | |
| 178 | mock_input = PRESUBMIT_test_mocks.MockInputApi() |
| 179 | mock_input.files = [ |
| 180 | PRESUBMIT_test_mocks.MockFile('ios/path/defaults_unittest.mm', |
| 181 | good_lines + bad_lines), |
| 182 | ] |
| 183 | mock_output = PRESUBMIT_test_mocks.MockOutputApi() |
| 184 | errors = PRESUBMIT._CheckNotUsingNSUserDefaults( |
| 185 | mock_input, mock_output) |
| 186 | self.assertEqual(len(errors), 1) |
| 187 | self.assertEqual('warning', errors[0].type) |
| 188 | self.assertFalse('ios/path/defaults_unittest.mm:1' in errors[0].message) |
| 189 | self.assertTrue('ios/path/defaults_unittest.mm:2' in errors[0].message) |
| 190 | self.assertTrue('ios/path/defaults_unittest.mm:3' in errors[0].message) |
| 191 | self.assertTrue('ios/path/defaults_unittest.mm:4' in errors[0].message) |
Federica Germinario | 6aeebd4 | 2024-09-17 11:59:15 | [diff] [blame] | 192 | |
Sylvain Defresne | fcda19f | 2017-06-27 10:14:01 | [diff] [blame] | 193 | if __name__ == '__main__': |
Gauthier Ambard | 1981d62 | 2022-09-13 09:34:49 | [diff] [blame] | 194 | unittest.main() |