import sys
import os
from os.path import abspath, join

from logilab.common.testlib import TestCase, unittest_main

from utils import TestReporter

from pylint.lint import PyLinter
from pylint import checkers

test_reporter = TestReporter()
linter = PyLinter()
linter.set_reporter(test_reporter)
linter.disable_message_category('I')
linter.config.persistent = 0
checkers.initialize(linter)

sys.path.insert(1, abspath('regrtest_data'))


class NonRegrTC(TestCase):
    def setUp(self):
        """call reporter.finalize() to cleanup
        pending messages if a test finished badly
        """
        linter.reporter.finalize()

    def test_package___path___manipulation(self):
        linter.check('package.__init__')
Ejemplo n.º 2
0
import sys
import os
from os.path import abspath, join

from logilab.common.testlib import TestCase, unittest_main

from utils import TestReporter

from pylint.lint import PyLinter
from pylint import checkers

test_reporter = TestReporter()
linter = PyLinter()
linter.set_reporter(test_reporter)
linter.disable_message_category('I')
linter.config.persistent = 0
checkers.initialize(linter)

sys.path.insert(1, abspath('regrtest_data'))

class NonRegrTC(TestCase):
    def setUp(self):
        """call reporter.finalize() to cleanup
        pending messages if a test finished badly
        """
        linter.reporter.finalize()
        
    def test_package___path___manipulation(self):
        linter.check('package.__init__')
        got = linter.reporter.finalize().strip()
