@@ -24,8 +24,8 @@ def cmd2_app():
2424def test_cmd2_command_completion_single_end (cmd2_app ):
2525 text = 'he'
2626 line = 'he'
27- begidx = 0
28- endidx = 2
27+ endidx = len ( line )
28+ begidx = endidx - len ( text )
2929 # It is at end of line, so extra space is present
3030 assert cmd2_app .completenames (text , line , begidx , endidx ) == ['help ' ]
3131
@@ -40,23 +40,23 @@ def test_cmd2_command_completion_single_mid(cmd2_app):
4040def test_cmd2_command_completion_multiple (cmd2_app ):
4141 text = 'h'
4242 line = 'h'
43- begidx = 0
44- endidx = 1
43+ endidx = len ( line )
44+ begidx = endidx - len ( text )
4545 # It is not at end of line, so no extra space
4646 assert cmd2_app .completenames (text , line , begidx , endidx ) == ['help' , 'history' ]
4747
4848def test_cmd2_command_completion_nomatch (cmd2_app ):
4949 text = 'z'
5050 line = 'z'
51- begidx = 0
52- endidx = 1
51+ endidx = len ( line )
52+ begidx = endidx - len ( text )
5353 assert cmd2_app .completenames (text , line , begidx , endidx ) == []
5454
5555def test_cmd2_help_completion_single_end (cmd2_app ):
5656 text = 'he'
5757 line = 'help he'
58- begidx = 5
59- endidx = 7
58+ endidx = len ( line )
59+ begidx = endidx - len ( text )
6060 # Even though it is at end of line, no extra space is present when tab completing a command name to get help on
6161 assert cmd2_app .completenames (text , line , begidx , endidx ) == ['help' ]
6262
@@ -70,52 +70,88 @@ def test_cmd2_help_completion_single_mid(cmd2_app):
7070def test_cmd2_help_completion_multiple (cmd2_app ):
7171 text = 'h'
7272 line = 'help h'
73- begidx = 5
74- endidx = 6
73+ endidx = len ( line )
74+ begidx = endidx - len ( text )
7575 assert cmd2_app .completenames (text , line , begidx , endidx ) == ['help' , 'history' ]
7676
7777def test_cmd2_help_completion_nomatch (cmd2_app ):
7878 text = 'z'
7979 line = 'help z'
80- begidx = 5
81- endidx = 6
80+ endidx = len ( line )
81+ begidx = endidx - len ( text )
8282 assert cmd2_app .completenames (text , line , begidx , endidx ) == []
8383
8484def test_shell_command_completion (cmd2_app ):
8585 if sys .platform == "win32" :
8686 text = 'calc'
87- line = 'shell calc'
88- begidx = 6
89- endidx = 10
90- assert cmd2_app .complete_shell (text , line , begidx , endidx ) == ['calc.exe ' ]
87+ line = 'shell {}' .format (text )
88+ expected = ['calc.exe ' ]
9189 else :
9290 text = 'eg'
93- line = '!eg'
94- begidx = 1
95- endidx = 3
96- assert cmd2_app .complete_shell (text , line , begidx , endidx ) == ['egrep ' ]
91+ line = '!{}' .format (text )
92+ expected = ['egrep ' ]
93+
94+ endidx = len (line )
95+ begidx = endidx - len (text )
96+ assert cmd2_app .complete_shell (text , line , begidx , endidx ) == expected
97+
98+ def test_shell_command_completion_doesnt_match_wildcards (cmd2_app ):
99+ if sys .platform == "win32" :
100+ text = 'c*'
101+ line = 'shell {}' .format (text )
102+ else :
103+ text = 'e*'
104+ line = '!{}' .format (text )
105+
106+ endidx = len (line )
107+ begidx = endidx - len (text )
108+ assert cmd2_app .complete_shell (text , line , begidx , endidx ) == []
97109
98110def test_shell_command_completion_multiple (cmd2_app ):
99111 if sys .platform == "win32" :
100112 text = 'c'
101- line = 'shell c'
102- begidx = 6
103- endidx = 7
104- assert 'calc.exe' in cmd2_app .complete_shell (text , line , begidx , endidx )
113+ line = 'shell {}' .format (text )
114+ expected = 'calc.exe'
105115 else :
106116 text = 'l'
107- line = '!l'
108- begidx = 1
109- endidx = 2
110- assert 'ls' in cmd2_app .complete_shell (text , line , begidx , endidx )
117+ line = '!{}' .format (text )
118+ expected = 'ls'
119+
120+ endidx = len (line )
121+ begidx = endidx - len (text )
122+ assert expected in cmd2_app .complete_shell (text , line , begidx , endidx )
111123
112124def test_shell_command_completion_nomatch (cmd2_app ):
113125 text = 'zzzz'
114126 line = 'shell zzzz'
115- begidx = 6
116- endidx = 10
127+ endidx = len ( line )
128+ begidx = endidx - len ( text )
117129 assert cmd2_app .complete_shell (text , line , begidx , endidx ) == []
118130
131+ def test_shell_command_completion_doesnt_complete_when_just_shell (cmd2_app ):
132+ text = ''
133+ if sys .platform == "win32" :
134+ line = 'shell' .format (text )
135+ else :
136+ line = '!' .format (text )
137+
138+ endidx = len (line )
139+ begidx = endidx - len (text )
140+ assert cmd2_app .complete_shell (text , line , begidx , endidx ) == []
141+
142+ def test_shell_command_completion_does_path_completion_when_after_command (cmd2_app , request ):
143+ test_dir = os .path .dirname (request .module .__file__ )
144+
145+ text = 'c'
146+ path = os .path .join (test_dir , text )
147+ line = 'shell cat {}' .format (path )
148+
149+ endidx = len (line )
150+ begidx = endidx - len (text )
151+
152+ assert cmd2_app .complete_shell (text , line , begidx , endidx ) == ['conftest.py ' ]
153+
154+
119155def test_path_completion_single_end (cmd2_app , request ):
120156 test_dir = os .path .dirname (request .module .__file__ )
121157
@@ -164,6 +200,80 @@ def test_path_completion_nomatch(cmd2_app, request):
164200
165201 assert cmd2_app .path_complete (text , line , begidx , endidx ) == []
166202
203+ def test_path_completion_cwd (cmd2_app ):
204+ # Run path complete with no path and no search text
205+ text = ''
206+ line = '!ls {}' .format (text )
207+ endidx = len (line )
208+ begidx = endidx - len (text )
209+ completions_empty = cmd2_app .path_complete (text , line , begidx , endidx )
210+
211+ # Run path complete with path set to the CWD
212+ cwd = os .getcwd ()
213+ line = '!ls {}' .format (cwd )
214+ endidx = len (line )
215+ begidx = endidx - len (text )
216+ completions_cwd = cmd2_app .path_complete (text , line , begidx , endidx )
217+
218+ # Verify that the results are the same in both cases and that there is something there
219+ assert completions_empty == completions_cwd
220+ assert completions_cwd
221+
222+ def test_path_completion_doesnt_match_wildcards (cmd2_app , request ):
223+ test_dir = os .path .dirname (request .module .__file__ )
224+
225+ text = 'c*'
226+ path = os .path .join (test_dir , text )
227+ line = '!cat {}' .format (path )
228+
229+ endidx = len (line )
230+ begidx = endidx - len (text )
231+
232+ # Currently path completion doesn't accept wildcards, so will always return empty results
233+ assert cmd2_app .path_complete (text , line , begidx , endidx ) == []
234+
235+ def test_path_completion_user_expansion (cmd2_app ):
236+ # Run path with just a tilde
237+ text = ''
238+ if sys .platform .startswith ('win' ):
239+ line = '!dir ~\{}' .format (text )
240+ else :
241+ line = '!ls ~{}' .format (text )
242+ endidx = len (line )
243+ begidx = endidx - len (text )
244+ completions_tilde = cmd2_app .path_complete (text , line , begidx , endidx )
245+
246+ # Run path complete on the user's home directory
247+ user_dir = os .path .expanduser ('~' )
248+ if sys .platform .startswith ('win' ):
249+ line = '!dir {}' .format (user_dir )
250+ else :
251+ line = '!ls {}' .format (user_dir )
252+ endidx = len (line )
253+ begidx = endidx - len (text )
254+ completions_home = cmd2_app .path_complete (text , line , begidx , endidx )
255+
256+ # Verify that the results are the same in both cases
257+ assert completions_tilde == completions_home
258+
259+ # This next assert fails on AppVeyor Windows containers, but works fine on my Windows 10 VM
260+ if not sys .platform .startswith ('win' ):
261+ # Verify that there is something there
262+ assert completions_tilde
263+
264+ def test_path_completion_directories_only (cmd2_app , request ):
265+ test_dir = os .path .dirname (request .module .__file__ )
266+
267+ text = 's'
268+ path = os .path .join (test_dir , text )
269+ line = '!cat {}' .format (path )
270+
271+ endidx = len (line )
272+ begidx = endidx - len (text )
273+
274+ assert cmd2_app .path_complete (text , line , begidx , endidx , dir_only = True ) == ['scripts' + os .path .sep ]
275+
276+
167277def test_parseline_command_and_args (cmd2_app ):
168278 line = 'help history'
169279 command , args , out_line = cmd2_app .parseline (line )
0 commit comments