7from collections
import defaultdict
16def _ReportErrorFileAndLine(filename, line_num, dummy_line):
17 """Default error formatter for _FindNewViolationsOfRule."""
18 return '%s:%s' % (filename, line_num)
22 def _FindNewViolationsOfRule(self, callable_rule, input_api,
23 source_file_filter=None,
24 error_formatter=_ReportErrorFileAndLine):
25 """Find all newly introduced violations of a per-line rule (a callable).
28 callable_rule: a callable taking a file extension and line of input
and
29 returning
True if the rule
is satisfied
and False if there was a
31 input_api: object to enumerate the affected files.
32 source_file_filter: a filter to be passed to the input api.
33 error_formatter: a callable taking (filename, line_number, line)
and
34 returning a formatted error string.
37 A list of the newly-introduced violations reported by the rule.
40 for f
in input_api.AffectedFiles(include_deletes=
False,
41 file_filter=source_file_filter):
46 extension = str(f.LocalPath()).rsplit(
'.', 1)[-1]
47 if all(callable_rule(extension, line)
for line
in f.NewContents()):
51 for line_num, line
in f.ChangedContents():
52 if not callable_rule(extension, line):
53 errors.append(error_formatter(
54 f.LocalPath(), line_num, line))
60 """Mock class for the InputApi class.
62 This class can be used
for unittests
for presubmit by initializing the
files
63 attribute
as the list of changed files.
66 DEFAULT_FILES_TO_SKIP = ()
92 self.
os_path.exists =
lambda x: x
in f_list
95 for file
in self.
files:
96 if file_filter
and not file_filter(file):
98 if not include_deletes
and file.Action() ==
'D':
104 for af
in affected_files:
105 lines = af.ChangedContents()
107 yield (af, line[0], line[1])
113 files_to_check=(), files_to_skip=()):
114 local_path = file.LocalPath()
115 found_in_files_to_check =
not files_to_check
117 if type(files_to_check)
is str:
119 'files_to_check should be an iterable of strings')
120 for pattern
in files_to_check:
121 compiled_pattern = re.compile(pattern)
122 if compiled_pattern.match(local_path):
123 found_in_files_to_check =
True
126 if type(files_to_skip)
is str:
128 'files_to_skip should be an iterable of strings')
129 for pattern
in files_to_skip:
130 compiled_pattern = re.compile(pattern)
131 if compiled_pattern.match(local_path):
133 return found_in_files_to_check
136 return [file.LocalPath()
for file
in self.
files]
142 if hasattr(filename,
'AbsoluteLocalPath'):
143 filename = filename.AbsoluteLocalPath()
144 for file_
in self.
files:
145 if file_.LocalPath() == filename:
146 return '\n'.
join(file_.NewContents())
148 raise IOError(
"No such file or directory: '%s'" % filename)
152 """Mock class for the OutputApi class.
154 An instance of this class can be passed to presubmit unittests
for outputting
155 various types of results.
159 def __init__(self, message, items=None, long_text=''):
168 def __init__(self, message, items=None, long_text=''):
169 MockOutputApi.PresubmitResult.__init__(
170 self, message, items, long_text)
174 def __init__(self, message, items=None, long_text=''):
175 MockOutputApi.PresubmitResult.__init__(
176 self, message, items, long_text)
180 def __init__(self, message, items=None, long_text=''):
181 MockOutputApi.PresubmitResult.__init__(
182 self, message, items, long_text)
186 def __init__(self, message, items=None, long_text=''):
187 MockOutputApi.PresubmitResult.__init__(
188 self, message, items, long_text)
199 """Mock class for the File class.
201 This class can be used to form the mock list of changed files in
202 MockInputApi
for presubmit unittests.
205 def __init__(self, local_path, new_contents, old_contents=None, action='A',
210 for i, l
in enumerate(new_contents)]
216 "--- /dev/null\n+++ %s\n@@ -0,0 +1,%d @@\n" %
217 (local_path,
len(new_contents)))
218 for l
in new_contents:
244 """os.path.basename is called on MockFile so we need an rfind method."""
248 """os.path.basename is called on MockFile so we need a get method."""
252 """os.path.basename is called on MockFile so we need a len method."""
256 """os.path.basename is called on MockFile so we need a replace method."""
266 """Mock class for Change class.
268 This class can be used in presubmit unittests to mock the query of the
for(const auto glyph :glyphs)
def AbsoluteLocalPath(self)
def AffectedFiles(self, include_dirs=False, include_deletes=True, file_filter=None)
def GitFootersFromDescription(self)
def __init__(self, changed_files)
def GenerateScmDiff(self)
def __init__(self, local_path, new_contents, old_contents=None, action='A', scm_diff=None)
def ChangedContents(self)
def replace(self, altsep, sep)
def AbsoluteLocalPath(self)
def __init__(self, message, items=None, long_text='')
def __init__(self, message, items=None, long_text='')
def __init__(self, message, items=None, long_text='')
def __init__(self, message, items=None, long_text='')
def __init__(self, message, items=None, long_text='')
def AppendCC(self, more_cc)
static void append(char **dst, size_t *count, const char *src, size_t n)
SIT bool all(const Vec< 1, T > &x)
static SkString join(const CommandLineFlags::StringArray &)