Exemple #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()))
Exemple #2
0
 def create_python_file(self, file_content):
     if self.file_required:
         tmpdir = safe_mkdtemp()
         with open(os.path.join(tmpdir, 'file.py'), 'w') as fp:
             fp.write(file_content)
             fp.close()
             return PythonFile.parse('file.py', root=tmpdir)
     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'), 'w') as fp:
       fp.write(file_content)
       fp.close()
       return PythonFile.parse('file.py', root=tmpdir)
   else:
     return PythonFile.from_statement(file_content)
Exemple #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))
Exemple #5
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')
Exemple #6
0
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from pants_test.contrib.python.checks.checker.plugin_test_base import CheckstylePluginTestBase

from pants.contrib.python.checks.checker.common import Nit, PythonFile
from pants.contrib.python.checks.checker.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):
        self.exemplar_fail(
            "T603",
            Nit.ERROR,
            """
# coding=utf-8
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import absolute_import, division, print_function, unicode_literals

from pants_test.contrib.python.checks.checker.plugin_test_base import CheckstylePluginTestBase

from pants.contrib.python.checks.checker.common import Nit, PythonFile
from pants.contrib.python.checks.checker.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):