-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathPRESUBMIT_test.py
More file actions
393 lines (346 loc) · 14.6 KB
/
Copy pathPRESUBMIT_test.py
File metadata and controls
393 lines (346 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env python3
# Copyright 2026 The Dawn & Tint Authors
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import os
import sys
import unittest
# Import PRESUBMIT first before depot_tools modifies sys.path
import PRESUBMIT
# Add depot_tools to path to import mocks
sys.path.append(
os.path.join(os.path.dirname(__file__), 'third_party', 'depot_tools'))
from testing_support.presubmit_canned_checks_test_mocks import (
MockAffectedFile, MockInputApi, MockOutputApi)
class MockOutputApiWithLocations(MockOutputApi):
class PresubmitResultLocation(object):
def __init__(self, file_path, start_line, end_line):
self.file_path = file_path
self.start_line = start_line
self.end_line = end_line
class PresubmitResult(object):
def __init__(self, message, items=None, long_text='', locations=None):
self.message = message
self.items = items
self.long_text = long_text
self.locations = locations or []
def __repr__(self):
return self.message
class PresubmitError(PresubmitResult):
def __init__(self, message, items=None, long_text='', locations=None):
super().__init__(message, items, long_text, locations)
self.type = 'error'
class PresubmitPromptWarning(PresubmitResult):
def __init__(self, message, items=None, long_text='', locations=None):
super().__init__(message, items, long_text, locations)
self.type = 'warning'
class CheckChangeTodoHasOwnerTest(unittest.TestCase):
def _create_mock_input_api(self):
mock_input_api = MockInputApi()
# Mock AffectedFiles to support file_filter
def mock_affected_files(include_dirs=False,
include_deletes=True,
file_filter=None):
if file_filter:
return [f for f in mock_input_api.files if file_filter(f)]
return mock_input_api.files
mock_input_api.change.AffectedFiles = mock_affected_files
return mock_input_api
def testNoTodo(self):
mock_input_api = self._create_mock_input_api()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'void Foo() {',
' int x = 0;',
'}',
])
]
errors = PRESUBMIT.CheckChangeTodoHasOwner(mock_input_api,
MockOutputApi())
self.assertEqual(0, len(errors))
def testValidTodo(self):
mock_input_api = self._create_mock_input_api()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'// TODO(crbug.com/123): fix this',
'// TODO: owner - fix this',
])
]
errors = PRESUBMIT.CheckChangeTodoHasOwner(mock_input_api,
MockOutputApi())
self.assertEqual(0, len(errors))
def testInvalidTodo(self):
mock_input_api = self._create_mock_input_api()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'// TODO: fix this',
])
]
errors = PRESUBMIT.CheckChangeTodoHasOwner(mock_input_api,
MockOutputApi())
self.assertEqual(1, len(errors))
self.assertIn('Found TODO with no issue number', errors[0].message)
def testDawnUnsafeTodoIgnored(self):
mock_input_api = self._create_mock_input_api()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'DAWN_UNSAFE_TODO(ptr[0] = 0);',
])
]
errors = PRESUBMIT.CheckChangeTodoHasOwner(mock_input_api,
MockOutputApi())
self.assertEqual(0, len(errors))
def testTodoAndDawnUnsafeTodo(self):
mock_input_api = self._create_mock_input_api()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'DAWN_UNSAFE_TODO(ptr[0] = 0); // TODO: fix this',
])
]
errors = PRESUBMIT.CheckChangeTodoHasOwner(mock_input_api,
MockOutputApi())
self.assertEqual(1, len(errors))
self.assertIn('Found TODO with no issue number', errors[0].message)
def testMixedTodos(self):
mock_input_api = self._create_mock_input_api()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'// TODO: fix this',
'DAWN_UNSAFE_TODO(ptr[0] = 0);',
])
]
errors = PRESUBMIT.CheckChangeTodoHasOwner(mock_input_api,
MockOutputApi())
self.assertEqual(1, len(errors))
self.assertIn('src/dawn/Foo.cpp:1', errors[0].message)
self.assertNotIn('src/dawn/Foo.cpp:2', errors[0].message)
def testPresubmitFilesIgnored(self):
mock_input_api = self._create_mock_input_api()
mock_input_api.files = [
MockAffectedFile('PRESUBMIT.py', [
'// TODO: fix this',
'DAWN_UNSAFE_TODO(ptr[0] = 0);',
]),
MockAffectedFile('PRESUBMIT_test.py', [
'// TODO: fix this',
'DAWN_UNSAFE_TODO(ptr[0] = 0);',
])
]
errors = PRESUBMIT.CheckChangeTodoHasOwner(mock_input_api,
MockOutputApi())
self.assertEqual(0, len(errors))
class CheckUnsafeBuffersSafetyCommentsTest(unittest.TestCase):
def testNoUsage(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'void Foo() {',
' int x = 0;',
'}',
])
]
errors = PRESUBMIT.CheckUnsafeBuffersSafetyComments(
mock_input_api, MockOutputApi())
self.assertEqual(0, len(errors))
def testValidUsageSameLine(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'void Foo() {',
' DAWN_UNSAFE_BUFFERS(ptr[0] = 0); // SAFETY: safe',
'}',
])
]
errors = PRESUBMIT.CheckUnsafeBuffersSafetyComments(
mock_input_api, MockOutputApi())
self.assertEqual(0, len(errors))
def testValidUsagePrecedingLine(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'void Foo() {',
' // SAFETY: safe',
' DAWN_UNSAFE_BUFFERS(ptr[0] = 0);',
'}',
])
]
errors = PRESUBMIT.CheckUnsafeBuffersSafetyComments(
mock_input_api, MockOutputApi())
self.assertEqual(0, len(errors))
def testValidUsagePrecedingLineWithOtherComments(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'void Foo() {',
' // SAFETY: safe',
' // some other info',
' DAWN_UNSAFE_BUFFERS(ptr[0] = 0);',
'}',
])
]
errors = PRESUBMIT.CheckUnsafeBuffersSafetyComments(
mock_input_api, MockOutputApi())
self.assertEqual(0, len(errors))
def testInvalidUsageNoComment(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'void Foo() {',
' DAWN_UNSAFE_BUFFERS(ptr[0] = 0);',
'}',
])
]
errors = PRESUBMIT.CheckUnsafeBuffersSafetyComments(
mock_input_api, MockOutputApi())
self.assertEqual(1, len(errors))
self.assertIn('DAWN_UNSAFE_BUFFERS usage must be accompanied',
errors[0].items[0])
def testInvalidUsageCommentNotSafety(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'void Foo() {',
' // this is a comment but not safety',
' DAWN_UNSAFE_BUFFERS(ptr[0] = 0);',
'}',
])
]
errors = PRESUBMIT.CheckUnsafeBuffersSafetyComments(
mock_input_api, MockOutputApi())
self.assertEqual(1, len(errors))
def testInvalidUsageCommentSeparatedByCode(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'void Foo() {',
' // SAFETY: safe',
' int x = 0;',
' DAWN_UNSAFE_BUFFERS(ptr[0] = 0);',
'}',
])
]
errors = PRESUBMIT.CheckUnsafeBuffersSafetyComments(
mock_input_api, MockOutputApi())
self.assertEqual(1, len(errors))
def testIgnoreCommentedUsage(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'void Foo() {',
' // DAWN_UNSAFE_BUFFERS(ptr[0] = 0);',
'}',
])
]
errors = PRESUBMIT.CheckUnsafeBuffersSafetyComments(
mock_input_api, MockOutputApi())
self.assertEqual(0, len(errors))
def testNonCppFilesIgnored(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.txt', [
'DAWN_UNSAFE_BUFFERS(ptr[0] = 0);',
])
]
errors = PRESUBMIT.CheckUnsafeBuffersSafetyComments(
mock_input_api, MockOutputApi())
self.assertEqual(0, len(errors))
class CheckBannedPatternsTest(unittest.TestCase):
def testNoUsage(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'void Foo() {',
' int x = 0;',
'}',
])
]
errors = PRESUBMIT.CheckNoBannedPatterns(mock_input_api,
MockOutputApiWithLocations())
self.assertEqual(0, len(errors))
def testBannedUnsafeTodo(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'void Foo() {',
' DAWN_UNSAFE_TODO(ptr[0] = 0);',
'}',
])
]
errors = PRESUBMIT.CheckNoBannedPatterns(mock_input_api,
MockOutputApiWithLocations())
self.assertEqual(1, len(errors))
self.assertIn('A banned pattern was used.', errors[0].message)
self.assertIn('src/dawn/Foo.cpp:2:', errors[0].message)
self.assertIn('Do not introduce new instances of DAWN_UNSAFE_TODO',
errors[0].message)
self.assertEqual(1, len(errors[0].locations))
loc = errors[0].locations[0]
self.assertEqual('src/dawn/Foo.cpp', loc.file_path)
self.assertEqual(2, loc.start_line)
self.assertEqual(2, loc.end_line)
def testBannedPragma(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'#pragma allow_unsafe_buffers',
])
]
errors = PRESUBMIT.CheckNoBannedPatterns(mock_input_api,
MockOutputApiWithLocations())
self.assertEqual(1, len(errors))
self.assertIn('A banned pattern was used.', errors[0].message)
self.assertIn('src/dawn/Foo.cpp:1:', errors[0].message)
self.assertIn('#pragma allow_unsafe_buffers is discouraged',
errors[0].message)
self.assertEqual(1, len(errors[0].locations))
loc = errors[0].locations[0]
self.assertEqual('src/dawn/Foo.cpp', loc.file_path)
self.assertEqual(1, loc.start_line)
self.assertEqual(1, loc.end_line)
def testCommentedUsageIgnored(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.cpp', [
'// DAWN_UNSAFE_TODO(ptr[0] = 0);',
'// #pragma allow_unsafe_buffers',
])
]
errors = PRESUBMIT.CheckNoBannedPatterns(mock_input_api,
MockOutputApiWithLocations())
self.assertEqual(0, len(errors))
def testNonCppFilesIgnored(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockAffectedFile('src/dawn/Foo.txt', [
'DAWN_UNSAFE_TODO(ptr[0] = 0);',
'#pragma allow_unsafe_buffers',
])
]
errors = PRESUBMIT.CheckNoBannedPatterns(mock_input_api,
MockOutputApiWithLocations())
self.assertEqual(0, len(errors))
if __name__ == '__main__':
unittest.main()