示例#1
0
 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()))
示例#2
0
 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)
示例#3
0
 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)
示例#4
0
  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))
示例#5
0
  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))
# 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):
示例#7
0
 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)
示例#8
0
 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')
# 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):
示例#10
0
 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')