##// END OF EJS Templates
Add failing test for duplicate dirs in completions
Thomas Kluyver -
Show More
@@ -1,140 +1,144 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Tests for completerlib.
2 """Tests for completerlib.
3
3
4 """
4 """
5 from __future__ import absolute_import
5 from __future__ import absolute_import
6
6
7 #-----------------------------------------------------------------------------
7 #-----------------------------------------------------------------------------
8 # Imports
8 # Imports
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 import os
11 import os
12 import shutil
12 import shutil
13 import sys
13 import sys
14 import tempfile
14 import tempfile
15 import unittest
15 import unittest
16 from os.path import join
16 from os.path import join
17
17
18 import nose.tools as nt
18 import nose.tools as nt
19
19
20 from IPython.core.completerlib import magic_run_completer, module_completion
20 from IPython.core.completerlib import magic_run_completer, module_completion
21 from IPython.utils import py3compat
21 from IPython.utils import py3compat
22 from IPython.utils.tempdir import TemporaryDirectory
22 from IPython.utils.tempdir import TemporaryDirectory
23 from IPython.testing.decorators import onlyif_unicode_paths
23 from IPython.testing.decorators import onlyif_unicode_paths
24
24
25
25
26 class MockEvent(object):
26 class MockEvent(object):
27 def __init__(self, line):
27 def __init__(self, line):
28 self.line = line
28 self.line = line
29
29
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31 # Test functions begin
31 # Test functions begin
32 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
33 class Test_magic_run_completer(unittest.TestCase):
33 class Test_magic_run_completer(unittest.TestCase):
34 files = [u"aao.py", u"a.py", u"b.py", u"aao.txt"]
34 files = [u"aao.py", u"a.py", u"b.py", u"aao.txt"]
35 dirs = [u"adir", "bdir"]
35
36
36 def setUp(self):
37 def setUp(self):
37 self.BASETESTDIR = tempfile.mkdtemp()
38 self.BASETESTDIR = tempfile.mkdtemp()
38 for fil in self.files:
39 for fil in self.files:
39 with open(join(self.BASETESTDIR, fil), "w") as sfile:
40 with open(join(self.BASETESTDIR, fil), "w") as sfile:
40 sfile.write("pass\n")
41 sfile.write("pass\n")
42 for d in self.dirs:
43 os.mkdir(join(self.BASETESTDIR, d))
44
41 self.oldpath = py3compat.getcwd()
45 self.oldpath = py3compat.getcwd()
42 os.chdir(self.BASETESTDIR)
46 os.chdir(self.BASETESTDIR)
43
47
44 def tearDown(self):
48 def tearDown(self):
45 os.chdir(self.oldpath)
49 os.chdir(self.oldpath)
46 shutil.rmtree(self.BASETESTDIR)
50 shutil.rmtree(self.BASETESTDIR)
47
51
48 def test_1(self):
52 def test_1(self):
49 """Test magic_run_completer, should match two alterntives
53 """Test magic_run_completer, should match two alterntives
50 """
54 """
51 event = MockEvent(u"%run a")
55 event = MockEvent(u"%run a")
52 mockself = None
56 mockself = None
53 match = set(magic_run_completer(mockself, event))
57 match = set(magic_run_completer(mockself, event))
54 self.assertEqual(match, set([u"a.py", u"aao.py"]))
58 self.assertEqual(match, {u"a.py", u"aao.py", u"adir/"})
55
59
56 def test_2(self):
60 def test_2(self):
57 """Test magic_run_completer, should match one alterntive
61 """Test magic_run_completer, should match one alterntive
58 """
62 """
59 event = MockEvent(u"%run aa")
63 event = MockEvent(u"%run aa")
60 mockself = None
64 mockself = None
61 match = set(magic_run_completer(mockself, event))
65 match = set(magic_run_completer(mockself, event))
62 self.assertEqual(match, set([u"aao.py"]))
66 self.assertEqual(match, set([u"aao.py"]))
63
67
64 def test_3(self):
68 def test_3(self):
65 """Test magic_run_completer with unterminated " """
69 """Test magic_run_completer with unterminated " """
66 event = MockEvent(u'%run "a')
70 event = MockEvent(u'%run "a')
67 mockself = None
71 mockself = None
68 match = set(magic_run_completer(mockself, event))
72 match = set(magic_run_completer(mockself, event))
69 self.assertEqual(match, set([u"a.py", u"aao.py"]))
73 self.assertEqual(match, {u"a.py", u"aao.py", u"adir/"})
70
74
71 def test_completion_more_args(self):
75 def test_completion_more_args(self):
72 event = MockEvent(u'%run a.py ')
76 event = MockEvent(u'%run a.py ')
73 match = set(magic_run_completer(None, event))
77 match = set(magic_run_completer(None, event))
74 self.assertEqual(match, set(self.files))
78 self.assertEqual(match, set(self.files + self.dirs))
75
79
76 def test_completion_in_dir(self):
80 def test_completion_in_dir(self):
77 # Github issue #3459
81 # Github issue #3459
78 event = MockEvent(u'%run a.py {}'.format(join(self.BASETESTDIR, 'a')))
82 event = MockEvent(u'%run a.py {}'.format(join(self.BASETESTDIR, 'a')))
79 print(repr(event.line))
83 print(repr(event.line))
80 match = set(magic_run_completer(None, event))
84 match = set(magic_run_completer(None, event))
81 self.assertEqual(match,
85 self.assertEqual(match, {join(self.BASETESTDIR, f) for
82 {join(self.BASETESTDIR, f) for f in (u'a.py', u'aao.py', u'aao.txt')})
86 f in (u'a.py', u'aao.py', u'aao.txt', u'adir')})
83
87
84 class Test_magic_run_completer_nonascii(unittest.TestCase):
88 class Test_magic_run_completer_nonascii(unittest.TestCase):
85 @onlyif_unicode_paths
89 @onlyif_unicode_paths
86 def setUp(self):
90 def setUp(self):
87 self.BASETESTDIR = tempfile.mkdtemp()
91 self.BASETESTDIR = tempfile.mkdtemp()
88 for fil in [u"aaø.py", u"a.py", u"b.py"]:
92 for fil in [u"aaø.py", u"a.py", u"b.py"]:
89 with open(join(self.BASETESTDIR, fil), "w") as sfile:
93 with open(join(self.BASETESTDIR, fil), "w") as sfile:
90 sfile.write("pass\n")
94 sfile.write("pass\n")
91 self.oldpath = py3compat.getcwd()
95 self.oldpath = py3compat.getcwd()
92 os.chdir(self.BASETESTDIR)
96 os.chdir(self.BASETESTDIR)
93
97
94 def tearDown(self):
98 def tearDown(self):
95 os.chdir(self.oldpath)
99 os.chdir(self.oldpath)
96 shutil.rmtree(self.BASETESTDIR)
100 shutil.rmtree(self.BASETESTDIR)
97
101
98 @onlyif_unicode_paths
102 @onlyif_unicode_paths
99 def test_1(self):
103 def test_1(self):
100 """Test magic_run_completer, should match two alterntives
104 """Test magic_run_completer, should match two alterntives
101 """
105 """
102 event = MockEvent(u"%run a")
106 event = MockEvent(u"%run a")
103 mockself = None
107 mockself = None
104 match = set(magic_run_completer(mockself, event))
108 match = set(magic_run_completer(mockself, event))
105 self.assertEqual(match, set([u"a.py", u"aaø.py"]))
109 self.assertEqual(match, set([u"a.py", u"aaø.py"]))
106
110
107 @onlyif_unicode_paths
111 @onlyif_unicode_paths
108 def test_2(self):
112 def test_2(self):
109 """Test magic_run_completer, should match one alterntive
113 """Test magic_run_completer, should match one alterntive
110 """
114 """
111 event = MockEvent(u"%run aa")
115 event = MockEvent(u"%run aa")
112 mockself = None
116 mockself = None
113 match = set(magic_run_completer(mockself, event))
117 match = set(magic_run_completer(mockself, event))
114 self.assertEqual(match, set([u"aaø.py"]))
118 self.assertEqual(match, set([u"aaø.py"]))
115
119
116 @onlyif_unicode_paths
120 @onlyif_unicode_paths
117 def test_3(self):
121 def test_3(self):
118 """Test magic_run_completer with unterminated " """
122 """Test magic_run_completer with unterminated " """
119 event = MockEvent(u'%run "a')
123 event = MockEvent(u'%run "a')
120 mockself = None
124 mockself = None
121 match = set(magic_run_completer(mockself, event))
125 match = set(magic_run_completer(mockself, event))
122 self.assertEqual(match, set([u"a.py", u"aaø.py"]))
126 self.assertEqual(match, set([u"a.py", u"aaø.py"]))
123
127
124 # module_completer:
128 # module_completer:
125
129
126 def test_import_invalid_module():
130 def test_import_invalid_module():
127 """Testing of issue https://github.com/ipython/ipython/issues/1107"""
131 """Testing of issue https://github.com/ipython/ipython/issues/1107"""
128 invalid_module_names = set(['foo-bar', 'foo:bar', '10foo'])
132 invalid_module_names = set(['foo-bar', 'foo:bar', '10foo'])
129 valid_module_names = set(['foobar'])
133 valid_module_names = set(['foobar'])
130 with TemporaryDirectory() as tmpdir:
134 with TemporaryDirectory() as tmpdir:
131 sys.path.insert( 0, tmpdir )
135 sys.path.insert( 0, tmpdir )
132 for name in invalid_module_names | valid_module_names:
136 for name in invalid_module_names | valid_module_names:
133 filename = os.path.join(tmpdir, name + '.py')
137 filename = os.path.join(tmpdir, name + '.py')
134 open(filename, 'w').close()
138 open(filename, 'w').close()
135
139
136 s = set( module_completion('import foo') )
140 s = set( module_completion('import foo') )
137 intersection = s.intersection(invalid_module_names)
141 intersection = s.intersection(invalid_module_names)
138 nt.assert_equal(intersection, set())
142 nt.assert_equal(intersection, set())
139
143
140 assert valid_module_names.issubset(s), valid_module_names.intersection(s)
144 assert valid_module_names.issubset(s), valid_module_names.intersection(s)
General Comments 0
You need to be logged in to leave comments. Login now