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