Debian Codegraph @

tests/checkers/unittest_unicode/unittest_bidirectional_unicode.py

raw symbols SCIP
1 # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
2 # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
3 # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
4
5 import itertools
6 import unicodedata
7 from pathlib import Path
8 from typing import cast
9
10 import astroid
11 import pytest
12 from astroid import nodes
13
14 import pylint.checkers.unicode
15 import pylint.interfaces
16 import pylint.testutils
17
18 from . import FakeNode
19
20 UNICODE_TESTS = Path(__file__).parent.parent.parent / "regrtest_data" / "unicode"
21
22
23 class TestBidirectionalUnicodeChecker(pylint.testutils.CheckerTestCase):
24 CHECKER_CLASS = pylint.checkers.unicode.UnicodeChecker
25
26 checker: pylint.checkers.unicode.UnicodeChecker
27
28 def test_finds_bidirectional_unicode_that_currently_not_parsed(self) -> None:
29 """Test an example from https://github.com/nickboucher/trojan-source/tree/main/Python
30 that is currently not working Python but producing a syntax error.
31
32 So we test this to make sure it stays like this
33 """
34 test_file = UNICODE_TESTS / "invisible_function.txt"
35
36 with pytest.raises(astroid.AstroidSyntaxError):
37 astroid.MANAGER.ast_from_string(test_file.read_text("utf-8"))
38
39 with pytest.raises(AssertionError):
40 # The following errors are not risen at the moment,
41 # But we keep this in order to allow writing the test fast, if
42 # the condition above isn't met anymore.
43 module = FakeNode(test_file.read_bytes())
44 with self.assertAddsMessages(
45 pylint.testutils.MessageTest(
46 msg_id="bidirectional-unicode",
47 confidence=pylint.interfaces.HIGH,
48 # node=module,
49 line=6,
50 end_line=10,
51 col_offset=0,
52 end_col_offset=17,
53 ),
54 pylint.testutils.MessageTest(
55 msg_id="bidirectional-unicode",
56 confidence=pylint.interfaces.HIGH,
57 line=10,
58 # node=module,
59 end_line=10,
60 col_offset=0,
61 end_col_offset=20,
62 ),
63 ):
64 self.checker.process_module(cast(nodes.Module, module))
65
66 @pytest.mark.parametrize(
67 "bad_string, codec",
68 [
69 pytest.param(
70 char,
71 codec,
72 id=f"{unicodedata.name(char)}_{codec}".replace(" ", "_"),
73 )
74 for char, codec in itertools.product(
75 pylint.checkers.unicode.BIDI_UNICODE,
76 ("utf-8", "utf-16le", "utf-16be", "utf-32le", "utf-32be"),
77 )
78 ],
79 )
80 def test_find_bidi_string(self, bad_string: str, codec: str) -> None:
81 """Ensure that all Bidirectional strings are detected.
82
83 Tests also UTF-16 and UTF-32.
84 """
85 expected = pylint.testutils.MessageTest(
86 msg_id="bidirectional-unicode",
87 confidence=pylint.interfaces.HIGH,
88 line=1,
89 # node=module,
90 end_line=1,
91 col_offset=0,
92 end_col_offset=3,
93 )
94
95 with self.assertAddsMessages(expected):
96 self.checker._check_bidi_chars(f"# {bad_string}".encode(codec), 1, codec)