1515import unittest
1616from os .path import join
1717
18+ import nose .tools as nt
19+
1820from IPython .core .completerlib import magic_run_completer , module_completion
1921from IPython .utils import py3compat
2022from IPython .utils .tempdir import TemporaryDirectory
@@ -29,11 +31,17 @@ def __init__(self, line):
2931# Test functions begin
3032#-----------------------------------------------------------------------------
3133class Test_magic_run_completer (unittest .TestCase ):
34+ files = [u"aao.py" , u"a.py" , u"b.py" , u"aao.txt" ]
35+ dirs = [u"adir/" , "bdir/" ]
36+
3237 def setUp (self ):
3338 self .BASETESTDIR = tempfile .mkdtemp ()
34- for fil in [ u"aao.py" , u"a.py" , u"b.py" ] :
39+ for fil in self . files :
3540 with open (join (self .BASETESTDIR , fil ), "w" ) as sfile :
3641 sfile .write ("pass\n " )
42+ for d in self .dirs :
43+ os .mkdir (join (self .BASETESTDIR , d ))
44+
3745 self .oldpath = py3compat .getcwd ()
3846 os .chdir (self .BASETESTDIR )
3947
@@ -47,7 +55,7 @@ def test_1(self):
4755 event = MockEvent (u"%run a" )
4856 mockself = None
4957 match = set (magic_run_completer (mockself , event ))
50- self .assertEqual (match , set ([ u"a.py" , u"aao.py" ]) )
58+ self .assertEqual (match , { u"a.py" , u"aao.py" , u"adir/" } )
5159
5260 def test_2 (self ):
5361 """Test magic_run_completer, should match one alterntive
@@ -62,23 +70,23 @@ def test_3(self):
6270 event = MockEvent (u'%run "a' )
6371 mockself = None
6472 match = set (magic_run_completer (mockself , event ))
65- self .assertEqual (match , set ([ u"a.py" , u"aao.py" ]) )
66-
67- def test_import_invalid_module (self ):
68- """Testing of issue https://github.com/ipython/ipython/issues/1107"""
69- invalid_module_names = set ([ 'foo-bar' , 'foo:bar' , '10foo' ] )
70- valid_module_names = set ([ 'foobar' ] )
71- with TemporaryDirectory () as tmpdir :
72- sys . path . insert ( 0 , tmpdir )
73- for name in invalid_module_names | valid_module_names :
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 )
73+ self .assertEqual (match , { u"a.py" , u"aao.py" , u"adir/" } )
74+
75+ def test_completion_more_args (self ):
76+ event = MockEvent ( u'%run a.py ' )
77+ match = set (magic_run_completer ( None , event ) )
78+ self . assertEqual ( match , set (self . files + self . dirs ) )
79+
80+ def test_completion_in_dir ( self ):
81+ # Github issue #3459
82+ event = MockEvent ( u'%run a.py {}' . format ( join (self . BASETESTDIR , 'a' )) )
83+ print ( repr ( event . line ) )
84+ match = set ( magic_run_completer ( None , event ))
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/' )} )
8290
8391class Test_magic_run_completer_nonascii (unittest .TestCase ):
8492 @onlyif_unicode_paths
@@ -119,3 +127,21 @@ def test_3(self):
119127 mockself = None
120128 match = set (magic_run_completer (mockself , event ))
121129 self .assertEqual (match , set ([u"a.py" , u"aaø.py" ]))
130+
131+ # module_completer:
132+
133+ def test_import_invalid_module ():
134+ """Testing of issue https://github.com/ipython/ipython/issues/1107"""
135+ invalid_module_names = set (['foo-bar' , 'foo:bar' , '10foo' ])
136+ valid_module_names = set (['foobar' ])
137+ with TemporaryDirectory () as tmpdir :
138+ sys .path .insert ( 0 , tmpdir )
139+ for name in invalid_module_names | valid_module_names :
140+ filename = os .path .join (tmpdir , name + '.py' )
141+ open (filename , 'w' ).close ()
142+
143+ s = set ( module_completion ('import foo' ) )
144+ intersection = s .intersection (invalid_module_names )
145+ nt .assert_equal (intersection , set ())
146+
147+ assert valid_module_names .issubset (s ), valid_module_names .intersection (s )
0 commit comments