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