Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /PRESUBMIT_test_mocks.py
blob: baca10648edcb06f9b7458929255db1244c7227e [file] [log] [blame]
Avi Drissman24976592022-09-12 15:24:31[diff] [blame]1# Copyright 2014 The Chromium Authors
gayane3dff8c22014-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 Hall59f8d0c72020-05-01 07:31:19[diff] [blame]5from collectionsimport defaultdict
Daniel Cheng13ca61a882017-08-25 15:11:25[diff] [blame]6import fnmatch
gayane3dff8c22014-12-04 17:09:51[diff] [blame]7import json
8import os
9import re
10import subprocess
11import sys
12
Andrew Grieve713b89b2024-10-15 20:20:08[diff] [blame]13
14_REPO_ROOT= os.path.abspath(os.path.dirname(__file__))
15
Daniel Cheng264a447d2017-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.
18def_ReportErrorFileAndLine(filename, line_num, dummy_line):
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]19"""Default error formatter for _FindNewViolationsOfRule."""
20return'%s:%s'%(filename, line_num)
Daniel Cheng264a447d2017-09-28 22:17:59[diff] [blame]21
22
23classMockCannedChecks(object):
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]24def_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 Cheng264a447d2017-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 Grieve66a2bc42024-10-04 21:20:20[diff] [blame]41 errors=[]
42for 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 Bershanskyi4253349482025-02-11 21:01:27[diff] [blame]48 extension= str(f.UnixLocalPath()).rsplit('.',1)[-1]
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]49if all(callable_rule(extension, line)for linein f.NewContents()):
50# No violation found in full text: can skip considering diff.
51continue
Daniel Cheng264a447d2017-09-28 22:17:59[diff] [blame]52
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]53for line_num, linein f.ChangedContents():
54ifnot callable_rule(extension, line):
55 errors.append(
56 error_formatter(f.LocalPath(), line_num, line))
Daniel Cheng264a447d2017-09-28 22:17:59[diff] [blame]57
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]58return errors
gayane3dff8c22014-12-04 17:09:51[diff] [blame]59
Zhiling Huang45cabf32018-03-10 00:50:03[diff] [blame]60
gayane3dff8c22014-12-04 17:09:51[diff] [blame]61classMockInputApi(object):
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]62"""Mock class for the InputApi class.
gayane3dff8c22014-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 Grieve66a2bc42024-10-04 21:20:20[diff] [blame]68 DEFAULT_FILES_TO_SKIP=()
Sylvain Defresnea8b73d252018-02-28 15:45:54[diff] [blame]69
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]70def __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 Wang130e7bdd2024-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.
83import 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 Grieve66a2bc42024-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 Grieve713b89b2024-10-15 20:20:08[diff] [blame]99 self.presubmit_local_path= os.path.dirname(
100 os.path.abspath(sys.argv[0]))
Andrew Grieve66a2bc42024-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
gayane3dff8c22014-12-04 17:09:51[diff] [blame]107
Andrew Grieve713b89b2024-10-15 20:20:08[diff] [blame]108defInitFiles(self, files):
109# Actual presubmit calls normpath, but too many tests break to do this
110# right in MockFile().
111for fin files:
112 f._local_path= os.path.normpath(f._local_path)
113 self.files= files
114 files_that_exist={
115 p.AbsoluteLocalPath()
116for pin filesif p.Action()!='D'
117}
118
119def mock_exists(path):
120ifnot os.path.isabs(path):
121 path= os.path.join(self.presubmit_local_path, path)
Andrew Grieveb77ac762024-11-29 15:01:48[diff] [blame]122 path= os.path.normpath(path)
Joanna Wang130e7bdd2024-12-10 17:39:03[diff] [blame]123return pathin files_that_existor any(
124 f.startswith(path)
125for fin files_that_exist)or os.path.exists(path)
126
127def mock_isfile(path):
128ifnot os.path.isabs(path):
129 path= os.path.join(self.presubmit_local_path, path)
130 path= os.path.normpath(path)
131return pathin files_that_existor os.path.isfile(path)
Andrew Grieve713b89b2024-10-15 20:20:08[diff] [blame]132
133def mock_glob(pattern,*args,**kwargs):
Joanna Wang130e7bdd2024-12-10 17:39:03[diff] [blame]134return fnmatch.filter(files_that_exist, pattern)
Andrew Grieve713b89b2024-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 Wang130e7bdd2024-12-10 17:39:03[diff] [blame]138 self.os_path.isfile= mock_isfile
Andrew Grieve713b89b2024-10-15 20:20:08[diff] [blame]139 self.glob= mock_glob
Zhiling Huang45cabf32018-03-10 00:50:03[diff] [blame]140
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]141defAffectedFiles(self, file_filter=None, include_deletes=True):
142for filein self.files:
143if file_filterandnot file_filter(file):
144continue
145ifnot include_deletesand file.Action()=='D':
146continue
147yield file
gayane3dff8c22014-12-04 17:09:51[diff] [blame]148
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]149defRightHandSideLines(self, source_file_filter=None):
150 affected_files= self.AffectedSourceFiles(source_file_filter)
151for afin affected_files:
152 lines= af.ChangedContents()
153for linein lines:
154yield(af, line[0], line[1])
Lukasz Anforowicz7016d05e2021-11-30 03:56:27[diff] [blame]155
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]156defAffectedSourceFiles(self, file_filter=None):
157return self.AffectedFiles(file_filter=file_filter,
158 include_deletes=False)
Sylvain Defresnea8b73d252018-02-28 15:45:54[diff] [blame]159
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]160defAffectedTestableFiles(self, file_filter=None):
161return self.AffectedFiles(file_filter=file_filter,
162 include_deletes=False)
Georg Neis9080e0602024-08-23 01:50:29[diff] [blame]163
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]164defFilterSourceFile(self, file, files_to_check=(), files_to_skip=()):
Anton Bershanskyi4253349482025-02-11 21:01:27[diff] [blame]165 local_path= file.UnixLocalPath()
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]166 found_in_files_to_check=not files_to_check
167if files_to_check:
168if type(files_to_check)is str:
169raiseTypeError(
170'files_to_check should be an iterable of strings')
171for patternin files_to_check:
172 compiled_pattern= re.compile(pattern)
173if compiled_pattern.match(local_path):
174 found_in_files_to_check=True
175break
176if files_to_skip:
177if type(files_to_skip)is str:
178raiseTypeError(
179'files_to_skip should be an iterable of strings')
180for patternin files_to_skip:
181 compiled_pattern= re.compile(pattern)
182if compiled_pattern.match(local_path):
183returnFalse
184return found_in_files_to_check
glidere61efad2015-02-18 17:39:43[diff] [blame]185
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]186defLocalPaths(self):
187return[file.LocalPath()for filein self.files]
davileene0426252015-03-02 21:10:41[diff] [blame]188
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]189defPresubmitLocalPath(self):
190return self.presubmit_local_path
gayane3dff8c22014-12-04 17:09:51[diff] [blame]191
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]192defReadFile(self, filename, mode='r'):
193if hasattr(filename,'AbsoluteLocalPath'):
194 filename= filename.AbsoluteLocalPath()
Andrew Grieveb77ac762024-11-29 15:01:48[diff] [blame]195 norm_filename= os.path.normpath(filename)
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]196for file_in self.files:
Andrew Grieveb77ac762024-11-29 15:01:48[diff] [blame]197 to_check=(file_.LocalPath(), file_.AbsoluteLocalPath())
198if filenamein to_checkor norm_filenamein to_check:
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]199return'\n'.join(file_.NewContents())
200# Otherwise, file is not in our mock API.
201raiseIOError("No such file or directory: '%s'"% filename)
gayane3dff8c22014-12-04 17:09:51[diff] [blame]202
203
204classMockOutputApi(object):
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]205"""Mock class for the OutputApi class.
gayane3dff8c22014-12-04 17:09:51[diff] [blame]206
Gao Shenga79ebd42022-08-08 17:25:59[diff] [blame]207 An instance of this class can be passed to presubmit unittests for outputting
gayane3dff8c22014-12-04 17:09:51[diff] [blame]208 various types of results.
209 """
210
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]211classPresubmitResult(object):
gayane3dff8c22014-12-04 17:09:51[diff] [blame]212
Ben Pastenee79d66112025-04-23 19:46:15[diff] [blame]213def __init__(self, message, items=None, long_text='', locations=[]):
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]214 self.message= message
215 self.items= items
216 self.long_text= long_text
Ben Pastenee79d66112025-04-23 19:46:15[diff] [blame]217 self.locations= locations
gayane940df072015-02-24 14:28:30[diff] [blame]218
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]219def __repr__(self):
220return self.message
gayane3dff8c22014-12-04 17:09:51[diff] [blame]221
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]222classPresubmitError(PresubmitResult):
gayane3dff8c22014-12-04 17:09:51[diff] [blame]223
Ben Pastenee79d66112025-04-23 19:46:15[diff] [blame]224def __init__(self,*args,**kwargs):
225MockOutputApi.PresubmitResult.__init__(self,*args,**kwargs)
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]226 self.type='error'
gayane3dff8c22014-12-04 17:09:51[diff] [blame]227
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]228classPresubmitPromptWarning(PresubmitResult):
gayane3dff8c22014-12-04 17:09:51[diff] [blame]229
Ben Pastenee79d66112025-04-23 19:46:15[diff] [blame]230def __init__(self,*args,**kwargs):
231MockOutputApi.PresubmitResult.__init__(self,*args,**kwargs)
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]232 self.type='warning'
Daniel Cheng7052cdf2017-11-21 19:23:29[diff] [blame]233
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]234classPresubmitNotifyResult(PresubmitResult):
235
Ben Pastenee79d66112025-04-23 19:46:15[diff] [blame]236def __init__(self,*args,**kwargs):
237MockOutputApi.PresubmitResult.__init__(self,*args,**kwargs)
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]238 self.type='notify'
239
240classPresubmitPromptOrNotify(PresubmitResult):
241
Ben Pastenee79d66112025-04-23 19:46:15[diff] [blame]242def __init__(self,*args,**kwargs):
243MockOutputApi.PresubmitResult.__init__(self,*args,**kwargs)
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]244 self.type='promptOrNotify'
245
Ben Pastenee79d66112025-04-23 19:46:15[diff] [blame]246classPresubmitResultLocation(object):
247
248def __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 Grieve66a2bc42024-10-04 21:20:20[diff] [blame]253def __init__(self):
254 self.more_cc=[]
255
256defAppendCC(self, more_cc):
257 self.more_cc.append(more_cc)
Daniel Cheng7052cdf2017-11-21 19:23:29[diff] [blame]258
gayane3dff8c22014-12-04 17:09:51[diff] [blame]259
260classMockFile(object):
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]261"""Mock class for the File class.
gayane3dff8c22014-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 Grieve66a2bc42024-10-04 21:20:20[diff] [blame]267def __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)
276for i, lin enumerate(new_contents)]
277 self._action= action
278if scm_diff:
279 self._scm_diff= scm_diff
280else:
281 self._scm_diff=("--- /dev/null\n+++ %s\n@@ -0,0 +1,%d @@\n"%
282(local_path, len(new_contents)))
283for lin new_contents:
284 self._scm_diff+="+%s\n"% l
285 self._old_contents= old_contentsor[]
gayane3dff8c22014-12-04 17:09:51[diff] [blame]286
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]287def __str__(self):
288return self._local_path
Luciano Pacheco23d752b02023-10-25 22:49:36[diff] [blame]289
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]290defAction(self):
291return self._action
dbeam37e8e7402016-02-10 22:58:20[diff] [blame]292
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]293defChangedContents(self):
294return self._changed_contents
gayane3dff8c22014-12-04 17:09:51[diff] [blame]295
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]296defNewContents(self):
297return self._new_contents
gayane3dff8c22014-12-04 17:09:51[diff] [blame]298
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]299defLocalPath(self):
300return self._local_path
gayane3dff8c22014-12-04 17:09:51[diff] [blame]301
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]302defAbsoluteLocalPath(self):
Andrew Grieve713b89b2024-10-15 20:20:08[diff] [blame]303return os.path.join(_REPO_ROOT, self._local_path)
rdevlin.cronin9ab806c2016-02-26 23:17:13[diff] [blame]304
Anton Bershanskyi4253349482025-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
308defUnixLocalPath(self):
309return self._local_path.replace('\\','/')
310
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]311defGenerateScmDiff(self):
312return self._scm_diff
jbriance9e12f162016-11-25 07:57:50[diff] [blame]313
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]314defOldContents(self):
315return self._old_contents
Yoland Yanb92fa522017-08-28 17:37:06[diff] [blame]316
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]317def rfind(self, p):
318"""Required when os.path.basename() is called on MockFile."""
319return self._local_path.rfind(p)
davileene0426252015-03-02 21:10:41[diff] [blame]320
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]321def __getitem__(self, i):
322"""Required when os.path.basename() is called on MockFile."""
323return self._local_path[i]
davileene0426252015-03-02 21:10:41[diff] [blame]324
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]325def __len__(self):
326"""Required when os.path.basename() is called on MockFile."""
327return len(self._local_path)
pastarmovj89f7ee12016-09-20 14:58:13[diff] [blame]328
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]329def replace(self, altsep, sep):
330"""Required when os.path.basename() is called on MockFile."""
331return self._local_path.replace(altsep, sep)
Julian Pastarmov4f7af532019-07-17 19:25:37[diff] [blame]332
gayane3dff8c22014-12-04 17:09:51[diff] [blame]333
glidere61efad2015-02-18 17:39:43[diff] [blame]334classMockAffectedFile(MockFile):
Andrew Grieve713b89b2024-10-15 20:20:08[diff] [blame]335pass
glidere61efad2015-02-18 17:39:43[diff] [blame]336
337
gayane3dff8c22014-12-04 17:09:51[diff] [blame]338classMockChange(object):
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]339"""Mock class for Change class.
gayane3dff8c22014-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 Grieve66a2bc42024-10-04 21:20:20[diff] [blame]345def __init__(self, changed_files):
346 self._changed_files= changed_files
347 self.author_email=None
348 self.footers= defaultdict(list)
gayane3dff8c22014-12-04 17:09:51[diff] [blame]349
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]350defLocalPaths(self):
351return self._changed_files
rdevlin.cronin113668252016-05-02 17:05:54[diff] [blame]352
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]353defAffectedFiles(self,
354 include_dirs=False,
355 include_deletes=True,
356 file_filter=None):
357return self._changed_files
Chris Hall59f8d0c72020-05-01 07:31:19[diff] [blame]358
Andrew Grieve66a2bc42024-10-04 21:20:20[diff] [blame]359defGitFootersFromDescription(self):
360return self.footers
Andrew Grieve713b89b2024-10-15 20:20:08[diff] [blame]361
362defRepositoryRoot(self):
363return _REPO_ROOT

[8]ページ先頭

©2009-2025 Movatter.jp