Show More
@@ -0,0 +1,61 b'' | |||||
|
1 | # -*- coding: utf-8 -*- | |||
|
2 | """Tests for completerlib. | |||
|
3 | ||||
|
4 | """ | |||
|
5 | from __future__ import absolute_import | |||
|
6 | ||||
|
7 | #----------------------------------------------------------------------------- | |||
|
8 | # Imports | |||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | ||||
|
11 | import os | |||
|
12 | import shutil | |||
|
13 | import sys | |||
|
14 | import tempfile | |||
|
15 | import unittest | |||
|
16 | from os.path import join | |||
|
17 | ||||
|
18 | import nose.tools as nt | |||
|
19 | from nose import SkipTest | |||
|
20 | ||||
|
21 | from IPython.core.completerlib import magic_run_completer | |||
|
22 | from IPython.testing import decorators as dec | |||
|
23 | from IPython.testing import tools as tt | |||
|
24 | from IPython.utils import py3compat | |||
|
25 | ||||
|
26 | ||||
|
27 | class MockEvent(object): | |||
|
28 | def __init__(self, line): | |||
|
29 | self.line = line | |||
|
30 | ||||
|
31 | #----------------------------------------------------------------------------- | |||
|
32 | # Test functions begin | |||
|
33 | #----------------------------------------------------------------------------- | |||
|
34 | class Test_magic_run_completer(unittest.TestCase): | |||
|
35 | def setUp(self): | |||
|
36 | self.BASETESTDIR = tempfile.mkdtemp() | |||
|
37 | for fil in [u"aaå.py", u"a.py", u"b.py"]: | |||
|
38 | with open(join(self.BASETESTDIR, fil), "w") as sfile: | |||
|
39 | sfile.write("pass\n") | |||
|
40 | self.oldpath = os.getcwdu() | |||
|
41 | os.chdir(self.BASETESTDIR) | |||
|
42 | ||||
|
43 | def tearDown(self): | |||
|
44 | os.chdir(self.oldpath) | |||
|
45 | shutil.rmtree(self.BASETESTDIR) | |||
|
46 | ||||
|
47 | def test_1(self): | |||
|
48 | """Test magic_run_completer, should match two alterntives | |||
|
49 | """ | |||
|
50 | event = MockEvent(u"%run a") | |||
|
51 | mockself = None | |||
|
52 | match = magic_run_completer(mockself, event) | |||
|
53 | self.assertEqual(match, [u"a.py", u"aaå.py",]) | |||
|
54 | ||||
|
55 | def test_2(self): | |||
|
56 | """Test magic_run_completer, should match one alterntive | |||
|
57 | """ | |||
|
58 | event = MockEvent(u"%run aa") | |||
|
59 | mockself = None | |||
|
60 | match = magic_run_completer(mockself, event) | |||
|
61 | self.assertEqual(match, [u"aaå.py",]) |
@@ -20,7 +20,6 b' import glob' | |||||
20 | import inspect |
|
20 | import inspect | |
21 | import os |
|
21 | import os | |
22 | import re |
|
22 | import re | |
23 | import shlex |
|
|||
24 | import sys |
|
23 | import sys | |
25 |
|
24 | |||
26 | # Third-party imports |
|
25 | # Third-party imports | |
@@ -31,6 +30,7 b' from zipimport import zipimporter' | |||||
31 | from IPython.core.completer import expand_user, compress_user |
|
30 | from IPython.core.completer import expand_user, compress_user | |
32 | from IPython.core.error import TryNext |
|
31 | from IPython.core.error import TryNext | |
33 | from IPython.utils import py3compat |
|
32 | from IPython.utils import py3compat | |
|
33 | from IPython.utils._process_common import arg_split | |||
34 |
|
34 | |||
35 | # FIXME: this should be pulled in with the right call via the component system |
|
35 | # FIXME: this should be pulled in with the right call via the component system | |
36 | from IPython.core.ipapi import get as get_ipython |
|
36 | from IPython.core.ipapi import get as get_ipython | |
@@ -56,40 +56,6 b" magic_run_re = re.compile(r'.*(\\.ipy|\\.py[w]?)$')" | |||||
56 | # Local utilities |
|
56 | # Local utilities | |
57 | #----------------------------------------------------------------------------- |
|
57 | #----------------------------------------------------------------------------- | |
58 |
|
58 | |||
59 | def shlex_split(x): |
|
|||
60 | """Helper function to split lines into segments. |
|
|||
61 | """ |
|
|||
62 | # shlex.split raises an exception if there is a syntax error in sh syntax |
|
|||
63 | # for example if no closing " is found. This function keeps dropping the |
|
|||
64 | # last character of the line until shlex.split does not raise |
|
|||
65 | # an exception. It adds end of the line to the result of shlex.split |
|
|||
66 | # |
|
|||
67 | # Example: |
|
|||
68 | # %run "c:/python -> ['%run','"c:/python'] |
|
|||
69 |
|
||||
70 | # shlex.split has unicode bugs in Python 2, so encode first to str |
|
|||
71 | if not py3compat.PY3: |
|
|||
72 | x = py3compat.cast_bytes(x) |
|
|||
73 |
|
||||
74 | endofline = [] |
|
|||
75 | while x != '': |
|
|||
76 | try: |
|
|||
77 | comps = shlex.split(x) |
|
|||
78 | if len(endofline) >= 1: |
|
|||
79 | comps.append(''.join(endofline)) |
|
|||
80 | if not py3compat.PY3: |
|
|||
81 | comps = [py3compat.cast_unicode(x) for x in comps] |
|
|||
82 | return comps |
|
|||
83 |
|
||||
84 | except ValueError: |
|
|||
85 | endofline = [x[-1:]]+endofline |
|
|||
86 | x = x[:-1] |
|
|||
87 |
|
||||
88 | x = ''.join(endofline) |
|
|||
89 | if not py3compat.PY3: |
|
|||
90 | x = py3compat.cast_unicode(x) |
|
|||
91 | return [x] |
|
|||
92 |
|
||||
93 | def module_list(path): |
|
59 | def module_list(path): | |
94 | """ |
|
60 | """ | |
95 | Return the list containing the names of the modules available in the given |
|
61 | Return the list containing the names of the modules available in the given | |
@@ -270,7 +236,7 b' def module_completer(self,event):' | |||||
270 | def magic_run_completer(self, event): |
|
236 | def magic_run_completer(self, event): | |
271 | """Complete files that end in .py or .ipy for the %run command. |
|
237 | """Complete files that end in .py or .ipy for the %run command. | |
272 | """ |
|
238 | """ | |
273 |
comps = |
|
239 | comps = arg_split(event.line) | |
274 | relpath = (len(comps) > 1 and comps[-1] or '').strip("'\"") |
|
240 | relpath = (len(comps) > 1 and comps[-1] or '').strip("'\"") | |
275 |
|
241 | |||
276 | #print("\nev=", event) # dbg |
|
242 | #print("\nev=", event) # dbg |
General Comments 0
You need to be logged in to leave comments.
Login now