Ejemplo n.º 3
0
class PyLinterTC(TestCase):
    def setUp(self):
        self.linter = PyLinter()
        self.linter.disable_message_category("I")
        self.linter.config.persistent = 0
        # register checkers
        checkers.initialize(self.linter)

    def test_message_help(self):
        msg = self.linter.get_message_help("F0001", checkerref=True)
        expected = ":F0001:\n  Used when an error occurred preventing the analysis of a module (unable to\n  find it for instance). This message belongs to the master checker."
        self.assertTextEqual(msg, expected)
        self.assertRaises(UnknownMessage, self.linter.get_message_help, "YB12")

    def test_enable_message(self):
        linter = self.linter
        linter.open()
        linter.set_current_module("toto")
        self.assert_(linter.is_message_enabled("W0101"))
        self.assert_(linter.is_message_enabled("W0102"))
        linter.disable_message("W0101", scope="package")
        linter.disable_message("W0102", scope="module", line=1)
        self.assert_(not linter.is_message_enabled("W0101"))
        self.assert_(not linter.is_message_enabled("W0102", 1))
        linter.set_current_module("tutu")
        self.assert_(not linter.is_message_enabled("W0101"))
        self.assert_(linter.is_message_enabled("W0102"))
        linter.enable_message("W0101", scope="package")
        linter.enable_message("W0102", scope="module", line=1)
        self.assert_(linter.is_message_enabled("W0101"))
        self.assert_(linter.is_message_enabled("W0102", 1))

    def test_enable_message_category(self):
        linter = self.linter
        linter.open()
        linter.set_current_module("toto")
        self.assert_(linter.is_message_enabled("W0101"))
        self.assert_(linter.is_message_enabled("R0102"))
        linter.disable_message_category("W", scope="package")
        linter.disable_message_category("R", scope="module")
        self.assert_(not linter.is_message_enabled("W0101"))
        self.assert_(not linter.is_message_enabled("R0102"))
        linter.set_current_module("tutu")
        self.assert_(not linter.is_message_enabled("W0101"))
        self.assert_(linter.is_message_enabled("R0102"))
        linter.enable_message_category("W", scope="package")
        linter.enable_message_category("R", scope="module")
        self.assert_(linter.is_message_enabled("W0101"))
        self.assert_(linter.is_message_enabled("R0102"))

    def test_enable_message_block(self):
        linter = self.linter
        linter.open()
        filepath = join(INPUTDIR, "func_block_disable_msg.py")
        linter.set_current_module("func_block_disable_msg")
        linter.process_module(open(filepath))
        orig_state = linter._module_msgs_state.copy()
        linter._module_msgs_state = {}
        linter.collect_block_lines(linter.get_astng(filepath, "func_block_disable_msg"), orig_state)
        # global (module level)
        self.assert_(linter.is_message_enabled("W0613"))
        self.assert_(linter.is_message_enabled("E1101"))
        # meth1
        self.assert_(linter.is_message_enabled("W0613", 13))
        # meth2
        self.assert_(not linter.is_message_enabled("W0613", 18))
        # meth3
        self.assert_(not linter.is_message_enabled("E1101", 24))
        self.assert_(linter.is_message_enabled("E1101", 26))
        # meth4
        self.assert_(not linter.is_message_enabled("E1101", 32))
        self.assert_(linter.is_message_enabled("E1101", 36))
        # meth5
        self.assert_(not linter.is_message_enabled("E1101", 42))
        self.assert_(not linter.is_message_enabled("E1101", 43))
        self.assert_(linter.is_message_enabled("E1101", 46))
        self.assert_(not linter.is_message_enabled("E1101", 49))
        self.assert_(not linter.is_message_enabled("E1101", 51))
        # meth6
        self.assert_(not linter.is_message_enabled("E1101", 57))
        self.assert_(linter.is_message_enabled("E1101", 61))
        self.assert_(not linter.is_message_enabled("E1101", 64))
        self.assert_(not linter.is_message_enabled("E1101", 66))

        self.assert_(linter.is_message_enabled("E0602", 57))
        self.assert_(linter.is_message_enabled("E0602", 61))
        self.assert_(not linter.is_message_enabled("E0602", 62))
        self.assert_(linter.is_message_enabled("E0602", 64))
        self.assert_(linter.is_message_enabled("E0602", 66))
        # meth7
        self.assert_(not linter.is_message_enabled("E1101", 70))
        self.assert_(linter.is_message_enabled("E1101", 72))
        self.assert_(linter.is_message_enabled("E1101", 75))
        self.assert_(linter.is_message_enabled("E1101", 77))

    def test_list_messages(self):
        sys.stdout = StringIO()
        try:
            # just invoke it, don't check the output
            self.linter.list_messages()
        finally:
            sys.stdout = sys.__stdout__

    def test_lint_ext_module_with_file_output(self):
        self.linter.config.files_output = True
        try:
            self.linter.check("StringIO")
            self.assert_(os.path.exists("pylint_StringIO.txt"))
            self.assert_(os.path.exists("pylint_global.txt"))
        finally:
            try:
                os.remove("pylint_StringIO.txt")
                os.remove("pylint_global.txt")
            except:
                pass

    def test_enable_report(self):
        self.assertEquals(self.linter.is_report_enabled("R0001"), True)
        self.linter.disable_report("R0001")
        self.assertEquals(self.linter.is_report_enabled("R0001"), False)
        self.linter.enable_report("R0001")
        self.assertEquals(self.linter.is_report_enabled("R0001"), True)

    def test_set_option_1(self):
        linter = self.linter
        linter.set_option("disable-msg", "C0111,W0142")
        self.assert_(not linter.is_message_enabled("C0111"))
        self.assert_(not linter.is_message_enabled("W0142"))
        self.assert_(linter.is_message_enabled("W0113"))

    def test_set_option_2(self):
        linter = self.linter
        linter.set_option("disable-msg", ("C0111", "W0142"))
        self.assert_(not linter.is_message_enabled("C0111"))
        self.assert_(not linter.is_message_enabled("W0142"))
        self.assert_(linter.is_message_enabled("W0113"))

    def test_enable_checkers1(self):
        self.linter.enable_checkers(["design"], False)
        self.assertEquals(
            sorted([c.name for c in self.linter._checkers.values() if c.is_enabled()]),
            [
                "basic",
                "classes",
                "exceptions",
                "format",
                "imports",
                "logging",
                "master",
                "metrics",
                "miscellaneous",
                "newstyle",
                "similarities",
                "string_format",
                "typecheck",
                "variables",
            ],
        )

    def test_enable_checkers2(self):
        self.linter.enable_checkers(["design"], True)
        self.assertEquals(
            sorted([c.name for c in self.linter._checkers.values() if c.is_enabled()]), ["design", "master"]
        )
