def test_backtick(self): node = astroid.extract_node('`test`') message = testutils.Message('backtick', node=node) with self.assertAddsMessages(message): self.checker.visit_repr(node)
def check_not_builtin(self, builtin_name, message): node = test_utils.extract_node(builtin_name + ' #@') with self.assertAddsMessages(testutils.Message(message, node=node)): self.checker.visit_name(node)
def test_print_statement(self): node = astroid.extract_node('print "Hello, World!" #@') message = testutils.Message('print-statement', node=node) with self.assertAddsMessages(message): self.checker.visit_print(node)
def test_old_raise_syntax(self): node = astroid.extract_node('raise Exception, "test"') message = testutils.Message('old-raise-syntax', node=node) with self.assertAddsMessages(message): self.checker.visit_raise(node)
def test_raising_string(self): node = astroid.extract_node('raise "Test"') message = testutils.Message('raising-string', node=node) with self.assertAddsMessages(message): self.checker.visit_raise(node)
def test_dict_view_method(self): for meth in ('keys', 'values', 'items'): node = astroid.extract_node('x.view%s() #@' % meth) message = testutils.Message('dict-view-method', node=node) with self.assertAddsMessages(message): self.checker.visit_call(node)
def test_next_method(self): node = astroid.extract_node('x.next() #@') message = testutils.Message('next-method-called', node=node) with self.assertAddsMessages(message): self.checker.visit_call(node)
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)
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 python_utils.open_file(filename, 'w') as tmp: tmp.write( u"""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 python_utils.open_file(filename, 'w') as tmp: tmp.write(u"""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_with_no_error_message = astroid.scoped_nodes.Module( name='test', doc='Custom test') temp_file = tempfile.NamedTemporaryFile() filename = temp_file.name with python_utils.open_file(filename, 'w') as tmp: tmp.write(u"""x = 'something dummy'""") node_with_no_error_message.file = filename node_with_no_error_message.path = filename checker_test_object.checker.process_module(node_with_no_error_message) with checker_test_object.assertNoMessages(): temp_file.close()
def test_finds_docstring_parameter(self): self.checker_test_object = testutils.CheckerTestCase() self.checker_test_object.CHECKER_CLASS = ( pylint_extensions.DocstringParameterChecker) self.checker_test_object.setup_method() valid_func_node, valid_return_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 self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_functiondef(valid_func_node) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_return(valid_return_node) valid_func_node, valid_yield_node = astroid.extract_node(""" def test(test_var_one, test_var_two): #@ \"\"\"Function to test docstring parameters.\"\"\" result = test_var_one + test_var_two yield result #@ """) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_functiondef(valid_func_node) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_yield(valid_yield_node) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_return(valid_yield_node) ( missing_yield_type_func_node, missing_yield_type_yield_node) = astroid.extract_node(""" class Test(python_utils.OBJECT): def __init__(self, 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 yield result #@ """) with self.checker_test_object.assertAddsMessages( testutils.Message( msg_id='redundant-returns-doc', node=missing_yield_type_func_node ), ): self.checker_test_object.checker.visit_functiondef( missing_yield_type_func_node) with self.checker_test_object.assertAddsMessages( testutils.Message( msg_id='missing-yield-doc', node=missing_yield_type_func_node ), testutils.Message( msg_id='missing-yield-type-doc', node=missing_yield_type_func_node ), ): self.checker_test_object.checker.visit_yieldfrom( missing_yield_type_yield_node) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_return( missing_yield_type_yield_node) ( missing_return_type_func_node, missing_return_type_return_node) = astroid.extract_node(""" class Test(python_utils.OBJECT): def __init__(self, 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. Yields: int. The test result. \"\"\" result = test_var_one + test_var_two return result #@ """) with self.checker_test_object.assertAddsMessages( testutils.Message( msg_id='redundant-yields-doc', node=missing_return_type_func_node ), ): self.checker_test_object.checker.visit_functiondef( missing_return_type_func_node) with self.checker_test_object.assertAddsMessages( testutils.Message( msg_id='missing-return-doc', node=missing_return_type_func_node ), testutils.Message( msg_id='missing-return-type-doc', node=missing_return_type_func_node ), ): self.checker_test_object.checker.visit_return( missing_return_type_return_node) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_yield( missing_return_type_return_node) valid_raise_node = astroid.extract_node(""" def func(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. Raises: Exception: An exception. \"\"\" raise Exception #@ """) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_raise(valid_raise_node) ( missing_raise_type_func_node, missing_raise_type_raise_node) = astroid.extract_node(""" def func(test_var_one, test_var_two): #@ \"\"\"Function to test raising exceptions. Args: test_var_one: int. First test variable. test_var_two: str. Second test variable. \"\"\" raise Exception #@ """) with self.checker_test_object.assertAddsMessages( testutils.Message( msg_id='missing-raises-doc', args=('Exception',), node=missing_raise_type_func_node ), ): self.checker_test_object.checker.visit_raise( missing_raise_type_raise_node) valid_raise_node = astroid.extract_node(""" class Test(python_utils.OBJECT): raise Exception #@ """) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_raise(valid_raise_node) valid_raise_node = astroid.extract_node(""" class Test(): @property def decorator_func(self): pass @decorator_func.setter @property def func(self): raise Exception #@ """) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_raise(valid_raise_node) valid_raise_node = astroid.extract_node(""" class Test(): def func(self): raise Exception #@ """) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_raise(valid_raise_node) valid_raise_node = astroid.extract_node(""" def func(): try: raise Exception #@ except Exception: pass """) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_raise(valid_raise_node) valid_raise_node = astroid.extract_node(""" def func(): \"\"\"Function to test raising exceptions.\"\"\" raise Exception #@ """) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_raise(valid_raise_node) valid_raise_node = astroid.extract_node(""" def my_func(self): \"\"\"This is a docstring. :raises NameError: Never. \"\"\" def ex_func(val): return RuntimeError(val) raise ex_func('hi') #@ raise NameError('hi') """) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_raise(valid_raise_node) valid_raise_node = astroid.extract_node(""" from unknown import Unknown def my_func(self): \"\"\"This is a docstring. :raises NameError: Never. \"\"\" raise Unknown('hi') #@ raise NameError('hi') """) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_raise(valid_raise_node) valid_raise_node = astroid.extract_node(""" def my_func(self): \"\"\"This is a docstring. :raises NameError: Never. \"\"\" def ex_func(val): def inner_func(value): return OSError(value) return RuntimeError(val) raise ex_func('hi') #@ raise NameError('hi') """) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_raise(valid_raise_node) valid_return_node = astroid.extract_node(""" def func(): \"\"\"Function to test return values.\"\"\" return None #@ """) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_return(valid_return_node) valid_return_node = astroid.extract_node(""" def func(): \"\"\"Function to test return values.\"\"\" return #@ """) with self.checker_test_object.assertNoMessages(): self.checker_test_object.checker.visit_return(valid_return_node) missing_param_func_node = astroid.extract_node(""" def func(test_var_one, test_var_two, *args, **kwargs): #@ \"\"\"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 self.checker_test_object.assertAddsMessages( testutils.Message( msg_id='missing-param-doc', node=missing_param_func_node, args=('args, kwargs',), ), ): self.checker_test_object.checker.visit_functiondef( missing_param_func_node) missing_param_func_node = astroid.extract_node(""" def func(test_var_one, test_var_two): #@ \"\"\"Function to test docstring parameters. Args: test_var_one: int. First test variable. invalid_var_name: str. Second test variable. Returns: int. The test result. \"\"\" result = test_var_one + test_var_two return result """) with self.checker_test_object.assertAddsMessages( testutils.Message( msg_id='missing-param-doc', node=missing_param_func_node, args=('test_var_two',), ), testutils.Message( msg_id='missing-type-doc', node=missing_param_func_node, args=('test_var_two',), ), testutils.Message( msg_id='differing-param-doc', node=missing_param_func_node, args=('invalid_var_name',), ), testutils.Message( msg_id='differing-type-doc', node=missing_param_func_node, args=('invalid_var_name',), ), ): self.checker_test_object.checker.visit_functiondef( missing_param_func_node) class_node, multiple_constructor_func_node = astroid.extract_node(""" class Test(): #@ \"\"\"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. \"\"\" def __init__(self, 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 self.checker_test_object.assertAddsMessages( testutils.Message( msg_id='multiple-constructor-doc', node=class_node, args=(class_node.name,), ), ): self.checker_test_object.checker.visit_functiondef( multiple_constructor_func_node)
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, func_call_node_four, func_call_node_five, func_call_node_six, class_call_node) = astroid.extract_node(""" class TestClass(): pass 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 def test_1(test_var_one, test_var_one): pass def test_2((a, b)): pass test(2, 5, test_var_three=6) #@ test(2) #@ test(2, 6, test_var_two=5, test_var_four="test_checker") #@ max(5, 1) #@ test_1(1, 2) #@ test_2((1, 2)) #@ TestClass() #@ """) 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) with checker_test_object.assertNoMessages(): checker_test_object.checker.visit_call(class_call_node) with checker_test_object.assertNoMessages(): checker_test_object.checker.visit_call(func_call_node_four) with checker_test_object.assertNoMessages(): checker_test_object.checker.visit_call(func_call_node_five) with checker_test_object.assertNoMessages(): checker_test_object.checker.visit_call(func_call_node_six)
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 python_utils.open_file(filename, 'w') as tmp: tmp.write( u"""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_with_no_error_message = astroid.scoped_nodes.Module( name='test', doc='Custom test') temp_file = tempfile.NamedTemporaryFile() filename = temp_file.name with python_utils.open_file(filename, 'w') as tmp: tmp.write( u"""\"""Some multiline docstring. \""" # Load JSON. master_translation_dict = json.loads( utils.get_file_contents(os.path.join( os.getcwd(), 'assets', 'i18n', 'en.json'))) """) node_with_no_error_message.file = filename node_with_no_error_message.path = filename checker_test_object.checker.process_module(node_with_no_error_message) with checker_test_object.assertNoMessages(): temp_file.close() node_with_no_error_message = astroid.scoped_nodes.Module( name='test', doc='Custom test') temp_file = tempfile.NamedTemporaryFile() filename = temp_file.name with python_utils.open_file(filename, 'w') as tmp: tmp.write( u"""self.post_json('/', self.payload, expect_errors=True, expected_status_int=401)""") node_with_no_error_message.file = filename node_with_no_error_message.path = filename checker_test_object.checker.process_module(node_with_no_error_message) with checker_test_object.assertNoMessages(): temp_file.close()
def test_checks_excessive_empty_lines(self): checker_test_object = testutils.CheckerTestCase() checker_test_object.CHECKER_CLASS = ( pylint_extensions.ExcessiveEmptyLinesChecker) checker_test_object.setup_method() node_excessive_empty_lines = astroid.scoped_nodes.Module( name='test', doc='Custom test') temp_file = tempfile.NamedTemporaryFile() filename = temp_file.name with python_utils.open_file(filename, 'w') as tmp: tmp.write( u"""def func1(): returns_something def func2(): returns_something """) node_excessive_empty_lines.file = filename node_excessive_empty_lines.path = filename checker_test_object.checker.process_module(node_excessive_empty_lines) with checker_test_object.assertAddsMessages( testutils.Message( msg_id='excessive-new-lines', line=5 ), ): temp_file.close() node_method_with_decorator = astroid.scoped_nodes.Module( name='test', doc='Custom test') temp_file = tempfile.NamedTemporaryFile() filename = temp_file.name with python_utils.open_file(filename, 'w') as tmp: tmp.write( u"""def func0(): returns_something @something def func1(): returns_something """) node_method_with_decorator.file = filename node_method_with_decorator.path = filename checker_test_object.checker.process_module(node_method_with_decorator) with checker_test_object.assertAddsMessages( testutils.Message( msg_id='excessive-new-lines', line=5 ), ): temp_file.close() node_with_no_error_message = astroid.scoped_nodes.Module( name='test', doc='Custom test') temp_file = tempfile.NamedTemporaryFile() filename = temp_file.name with python_utils.open_file(filename, 'w') as tmp: tmp.write( u"""def func0(): returns_something def func1(): returns_something def func2(): returns_something @something def func3(): returns_something """) node_with_no_error_message.file = filename node_with_no_error_message.path = filename checker_test_object.checker.process_module(node_with_no_error_message) with checker_test_object.assertNoMessages(): temp_file.close()
def test_native_string(self): arg = "val = 'abc'" tokens = self.tokenize(arg) with self.assertAddsMessages(testutils.Message('native-string', line=1)): self.checker.process_tokens(tokens)
def test_division(self): node = astroid.extract_node('3 / 2 #@') message = testutils.Message('old-division', node=node) with self.assertAddsMessages(message): self.checker.visit_binop(node)
def test_relative_from_import(self): node = test_utils.extract_node('from os import path #@') message = testutils.Message('no-absolute-import', node=node) with self.assertAddsMessages(message): self.checker.visit_import(node)
def check_bad_builtin(self, builtin_name): node = astroid.extract_node(builtin_name + ' #@') message = builtin_name.lower() + '-builtin' with self.assertAddsMessages(testutils.Message(message, node=node)): self.checker.visit_name(node)
def test_dict_iter_method(self): for meth in ('keys', 'values', 'items'): node = test_utils.extract_node('x.iter%s() #@' % meth) message = testutils.Message('dict-iter-method', node=node) with self.assertAddsMessages(message): self.checker.visit_callfunc(node)
def test_dict_view_method_on_dict(self): node = astroid.extract_node('{}.viewkeys()') message = testutils.Message('dict-view-method', node=node) with self.assertAddsMessages(message): self.checker.visit_call(node)
def test_dict_iter_method_on_dict(self): node = test_utils.extract_node('{}.iterkeys()') message = testutils.Message('dict-iter-method', node=node) with self.assertAddsMessages(message): self.checker.visit_callfunc(node)
def test_parameter_unpacking(self): node = astroid.extract_node('def func((a, b)):#@\n pass') arg = node.args.args[0] with self.assertAddsMessages(testutils.Message('parameter-unpacking', node=arg)): self.checker.visit_arguments(node.args)
def test_dict_view_method(self): for meth in ("keys", "values", "items"): node = astroid.extract_node("x.view%s() #@" % meth) message = testutils.Message("dict-view-method", node=node) with self.assertAddsMessages(message): self.checker.visit_call(node)
def test_invalid_open_codec(self): node = astroid.extract_node('open(foobar, encoding="hex") #@') message = testutils.Message('invalid-str-codec', node=node) with self.assertAddsMessages(message): self.checker.visit_call(node)
def test_invalid_codec(self): node = astroid.extract_node('foobar.encode("hex") #@') message = testutils.Message("invalid-str-codec", node=node) with self.assertAddsMessages(message): self.checker.visit_call(node)
def _test_token_message(self, code, symbolic_message): tokens = testutils._tokenize_str(code) message = testutils.Message(symbolic_message, line=1) with self.assertAddsMessages(message): self.checker.process_tokens(tokens)
def test_relative_import(self): node = astroid.extract_node('import string #@') message = testutils.Message('no-absolute-import', node=node) with self.assertAddsMessages(message): self.checker.visit_import(node)