diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2019-03-08 20:21:00 +0100 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2019-03-09 11:09:29 +0100 |
commit | b5faf228561266786800e1a788fe9b01d25c55dc (patch) | |
tree | 948926f310d4daf03d4c281bba5ceb14df71b234 /pylint/test | |
parent | 02e0e37a19c0999a2159b090daa0545a97dee2f6 (diff) | |
download | pylint-git-b5faf228561266786800e1a788fe9b01d25c55dc.tar.gz |
Refactor - Create a unittest file for MessageStore
Taken from unittest_lint.py
Diffstat (limited to 'pylint/test')
-rw-r--r-- | pylint/test/message/unittest_message_store.py | 109 | ||||
-rw-r--r-- | pylint/test/unittest_lint.py | 129 |
2 files changed, 124 insertions, 114 deletions
diff --git a/pylint/test/message/unittest_message_store.py b/pylint/test/message/unittest_message_store.py new file mode 100644 index 000000000..09ff6d4c8 --- /dev/null +++ b/pylint/test/message/unittest_message_store.py @@ -0,0 +1,109 @@ +# -*- coding: utf-8 -*- + +# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html +# For details: https://github.com/PyCQA/pylint/blob/master/COPYING + +from contextlib import redirect_stdout +from io import StringIO + +import pytest + +from pylint.exceptions import InvalidMessageError, UnknownMessageError +from pylint.message import MessageDefinition, MessagesStore + + +@pytest.fixture +def store(): + store = MessagesStore() + + class Checker(object): + name = "achecker" + msgs = { + "W1234": ( + "message", + "msg-symbol", + "msg description.", + {"old_names": [("W0001", "old-symbol")]}, + ), + "E1234": ( + "Duplicate keyword argument %r in %s call", + "duplicate-keyword-arg", + "Used when a function call passes the same keyword argument multiple times.", + {"maxversion": (2, 6)}, + ), + } + + store.register_messages_from_checker(Checker()) + return store + + +class TestMessagesStore(object): + def _compare_messages(self, desc, msg, checkerref=False): + assert desc == msg.format_help(checkerref=checkerref) + + def test_check_message_id(self, store): + assert isinstance(store.get_message_definitions("W1234")[0], MessageDefinition) + with pytest.raises(UnknownMessageError): + store.get_message_definitions("YB12") + + def test_message_help(self, store): + message_definition = store.get_message_definitions("W1234")[0] + self._compare_messages( + """:msg-symbol (W1234): *message* + msg description. This message belongs to the achecker checker.""", + message_definition, + checkerref=True, + ) + self._compare_messages( + """:msg-symbol (W1234): *message* + msg description.""", + message_definition, + checkerref=False, + ) + + def test_message_help_minmax(self, store): + # build the message manually to be python version independent + message_definition = store.get_message_definitions("E1234")[0] + self._compare_messages( + """:duplicate-keyword-arg (E1234): *Duplicate keyword argument %r in %s call* + Used when a function call passes the same keyword argument multiple times. + This message belongs to the achecker checker. It can't be emitted when using + Python >= 2.6.""", + message_definition, + checkerref=True, + ) + self._compare_messages( + """:duplicate-keyword-arg (E1234): *Duplicate keyword argument %r in %s call* + Used when a function call passes the same keyword argument multiple times. + This message can't be emitted when using Python >= 2.6.""", + message_definition, + checkerref=False, + ) + + def test_list_messages(self, store): + output = StringIO() + with redirect_stdout(output): + store.list_messages() + # cursory examination of the output: we're mostly testing it completes + assert ":msg-symbol (W1234): *message*" in output.getvalue() + + def test_add_renamed_message(self, store): + store.add_renamed_message("W1234", "old-bad-name", "msg-symbol") + assert "msg-symbol" == store.get_message_definitions("W1234")[0].symbol + assert "msg-symbol" == store.get_message_definitions("old-bad-name")[0].symbol + + def test_add_renamed_message_invalid(self, store): + # conflicting message ID + with pytest.raises(InvalidMessageError) as cm: + store.add_renamed_message( + "W1234", "old-msg-symbol", "duplicate-keyword-arg" + ) + expected = ( + "Message id 'W1234' cannot have both 'msg-symbol' and 'old-msg-symbol' " + "as symbolic name." + ) + assert str(cm.value) == expected + + def test_renamed_message_register(self, store): + assert "msg-symbol" == store.get_message_definitions("W0001")[0].symbol + assert "msg-symbol" == store.get_message_definitions("old-symbol")[0].symbol diff --git a/pylint/test/unittest_lint.py b/pylint/test/unittest_lint.py index efd9d07a9..3e5855834 100644 --- a/pylint/test/unittest_lint.py +++ b/pylint/test/unittest_lint.py @@ -26,35 +26,33 @@ # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html # For details: https://github.com/PyCQA/pylint/blob/master/COPYING -from contextlib import contextmanager, redirect_stdout -import sys import os import re +import sys import tempfile -from shutil import rmtree -from os import getcwd, chdir -from os.path import join, basename, dirname, isdir, abspath, sep +from contextlib import contextmanager, redirect_stdout from importlib import reload from io import StringIO +from os import chdir, getcwd +from os.path import abspath, basename, dirname, isdir, join, sep +from shutil import rmtree + +import pytest -from pylint import config, lint -from pylint.lint import PyLinter, Run, preprocess_options, ArgumentPreprocessingError +import pylint.testutils as testutils +from pylint import checkers, config, exceptions, interfaces, lint +from pylint.checkers.utils import check_messages +from pylint.exceptions import InvalidMessageError, UnknownMessageError +from pylint.lint import ArgumentPreprocessingError, PyLinter, Run, preprocess_options from pylint.message import ( + MSG_STATE_CONFIDENCE, MSG_STATE_SCOPE_CONFIG, MSG_STATE_SCOPE_MODULE, - MSG_STATE_CONFIDENCE, - MessagesStore, MessageDefinition, + MessagesStore, ) -from pylint.utils import FileState, tokenize_module -from pylint.exceptions import InvalidMessageError, UnknownMessageError -import pylint.testutils as testutils from pylint.reporters import text -from pylint import checkers -from pylint.checkers.utils import check_messages -from pylint import exceptions -from pylint import interfaces -import pytest +from pylint.utils import FileState, tokenize_module if os.name == "java": if os._name == "nt": @@ -725,103 +723,6 @@ class TestPreprocessOptions(object): ) -@pytest.fixture -def store(): - store = MessagesStore() - - class Checker(object): - name = "achecker" - msgs = { - "W1234": ( - "message", - "msg-symbol", - "msg description.", - {"old_names": [("W0001", "old-symbol")]}, - ), - "E1234": ( - "Duplicate keyword argument %r in %s call", - "duplicate-keyword-arg", - "Used when a function call passes the same keyword argument multiple times.", - {"maxversion": (2, 6)}, - ), - } - - store.register_messages_from_checker(Checker()) - return store - - -class TestMessagesStore(object): - def _compare_messages(self, desc, msg, checkerref=False): - assert desc == msg.format_help(checkerref=checkerref) - - def test_check_message_id(self, store): - assert isinstance(store.get_message_definitions("W1234")[0], MessageDefinition) - with pytest.raises(UnknownMessageError): - store.get_message_definitions("YB12") - - def test_message_help(self, store): - message_definition = store.get_message_definitions("W1234")[0] - self._compare_messages( - """:msg-symbol (W1234): *message* - msg description. This message belongs to the achecker checker.""", - message_definition, - checkerref=True, - ) - self._compare_messages( - """:msg-symbol (W1234): *message* - msg description.""", - message_definition, - checkerref=False, - ) - - def test_message_help_minmax(self, store): - # build the message manually to be python version independent - message_definition = store.get_message_definitions("E1234")[0] - self._compare_messages( - """:duplicate-keyword-arg (E1234): *Duplicate keyword argument %r in %s call* - Used when a function call passes the same keyword argument multiple times. - This message belongs to the achecker checker. It can't be emitted when using - Python >= 2.6.""", - message_definition, - checkerref=True, - ) - self._compare_messages( - """:duplicate-keyword-arg (E1234): *Duplicate keyword argument %r in %s call* - Used when a function call passes the same keyword argument multiple times. - This message can't be emitted when using Python >= 2.6.""", - message_definition, - checkerref=False, - ) - - def test_list_messages(self, store): - output = StringIO() - with redirect_stdout(output): - store.list_messages() - # cursory examination of the output: we're mostly testing it completes - assert ":msg-symbol (W1234): *message*" in output.getvalue() - - def test_add_renamed_message(self, store): - store.add_renamed_message("W1234", "old-bad-name", "msg-symbol") - assert "msg-symbol" == store.get_message_definitions("W1234")[0].symbol - assert "msg-symbol" == store.get_message_definitions("old-bad-name")[0].symbol - - def test_add_renamed_message_invalid(self, store): - # conflicting message ID - with pytest.raises(InvalidMessageError) as cm: - store.add_renamed_message( - "W1234", "old-msg-symbol", "duplicate-keyword-arg" - ) - expected = ( - "Message id 'W1234' cannot have both 'msg-symbol' and 'old-msg-symbol' " - "as symbolic name." - ) - assert str(cm.value) == expected - - def test_renamed_message_register(self, store): - assert "msg-symbol" == store.get_message_definitions("W0001")[0].symbol - assert "msg-symbol" == store.get_message_definitions("old-symbol")[0].symbol - - def test_custom_should_analyze_file(): """Check that we can write custom should_analyze_file that work even for arguments. |