##// END OF EJS Templates
Partially normalise paths for Windows tests
Thomas Kluyver -
Show More
@@ -1,144 +1,147 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 35 dirs = [u"adir/", "bdir/"]
36 36
37 37 def setUp(self):
38 38 self.BASETESTDIR = tempfile.mkdtemp()
39 39 for fil in self.files:
40 40 with open(join(self.BASETESTDIR, fil), "w") as sfile:
41 41 sfile.write("pass\n")
42 42 for d in self.dirs:
43 43 os.mkdir(join(self.BASETESTDIR, d))
44 44
45 45 self.oldpath = py3compat.getcwd()
46 46 os.chdir(self.BASETESTDIR)
47 47
48 48 def tearDown(self):
49 49 os.chdir(self.oldpath)
50 50 shutil.rmtree(self.BASETESTDIR)
51 51
52 52 def test_1(self):
53 53 """Test magic_run_completer, should match two alterntives
54 54 """
55 55 event = MockEvent(u"%run a")
56 56 mockself = None
57 57 match = set(magic_run_completer(mockself, event))
58 58 self.assertEqual(match, {u"a.py", u"aao.py", u"adir/"})
59 59
60 60 def test_2(self):
61 61 """Test magic_run_completer, should match one alterntive
62 62 """
63 63 event = MockEvent(u"%run aa")
64 64 mockself = None
65 65 match = set(magic_run_completer(mockself, event))
66 66 self.assertEqual(match, set([u"aao.py"]))
67 67
68 68 def test_3(self):
69 69 """Test magic_run_completer with unterminated " """
70 70 event = MockEvent(u'%run "a')
71 71 mockself = None
72 72 match = set(magic_run_completer(mockself, event))
73 73 self.assertEqual(match, {u"a.py", u"aao.py", u"adir/"})
74 74
75 75 def test_completion_more_args(self):
76 76 event = MockEvent(u'%run a.py ')
77 77 match = set(magic_run_completer(None, event))
78 78 self.assertEqual(match, set(self.files + self.dirs))
79 79
80 80 def test_completion_in_dir(self):
81 81 # Github issue #3459
82 82 event = MockEvent(u'%run a.py {}'.format(join(self.BASETESTDIR, 'a')))
83 83 print(repr(event.line))
84 84 match = set(magic_run_completer(None, event))
85 self.assertEqual(match, {join(self.BASETESTDIR, f) for
86 f in (u'a.py', u'aao.py', u'aao.txt', u'adir/')})
85 # We specifically use replace here rather than normpath, because
86 # at one point there were duplicates 'adir' and 'adir/', and normpath
87 # would hide the failure for that.
88 self.assertEqual(match, {join(self.BASETESTDIR, f).replace('\\','/')
89 for f in (u'a.py', u'aao.py', u'aao.txt', u'adir/')})
87 90
88 91 class Test_magic_run_completer_nonascii(unittest.TestCase):
89 92 @onlyif_unicode_paths
90 93 def setUp(self):
91 94 self.BASETESTDIR = tempfile.mkdtemp()
92 95 for fil in [u"aaø.py", u"a.py", u"b.py"]:
93 96 with open(join(self.BASETESTDIR, fil), "w") as sfile:
94 97 sfile.write("pass\n")
95 98 self.oldpath = py3compat.getcwd()
96 99 os.chdir(self.BASETESTDIR)
97 100
98 101 def tearDown(self):
99 102 os.chdir(self.oldpath)
100 103 shutil.rmtree(self.BASETESTDIR)
101 104
102 105 @onlyif_unicode_paths
103 106 def test_1(self):
104 107 """Test magic_run_completer, should match two alterntives
105 108 """
106 109 event = MockEvent(u"%run a")
107 110 mockself = None
108 111 match = set(magic_run_completer(mockself, event))
109 112 self.assertEqual(match, set([u"a.py", u"aaø.py"]))
110 113
111 114 @onlyif_unicode_paths
112 115 def test_2(self):
113 116 """Test magic_run_completer, should match one alterntive
114 117 """
115 118 event = MockEvent(u"%run aa")
116 119 mockself = None
117 120 match = set(magic_run_completer(mockself, event))
118 121 self.assertEqual(match, set([u"aaø.py"]))
119 122
120 123 @onlyif_unicode_paths
121 124 def test_3(self):
122 125 """Test magic_run_completer with unterminated " """
123 126 event = MockEvent(u'%run "a')
124 127 mockself = None
125 128 match = set(magic_run_completer(mockself, event))
126 129 self.assertEqual(match, set([u"a.py", u"aaø.py"]))
127 130
128 131 # module_completer:
129 132
130 133 def test_import_invalid_module():
131 134 """Testing of issue https://github.com/ipython/ipython/issues/1107"""
132 135 invalid_module_names = set(['foo-bar', 'foo:bar', '10foo'])
133 136 valid_module_names = set(['foobar'])
134 137 with TemporaryDirectory() as tmpdir:
135 138 sys.path.insert( 0, tmpdir )
136 139 for name in invalid_module_names | valid_module_names:
137 140 filename = os.path.join(tmpdir, name + '.py')
138 141 open(filename, 'w').close()
139 142
140 143 s = set( module_completion('import foo') )
141 144 intersection = s.intersection(invalid_module_names)
142 145 nt.assert_equal(intersection, set())
143 146
144 147 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