Debian Codegraph @

tests/test_completerlib.py

raw
1 # -*- coding: utf-8 -*-
2 """Tests for completerlib."""
3
4 # -----------------------------------------------------------------------------
5 # Imports
6 # -----------------------------------------------------------------------------
7
8 import os
9 import shutil
10 import sys
11 import tempfile
12 import unittest
13 from os.path import join
14
15 from tempfile import TemporaryDirectory
16
17 from IPython.core.completerlib import magic_run_completer, module_completion, try_import
18 from IPython.testing.decorators import onlyif_unicode_paths
19
20
21 class MockEvent(object):
22 def __init__(self, line):
23 self.line = line
24
25
26 # -----------------------------------------------------------------------------
27 # Test functions begin
28 # -----------------------------------------------------------------------------
29 class Test_magic_run_completer(unittest.TestCase):
30 files = ["aao.py", "a.py", "b.py", "aao.txt"]
31 dirs = ["adir/", "bdir/"]
32
33 def setUp(self):
34 self.BASETESTDIR = tempfile.mkdtemp()
35 for fil in self.files:
36 with open(join(self.BASETESTDIR, fil), "w", encoding="utf-8") as sfile:
37 sfile.write("pass\n")
38 for d in self.dirs:
39 os.mkdir(join(self.BASETESTDIR, d))
40
41 self.oldpath = os.getcwd()
42 os.chdir(self.BASETESTDIR)
43
44 def tearDown(self):
45 os.chdir(self.oldpath)
46 shutil.rmtree(self.BASETESTDIR)
47
48 def test_1(self):
49 """Test magic_run_completer, should match two alternatives"""
50 event = MockEvent("%run a")
51 mockself = None
52 match = set(magic_run_completer(mockself, event))
53 self.assertEqual(match, {"a.py", "aao.py", "adir/"})
54
55 def test_2(self):
56 """Test magic_run_completer, should match one alternative"""
57 event = MockEvent("%run aa")
58 mockself = None
59 match = set(magic_run_completer(mockself, event))
60 self.assertEqual(match, {"aao.py"})
61
62 def test_3(self):
63 """Test magic_run_completer with unterminated " """
64 event = MockEvent('%run "a')
65 mockself = None
66 match = set(magic_run_completer(mockself, event))
67 self.assertEqual(match, {"a.py", "aao.py", "adir/"})
68
69 def test_completion_more_args(self):
70 event = MockEvent("%run a.py ")
71 match = set(magic_run_completer(None, event))
72 self.assertEqual(match, set(self.files + self.dirs))
73
74 def test_completion_in_dir(self):
75 # Github issue #3459
76 event = MockEvent("%run a.py {}".format(join(self.BASETESTDIR, "a")))
77 print(repr(event.line))
78 match = set(magic_run_completer(None, event))
79 # We specifically use replace here rather than normpath, because
80 # at one point there were duplicates 'adir' and 'adir/', and normpath
81 # would hide the failure for that.
82 self.assertEqual(
83 match,
84 {
85 join(self.BASETESTDIR, f).replace("\\", "/")
86 for f in ("a.py", "aao.py", "aao.txt", "adir/")
87 },
88 )
89
90
91 class Test_magic_run_completer_nonascii(unittest.TestCase):
92 @onlyif_unicode_paths
93 def setUp(self):
94 self.BASETESTDIR = tempfile.mkdtemp()
95 for fil in ["aaø.py", "a.py", "b.py"]:
96 with open(join(self.BASETESTDIR, fil), "w", encoding="utf-8") as sfile:
97 sfile.write("pass\n")
98 self.oldpath = os.getcwd()
99 os.chdir(self.BASETESTDIR)
100
101 def tearDown(self):
102 os.chdir(self.oldpath)
103 shutil.rmtree(self.BASETESTDIR)
104
105 @onlyif_unicode_paths
106 def test_1(self):
107 """Test magic_run_completer, should match two alternatives"""
108 event = MockEvent("%run a")
109 mockself = None
110 match = set(magic_run_completer(mockself, event))
111 self.assertEqual(match, {"a.py", "aaø.py"})
112
113 @onlyif_unicode_paths
114 def test_2(self):
115 """Test magic_run_completer, should match one alternative"""
116 event = MockEvent("%run aa")
117 mockself = None
118 match = set(magic_run_completer(mockself, event))
119 self.assertEqual(match, {"aaø.py"})
120
121 @onlyif_unicode_paths
122 def test_3(self):
123 """Test magic_run_completer with unterminated " """
124 event = MockEvent('%run "a')
125 mockself = None
126 match = set(magic_run_completer(mockself, event))
127 self.assertEqual(match, {"a.py", "aaø.py"})
128
129
130 # module_completer:
131
132
133 def test_import_invalid_module():
134 """Testing of issue https://github.com/ipython/ipython/issues/1107"""
135 invalid_module_names = {"foo-bar", "foo:bar", "10foo"}
136 valid_module_names = {"foobar"}
137 with TemporaryDirectory() as tmpdir:
138 sys.path.insert(0, tmpdir)
139 for name in invalid_module_names | valid_module_names:
140 filename = os.path.join(tmpdir, name + ".py")
141 open(filename, "w", encoding="utf-8").close()
142
143 s = set(module_completion("import foo"))
144 intersection = s.intersection(invalid_module_names)
145 assert intersection == set()
146
147 assert valid_module_names.issubset(s), valid_module_names.intersection(s)
148
149
150 def test_bad_module_all():
151 """Test module with invalid __all__
152
153 https://github.com/ipython/ipython/issues/9678
154 """
155 testsdir = os.path.dirname(__file__)
156 sys.path.insert(0, testsdir)
157 try:
158 results = module_completion("from bad_all import ")
159 assert "puppies" in results
160 for r in results:
161 assert isinstance(r, str)
162
163 # bad_all doesn't contain submodules, but this completion
164 # should finish without raising an exception:
165 results = module_completion("import bad_all.")
166 assert results == []
167 finally:
168 sys.path.remove(testsdir)
169
170
171 def test_module_without_init():
172 """
173 Test module without __init__.py.
174
175 https://github.com/ipython/ipython/issues/11226
176 """
177 fake_module_name = "foo_xder_134"
178 with TemporaryDirectory() as tmpdir:
179 sys.path.insert(0, tmpdir)
180 try:
181 os.makedirs(os.path.join(tmpdir, fake_module_name))
182 s = try_import(mod=fake_module_name)
183 assert s == [], f"for module {fake_module_name}"
184 finally:
185 sys.path.remove(tmpdir)
186
187
188 def test_valid_exported_submodules():
189 """
190 Test checking exported (__all__) objects are submodules
191 """
192 results = module_completion("import os.pa")
193 # ensure we get a valid submodule:
194 assert "os.path" in results
195 # ensure we don't get objects that aren't submodules:
196 assert "os.pathconf" not in results