Avi Drissman | 2497659 | 2022-09-12 15:24:31 | [diff] [blame] | 1 | # Copyright 2014 The Chromium Authors |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5 | from collectionsimport defaultdict |
Daniel Cheng | 13ca61a88 | 2017-08-25 15:11:25 | [diff] [blame] | 6 | import fnmatch |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 7 | import json |
| 8 | import os |
| 9 | import re |
| 10 | import subprocess |
| 11 | import sys |
| 12 | |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 13 | |
| 14 | _REPO_ROOT= os.path.abspath(os.path.dirname(__file__)) |
| 15 | |
Daniel Cheng | 264a447d | 2017-09-28 22:17:59 | [diff] [blame] | 16 | # TODO(dcheng): It's kind of horrible that this is copy and pasted from |
| 17 | # presubmit_canned_checks.py, but it's far easier than any of the alternatives. |
| 18 | def_ReportErrorFileAndLine(filename, line_num, dummy_line): |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 19 | """Default error formatter for _FindNewViolationsOfRule.""" |
| 20 | return'%s:%s'%(filename, line_num) |
Daniel Cheng | 264a447d | 2017-09-28 22:17:59 | [diff] [blame] | 21 | |
| 22 | |
| 23 | classMockCannedChecks(object): |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 24 | def_FindNewViolationsOfRule(self, callable_rule, input_api, |
| 25 | source_file_filter=None, |
| 26 | error_formatter=_ReportErrorFileAndLine): |
| 27 | """Find all newly introduced violations of a per-line rule (a callable). |
Daniel Cheng | 264a447d | 2017-09-28 22:17:59 | [diff] [blame] | 28 | |
| 29 | Arguments: |
| 30 | callable_rule: a callable taking a file extension and line of input and |
| 31 | returning True if the rule is satisfied and False if there was a |
| 32 | problem. |
| 33 | input_api: object to enumerate the affected files. |
| 34 | source_file_filter: a filter to be passed to the input api. |
| 35 | error_formatter: a callable taking (filename, line_number, line) and |
| 36 | returning a formatted error string. |
| 37 | |
| 38 | Returns: |
| 39 | A list of the newly-introduced violations reported by the rule. |
| 40 | """ |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 41 | errors=[] |
| 42 | for fin input_api.AffectedFiles(include_deletes=False, |
| 43 | file_filter=source_file_filter): |
| 44 | # For speed, we do two passes, checking first the full file. |
| 45 | # Shelling out to the SCM to determine the changed region can be |
| 46 | # quite expensive on Win32. Assuming that most files will be kept |
| 47 | # problem-free, we can skip the SCM operations most of the time. |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 48 | extension= str(f.UnixLocalPath()).rsplit('.',1)[-1] |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 49 | if all(callable_rule(extension, line)for linein f.NewContents()): |
| 50 | # No violation found in full text: can skip considering diff. |
| 51 | continue |
Daniel Cheng | 264a447d | 2017-09-28 22:17:59 | [diff] [blame] | 52 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 53 | for line_num, linein f.ChangedContents(): |
| 54 | ifnot callable_rule(extension, line): |
| 55 | errors.append( |
| 56 | error_formatter(f.LocalPath(), line_num, line)) |
Daniel Cheng | 264a447d | 2017-09-28 22:17:59 | [diff] [blame] | 57 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 58 | return errors |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 59 | |
Zhiling Huang | 45cabf3 | 2018-03-10 00:50:03 | [diff] [blame] | 60 | |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 61 | classMockInputApi(object): |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 62 | """Mock class for the InputApi class. |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 63 | |
| 64 | This class can be used for unittests for presubmit by initializing the files |
| 65 | attribute as the list of changed files. |
| 66 | """ |
| 67 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 68 | DEFAULT_FILES_TO_SKIP=() |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 69 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 70 | def __init__(self): |
| 71 | self.basename= os.path.basename |
| 72 | self.canned_checks=MockCannedChecks() |
| 73 | self.fnmatch= fnmatch |
| 74 | self.json= json |
| 75 | self.re= re |
Joanna Wang | 130e7bdd | 2024-12-10 17:39:03 | [diff] [blame] | 76 | |
| 77 | # We want os_path.exists() and os_path.isfile() to work for files |
| 78 | # that are both in the filesystem and mock files we have added |
| 79 | # via InitFiles(). |
| 80 | # By setting os_path to a copy of os.path rather than directly we |
| 81 | # can not only have os_path.exists() be a combined output for fake |
| 82 | # files and real files in the filesystem. |
| 83 | import importlib.util |
| 84 | SPEC_OS_PATH= importlib.util.find_spec('os.path') |
| 85 | os_path1= importlib.util.module_from_spec(SPEC_OS_PATH) |
| 86 | SPEC_OS_PATH.loader.exec_module(os_path1) |
| 87 | sys.modules['os_path1']= os_path1 |
| 88 | self.os_path= os_path1 |
| 89 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 90 | self.platform= sys.platform |
| 91 | self.python_executable= sys.executable |
| 92 | self.python3_executable= sys.executable |
| 93 | self.platform= sys.platform |
| 94 | self.subprocess= subprocess |
| 95 | self.sys= sys |
| 96 | self.files=[] |
| 97 | self.is_committing=False |
| 98 | self.change=MockChange([]) |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 99 | self.presubmit_local_path= os.path.dirname( |
| 100 | os.path.abspath(sys.argv[0])) |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 101 | self.is_windows= sys.platform=='win32' |
| 102 | self.no_diffs=False |
| 103 | # Although this makes assumptions about command line arguments used by |
| 104 | # test scripts that create mocks, it is a convenient way to set up the |
| 105 | # verbosity via the input api. |
| 106 | self.verbose='--verbose'in sys.argv |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 107 | |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 108 | defInitFiles(self, files): |
| 109 | # Actual presubmit calls normpath, but too many tests break to do this |
| 110 | # right in MockFile(). |
| 111 | for fin files: |
| 112 | f._local_path= os.path.normpath(f._local_path) |
| 113 | self.files= files |
| 114 | files_that_exist={ |
| 115 | p.AbsoluteLocalPath() |
| 116 | for pin filesif p.Action()!='D' |
| 117 | } |
| 118 | |
| 119 | def mock_exists(path): |
| 120 | ifnot os.path.isabs(path): |
| 121 | path= os.path.join(self.presubmit_local_path, path) |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 122 | path= os.path.normpath(path) |
Joanna Wang | 130e7bdd | 2024-12-10 17:39:03 | [diff] [blame] | 123 | return pathin files_that_existor any( |
| 124 | f.startswith(path) |
| 125 | for fin files_that_exist)or os.path.exists(path) |
| 126 | |
| 127 | def mock_isfile(path): |
| 128 | ifnot os.path.isabs(path): |
| 129 | path= os.path.join(self.presubmit_local_path, path) |
| 130 | path= os.path.normpath(path) |
| 131 | return pathin files_that_existor os.path.isfile(path) |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 132 | |
| 133 | def mock_glob(pattern,*args,**kwargs): |
Joanna Wang | 130e7bdd | 2024-12-10 17:39:03 | [diff] [blame] | 134 | return fnmatch.filter(files_that_exist, pattern) |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 135 | |
| 136 | # Do not stub these in the constructor to not break existing tests. |
| 137 | self.os_path.exists= mock_exists |
Joanna Wang | 130e7bdd | 2024-12-10 17:39:03 | [diff] [blame] | 138 | self.os_path.isfile= mock_isfile |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 139 | self.glob= mock_glob |
Zhiling Huang | 45cabf3 | 2018-03-10 00:50:03 | [diff] [blame] | 140 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 141 | defAffectedFiles(self, file_filter=None, include_deletes=True): |
| 142 | for filein self.files: |
| 143 | if file_filterandnot file_filter(file): |
| 144 | continue |
| 145 | ifnot include_deletesand file.Action()=='D': |
| 146 | continue |
| 147 | yield file |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 148 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 149 | defRightHandSideLines(self, source_file_filter=None): |
| 150 | affected_files= self.AffectedSourceFiles(source_file_filter) |
| 151 | for afin affected_files: |
| 152 | lines= af.ChangedContents() |
| 153 | for linein lines: |
| 154 | yield(af, line[0], line[1]) |
Lukasz Anforowicz | 7016d05e | 2021-11-30 03:56:27 | [diff] [blame] | 155 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 156 | defAffectedSourceFiles(self, file_filter=None): |
| 157 | return self.AffectedFiles(file_filter=file_filter, |
| 158 | include_deletes=False) |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 159 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 160 | defAffectedTestableFiles(self, file_filter=None): |
| 161 | return self.AffectedFiles(file_filter=file_filter, |
| 162 | include_deletes=False) |
Georg Neis | 9080e060 | 2024-08-23 01:50:29 | [diff] [blame] | 163 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 164 | defFilterSourceFile(self, file, files_to_check=(), files_to_skip=()): |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 165 | local_path= file.UnixLocalPath() |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 166 | found_in_files_to_check=not files_to_check |
| 167 | if files_to_check: |
| 168 | if type(files_to_check)is str: |
| 169 | raiseTypeError( |
| 170 | 'files_to_check should be an iterable of strings') |
| 171 | for patternin files_to_check: |
| 172 | compiled_pattern= re.compile(pattern) |
| 173 | if compiled_pattern.match(local_path): |
| 174 | found_in_files_to_check=True |
| 175 | break |
| 176 | if files_to_skip: |
| 177 | if type(files_to_skip)is str: |
| 178 | raiseTypeError( |
| 179 | 'files_to_skip should be an iterable of strings') |
| 180 | for patternin files_to_skip: |
| 181 | compiled_pattern= re.compile(pattern) |
| 182 | if compiled_pattern.match(local_path): |
| 183 | returnFalse |
| 184 | return found_in_files_to_check |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 185 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 186 | defLocalPaths(self): |
| 187 | return[file.LocalPath()for filein self.files] |
davileen | e042625 | 2015-03-02 21:10:41 | [diff] [blame] | 188 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 189 | defPresubmitLocalPath(self): |
| 190 | return self.presubmit_local_path |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 191 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 192 | defReadFile(self, filename, mode='r'): |
| 193 | if hasattr(filename,'AbsoluteLocalPath'): |
| 194 | filename= filename.AbsoluteLocalPath() |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 195 | norm_filename= os.path.normpath(filename) |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 196 | for file_in self.files: |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 197 | to_check=(file_.LocalPath(), file_.AbsoluteLocalPath()) |
| 198 | if filenamein to_checkor norm_filenamein to_check: |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 199 | return'\n'.join(file_.NewContents()) |
| 200 | # Otherwise, file is not in our mock API. |
| 201 | raiseIOError("No such file or directory: '%s'"% filename) |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 202 | |
| 203 | |
| 204 | classMockOutputApi(object): |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 205 | """Mock class for the OutputApi class. |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 206 | |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 207 | An instance of this class can be passed to presubmit unittests for outputting |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 208 | various types of results. |
| 209 | """ |
| 210 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 211 | classPresubmitResult(object): |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 212 | |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 213 | def __init__(self, message, items=None, long_text='', locations=[]): |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 214 | self.message= message |
| 215 | self.items= items |
| 216 | self.long_text= long_text |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 217 | self.locations= locations |
gayane | 940df07 | 2015-02-24 14:28:30 | [diff] [blame] | 218 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 219 | def __repr__(self): |
| 220 | return self.message |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 221 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 222 | classPresubmitError(PresubmitResult): |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 223 | |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 224 | def __init__(self,*args,**kwargs): |
| 225 | MockOutputApi.PresubmitResult.__init__(self,*args,**kwargs) |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 226 | self.type='error' |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 227 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 228 | classPresubmitPromptWarning(PresubmitResult): |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 229 | |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 230 | def __init__(self,*args,**kwargs): |
| 231 | MockOutputApi.PresubmitResult.__init__(self,*args,**kwargs) |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 232 | self.type='warning' |
Daniel Cheng | 7052cdf | 2017-11-21 19:23:29 | [diff] [blame] | 233 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 234 | classPresubmitNotifyResult(PresubmitResult): |
| 235 | |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 236 | def __init__(self,*args,**kwargs): |
| 237 | MockOutputApi.PresubmitResult.__init__(self,*args,**kwargs) |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 238 | self.type='notify' |
| 239 | |
| 240 | classPresubmitPromptOrNotify(PresubmitResult): |
| 241 | |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 242 | def __init__(self,*args,**kwargs): |
| 243 | MockOutputApi.PresubmitResult.__init__(self,*args,**kwargs) |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 244 | self.type='promptOrNotify' |
| 245 | |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 246 | classPresubmitResultLocation(object): |
| 247 | |
| 248 | def __init__(self, file_path, start_line, end_line): |
| 249 | self.file_path= file_path |
| 250 | self.start_line= start_line |
| 251 | self.end_line= end_line |
| 252 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 253 | def __init__(self): |
| 254 | self.more_cc=[] |
| 255 | |
| 256 | defAppendCC(self, more_cc): |
| 257 | self.more_cc.append(more_cc) |
Daniel Cheng | 7052cdf | 2017-11-21 19:23:29 | [diff] [blame] | 258 | |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 259 | |
| 260 | classMockFile(object): |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 261 | """Mock class for the File class. |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 262 | |
| 263 | This class can be used to form the mock list of changed files in |
| 264 | MockInputApi for presubmit unittests. |
| 265 | """ |
| 266 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 267 | def __init__(self, |
| 268 | local_path, |
| 269 | new_contents, |
| 270 | old_contents=None, |
| 271 | action='A', |
| 272 | scm_diff=None): |
| 273 | self._local_path= local_path |
| 274 | self._new_contents= new_contents |
| 275 | self._changed_contents=[(i+1, l) |
| 276 | for i, lin enumerate(new_contents)] |
| 277 | self._action= action |
| 278 | if scm_diff: |
| 279 | self._scm_diff= scm_diff |
| 280 | else: |
| 281 | self._scm_diff=("--- /dev/null\n+++ %s\n@@ -0,0 +1,%d @@\n"% |
| 282 | (local_path, len(new_contents))) |
| 283 | for lin new_contents: |
| 284 | self._scm_diff+="+%s\n"% l |
| 285 | self._old_contents= old_contentsor[] |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 286 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 287 | def __str__(self): |
| 288 | return self._local_path |
Luciano Pacheco | 23d752b0 | 2023-10-25 22:49:36 | [diff] [blame] | 289 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 290 | defAction(self): |
| 291 | return self._action |
dbeam | 37e8e740 | 2016-02-10 22:58:20 | [diff] [blame] | 292 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 293 | defChangedContents(self): |
| 294 | return self._changed_contents |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 295 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 296 | defNewContents(self): |
| 297 | return self._new_contents |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 298 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 299 | defLocalPath(self): |
| 300 | return self._local_path |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 301 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 302 | defAbsoluteLocalPath(self): |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 303 | return os.path.join(_REPO_ROOT, self._local_path) |
rdevlin.cronin | 9ab806c | 2016-02-26 23:17:13 | [diff] [blame] | 304 | |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 305 | # This method must be functionally identical to |
| 306 | # AffectedFile.UnixLocalPath(), but must normalize Windows-style |
| 307 | # paths even on non-Windows platforms because tests contain them |
| 308 | defUnixLocalPath(self): |
| 309 | return self._local_path.replace('\\','/') |
| 310 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 311 | defGenerateScmDiff(self): |
| 312 | return self._scm_diff |
jbriance | 9e12f16 | 2016-11-25 07:57:50 | [diff] [blame] | 313 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 314 | defOldContents(self): |
| 315 | return self._old_contents |
Yoland Yan | b92fa52 | 2017-08-28 17:37:06 | [diff] [blame] | 316 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 317 | def rfind(self, p): |
| 318 | """Required when os.path.basename() is called on MockFile.""" |
| 319 | return self._local_path.rfind(p) |
davileen | e042625 | 2015-03-02 21:10:41 | [diff] [blame] | 320 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 321 | def __getitem__(self, i): |
| 322 | """Required when os.path.basename() is called on MockFile.""" |
| 323 | return self._local_path[i] |
davileen | e042625 | 2015-03-02 21:10:41 | [diff] [blame] | 324 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 325 | def __len__(self): |
| 326 | """Required when os.path.basename() is called on MockFile.""" |
| 327 | return len(self._local_path) |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 328 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 329 | def replace(self, altsep, sep): |
| 330 | """Required when os.path.basename() is called on MockFile.""" |
| 331 | return self._local_path.replace(altsep, sep) |
Julian Pastarmov | 4f7af53 | 2019-07-17 19:25:37 | [diff] [blame] | 332 | |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 333 | |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 334 | classMockAffectedFile(MockFile): |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 335 | pass |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 336 | |
| 337 | |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 338 | classMockChange(object): |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 339 | """Mock class for Change class. |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 340 | |
| 341 | This class can be used in presubmit unittests to mock the query of the |
| 342 | current change. |
| 343 | """ |
| 344 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 345 | def __init__(self, changed_files): |
| 346 | self._changed_files= changed_files |
| 347 | self.author_email=None |
| 348 | self.footers= defaultdict(list) |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 349 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 350 | defLocalPaths(self): |
| 351 | return self._changed_files |
rdevlin.cronin | 11366825 | 2016-05-02 17:05:54 | [diff] [blame] | 352 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 353 | defAffectedFiles(self, |
| 354 | include_dirs=False, |
| 355 | include_deletes=True, |
| 356 | file_filter=None): |
| 357 | return self._changed_files |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 358 | |
Andrew Grieve | 66a2bc4 | 2024-10-04 21:20:20 | [diff] [blame] | 359 | defGitFootersFromDescription(self): |
| 360 | return self.footers |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 361 | |
| 362 | defRepositoryRoot(self): |
| 363 | return _REPO_ROOT |