diff options
author | Ashley Whetter <ashley@awhetter.co.uk> | 2019-06-14 22:28:42 -0700 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2019-06-20 10:02:14 +0200 |
commit | 33b8185a455c1686d038258697bb93005f2441c2 (patch) | |
tree | 4a50ccac775c009436e45803129e428ed694065f /tests/utils | |
parent | 7081d91f30728653000bdfc59ea85a3395f96418 (diff) | |
download | pylint-git-33b8185a455c1686d038258697bb93005f2441c2.tar.gz |
Stopped installing tests with package
Diffstat (limited to 'tests/utils')
-rw-r--r-- | tests/utils/unittest_ast_walker.py | 69 | ||||
-rw-r--r-- | tests/utils/unittest_utils.py | 51 |
2 files changed, 120 insertions, 0 deletions
diff --git a/tests/utils/unittest_ast_walker.py b/tests/utils/unittest_ast_walker.py new file mode 100644 index 000000000..b8fb78eac --- /dev/null +++ b/tests/utils/unittest_ast_walker.py @@ -0,0 +1,69 @@ +# -*- 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 + +import warnings + +import astroid + +from pylint.checkers.utils import check_messages +from pylint.utils import ASTWalker + + +class TestASTWalker(object): + class MockLinter(object): + def __init__(self, msgs): + self._msgs = msgs + + def is_message_enabled(self, msgid): + return self._msgs.get(msgid, True) + + class Checker(object): + def __init__(self): + self.called = set() + + @check_messages("first-message") + def visit_module(self, module): + self.called.add("module") + + @check_messages("second-message") + def visit_call(self, module): + raise NotImplementedError + + @check_messages("second-message", "third-message") + def visit_assignname(self, module): + self.called.add("assignname") + + @check_messages("second-message") + def leave_assignname(self, module): + raise NotImplementedError + + def test_check_messages(self): + linter = self.MockLinter( + {"first-message": True, "second-message": False, "third-message": True} + ) + walker = ASTWalker(linter) + checker = self.Checker() + walker.add_checker(checker) + walker.walk(astroid.parse("x = func()")) + assert {"module", "assignname"} == checker.called + + def test_deprecated_methods(self): + class Checker(object): + def __init__(self): + self.called = False + + @check_messages("first-message") + def visit_assname(self, node): + self.called = True + + linter = self.MockLinter({"first-message": True}) + walker = ASTWalker(linter) + checker = Checker() + walker.add_checker(checker) + with warnings.catch_warnings(record=True): + warnings.simplefilter("always") + walker.walk(astroid.parse("x = 1")) + + assert not checker.called diff --git a/tests/utils/unittest_utils.py b/tests/utils/unittest_utils.py new file mode 100644 index 000000000..a009391de --- /dev/null +++ b/tests/utils/unittest_utils.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2013-2014 Google, Inc. +# Copyright (c) 2013-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr> +# Copyright (c) 2014 Arun Persaud <arun@nubati.net> +# Copyright (c) 2015-2018 Claudiu Popa <pcmanticore@gmail.com> +# Copyright (c) 2015 Aru Sahni <arusahni@gmail.com> +# Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro> +# Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com> +# Copyright (c) 2016 Glenn Matthews <glenn@e-dad.net> +# Copyright (c) 2017-2018 Anthony Sottile <asottile@umich.edu> +# Copyright (c) 2017 Pierre Sassoulas <pierre.sassoulas@cea.fr> +# Copyright (c) 2017 ttenhoeve-aa <ttenhoeve@appannie.com> +# Copyright (c) 2017 Łukasz Rogalski <rogalski.91@gmail.com> +# Copyright (c) 2018 Pierre Sassoulas <pierre.sassoulas@wisebim.fr> + +# 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 + +import io +import re + +from pylint.utils import utils + + +def test__basename_in_blacklist_re_match(): + patterns = [re.compile(".*enchilada.*"), re.compile("unittest_.*")] + assert utils._basename_in_blacklist_re("unittest_utils.py", patterns) + assert utils._basename_in_blacklist_re("cheese_enchiladas.xml", patterns) + + +def test__basename_in_blacklist_re_nomatch(): + patterns = [re.compile(".*enchilada.*"), re.compile("unittest_.*")] + assert not utils._basename_in_blacklist_re("test_utils.py", patterns) + assert not utils._basename_in_blacklist_re("enchilad.py", patterns) + + +def test_decoding_stream_unknown_encoding(): + """decoding_stream should fall back to *some* decoding when given an + unknown encoding. + """ + binary_io = io.BytesIO(b"foo\nbar") + stream = utils.decoding_stream(binary_io, "garbage-encoding") + # should still act like a StreamReader + ret = stream.readlines() + assert ret == ["foo\n", "bar"] + + +def test_decoding_stream_known_encoding(): + binary_io = io.BytesIO("€".encode("cp1252")) + stream = utils.decoding_stream(binary_io, "cp1252") + assert stream.read() == "€" |