summaryrefslogtreecommitdiff
path: root/testutils.py
diff options
context:
space:
mode:
authorDan Goldsmith <djgoldsmith@googlemail.com>2014-04-11 16:22:29 +0100
committerDan Goldsmith <djgoldsmith@googlemail.com>2014-04-11 16:22:29 +0100
commitfe36ec0486cfc9aba10d8d87b0f42077ad1e78db (patch)
tree046fbf0baf2ae392120ed930aeafe63a6df83cb2 /testutils.py
parent3804162e9a3f0e320867ffb45e09a43f9d0156c2 (diff)
parent3965c47e53a1c06c709048d932fe01dcf947c032 (diff)
downloadpylint-fe36ec0486cfc9aba10d8d87b0f42077ad1e78db.tar.gz
Merge with trunk
Diffstat (limited to 'testutils.py')
-rw-r--r--testutils.py68
1 files changed, 53 insertions, 15 deletions
diff --git a/testutils.py b/testutils.py
index a61fa7f..1658e10 100644
--- a/testutils.py
+++ b/testutils.py
@@ -12,17 +12,19 @@
#
# 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.,
-# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""functional/non regression tests for pylint"""
+from __future__ import with_statement
import collections
import contextlib
+import functools
import sys
import re
from glob import glob
from os import linesep
-from os.path import abspath, dirname, join, basename, splitext
+from os.path import abspath, basename, dirname, isdir, join, splitext
from cStringIO import StringIO
from logilab.common import testlib
@@ -75,7 +77,8 @@ def get_tests_info(input_dir, msg_dir, prefix, suffix):
if py_rest.isdigit() and SYS_VERS_STR >= py_rest:
break
else:
- outfile = None
+ # This will provide an error message indicating the missing filename.
+ outfile = join(msg_dir, fbase + '.txt')
result.append((infile, outfile))
return result
@@ -146,6 +149,23 @@ class UnittestLinter(object):
def add_stats(self, **kwargs):
for name, value in kwargs.iteritems():
self.stats[name] = value
+ return self.stats
+
+
+def set_config(**kwargs):
+ """Decorator for setting config values on a checker."""
+ def _Wrapper(fun):
+ @functools.wraps(fun)
+ def _Forward(self):
+ for key, value in kwargs.iteritems():
+ setattr(self.checker.config, key, value)
+ if isinstance(self, CheckerTestCase):
+ # reopen checker in case, it may be interested in configuration change
+ self.checker.open()
+ fun(self)
+
+ return _Forward
+ return _Wrapper
class CheckerTestCase(testlib.TestCase):
@@ -159,7 +179,6 @@ class CheckerTestCase(testlib.TestCase):
for key, value in self.CONFIG.iteritems():
setattr(self.checker.config, key, value)
self.checker.open()
- self.checker.stats = self.linter.stats
@contextlib.contextmanager
def assertNoMessages(self):
@@ -236,6 +255,9 @@ class LintTestUsingModule(testlib.TestCase):
for name, file in self.depends]
self._test(tocheck)
+ def _check_result(self, got):
+ self.assertMultiLineEqual(self._get_expected(), got)
+
def _test(self, tocheck):
if INFO_TEST_RGX.match(self.module):
self.linter.enable('I')
@@ -250,29 +272,44 @@ class LintTestUsingModule(testlib.TestCase):
print ex
ex.__str__ = exception_str
raise
- got = self.linter.reporter.finalize()
- self.assertMultiLineEqual(self._get_expected(), got)
+ self._check_result(self.linter.reporter.finalize())
+ def _has_output(self):
+ return not self.module.startswith('func_noerror_')
def _get_expected(self):
- if self.module.startswith('func_noerror_'):
- expected = ''
+ if self._has_output() and self.output:
+ with open(self.output, 'U') as fobj:
+ return fobj.read().strip() + '\n'
else:
- output = open(self.output, 'U')
- expected = output.read().strip() + '\n'
- output.close()
- return expected
+ return ''
class LintTestUsingFile(LintTestUsingModule):
_TEST_TYPE = 'file'
def test_functionality(self):
- tocheck = [join(self.INPUT_DIR, self.module + '.py')]
+ importable = join(self.INPUT_DIR, self.module)
+ # python also prefers packages over simple modules.
+ if not isdir(importable):
+ importable += '.py'
+ tocheck = [importable]
if self.depends:
tocheck += [join(self.INPUT_DIR, name) for name, _file in self.depends]
self._test(tocheck)
+class LintTestUpdate(LintTestUsingModule):
+
+ _TEST_TYPE = 'update'
+
+ def _check_result(self, got):
+ if self._has_output():
+ if got != self._get_expected():
+ if not self.output:
+ self.output = join(self.MSG_DIR, '%s.txt' % (self.module,))
+ with open(self.output, 'w') as fobj:
+ fobj.write(got)
+
# Callback
def cb_test_gen(base_class):
@@ -299,8 +336,9 @@ def make_tests(input_dir, msg_dir, filter_rgx, callbacks):
else:
is_to_run = lambda x: 1
tests = []
- for module_file, messages_file in get_tests_info(input_dir, msg_dir,
- 'func_', '.py'):
+ for module_file, messages_file in (
+ get_tests_info(input_dir, msg_dir, 'func_', '')
+ ):
if not is_to_run(module_file):
continue
base = module_file.replace('func_', '').replace('.py', '')