File: test_gather_imports.py

package info (click to toggle)
python-libcst 1.4.0-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,928 kB
  • sloc: python: 76,235; makefile: 10; sh: 2
file content (175 lines) | stat: -rw-r--r-- 6,031 bytes parent folder | download
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#
from libcst import parse_module
from libcst.codemod import CodemodContext, CodemodTest
from libcst.codemod.visitors import GatherImportsVisitor
from libcst.testing.utils import UnitTest


class TestGatherImportsVisitor(UnitTest):
    def gather_imports(self, code: str) -> GatherImportsVisitor:
        transform_instance = GatherImportsVisitor(
            CodemodContext(full_module_name="a.b.foobar", full_package_name="a.b")
        )
        input_tree = parse_module(CodemodTest.make_fixture_data(code))
        input_tree.visit(transform_instance)
        return transform_instance

    def test_gather_nothing(self) -> None:
        code = """
            def foo() -> None:
                pass

            def bar() -> int:
                return 5
        """
        gatherer = self.gather_imports(code)
        self.assertEqual(gatherer.module_imports, set())
        self.assertEqual(gatherer.object_mapping, {})
        self.assertEqual(gatherer.module_aliases, {})
        self.assertEqual(gatherer.alias_mapping, {})
        self.assertEqual(len(gatherer.all_imports), 0)

    def test_gather_module(self) -> None:
        code = """
            import a.b.c
            import d

            def foo() -> None:
                pass

            def bar() -> int:
                return 5
        """
        gatherer = self.gather_imports(code)
        self.assertEqual(gatherer.module_imports, {"a.b.c", "d"})
        self.assertEqual(gatherer.object_mapping, {})
        self.assertEqual(gatherer.module_aliases, {})
        self.assertEqual(gatherer.alias_mapping, {})
        self.assertEqual(len(gatherer.all_imports), 2)

    def test_gather_aliased_module(self) -> None:
        code = """
            import a.b.c as e
            import d as f

            def foo() -> None:
                pass

            def bar() -> int:
                return 5
        """
        gatherer = self.gather_imports(code)
        self.assertEqual(gatherer.module_imports, set())
        self.assertEqual(gatherer.object_mapping, {})
        self.assertEqual(gatherer.module_aliases, {"a.b.c": "e", "d": "f"})
        self.assertEqual(gatherer.alias_mapping, {})
        self.assertEqual(len(gatherer.all_imports), 2)

    def test_gather_object(self) -> None:
        code = """
            from a.b.c import d, e, f

            def foo() -> None:
                pass

            def bar() -> int:
                return 5
        """
        gatherer = self.gather_imports(code)
        self.assertEqual(gatherer.module_imports, set())
        self.assertEqual(gatherer.object_mapping, {"a.b.c": {"d", "e", "f"}})
        self.assertEqual(gatherer.module_aliases, {})
        self.assertEqual(gatherer.alias_mapping, {})
        self.assertEqual(len(gatherer.all_imports), 1)

    def test_gather_object_disjoint(self) -> None:
        code = """
            from a.b.c import d, e
            from a.b.c import f

            def foo() -> None:
                pass

            def bar() -> int:
                return 5
        """
        gatherer = self.gather_imports(code)
        self.assertEqual(gatherer.module_imports, set())
        self.assertEqual(gatherer.object_mapping, {"a.b.c": {"d", "e", "f"}})
        self.assertEqual(gatherer.module_aliases, {})
        self.assertEqual(gatherer.alias_mapping, {})
        self.assertEqual(len(gatherer.all_imports), 2)

    def test_gather_aliased_object(self) -> None:
        code = """
            from a.b.c import d as e, f as g

            def foo() -> None:
                pass

            def bar() -> int:
                return 5
        """
        gatherer = self.gather_imports(code)
        self.assertEqual(gatherer.module_imports, set())
        self.assertEqual(gatherer.object_mapping, {})
        self.assertEqual(gatherer.module_aliases, {})
        self.assertEqual(gatherer.alias_mapping, {"a.b.c": [("d", "e"), ("f", "g")]})
        self.assertEqual(len(gatherer.all_imports), 1)

    def test_gather_aliased_object_disjoint(self) -> None:
        code = """
            from a.b.c import d as e
            from a.b.c import f as g

            def foo() -> None:
                pass

            def bar() -> int:
                return 5
        """
        gatherer = self.gather_imports(code)
        self.assertEqual(gatherer.module_imports, set())
        self.assertEqual(gatherer.object_mapping, {})
        self.assertEqual(gatherer.module_aliases, {})
        self.assertEqual(gatherer.alias_mapping, {"a.b.c": [("d", "e"), ("f", "g")]})
        self.assertEqual(len(gatherer.all_imports), 2)

    def test_gather_aliased_object_mixed(self) -> None:
        code = """
            from a.b.c import d as e, f, g

            def foo() -> None:
                pass

            def bar() -> int:
                return 5
        """
        gatherer = self.gather_imports(code)
        self.assertEqual(gatherer.module_imports, set())
        self.assertEqual(gatherer.object_mapping, {"a.b.c": {"f", "g"}})
        self.assertEqual(gatherer.module_aliases, {})
        self.assertEqual(gatherer.alias_mapping, {"a.b.c": [("d", "e")]})
        self.assertEqual(len(gatherer.all_imports), 1)

    def test_gather_relative_object(self) -> None:
        code = """
            from .c import d as e, f, g
            from a.b.c import h, i, j

            def foo() -> None:
                pass

            def bar() -> int:
                return 5
        """
        gatherer = self.gather_imports(code)
        self.assertEqual(gatherer.module_imports, set())
        self.assertEqual(gatherer.object_mapping, {"a.b.c": {"f", "g", "h", "i", "j"}})
        self.assertEqual(gatherer.module_aliases, {})
        self.assertEqual(gatherer.alias_mapping, {"a.b.c": [("d", "e")]})
        self.assertEqual(len(gatherer.all_imports), 2)