Ejemplo n.º 4
0
class PyLinterTC(TestCase):
    def setUp(self):
        self.linter = PyLinter()
        self.linter.disable_message_category('I')
        self.linter.config.persistent = 0
        # register checkers
        checkers.initialize(self.linter)

    def test_message_help(self):
        msg = self.linter.get_message_help('F0001', checkerref=True)
        expected = ':F0001:\n  Used when an error occurred preventing the analysis of a module (unable to\n  find it for instance). This message belongs to the master checker.'
        self.assertTextEqual(msg, expected)
        self.assertRaises(UnknownMessage, self.linter.get_message_help, 'YB12')

    def test_enable_message(self):
        linter = self.linter
        linter.open()
        linter.set_current_module('toto')
        self.assert_(linter.is_message_enabled('W0101'))
        self.assert_(linter.is_message_enabled('W0102'))
        linter.disable_message('W0101', scope='package')
        linter.disable_message('W0102', scope='module', line=1)
        self.assert_(not linter.is_message_enabled('W0101'))
        self.assert_(not linter.is_message_enabled('W0102', 1))
        linter.set_current_module('tutu')
        self.assert_(not linter.is_message_enabled('W0101'))
        self.assert_(linter.is_message_enabled('W0102'))
        linter.enable_message('W0101', scope='package')
        linter.enable_message('W0102', scope='module', line=1)
        self.assert_(linter.is_message_enabled('W0101'))
        self.assert_(linter.is_message_enabled('W0102', 1))

    def test_enable_message_category(self):
        linter = self.linter
        linter.open()
        linter.set_current_module('toto')
        self.assert_(linter.is_message_enabled('W0101'))
        self.assert_(linter.is_message_enabled('R0102'))
        linter.disable_message_category('W', scope='package')
        linter.disable_message_category('R', scope='module')
        self.assert_(not linter.is_message_enabled('W0101'))
        self.assert_(not linter.is_message_enabled('R0102'))
        linter.set_current_module('tutu')
        self.assert_(not linter.is_message_enabled('W0101'))
        self.assert_(linter.is_message_enabled('R0102'))
        linter.enable_message_category('W', scope='package')
        linter.enable_message_category('R', scope='module')
        self.assert_(linter.is_message_enabled('W0101'))
        self.assert_(linter.is_message_enabled('R0102'))

    def test_enable_message_block(self):
        linter = self.linter
        linter.open()
        filepath = join(INPUTDIR, 'func_block_disable_msg.py')
        linter.set_current_module('func_block_disable_msg')
        linter.process_module(open(filepath))
        orig_state = linter._module_msgs_state.copy()
        linter._module_msgs_state = {}
        linter.collect_block_lines(
            linter.get_astng(filepath, 'func_block_disable_msg'), orig_state)
        # global (module level)
        self.assert_(linter.is_message_enabled('W0613'))
        self.assert_(linter.is_message_enabled('E1101'))
        # meth1
        self.assert_(linter.is_message_enabled('W0613', 13))
        # meth2
        self.assert_(not linter.is_message_enabled('W0613', 18))
        # meth3
        self.assert_(not linter.is_message_enabled('E1101', 24))
        self.assert_(linter.is_message_enabled('E1101', 26))
        # meth4
        self.assert_(not linter.is_message_enabled('E1101', 32))
        self.assert_(linter.is_message_enabled('E1101', 36))
        # meth5
        self.assert_(not linter.is_message_enabled('E1101', 42))
        self.assert_(not linter.is_message_enabled('E1101', 43))
        self.assert_(linter.is_message_enabled('E1101', 46))
        self.assert_(not linter.is_message_enabled('E1101', 49))
        self.assert_(not linter.is_message_enabled('E1101', 51))
        # meth6
        self.assert_(not linter.is_message_enabled('E1101', 57))
        self.assert_(linter.is_message_enabled('E1101', 61))
        self.assert_(not linter.is_message_enabled('E1101', 64))
        self.assert_(not linter.is_message_enabled('E1101', 66))

        self.assert_(linter.is_message_enabled('E0602', 57))
        self.assert_(linter.is_message_enabled('E0602', 61))
        self.assert_(not linter.is_message_enabled('E0602', 62))
        self.assert_(linter.is_message_enabled('E0602', 64))
        self.assert_(linter.is_message_enabled('E0602', 66))
        # meth7
        self.assert_(not linter.is_message_enabled('E1101', 70))
        self.assert_(linter.is_message_enabled('E1101', 72))
        self.assert_(linter.is_message_enabled('E1101', 75))
        self.assert_(linter.is_message_enabled('E1101', 77))

    def test_list_messages(self):
        sys.stdout = StringIO()
        try:
            # just invoke it, don't check the output
            self.linter.list_messages()
        finally:
            sys.stdout = sys.__stdout__

    def test_lint_ext_module_with_file_output(self):
        self.linter.config.files_output = True
        try:
            self.linter.check('StringIO')
            self.assert_(os.path.exists('pylint_StringIO.txt'))
            self.assert_(os.path.exists('pylint_global.txt'))
        finally:
            try:
                os.remove('pylint_StringIO.txt')
                os.remove('pylint_global.txt')
            except:
                pass

    def test_enable_report(self):
        self.assertEquals(self.linter.is_report_enabled('R0001'), True)
        self.linter.disable_report('R0001')
        self.assertEquals(self.linter.is_report_enabled('R0001'), False)
        self.linter.enable_report('R0001')
        self.assertEquals(self.linter.is_report_enabled('R0001'), True)

    def test_set_option_1(self):
        linter = self.linter
        linter.set_option('disable-msg', 'C0111,W0142')
        self.assert_(not linter.is_message_enabled('C0111'))
        self.assert_(not linter.is_message_enabled('W0142'))
        self.assert_(linter.is_message_enabled('W0113'))

    def test_set_option_2(self):
        linter = self.linter
        linter.set_option('disable-msg', ('C0111', 'W0142'))
        self.assert_(not linter.is_message_enabled('C0111'))
        self.assert_(not linter.is_message_enabled('W0142'))
        self.assert_(linter.is_message_enabled('W0113'))

    def test_enable_checkers1(self):
        self.linter.enable_checkers(['design'], False)
        self.assertEquals(
            sorted([
                c.name for c in self.linter._checkers.values()
                if c.is_enabled()
            ]), [
                'basic', 'classes', 'exceptions', 'format', 'imports',
                'logging', 'master', 'metrics', 'miscellaneous', 'newstyle',
                'similarities', 'string_format', 'typecheck', 'variables'
            ])

    def test_enable_checkers2(self):
        self.linter.enable_checkers(['design'], True)
        self.assertEquals(
            sorted([
                c.name for c in self.linter._checkers.values()
                if c.is_enabled()
            ]), ['design', 'master'])
