forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pylint_extensions_test.py
439 lines (375 loc) · 15.7 KB
/
pylint_extensions_test.py
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# coding: utf-8
#
# Copyright 2018 The Oppia Authors. All Rights Reserved.
#
# 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
#
# http://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.
#
# For details on how to write such tests, please refer to
# https://github.com/oppia/oppia/wiki/Writing-Tests-For-Pylint
"""Unit tests for scripts/pylint_extensions."""
import os
import sys
import tempfile
import unittest
_PARENT_DIR = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
_PYLINT_PATH = os.path.join(_PARENT_DIR, 'oppia_tools', 'pylint-1.9.3')
sys.path.insert(0, _PYLINT_PATH)
# Since these module needs to be imported after adding Pylint path,
# we need to disable isort for the below lines to prevent import
# order errors.
# pylint: disable=wrong-import-position
# pylint: disable=relative-import
import astroid # isort:skip
import pylint_extensions # isort:skip
from pylint import testutils # isort:skip
# pylint: enable=wrong-import-position
# pylint: enable=relative-import
class ExplicitKeywordArgsCheckerTests(unittest.TestCase):
def test_finds_non_explicit_keyword_args(self):
checker_test_object = testutils.CheckerTestCase()
checker_test_object.CHECKER_CLASS = (
pylint_extensions.ExplicitKeywordArgsChecker)
checker_test_object.setup_method()
func_call_node_one, func_call_node_two, func_call_node_three = (
astroid.extract_node("""
def test(test_var_one, test_var_two=4, test_var_three=5, test_var_four="test_checker"):
test_var_five = test_var_two + test_var_three
return test_var_five
test(2, 5, test_var_three=6) #@
test(2) #@
test(2, 6, test_var_two=5, test_var_four="test_checker") #@
"""))
with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='non-explicit-keyword-args',
node=func_call_node_one,
args=(
'\'test_var_two\'',
'function',
'test'
)
),
):
checker_test_object.checker.visit_call(
func_call_node_one)
with checker_test_object.assertNoMessages():
checker_test_object.checker.visit_call(
func_call_node_two)
with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='non-explicit-keyword-args',
node=func_call_node_three,
args=(
'\'test_var_three\'',
'function',
'test'
)
)
):
checker_test_object.checker.visit_call(
func_call_node_three)
class HangingIndentCheckerTests(unittest.TestCase):
def test_finds_hanging_indent(self):
checker_test_object = testutils.CheckerTestCase()
checker_test_object.CHECKER_CLASS = (
pylint_extensions.HangingIndentChecker)
checker_test_object.setup_method()
node_break_after_hanging_indent = astroid.scoped_nodes.Module(
name='test',
doc='Custom test')
temp_file = tempfile.NamedTemporaryFile()
filename = temp_file.name
with open(filename, 'w') as tmp:
tmp.write(
"""self.post_json('/ml/trainedclassifierhandler',
self.payload, expect_errors=True, expected_status_int=401)
""")
node_break_after_hanging_indent.file = filename
node_break_after_hanging_indent.path = filename
checker_test_object.checker.process_module(
node_break_after_hanging_indent)
with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='no-break-after-hanging-indent',
line=1
),
):
temp_file.close()
node_no_err_message = astroid.scoped_nodes.Module(
name='test',
doc='Custom test')
temp_file = tempfile.NamedTemporaryFile()
filename = temp_file.name
with open(filename, 'w') as tmp:
tmp.write(
"""master_translation_dict = json.loads(
utils.get_file_contents(os.path.join(
os.getcwd(), 'assets', 'i18n', 'en.json')))
""")
node_no_err_message.file = filename
node_no_err_message.path = filename
checker_test_object.checker.process_module(node_no_err_message)
with checker_test_object.assertNoMessages():
temp_file.close()
class DocstringParameterCheckerTests(unittest.TestCase):
def test_finds_docstring_parameter(self):
checker_test_object = testutils.CheckerTestCase()
checker_test_object.CHECKER_CLASS = (
pylint_extensions.DocstringParameterChecker)
checker_test_object.setup_method()
func_node = astroid.extract_node("""
def test(test_var_one, test_var_two): #@
\"\"\"Function to test docstring parameters.
Args:
test_var_one: int. First test variable.
test_var_two: str. Second test variable.
Returns:
int. The test result.
\"\"\"
result = test_var_one + test_var_two
return result
""")
with checker_test_object.assertNoMessages():
checker_test_object.checker.visit_functiondef(func_node)
class ImportOnlyModulesCheckerTests(unittest.TestCase):
def test_finds_import_from(self):
checker_test_object = testutils.CheckerTestCase()
checker_test_object.CHECKER_CLASS = (
pylint_extensions.ImportOnlyModulesChecker)
checker_test_object.setup_method()
importfrom_node1 = astroid.extract_node("""
from os import path #@
import sys
""")
with checker_test_object.assertNoMessages():
checker_test_object.checker.visit_importfrom(importfrom_node1)
importfrom_node2 = astroid.extract_node("""
from os import error #@
import sys
""")
with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='import-only-modules',
node=importfrom_node2,
args=('error', 'os')
),
):
checker_test_object.checker.visit_importfrom(
importfrom_node2)
class BackslashContinuationCheckerTests(unittest.TestCase):
def test_finds_backslash_continuation(self):
checker_test_object = testutils.CheckerTestCase()
checker_test_object.CHECKER_CLASS = (
pylint_extensions.BackslashContinuationChecker)
checker_test_object.setup_method()
node = astroid.scoped_nodes.Module(name='test', doc='Custom test')
temp_file = tempfile.NamedTemporaryFile()
filename = temp_file.name
with open(filename, 'w') as tmp:
tmp.write(
"""message1 = 'abc'\\\n""" # pylint: disable=backslash-continuation
"""'cde'\\\n""" # pylint: disable=backslash-continuation
"""'xyz'
message2 = 'abc\\\\'
message3 = (
'abc\\\\'
'xyz\\\\'
)
""")
node.file = filename
node.path = filename
checker_test_object.checker.process_module(node)
with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='backslash-continuation',
line=1
),
testutils.Message(
msg_id='backslash-continuation',
line=2
),
):
temp_file.close()
class FunctionArgsOrderCheckerTests(unittest.TestCase):
def test_finds_function_def(self):
checker_test_object = testutils.CheckerTestCase()
checker_test_object.CHECKER_CLASS = (
pylint_extensions.FunctionArgsOrderChecker)
checker_test_object.setup_method()
functiondef_node1 = astroid.extract_node("""
def test(self,test_var_one, test_var_two): #@
result = test_var_one + test_var_two
return result
""")
with checker_test_object.assertNoMessages():
checker_test_object.checker.visit_functiondef(functiondef_node1)
functiondef_node2 = astroid.extract_node("""
def test(test_var_one, test_var_two, self): #@
result = test_var_one + test_var_two
return result
""")
with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='function-args-order-self',
node=functiondef_node2
),
):
checker_test_object.checker.visit_functiondef(functiondef_node2)
class RestrictedImportCheckerTests(unittest.TestCase):
def test_detect_restricted_import(self):
checker_test_object = testutils.CheckerTestCase()
checker_test_object.CHECKER_CLASS = (
pylint_extensions.RestrictedImportChecker)
checker_test_object.setup_method()
# Tests the case wherein storage layer imports domain layer
# in import statements.
node_err_import = astroid.extract_node("""
import core.domain.activity_domain #@
""")
node_err_import.root().name = 'oppia.core.storage.topic'
with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='invalid-import',
node=node_err_import,
args=('domain', 'storage'),
),
):
checker_test_object.checker.visit_import(node_err_import)
# Tests the case wherein storage layer does not import domain layer
# in import statements.
node_no_err_import = astroid.extract_node("""
import core.platform.email.gae_email_services #@
""")
node_no_err_import.root().name = 'oppia.core.storage.topic'
with checker_test_object.assertNoMessages():
checker_test_object.checker.visit_import(node_no_err_import)
# Tests the case wherein storage layer imports domain layer
# in import-from statements.
node_err_importfrom = astroid.extract_node("""
from core.domain import activity_domain #@
""")
node_err_importfrom.root().name = 'oppia.core.storage.topic'
with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='invalid-import',
node=node_err_importfrom,
args=('domain', 'storage'),
)
):
checker_test_object.checker.visit_importfrom(node_err_importfrom)
# Tests the case wherein storage layer does not import domain layer
# in import-from statements.
node_no_err_importfrom = astroid.extract_node("""
from core.platform.email import gae_email_services #@
""")
node_no_err_importfrom.root().name = 'oppia.core.storage.topicl'
with checker_test_object.assertNoMessages():
checker_test_object.checker.visit_importfrom(node_no_err_importfrom)
# Tests the case wherein domain layer imports controller layer
# in import statements.
node_err_import = astroid.extract_node("""
import core.controllers.acl_decorators #@
""")
node_err_import.root().name = 'oppia.core.domain'
with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='invalid-import',
node=node_err_import,
args=('controller', 'domain'),
),
):
checker_test_object.checker.visit_import(node_err_import)
# Tests the case wherein domain layer does not import controller layer
# in import statements.
node_no_err_import = astroid.extract_node("""
import core.platform.email.gae_email_services_test #@
""")
node_no_err_import.root().name = 'oppia.core.domain'
with checker_test_object.assertNoMessages():
checker_test_object.checker.visit_import(node_no_err_import)
# Tests the case wherein domain layer imports controller layer
# in import-from statements.
node_err_importfrom = astroid.extract_node("""
from core.controllers import acl_decorators #@
""")
node_err_importfrom.root().name = 'oppia.core.domain'
with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='invalid-import',
node=node_err_importfrom,
args=('controller', 'domain'),
)
):
checker_test_object.checker.visit_importfrom(node_err_importfrom)
# Tests the case wherein domain layer does not import controller layer
# in import-from statements.
node_no_err_importfrom = astroid.extract_node("""
from core.platform.email import gae_email_services_test #@
""")
node_no_err_importfrom.root().name = 'oppia.core.domain'
with checker_test_object.assertNoMessages():
checker_test_object.checker.visit_importfrom(node_no_err_importfrom)
class SingleCharAndNewlineAtEOFCheckerTests(unittest.TestCase):
def test_checks_single_char_and_newline_eof(self):
checker_test_object = testutils.CheckerTestCase()
checker_test_object.CHECKER_CLASS = (
pylint_extensions.SingleCharAndNewlineAtEOFChecker)
checker_test_object.setup_method()
node_missing_newline_at_eof = astroid.scoped_nodes.Module(
name='test',
doc='Custom test')
temp_file = tempfile.NamedTemporaryFile()
filename = temp_file.name
with open(filename, 'w') as tmp:
tmp.write(
"""c = 'something dummy'
""")
node_missing_newline_at_eof.file = filename
node_missing_newline_at_eof.path = filename
checker_test_object.checker.process_module(node_missing_newline_at_eof)
with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='newline-at-eof',
line=2
),
):
temp_file.close()
node_single_char_file = astroid.scoped_nodes.Module(
name='test',
doc='Custom test')
temp_file = tempfile.NamedTemporaryFile()
filename = temp_file.name
with open(filename, 'w') as tmp:
tmp.write("""1""")
node_single_char_file.file = filename
node_single_char_file.path = filename
checker_test_object.checker.process_module(node_single_char_file)
with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='only-one-character',
line=1
),
):
temp_file.close()
node_no_err_message = astroid.scoped_nodes.Module(
name='test',
doc='Custom test')
temp_file = tempfile.NamedTemporaryFile()
filename = temp_file.name
with open(filename, 'w') as tmp:
tmp.write("""x = 'something dummy'""")
node_no_err_message.file = filename
node_no_err_message.path = filename
checker_test_object.checker.process_module(node_no_err_message)
with checker_test_object.assertNoMessages():
temp_file.close()