diff options
Diffstat (limited to 'pylint/testutils.py')
-rw-r--r-- | pylint/testutils.py | 56 |
1 files changed, 32 insertions, 24 deletions
diff --git a/pylint/testutils.py b/pylint/testutils.py index 0ed4b96a4..f22b897e8 100644 --- a/pylint/testutils.py +++ b/pylint/testutils.py @@ -44,8 +44,8 @@ from pylint.lint import PyLinter # Utils -SYS_VERS_STR = '%d%d%d' % sys.version_info[:3] -TITLE_UNDERLINES = ['', '=', '-', '.'] +SYS_VERS_STR = "%d%d%d" % sys.version_info[:3] +TITLE_UNDERLINES = ["", "=", "-", "."] PREFIX = abspath(dirname(__file__)) PY3K = sys.version_info[0] == 3 @@ -62,28 +62,28 @@ def _get_tests_info(input_dir, msg_dir, prefix, suffix): lower versions -> message with highest num """ result = [] - for fname in glob(join(input_dir, prefix + '*' + suffix)): + for fname in glob(join(input_dir, prefix + "*" + suffix)): infile = basename(fname) fbase = splitext(infile)[0] # filter input files : - pyrestr = fbase.rsplit('_py', 1)[-1] # like _26 or 26 - if pyrestr.isdigit(): # '24', '25'... + pyrestr = fbase.rsplit("_py", 1)[-1] # like _26 or 26 + if pyrestr.isdigit(): # '24', '25'... if SYS_VERS_STR < pyrestr: continue - if pyrestr.startswith('_') and pyrestr[1:].isdigit(): + if pyrestr.startswith("_") and pyrestr[1:].isdigit(): # skip test for higher python versions if SYS_VERS_STR >= pyrestr[1:]: continue - messages = glob(join(msg_dir, fbase + '*.txt')) + messages = glob(join(msg_dir, fbase + "*.txt")) # the last one will be without ext, i.e. for all or upper versions: if messages: for outfile in sorted(messages, reverse=True): - py_rest = outfile.rsplit('_py', 1)[-1][:-4] + py_rest = outfile.rsplit("_py", 1)[-1][:-4] if py_rest.isdigit() and SYS_VERS_STR >= py_rest: break else: # This will provide an error message indicating the missing filename. - outfile = join(msg_dir, fbase + '.txt') + outfile = join(msg_dir, fbase + ".txt") result.append((infile, outfile)) return result @@ -93,7 +93,7 @@ class TestReporter(BaseReporter): __implements__ = IReporter - def __init__(self): # pylint: disable=super-init-not-called + def __init__(self): # pylint: disable=super-init-not-called self.message_ids = {} self.reset() @@ -111,13 +111,13 @@ class TestReporter(BaseReporter): msg = msg.msg self.message_ids[msg_id] = 1 if obj: - obj = ':%s' % obj + obj = ":%s" % obj sigle = msg_id[0] - if PY3K and linesep != '\n': + if PY3K and linesep != "\n": # 2to3 writes os.linesep instead of using # the previosly used line separators - msg = msg.replace('\r\n', '\n') - self.messages.append('%s:%3s%s: %s' % (sigle, line, obj, msg)) + msg = msg.replace("\r\n", "\n") + self.messages.append("%s:%3s%s: %s" % (sigle, line, obj, msg)) def finalize(self): self.messages.sort() @@ -130,6 +130,7 @@ class TestReporter(BaseReporter): # pylint: disable=unused-argument def on_set_current_module(self, module, filepath): pass + # pylint: enable=unused-argument def display_reports(self, layout): @@ -139,7 +140,6 @@ class TestReporter(BaseReporter): class MinimalTestReporter(BaseReporter): - def handle_message(self, msg): self.messages.append(msg) @@ -149,8 +149,9 @@ class MinimalTestReporter(BaseReporter): _display = None -class Message(collections.namedtuple('Message', - ['msg_id', 'line', 'node', 'args', 'confidence'])): +class Message( + collections.namedtuple("Message", ["msg_id", "line", "node", "args", "confidence"]) +): def __new__(cls, msg_id, line=None, node=None, args=None, confidence=None): return tuple.__new__(cls, (msg_id, line, node, args, confidence)) @@ -166,6 +167,7 @@ class Message(collections.namedtuple('Message', class UnittestLinter: """A fake linter class to capture checker messages.""" + # pylint: disable=unused-argument, no-self-use def __init__(self): @@ -178,8 +180,9 @@ class UnittestLinter: finally: self._messages = [] - def add_message(self, msg_id, line=None, node=None, args=None, confidence=None, - col_offset=None): + def add_message( + self, msg_id, line=None, node=None, args=None, confidence=None, col_offset=None + ): # Do not test col_offset for now since changing Message breaks everything self._messages.append(Message(msg_id, line, node, args, confidence)) @@ -195,8 +198,10 @@ class UnittestLinter: def options_providers(self): return linter.options_providers + def set_config(**kwargs): """Decorator for setting config values on a checker.""" + def _wrapper(fun): @functools.wraps(fun) def _forward(self): @@ -208,17 +213,19 @@ def set_config(**kwargs): fun(self) return _forward + return _wrapper class CheckerTestCase: """A base testcase class for unit testing individual checker classes.""" + CHECKER_CLASS = None CONFIG = {} def setup_method(self): self.linter = UnittestLinter() - self.checker = self.CHECKER_CLASS(self.linter) # pylint: disable=not-callable + self.checker = self.CHECKER_CLASS(self.linter) # pylint: disable=not-callable for key, value in self.CONFIG.items(): setattr(self.checker.config, key, value) self.checker.open() @@ -239,9 +246,10 @@ class CheckerTestCase: """ yield got = self.linter.release_messages() - msg = ('Expected messages did not match actual.\n' - 'Expected:\n%s\nGot:\n%s' % ('\n'.join(repr(m) for m in messages), - '\n'.join(repr(m) for m in got))) + msg = "Expected messages did not match actual.\n" "Expected:\n%s\nGot:\n%s" % ( + "\n".join(repr(m) for m in messages), + "\n".join(repr(m) for m in got), + ) assert list(messages) == got, msg def walk(self, node): @@ -278,7 +286,7 @@ def _create_tempfile(content=None): if content: if sys.version_info >= (3, 0): # erff - os.write(file_handle, bytes(content, 'ascii')) + os.write(file_handle, bytes(content, "ascii")) else: os.write(file_handle, content) try: |