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