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