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