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