aboutsummaryrefslogtreecommitdiff
path: root/pw_presubmit/py/todo_check_test.py
blob: 36e435a0ffdb71b2a8caf5681b310e3037591d68 (plain)
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
#!/usr/bin/env python3
# Copyright 2022 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Tests for todo_check."""

from pathlib import Path
import re
import unittest
from unittest.mock import MagicMock, mock_open, patch

from pw_presubmit import todo_check

# pylint: disable=attribute-defined-outside-init
# todo-check: disable


# pylint: disable-next=too-many-public-methods
class TestTodoCheck(unittest.TestCase):
    """Test TODO checker."""

    def _run(self, regex: re.Pattern, contents: str) -> None:
        self.ctx = MagicMock()
        self.ctx.fail = MagicMock()
        path = MagicMock(spec=Path('foo/bar'))

        def mocked_open_read(*args, **kwargs):
            return mock_open(read_data=contents)(*args, **kwargs)

        with patch.object(path, 'open', mocked_open_read):
            # pylint: disable=protected-access
            todo_check._process_file(self.ctx, regex, path)

            # pylint: enable=protected-access

    def _run_bugs_users(self, contents: str) -> None:
        self._run(todo_check.BUGS_OR_USERNAMES, contents)

    def _run_bugs(self, contents: str) -> None:
        self._run(todo_check.BUGS_ONLY, contents)

    def test_one_bug_legacy(self) -> None:
        contents = 'TODO(b/123): foo\n'
        self._run_bugs_users(contents)
        self.ctx.fail.assert_not_called()
        self._run_bugs(contents)
        self.ctx.fail.assert_not_called()

    def test_one_bug_new(self) -> None:
        contents = 'TODO: b/123 - foo\n'
        self._run_bugs_users(contents)
        self.ctx.fail.assert_not_called()
        self._run_bugs(contents)
        self.ctx.fail.assert_not_called()

    def test_one_bug_short_url(self) -> None:
        contents = 'TODO: https://pwbug.dev/123 - foo\n'
        self._run_bugs_users(contents)
        self.ctx.fail.assert_not_called()
        self._run_bugs(contents)
        self.ctx.fail.assert_not_called()

    def test_one_bug_shorter_url(self) -> None:
        contents = 'TODO: pwbug.dev/123 - foo\n'
        self._run_bugs_users(contents)
        self.ctx.fail.assert_not_called()
        self._run_bugs(contents)
        self.ctx.fail.assert_not_called()

    def test_one_bug_full_url(self) -> None:
        contents = 'TODO: https://issues.pigweed.dev/issues/123 - foo\n'
        self._run_bugs_users(contents)
        self.ctx.fail.assert_not_called()
        self._run_bugs(contents)
        self.ctx.fail.assert_not_called()

    def test_two_bugs_legacy(self) -> None:
        contents = 'TODO(b/123, b/456): foo\n'
        self._run_bugs_users(contents)
        self.ctx.fail.assert_not_called()
        self._run_bugs(contents)
        self.ctx.fail.assert_not_called()

    def test_two_bugs_new(self) -> None:
        contents = 'TODO: b/123, b/456 - foo\n'
        self._run_bugs_users(contents)
        self.ctx.fail.assert_not_called()
        self._run_bugs(contents)
        self.ctx.fail.assert_not_called()

    def test_three_bugs_legacy(self) -> None:
        contents = 'TODO(b/123,b/456,b/789): foo\n'
        self._run_bugs_users(contents)
        self.ctx.fail.assert_not_called()
        self._run_bugs(contents)
        self.ctx.fail.assert_not_called()

    def test_three_bugs_new(self) -> None:
        contents = 'TODO: b/123,b/456,b/789 - foo\n'
        self._run_bugs_users(contents)
        self.ctx.fail.assert_not_called()
        self._run_bugs(contents)
        self.ctx.fail.assert_not_called()

    def test_one_username_legacy(self) -> None:
        self._run_bugs_users('TODO(usera): foo\n')
        self.ctx.fail.assert_not_called()

    def test_one_username_new(self) -> None:
        self._run_bugs_users('TODO: usera@ - foo\n')
        self.ctx.fail.assert_not_called()

    def test_one_username_new_noat(self) -> None:
        self._run_bugs_users('TODO: usera - foo\n')
        self.ctx.fail.assert_called()

    def test_one_username_new_short_domain(self) -> None:
        self._run_bugs_users('TODO: usera@com - foo\n')
        self.ctx.fail.assert_called()

    def test_one_username_new_medium_domain(self) -> None:
        self._run_bugs_users('TODO: usera@example.com - foo\n')
        self.ctx.fail.assert_not_called()

    def test_one_username_new_long_domain(self) -> None:
        self._run_bugs_users('TODO: usera@a.b.c.d.example.com - foo\n')
        self.ctx.fail.assert_not_called()

    def test_two_usernames_legacy(self) -> None:
        self._run_bugs_users('TODO(usera, userb): foo\n')
        self.ctx.fail.assert_not_called()

    def test_two_usernames_new(self) -> None:
        self._run_bugs_users('TODO: usera@, userb@ - foo\n')
        self.ctx.fail.assert_not_called()

    def test_three_usernames_legacy(self) -> None:
        self._run_bugs_users('TODO(usera,userb,userc): foo\n')
        self.ctx.fail.assert_not_called()

    def test_three_usernames_new(self) -> None:
        self._run_bugs_users('TODO: usera@,userb@example.com,userc@ - foo\n')
        self.ctx.fail.assert_not_called()

    def test_username_not_allowed_legacy(self) -> None:
        self._run_bugs('TODO(usera): foo\n')
        self.ctx.fail.assert_called()

    def test_username_not_allowed_new(self) -> None:
        self._run_bugs('TODO: usera@ - foo\n')
        self.ctx.fail.assert_called()

    def test_space_after_todo_bugsonly_legacy(self) -> None:
        self._run_bugs('TODO (b/123): foo\n')
        self.ctx.fail.assert_called()

    def test_space_after_todo_bugsonly_new(self) -> None:
        self._run_bugs('TODO : b/123 - foo\n')
        self.ctx.fail.assert_called()

    def test_space_after_todo_bugsusers_legacy(self) -> None:
        self._run_bugs_users('TODO (b/123): foo\n')
        self.ctx.fail.assert_called()

    def test_space_after_todo_bugsusers_new(self) -> None:
        self._run_bugs_users('TODO : b/123 - foo\n')
        self.ctx.fail.assert_called()

    def test_space_before_bug_bugsonly_legacy(self) -> None:
        self._run_bugs('TODO( b/123): foo\n')
        self.ctx.fail.assert_called()

    def test_no_space_before_bug_bugsonly_new(self) -> None:
        self._run_bugs('TODO:b/123 - foo\n')
        self.ctx.fail.assert_called()

    def test_space_before_bug_bugsusers_legacy(self) -> None:
        self._run_bugs_users('TODO( b/123): foo\n')
        self.ctx.fail.assert_called()

    def test_no_space_before_bug_bugsusers_new(self) -> None:
        self._run_bugs_users('TODO:b/123 - foo\n')
        self.ctx.fail.assert_called()

    def test_space_after_bug_bugsonly_legacy(self) -> None:
        self._run_bugs('TODO(b/123 ): foo\n')
        self.ctx.fail.assert_called()

    def test_no_space_after_bug_bugsonly_new(self) -> None:
        self._run_bugs('TODO: b/123- foo\n')
        self.ctx.fail.assert_called()

    def test_no_space_before_explanation_bugsonly_new(self) -> None:
        self._run_bugs('TODO: b/123 -foo\n')
        self.ctx.fail.assert_called()

    def test_space_after_bug_bugsusers_legacy(self) -> None:
        self._run_bugs_users('TODO(b/123 ): foo\n')
        self.ctx.fail.assert_called()

    def test_no_space_before_explanation_bugsusers_new(self) -> None:
        self._run_bugs_users('TODO: b/123 -foo\n')
        self.ctx.fail.assert_called()

    def test_missing_explanation_bugsonly_legacy(self) -> None:
        self._run_bugs('TODO: b/123 -\n')
        self.ctx.fail.assert_called()

    def test_missing_explanation_bugsonly_new(self) -> None:
        self._run_bugs('TODO: b/123\n')
        self.ctx.fail.assert_called()

    def test_missing_explanation_bugsusers_legacy(self) -> None:
        self._run_bugs_users('TODO: b/123 -\n')
        self.ctx.fail.assert_called()

    def test_missing_explanation_bugsusers_new(self) -> None:
        self._run_bugs_users('TODO: b/123\n')
        self.ctx.fail.assert_called()

    def test_not_a_bug_bugsonly_legacy(self) -> None:
        self._run_bugs('TODO(cl/123): foo\n')
        self.ctx.fail.assert_called()

    def test_not_a_bug_bugsonly_new(self) -> None:
        self._run_bugs('TODO: cl/123 - foo\n')
        self.ctx.fail.assert_called()

    def test_not_a_bug_bugsusers_legacy(self) -> None:
        self._run_bugs_users('TODO(cl/123): foo\n')
        self.ctx.fail.assert_called()

    def test_not_a_bug_bugsusers_new(self) -> None:
        self._run_bugs_users('TODO: cl/123 - foo\n')
        self.ctx.fail.assert_called()

    def test_but_not_bug_bugsonly_legacy(self) -> None:
        self._run_bugs('TODO(b/123, cl/123): foo\n')
        self.ctx.fail.assert_called()

    def test_but_not_bug_bugsonly_new(self) -> None:
        self._run_bugs('TODO: b/123, cl/123 - foo\n')
        self.ctx.fail.assert_called()

    def test_bug_not_bug_bugsusers_legacy(self) -> None:
        self._run_bugs_users('TODO(b/123, cl/123): foo\n')
        self.ctx.fail.assert_called()

    def test_bug_not_bug_bugsusers_new(self) -> None:
        self._run_bugs_users('TODO: b/123, cl/123 - foo\n')
        self.ctx.fail.assert_called()

    def test_empty_bugsonly_legacy(self) -> None:
        self._run_bugs('TODO(): foo\n')
        self.ctx.fail.assert_called()

    def test_empty_bugsonly_new(self) -> None:
        self._run_bugs('TODO: - foo\n')
        self.ctx.fail.assert_called()

    def test_empty_bugsusers_legacy(self) -> None:
        self._run_bugs_users('TODO(): foo\n')
        self.ctx.fail.assert_called()

    def test_empty_bugsusers_new(self) -> None:
        self._run_bugs_users('TODO: - foo\n')
        self.ctx.fail.assert_called()

    def test_bare_bugsonly_legacy(self) -> None:
        self._run_bugs('TODO: foo\n')
        self.ctx.fail.assert_called()

    def test_bare_bugsonly_new(self) -> None:
        self._run_bugs('TODO: foo\n')
        self.ctx.fail.assert_called()

    def test_bare_bugsusers_legacy(self) -> None:
        self._run_bugs_users('TODO: foo\n')
        self.ctx.fail.assert_called()

    def test_bare_bugsusers_new(self) -> None:
        self._run_bugs_users('TODO: foo\n')
        self.ctx.fail.assert_called()

    def test_fuchsia(self) -> None:
        self._run_bugs_users('TODO(fxbug.dev/123): foo\n')
        self.ctx.fail.assert_not_called()

    def test_fuchsia_two_bugs(self) -> None:
        self._run_bugs_users('TODO(fxbug.dev/123,fxbug.dev/321): bar\n')
        self.ctx.fail.assert_not_called()

    def test_bazel_gh_issue(self) -> None:
        contents = (
            'TODO: https://github.com/bazelbuild/bazel/issues/12345 - '
            'Bazel sometimes works\n'
        )
        self._run_bugs_users(contents)
        self.ctx.fail.assert_not_called()
        self._run_bugs(contents)
        self.ctx.fail.assert_not_called()

    def test_bazel_gh_issue_underscore(self) -> None:
        contents = (
            'TODO: https://github.com/bazelbuild/rules_cc/issues/678910 - '
            'Sometimes it does not work\n'
        )
        self._run_bugs_users(contents)
        self.ctx.fail.assert_not_called()
        self._run_bugs(contents)
        self.ctx.fail.assert_not_called()


if __name__ == '__main__':
    unittest.main()

# todo-check: enable