summaryrefslogtreecommitdiff
path: root/tests/extensions/test_private_import.py
blob: a10384eacd42c5580d95b8e31628c4dc416fa5d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt

"""Tests the local module directory comparison logic which requires mocking file directories"""

from unittest.mock import MagicMock, patch

import astroid

from pylint.extensions import private_import
from pylint.interfaces import HIGH
from pylint.testutils import CheckerTestCase, MessageTest


class TestPrivateImport(CheckerTestCase):
    """The mocked dirname is the directory of the file being linted, the node is code inside that file"""

    CHECKER_CLASS = private_import.PrivateImportChecker

    @patch("pathlib.Path.parent")
    def test_internal_module(self, parent: MagicMock) -> None:
        parent.parts = ("", "dir", "module")
        import_from = astroid.extract_node("""from module import _file""")

        with self.assertNoMessages():
            self.checker.visit_importfrom(import_from)

    @patch("pathlib.Path.parent")
    def test_external_module_nested(self, parent: MagicMock) -> None:
        parent.parts = ("", "dir", "module", "module_files", "util")

        import_from = astroid.extract_node("""from module import _file""")

        with self.assertNoMessages():
            self.checker.visit_importfrom(import_from)

    @patch("pathlib.Path.parent")
    def test_external_module_dot_import(self, parent: MagicMock) -> None:
        parent.parts = ("", "dir", "outer", "inner", "module_files", "util")

        import_from = astroid.extract_node("""from outer.inner import _file""")

        with self.assertNoMessages():
            self.checker.visit_importfrom(import_from)

    @patch("pathlib.Path.parent")
    def test_external_module_dot_import_outer_only(self, parent: MagicMock) -> None:
        parent.parts = ("", "dir", "outer", "extensions")

        import_from = astroid.extract_node("""from outer.inner import _file""")

        with self.assertNoMessages():
            self.checker.visit_importfrom(import_from)

    @patch("pathlib.Path.parent")
    def test_external_module(self, parent: MagicMock) -> None:
        parent.parts = ("", "dir", "other")

        import_from = astroid.extract_node("""from module import _file""")

        msg = MessageTest(
            msg_id="import-private-name",
            node=import_from,
            line=1,
            col_offset=0,
            end_line=1,
            end_col_offset=24,
            args=("object", "_file"),
            confidence=HIGH,
        )
        with self.assertAddsMessages(msg):
            self.checker.visit_importfrom(import_from)