summaryrefslogtreecommitdiff
path: root/pylint/test/test_regr.py
blob: 94085c34a0bdfaec9bccf1c0939c1effdd9c9b23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Copyright (c) 2005-2014 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""non regression tests for pylint, which requires a too specific configuration
to be incorporated in the automatic functional test framework
"""

import sys
import platform
import os
from os.path import abspath, dirname, join
import unittest

from pylint.testutils import TestReporter
from pylint.lint import PyLinter
from pylint import checkers

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

REGR_DATA = join(dirname(abspath(__file__)), 'regrtest_data')
sys.path.insert(1, REGR_DATA)

class NonRegrTC(unittest.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()
        self.assertEqual(got, '')

    def test_package___init___precedence(self):
        linter.check('precedence_test')
        got = linter.reporter.finalize().strip()
        self.assertEqual(got, '')

    def test_check_package___init__(self):
        for variation in ('package.__init__', join(REGR_DATA, 'package', '__init__.py')):
            linter.check(variation)
            got = linter.reporter.finalize().strip()
            checked = list(linter.stats['by_module'].keys())
            self.assertEqual(checked, ['package.__init__'],
                             '%s: %s' % (variation, checked))
        cwd = os.getcwd()
        os.chdir(join(REGR_DATA, 'package'))
        sys.path.insert(0, '')
        try:
            for variation in ('__init__', '__init__.py'):
                linter.check(variation)
                got = linter.reporter.finalize().strip()
                checked = list(linter.stats['by_module'].keys())
                self.assertEqual(checked, ['__init__'],
                                 '%s: %s' % (variation, checked))
        finally:
            sys.path.pop(0)
            os.chdir(cwd)

    def test_numarray_inference(self):
        try:
            from numarray import random_array
        except ImportError:
            self.skipTest('test skipped: numarray.random_array is not available')
        linter.check(join(REGR_DATA, 'numarray_inf.py'))
        got = linter.reporter.finalize().strip()
        self.assertEqual(got, "E:  5: Instance of 'int' has no 'astype' member (but some types could not be inferred)")

    def test_numarray_import(self):
        try:
            import numarray
        except ImportError:
            self.skipTest('test skipped: numarray is not available')
        linter.check(join(REGR_DATA, 'numarray_import.py'))
        got = linter.reporter.finalize().strip()
        self.assertEqual(got, '')

    def test_class__doc__usage(self):
        linter.check(join(REGR_DATA, 'classdoc_usage.py'))
        got = linter.reporter.finalize().strip()
        self.assertEqual(got, '')

    def test_package_import_relative_subpackage_no_attribute_error(self):
        linter.check('import_package_subpackage_module')
        got = linter.reporter.finalize().strip()
        self.assertEqual(got, '')

    def test_import_assign_crash(self):
        linter.check(join(REGR_DATA, 'import_assign.py'))

    def test_special_attr_scope_lookup_crash(self):
        linter.check(join(REGR_DATA, 'special_attr_scope_lookup_crash.py'))

    def test_module_global_crash(self):
        linter.check(join(REGR_DATA, 'module_global.py'))
        got = linter.reporter.finalize().strip()
        self.assertEqual(got, '')

    def test_decimal_inference(self):
        linter.check(join(REGR_DATA, 'decimal_inference.py'))
        got = linter.reporter.finalize().strip()
        self.assertEqual(got, "")

    def test_descriptor_crash(self):
        for fname in os.listdir(REGR_DATA):
            if fname.endswith('_crash.py'):
                linter.check(join(REGR_DATA, fname))
                linter.reporter.finalize().strip()

    def test_try_finally_disable_msg_crash(self):
        linter.check(join(REGR_DATA, 'try_finally_disable_msg_crash'))

    def test___path__(self):
        linter.check('pylint.checkers.__init__')
        messages = linter.reporter.finalize().strip()
        self.assertFalse('__path__' in messages, messages)

    def test_absolute_import(self):
        linter.check(join(REGR_DATA, 'absimp', 'string.py'))
        got = linter.reporter.finalize().strip()
        self.assertEqual(got, "")

    def test_no_context_file(self):
        expected = "Unused import missing"
        linter.check(join(REGR_DATA, 'bad_package'))
        got = linter.reporter.finalize().strip()
        self.assertIn(expected, got)


if __name__ == '__main__':
    unittest.main()