class PyLinterTC(TestCase):
    
    def setUp(self):
        self.linter = PyLinter()
        self.linter.disable_message_category('I')
        self.linter.config.persistent = 0
        # register checkers
        checkers.initialize(self.linter)
        
    def test_message_help(self):
        msg = self.linter.get_message_help('F0001', checkerref=True)
        expected = ':F0001:\n  Used when an error occured preventing the analysis of a module (unable to find\n  it for instance). This message belongs to the master checker.'
        self.assertTextEqual(msg, expected)
        self.assertRaises(UnknownMessage, self.linter.get_message_help, 'YB12')
        
    def test_enable_message(self):
        linter = self.linter
        linter.open()
        linter.set_current_module('toto')
        self.assert_(linter.is_message_enabled('W0101'))
        self.assert_(linter.is_message_enabled('W0102'))
        linter.disable_message('W0101', scope='package')
        linter.disable_message('W0102', scope='module', line=1)
        self.assert_(not linter.is_message_enabled('W0101'))
        self.assert_(not linter.is_message_enabled('W0102', 1))
        linter.set_current_module('tutu')
        self.assert_(not linter.is_message_enabled('W0101'))
        self.assert_(linter.is_message_enabled('W0102'))        
        linter.enable_message('W0101', scope='package')
        linter.enable_message('W0102', scope='module', line=1)
        self.assert_(linter.is_message_enabled('W0101'))
        self.assert_(linter.is_message_enabled('W0102', 1))

    def test_enable_message_category(self):
        linter = self.linter
        linter.open()
        linter.set_current_module('toto')
        self.assert_(linter.is_message_enabled('W0101'))
        self.assert_(linter.is_message_enabled('R0102'))
        linter.disable_message_category('W', scope='package')
        linter.disable_message_category('REFACTOR', scope='module')
        self.assert_(not linter.is_message_enabled('W0101'))
        self.assert_(not linter.is_message_enabled('R0102'))
        linter.set_current_module('tutu')
        self.assert_(not linter.is_message_enabled('W0101'))
        self.assert_(linter.is_message_enabled('R0102'))        
        linter.enable_message_category('WARNING', scope='package')
        linter.enable_message_category('R', scope='module')
        self.assert_(linter.is_message_enabled('W0101'))
        self.assert_(linter.is_message_enabled('R0102'))

    def test_enable_message_block(self):
        linter = self.linter
        linter.open()
        filepath = join(INPUTDIR, 'func_block_disable_msg.py')
        linter.set_current_module('func_block_disable_msg')
        linter.process_module(open(filepath))
        orig_state = linter._module_msgs_state.copy()
        linter._module_msgs_state = {}
        linter.collect_block_lines(linter.get_astng(filepath, 'func_block_disable_msg'), orig_state)
        # global (module level)
        self.assert_(linter.is_message_enabled('W0613'))
        self.assert_(linter.is_message_enabled('E1101'))
        # meth1
        self.assert_(linter.is_message_enabled('W0613', 13))
        # meth2
        self.assert_(not linter.is_message_enabled('W0613', 18))
        # meth3
        self.assert_(not linter.is_message_enabled('E1101', 24))
        self.assert_(linter.is_message_enabled('E1101', 26))
        # meth4        
        self.assert_(not linter.is_message_enabled('E1101', 32))
        self.assert_(linter.is_message_enabled('E1101', 36))
        # meth5
        self.assert_(not linter.is_message_enabled('E1101', 42))
        self.assert_(not linter.is_message_enabled('E1101', 43))
        self.assert_(linter.is_message_enabled('E1101', 46))
        self.assert_(not linter.is_message_enabled('E1101', 49))
        self.assert_(not linter.is_message_enabled('E1101', 51))
        # meth6
        self.assert_(not linter.is_message_enabled('E1101', 57))
        self.assert_(linter.is_message_enabled('E1101', 61))
        self.assert_(not linter.is_message_enabled('E1101', 64))
        self.assert_(not linter.is_message_enabled('E1101', 66))
        
        self.assert_(linter.is_message_enabled('E0602', 57))
        self.assert_(linter.is_message_enabled('E0602', 61))
        self.assert_(not linter.is_message_enabled('E0602', 62))
        self.assert_(linter.is_message_enabled('E0602', 64))
        self.assert_(linter.is_message_enabled('E0602', 66))
        # meth7
        self.assert_(not linter.is_message_enabled('E1101', 70))
        self.assert_(linter.is_message_enabled('E1101', 72))
        self.assert_(linter.is_message_enabled('E1101', 75))
        self.assert_(linter.is_message_enabled('E1101', 77))
        
    def test_list_messages(self):
        sys.stdout = StringIO()
        try:
            # just invoke it, don't check the output
            self.linter.list_messages()
        finally:
            sys.stdout = sys.__stdout__

    def test_lint_ext_module_with_file_output(self):
        self.linter.config.files_output = True
        try:
            self.linter.check('StringIO')
            self.assert_(os.path.exists('pylint_StringIO.txt'))
            self.assert_(os.path.exists('pylint_global.txt'))
        finally:
            try:
                os.remove('pylint_StringIO.txt')
                os.remove('pylint_global.txt')
            except:
                pass

    def test_enable_report(self):
        self.assertEquals(self.linter.is_report_enabled('R0001'), True)
        self.linter.disable_report('R0001')
        self.assertEquals(self.linter.is_report_enabled('R0001'), False)
        self.linter.enable_report('R0001')
        self.assertEquals(self.linter.is_report_enabled('R0001'), True)

    def test_set_option_1(self):
        linter = self.linter
        linter.set_option('disable-msg', 'C0111,W0142')
        self.assert_(not linter.is_message_enabled('C0111'))
        self.assert_(not linter.is_message_enabled('W0142'))
        self.assert_(linter.is_message_enabled('W0113'))

    def test_set_option_2(self):
        linter = self.linter
        linter.set_option('disable-msg', ('C0111', 'W0142') )
        self.assert_(not linter.is_message_enabled('C0111'))
        self.assert_(not linter.is_message_enabled('W0142'))
        self.assert_(linter.is_message_enabled('W0113'))

    def test_enable_checkers1(self):
        self.linter.enable_checkers(['design'], False)
        self.assertEquals(sorted([c.name for c in self.linter._checkers.values()
                                  if c.is_enabled()]),
                          ['basic', 'classes', 'exceptions', 'format', 'imports',
                           'master', 'metrics', 'miscellaneous', 'newstyle',
                           'similarities', 'typecheck', 'variables'])
        
    def test_enable_checkers2(self):
        self.linter.enable_checkers(['design'], True)
        self.assertEquals(sorted([c.name for c in self.linter._checkers.values()
                           if c.is_enabled()]),
                          ['design', 'master'])