def test_syntax_error_in_parsing(self): with self.assertRaises(CheckSyntaxError) as cm: PythonFile.from_statement("""print('unfinished""",'myfile.py') self.assertEqual( """E901:ERROR myfile.py:001 SyntaxError: EOL while scanning string literal\n""" """ |print('unfinished""", str(cm.exception.as_nit()))
def create_python_file(self, file_content): if self.file_required: tmpdir = safe_mkdtemp() with open(os.path.join(tmpdir, 'file.py'), 'wb') as fp: fp.write(file_content) fp.close() return PythonFile.parse(fp.name) else: return PythonFile.from_statement(file_content)
def create_python_file(self, file_content): if self.file_required: tmpdir = safe_mkdtemp() with open(os.path.join(tmpdir, 'file.py'), 'wb') as fp: fp.write(file_content) fp.close() return PythonFile.parse(fp.name) else: return PythonFile.from_statement(file_content)
def check_file(self, filename): """Process python file looking for indications of problems. :param filename: (str) Python source filename :return: (bool) flag indicating failure """ try: python_file = PythonFile.parse(filename) except SyntaxError as e: print('{filename}:SyntaxError: {error}'.format(filename=filename, error=e)) return True # If the user specifies an invalid severity use comment. severity = Nit.SEVERITY.get(self.options.severity, Nit.COMMENT) should_fail = False fail_threshold = Nit.WARNING if self.options.strict else Nit.ERROR for i, nit in enumerate(self.get_nits(python_file)): if i == 0: print( ) # Add an extra newline to clean up the output only if we have nits. if nit.severity >= severity: print('{nit}\n'.format(nit=nit)) should_fail |= (nit.severity >= fail_threshold) return should_fail
def check_file(self, filename): """Process python file looking for indications of problems. :param filename: (str) Python source filename :return: (int) number of failures """ try: python_file = PythonFile.parse(os.path.join(get_buildroot(), filename)) except SyntaxError as e: print('{filename}:SyntaxError: {error}'.format(filename=filename, error=e)) return 1 # If the user specifies an invalid severity use comment. severity = Nit.SEVERITY.get(self.options.severity, Nit.COMMENT) failure_count = 0 fail_threshold = Nit.WARNING if self.options.strict else Nit.ERROR for i, nit in enumerate(self.get_nits(python_file)): if i == 0: print() # Add an extra newline to clean up the output only if we have nits. if nit.severity >= severity: print('{nit}\n'.format(nit=nit)) if nit.severity >= fail_threshold: failure_count += 1 return failure_count
def check_file(self, filename): """Process python file looking for indications of problems. :param filename: (str) Python source filename :return: (int) number of failures """ try: python_file = PythonFile.parse( os.path.join(get_buildroot(), filename)) except SyntaxError as e: print('{filename}:SyntaxError: {error}'.format(filename=filename, error=e)) return 1 # If the user specifies an invalid severity use comment. severity = Nit.SEVERITY.get(self.options.severity, Nit.COMMENT) failure_count = 0 fail_threshold = Nit.WARNING if self.options.strict else Nit.ERROR for i, nit in enumerate(self.get_nits(python_file)): if i == 0: print( ) # Add an extra newline to clean up the output only if we have nits. if nit.severity >= severity: print('{nit}\n'.format(nit=nit)) if nit.severity >= fail_threshold: failure_count += 1 return failure_count
def check_file(self, filename): """Process python file looking for indications of problems. :param filename: (str) Python source filename :return: (bool) flag indicating failure """ try: python_file = PythonFile.parse(filename) except SyntaxError as e: print('{filename}:SyntaxError: {error}'.format(filename=filename, error=e)) return True # If the user specifies an invalid severity use comment. severity = Nit.SEVERITY.get(self.options.severity, Nit.COMMENT) should_fail = False fail_threshold = Nit.WARNING if self.options.strict else Nit.ERROR for i, nit in enumerate(self.get_nits(python_file)): if i == 0: print() # Add an extra newline to clean up the output only if we have nits. if nit.severity >= severity: print('{nit}\n'.format(nit=nit)) should_fail |= (nit.severity >= fail_threshold) return should_fail
def get_nits(self, filename): """Iterate over the instances style checker and yield Nits. :param filename: str pointing to a file within the buildroot. """ try: python_file = PythonFile.parse(filename, root=get_buildroot()) except CheckSyntaxError as e: yield e.as_nit() return if noqa_file_filter(python_file): return if self.options.suppress: # Filter out any suppressed plugins check_plugins = [plugin for plugin in self._plugins if self.excluder.should_include(filename, plugin.name)] else: check_plugins = self._plugins for plugin in check_plugins: for i, nit in enumerate(plugin.checker(python_file)): if i == 0: # NB: Add debug log header for nits from each plugin, but only if there are nits from it. self.context.log.debug('Nits from plugin {} for {}'.format(plugin.name, filename)) if not nit.has_lines_to_display: yield nit continue if all(not line_contains_noqa(line) for line in nit.lines): yield nit
def get_nits(self, filename): """Iterate over the instances style checker and yield Nits. :param filename: str pointing to a file within the buildroot. """ try: python_file = PythonFile.parse(filename, root=get_buildroot()) except CheckSyntaxError as e: yield e.as_nit() return if noqa_file_filter(python_file): return if self.options.suppress: # Filter out any suppressed plugins check_plugins = [plugin for plugin in self._plugins if self.excluder.should_include(filename, plugin.name)] else: check_plugins = self._plugins for plugin in check_plugins: for i, nit in enumerate(plugin.checker(python_file)): if i == 0: # NB: Add debug log header for nits from each plugin, but only if there are nits from it. self.context.log.debug('Nits from plugin {} for {}'.format(plugin.name, filename)) if not nit.has_lines_to_display: yield nit continue if all(not line_contains_noqa(line) for line in nit.lines): yield nit
def test_style_error(self): """Test error with actual AST node. Verify that when we fetch a node form AST and create an error we get the same result as generating the error manually. """ plugin = MinimalCheckstylePlugin({}, PythonFile.from_statement(FILE_TEXT)) import_from = None for node in ast.walk(self._python_file_for_testing().tree): if isinstance(node, ast.ImportFrom): import_from = node ast_error = plugin.error('B380', "I don't like your from import!", import_from) error = plugin.error('B380', "I don't like your from import!", 2) self.assertEqual(str(ast_error), str(error))
def test_style_error(self): """Test error with actual AST node. Verify that when we fetch a node form AST and create an error we get the same result as generating the error manually. """ plugin = MinimalCheckstylePlugin({}, PythonFile.from_statement(FILE_TEXT)) import_from = None for node in ast.walk(self._python_file_for_testing().tree): if isinstance(node, ast.ImportFrom): import_from = node ast_error = plugin.error('B380', "I don't like your from import!", import_from) error = plugin.error('B380', "I don't like your from import!", 2) self.assertEqual(str(ast_error), str(error))
def _python_file_for_testing(self): """Pytest Fixture to create a test python file from statement.""" return PythonFile.from_statement(self._statement_for_testing(), 'keeper.py')
def test_python_file_absolute_path_and_root_fails(self): with self.assertRaises(ValueError): PythonFile.parse('/absolute/dir', root='/other/abs/dir')
def _python_file_for_testing(self): """Pytest Fixture to create a test python file from statement.""" return PythonFile.from_statement(self._statement_for_testing(), 'keeper.py')
def init_file(self, filename, lines, expected, line_offset): super(PantsReporter, self).init_file(filename, lines, expected, line_offset) self._python_file = PythonFile.parse(filename) self._errors = []
def init_file(self, filename, lines, expected, line_offset): super(PantsReporter, self).init_file(filename, lines, expected, line_offset) self._python_file = PythonFile.parse(filename) self._errors = []
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md). # Licensed under the Apache License, Version 2.0 (see LICENSE). from __future__ import (absolute_import, division, generators, nested_scopes, print_function, unicode_literals, with_statement) from pants_test.contrib.python.checks.tasks.checkstyle.plugin_test_base import \ CheckstylePluginTestBase from pants.contrib.python.checks.tasks.checkstyle.common import Nit, PythonFile from pants.contrib.python.checks.tasks.checkstyle.future_compatibility import FutureCompatibility BAD_CLASS = PythonFile.from_statement(""" class Distiller(object): CONSTANT = "foo" def foo(self, value): return os.path.join(Distiller.CONSTANT, value) """) class FutureCompatibilityTest(CheckstylePluginTestBase): plugin_type = FutureCompatibility def exemplar_fail(self, code, severity, statement): self.assertNit(statement, code, severity) def exemplar_pass(self, statement): self.assertNoNits(statement) def test_xrange(self):
def get_plugin(self, file_content, **options): python_file = PythonFile.from_statement(file_content) full_options = copy.copy(options) full_options['skip'] = False options_object = create_options({'foo': full_options}).for_scope('foo') return self.plugin_type(options_object, python_file)
def test_python_file_absolute_path_and_root_fails(self): with self.assertRaises(ValueError): PythonFile.parse('/absolute/dir', root='/other/abs/dir')
# Licensed under the Apache License, Version 2.0 (see LICENSE). from __future__ import (absolute_import, division, generators, nested_scopes, print_function, unicode_literals, with_statement) from pants_test.contrib.python.checks.tasks.checkstyle.plugin_test_base import \ CheckstylePluginTestBase from pants.contrib.python.checks.tasks.checkstyle.common import Nit, PythonFile from pants.contrib.python.checks.tasks.checkstyle.future_compatibility import FutureCompatibility BAD_CLASS = PythonFile.from_statement(""" class Distiller(object): CONSTANT = "foo" def foo(self, value): return os.path.join(Distiller.CONSTANT, value) """) class FutureCompatibilityTest(CheckstylePluginTestBase): plugin_type = FutureCompatibility def exemplar_fail(self, code, severity, statement): self.assertNit(statement, code, severity) def exemplar_pass(self, statement): self.assertNoNits(statement) def test_xrange(self):