Example #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()))
Example #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)
Example #3
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)
Example #4
0
  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=self._root_dir)
    except CheckSyntaxError as e:
      yield e.as_nit()
      return

    if noqa_file_filter(python_file):
      return

    if self._excluder:
      # Filter out any suppressed plugins
      check_plugins = [(plugin_name, plugin_factory)
                       for plugin_name, plugin_factory in self._plugin_factories.items()
                       if self._excluder.should_include(filename, plugin_name)]
    else:
      check_plugins = self._plugin_factories.items()

    for plugin_name, plugin_factory in check_plugins:
      for i, nit in enumerate(plugin_factory(python_file)):
        if i == 0:
          # NB: Add debug log header for nits from each plugin, but only if there are nits from it.
          self.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
Example #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))
    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=self._root_dir)
        except CheckSyntaxError as e:
            yield e.as_nit()
            return

        if noqa_file_filter(python_file):
            return

        if self._excluder:
            # Filter out any suppressed plugins
            check_plugins = [
                (plugin_name, plugin_factory) for plugin_name, plugin_factory
                in self._plugin_factories.items()
                if self._excluder.should_include(filename, plugin_name)
            ]
        else:
            check_plugins = self._plugin_factories.items()

        for plugin_name, plugin_factory in check_plugins:
            for i, nit in enumerate(plugin_factory(python_file)):
                if i == 0:
                    # NB: Add debug log header for nits from each plugin, but only if there are nits from it.
                    self.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
Example #7
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')
Example #8
0
 def test_python_file_absolute_path_and_root_fails(self):
   with self.assertRaises(ValueError):
     PythonFile.parse('/absolute/dir', root='/other/abs/dir')
Example #9
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):