blob: 892da50d13441fe7bb177dfb133d3dbfe019ec43 [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
Bram Moolenaarea5d6fa2017-08-18 21:07:11 +02003if !has('terminal')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02004 finish
5endif
6
7source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01008source screendump.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02009
Bram Moolenaarb81bc772017-08-11 22:45:01 +020010let s:python = PythonProg()
11
Bram Moolenaar94053a52017-08-01 21:44:33 +020012" Open a terminal with a shell, assign the job to g:job and return the buffer
13" number.
Bram Moolenaar05aafed2017-08-11 19:12:11 +020014func Run_shell_in_terminal(options)
Bram Moolenaarba6febd2017-10-30 21:56:23 +010015 if has('win32')
16 let buf = term_start([&shell,'/k'], a:options)
17 else
18 let buf = term_start(&shell, a:options)
19 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020020
21 let termlist = term_list()
22 call assert_equal(1, len(termlist))
23 call assert_equal(buf, termlist[0])
24
25 let g:job = term_getjob(buf)
26 call assert_equal(v:t_job, type(g:job))
27
Bram Moolenaar35422f42017-08-05 16:33:56 +020028 let string = string({'job': term_getjob(buf)})
29 call assert_match("{'job': 'process \\d\\+ run'}", string)
30
Bram Moolenaar94053a52017-08-01 21:44:33 +020031 return buf
32endfunc
33
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020034func Test_terminal_basic()
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020035 au BufWinEnter * if &buftype == 'terminal' | let b:done = 'yes' | endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +020036 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020037
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020038 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020039 call assert_match('^/dev/', job_info(g:job).tty_out)
40 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020041 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020042 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
43 call assert_match('^\\\\.\\pipe\\', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020044 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020045 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020046 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020047 call assert_match('%aR[^\n]*running]', execute('ls'))
48
Bram Moolenaar94053a52017-08-01 21:44:33 +020049 call Stop_shell_in_terminal(buf)
50 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020051 call assert_equal('n', mode())
52 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020053
Bram Moolenaar94053a52017-08-01 21:44:33 +020054 " closing window wipes out the terminal buffer a with finished job
55 close
56 call assert_equal("", bufname(buf))
57
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020058 au! BufWinEnter
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020059 unlet g:job
60endfunc
61
62func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020063 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020064 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020065 call term_wait(buf)
66
67 setlocal modifiable
68 exe "normal Axxx\<Esc>"
69 call assert_fails(buf . 'bwipe', 'E517')
70 undo
71
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020072 exe buf . 'bwipe'
73 unlet g:job
74endfunc
75
Bram Moolenaar94053a52017-08-01 21:44:33 +020076func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020077 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +020078 call assert_fails(buf . 'bwipe', 'E517')
79 exe buf . 'bwipe!'
Bram Moolenaar94053a52017-08-01 21:44:33 +020080 call WaitFor('job_status(g:job) == "dead"')
81 call assert_equal('dead', job_status(g:job))
82 call assert_equal("", bufname(buf))
83
84 unlet g:job
85endfunc
86
Bram Moolenaar8adb0d02017-09-17 19:08:02 +020087func Test_terminal_split_quit()
88 let buf = Run_shell_in_terminal({})
89 call term_wait(buf)
90 split
91 quit!
92 call term_wait(buf)
93 sleep 50m
94 call assert_equal('run', job_status(g:job))
95
96 quit!
97 call WaitFor('job_status(g:job) == "dead"')
98 call assert_equal('dead', job_status(g:job))
99
100 exe buf . 'bwipe'
101 unlet g:job
102endfunc
103
Bram Moolenaar94053a52017-08-01 21:44:33 +0200104func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200105 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200106 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200107 quit
108 for nr in range(1, winnr('$'))
109 call assert_notequal(winbufnr(nr), buf)
110 endfor
111 call assert_true(bufloaded(buf))
112 call assert_true(buflisted(buf))
113
114 exe 'split ' . buf . 'buf'
115 call Stop_shell_in_terminal(buf)
116 exe buf . 'bwipe'
117
118 unlet g:job
119endfunc
120
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200121func! s:Nasty_exit_cb(job, st)
122 exe g:buf . 'bwipe!'
123 let g:buf = 0
124endfunc
125
Bram Moolenaar9d189612017-09-09 18:11:00 +0200126func Get_cat_123_cmd()
127 if has('win32')
128 return 'cmd /c "cls && color 2 && echo 123"'
129 else
130 call writefile(["\<Esc>[32m123"], 'Xtext')
131 return "cat Xtext"
132 endif
133endfunc
134
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200135func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200136 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200137 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
138 let g:job = term_getjob(g:buf)
139
140 call WaitFor('job_status(g:job) == "dead"')
141 call WaitFor('g:buf == 0')
142 unlet g:buf
143 unlet g:job
144 call delete('Xtext')
145endfunc
146
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200147func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200148 let l = term_scrape(a:buf, 0)
149 call assert_true(len(l) == 0)
150 let l = term_scrape(a:buf, 999)
151 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200152 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200153 call assert_true(len(l) > 0)
154 call assert_equal('1', l[0].chars)
155 call assert_equal('2', l[1].chars)
156 call assert_equal('3', l[2].chars)
157 call assert_equal('#00e000', l[0].fg)
158 if &background == 'light'
159 call assert_equal('#ffffff', l[0].bg)
160 else
161 call assert_equal('#000000', l[0].bg)
162 endif
163
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200164 let l = term_getline(a:buf, -1)
165 call assert_equal('', l)
166 let l = term_getline(a:buf, 0)
167 call assert_equal('', l)
168 let l = term_getline(a:buf, 999)
169 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200170 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200171 call assert_equal('123', l)
172endfunc
173
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200174func Test_terminal_scrape_123()
175 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200176 let buf = term_start(cmd)
177
178 let termlist = term_list()
179 call assert_equal(1, len(termlist))
180 call assert_equal(buf, termlist[0])
181
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200182 " Nothing happens with invalid buffer number
183 call term_wait(1234)
184
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200185 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200186 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200187 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100188 call WaitFor({-> len(term_scrape(buf, 1)) == 3})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200189 call Check_123(buf)
190
191 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100192 let job = term_getjob(buf)
193 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200194 call term_wait(buf)
195 call Check_123(buf)
196
197 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200198 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200199endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200200
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200201func Test_terminal_scrape_multibyte()
202 if !has('multi_byte')
203 return
204 endif
205 call writefile(["léttまrs"], 'Xtext')
206 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200207 " Run cmd with UTF-8 codepage to make the type command print the expected
208 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100209 let buf = term_start("cmd /K chcp 65001")
210 call term_sendkeys(buf, "type Xtext\<CR>")
211 call term_sendkeys(buf, "exit\<CR>")
212 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200213 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100214 let buf = term_start("cat Xtext")
215 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200216 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200217
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100218 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
219 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200220 call assert_true(len(l) >= 7)
221 call assert_equal('l', l[0].chars)
222 call assert_equal('é', l[1].chars)
223 call assert_equal(1, l[1].width)
224 call assert_equal('t', l[2].chars)
225 call assert_equal('t', l[3].chars)
226 call assert_equal('ま', l[4].chars)
227 call assert_equal(2, l[4].width)
228 call assert_equal('r', l[5].chars)
229 call assert_equal('s', l[6].chars)
230
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100231 let job = term_getjob(buf)
232 call WaitFor({-> job_status(job) == "dead"})
233 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200234
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100235 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200236 call delete('Xtext')
237endfunc
238
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200239func Test_terminal_scroll()
240 call writefile(range(1, 200), 'Xtext')
241 if has('win32')
242 let cmd = 'cmd /c "type Xtext"'
243 else
244 let cmd = "cat Xtext"
245 endif
246 let buf = term_start(cmd)
247
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100248 let job = term_getjob(buf)
249 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200250 call term_wait(buf)
251 if has('win32')
252 " TODO: this should not be needed
253 sleep 100m
254 endif
255
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200256 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200257 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200258 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200259 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200260 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200261 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200262 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200263
264 exe buf . 'bwipe'
265 call delete('Xtext')
266endfunc
267
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200268func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200269 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200270
Bram Moolenaarb2412082017-08-20 18:09:14 +0200271 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200272 let size = term_getsize('')
273 bwipe!
274 call assert_equal(5, size[0])
275
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200276 call term_start(cmd, {'term_rows': 6})
277 let size = term_getsize('')
278 bwipe!
279 call assert_equal(6, size[0])
280
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200281 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200282 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200283 let size = term_getsize('')
284 bwipe!
285 call assert_equal([5, 33], size)
286
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200287 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
288 let size = term_getsize('')
289 bwipe!
290 call assert_equal([6, 36], size)
291
Bram Moolenaarb2412082017-08-20 18:09:14 +0200292 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200293 let size = term_getsize('')
294 bwipe!
295 call assert_equal(20, size[1])
296
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200297 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
298 let size = term_getsize('')
299 bwipe!
300 call assert_equal(26, size[1])
301
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200302 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200303 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200304 let size = term_getsize('')
305 bwipe!
306 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200307
308 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
309 let size = term_getsize('')
310 bwipe!
311 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200312
313 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200314endfunc
315
316func Test_terminal_curwin()
317 let cmd = Get_cat_123_cmd()
318 call assert_equal(1, winnr('$'))
319
320 split dummy
321 exe 'terminal ++curwin ' . cmd
322 call assert_equal(2, winnr('$'))
323 bwipe!
324
325 split dummy
326 call term_start(cmd, {'curwin': 1})
327 call assert_equal(2, winnr('$'))
328 bwipe!
329
330 split dummy
331 call setline(1, 'change')
332 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
333 call assert_equal(2, winnr('$'))
334 exe 'terminal! ++curwin ' . cmd
335 call assert_equal(2, winnr('$'))
336 bwipe!
337
338 split dummy
339 call setline(1, 'change')
340 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
341 call assert_equal(2, winnr('$'))
342 bwipe!
343
344 split dummy
345 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200346 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200347endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200348
Bram Moolenaarff546792017-11-21 14:47:57 +0100349func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200350 if s:python != ''
351 let cmd = s:python . " test_short_sleep.py"
352 let waittime = 500
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200353 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200354 echo 'This will take five seconds...'
355 let waittime = 2000
356 if has('win32')
357 let cmd = $windir . '\system32\timeout.exe 1'
358 else
359 let cmd = 'sleep 1'
360 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200361 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100362 return [cmd, waittime]
363endfunc
364
365func Test_terminal_finish_open_close()
366 call assert_equal(1, winnr('$'))
367
368 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200369
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100370 " shell terminal closes automatically
371 terminal
372 let buf = bufnr('%')
373 call assert_equal(2, winnr('$'))
374 " Wait for the shell to display a prompt
375 call WaitFor({-> term_getline(buf, 1) != ""})
376 call Stop_shell_in_terminal(buf)
377 call WaitFor("winnr('$') == 1", waittime)
378
379 " shell terminal that does not close automatically
380 terminal ++noclose
381 let buf = bufnr('%')
382 call assert_equal(2, winnr('$'))
383 " Wait for the shell to display a prompt
384 call WaitFor({-> term_getline(buf, 1) != ""})
385 call Stop_shell_in_terminal(buf)
386 call assert_equal(2, winnr('$'))
387 quit
388 call assert_equal(1, winnr('$'))
389
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200390 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200391 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200392 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200393 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200394
395 call term_start(cmd, {'term_finish': 'close'})
396 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200397 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200398 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200399 call assert_equal(1, winnr('$'))
400
401 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200402 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200403 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200404 call assert_equal(2, winnr('$'))
405 bwipe
406
407 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200408 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200409 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200410 call assert_equal(2, winnr('$'))
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200411 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200412
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200413 exe 'terminal ++hidden ++open ' . cmd
414 call assert_equal(1, winnr('$'))
415 call WaitFor("winnr('$') == 2", waittime)
416 call assert_equal(2, winnr('$'))
417 bwipe
418
419 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
420 call assert_equal(1, winnr('$'))
421 call WaitFor("winnr('$') == 2", waittime)
422 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200423 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200424
425 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
426 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
427 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
428 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
429
430 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200431 close!
Bram Moolenaar37c45832017-08-12 16:01:04 +0200432 call WaitFor("winnr('$') == 2", waittime)
433 call assert_equal(2, winnr('$'))
434 call assert_equal(4, winheight(0))
435 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200436endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200437
438func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200439 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200440 return
441 endif
442 call mkdir('Xdir')
443 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200444 call WaitFor('"Xdir" == fnamemodify(getline(1), ":t")')
445 call assert_equal('Xdir', fnamemodify(getline(1), ":t"))
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200446
447 exe buf . 'bwipe'
448 call delete('Xdir', 'rf')
449endfunc
450
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100451func Test_terminal_servername()
452 if !has('clientserver')
453 return
454 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100455 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100456 " Wait for the shell to display a prompt
Bram Moolenaar012eb662018-03-13 17:55:27 +0100457 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100458 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100459 call term_sendkeys(buf, "echo %VIM_SERVERNAME%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100460 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100461 call term_sendkeys(buf, "echo $VIM_SERVERNAME\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100462 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100463 call term_wait(buf)
464 call Stop_shell_in_terminal(buf)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100465 call WaitFor('getline(2) == v:servername')
466 call assert_equal(v:servername, getline(2))
467
Bram Moolenaar012eb662018-03-13 17:55:27 +0100468 exe buf . 'bwipe'
469 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100470endfunc
471
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200472func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100473 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200474 " Wait for the shell to display a prompt
Bram Moolenaar012eb662018-03-13 17:55:27 +0100475 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100476 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100477 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100478 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100479 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100480 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100481 call term_wait(buf)
482 call Stop_shell_in_terminal(buf)
Bram Moolenaar51c23682017-08-14 21:45:00 +0200483 call WaitFor('getline(2) == "correct"')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200484 call assert_equal('correct', getline(2))
485
Bram Moolenaar012eb662018-03-13 17:55:27 +0100486 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200487endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200488
489" must be last, we can't go back from GUI to terminal
490func Test_zz_terminal_in_gui()
Bram Moolenaar9f0139a2017-08-13 20:26:20 +0200491 if !CanRunGui()
Bram Moolenaar679653e2017-08-13 14:13:19 +0200492 return
493 endif
Bram Moolenaar97f65fa2017-08-29 20:42:07 +0200494
495 " Ignore the "failed to create input context" error.
496 call test_ignore_error('E285:')
497
Bram Moolenaar679653e2017-08-13 14:13:19 +0200498 gui -f
499
500 call assert_equal(1, winnr('$'))
501 let buf = Run_shell_in_terminal({'term_finish': 'close'})
502 call Stop_shell_in_terminal(buf)
503 call term_wait(buf)
504
505 " closing window wipes out the terminal buffer a with finished job
506 call WaitFor("winnr('$') == 1")
507 call assert_equal(1, winnr('$'))
508 call assert_equal("", bufname(buf))
509
510 unlet g:job
511endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200512
513func Test_terminal_list_args()
514 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
515 call assert_fails(buf . 'bwipe', 'E517')
516 exe buf . 'bwipe!'
517 call assert_equal("", bufname(buf))
518endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200519
520func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100521 let buf = term_start(&shell)
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200522 if has('mac')
523 " The shell or something else has a problem dealing with more than 1000
524 " characters at the same time.
525 let len = 1000
526 else
527 let len = 5000
528 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200529
530 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100531 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200532 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100533 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200534
535 " On MS-Windows there is an extra empty line below "done". Find "done" in
536 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100537 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100538 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100539 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200540 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100541 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200542 endif
543 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200544
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100545 let g:job = term_getjob(buf)
546 call Stop_shell_in_terminal(buf)
547 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200548 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200549 bwipe
550endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200551
552func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200553 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200554 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200555 endif
556 new
557 call setline(1, ['one', 'two', 'three'])
558 %term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200559 call WaitFor('getline("$") =~ "3"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200560 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200561 call assert_equal(['3', '3', '14'], nrs)
562 bwipe
563
Bram Moolenaardada6d22017-09-02 17:18:35 +0200564 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200565 call setline(1, ['one', 'two', 'three', 'four'])
566 2,3term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200567 call WaitFor('getline("$") =~ "2"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200568 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200569 call assert_equal(['2', '2', '10'], nrs)
570 bwipe
571
Bram Moolenaardada6d22017-09-02 17:18:35 +0200572 if executable('python')
573 new
574 call setline(1, ['print("hello")'])
575 1term ++eof=exit() python
576 " MS-Windows echoes the input, Unix doesn't.
577 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
578 if getline(1) =~ 'hello'
579 call assert_equal('hello', getline(1))
580 else
581 call assert_equal('hello', getline(line('$') - 1))
582 endif
583 bwipe
584
585 if has('win32')
586 new
587 call setline(1, ['print("hello")'])
588 1term ++eof=<C-Z> python
589 call WaitFor('getline("$") =~ "Z"')
590 call assert_equal('hello', getline(line('$') - 1))
591 bwipe
592 endif
593 endif
594
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200595 bwipe!
596endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200597
598func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200599 " Todo: make this work in the GUI
600 if !has('gui_running')
601 return
602 endif
603 let buf = term_start('NONE', {})
604 call assert_notequal(0, buf)
605
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200606 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200607 call assert_notequal('', pty)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200608 if has('win32')
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200609 silent exe '!start cmd /c "echo look here > ' . pty . '"'
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200610 else
611 call system('echo "look here" > ' . pty)
612 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100613 call WaitFor({-> term_getline(buf, 1) =~ "look here"})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200614
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200615 call assert_match('look here', term_getline(buf, 1))
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200616 bwipe!
617endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200618
619func Test_terminal_special_chars()
620 " this file name only works on Unix
621 if !has('unix')
622 return
623 endif
624 call mkdir('Xdir with spaces')
625 call writefile(['x'], 'Xdir with spaces/quoted"file')
626 term ls Xdir\ with\ spaces/quoted\"file
627 call WaitFor('term_getline("", 1) =~ "quoted"')
628 call assert_match('quoted"file', term_getline('', 1))
629 call term_wait('')
630
631 call delete('Xdir with spaces', 'rf')
632 bwipe
633endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200634
635func Test_terminal_wrong_options()
636 call assert_fails('call term_start(&shell, {
637 \ "in_io": "file",
638 \ "in_name": "xxx",
639 \ "out_io": "file",
640 \ "out_name": "xxx",
641 \ "err_io": "file",
642 \ "err_name": "xxx"
643 \ })', 'E474:')
644 call assert_fails('call term_start(&shell, {
645 \ "out_buf": bufnr("%")
646 \ })', 'E474:')
647 call assert_fails('call term_start(&shell, {
648 \ "err_buf": bufnr("%")
649 \ })', 'E474:')
650endfunc
651
652func Test_terminal_redir_file()
Bram Moolenaar17833372017-09-04 22:23:19 +0200653 " TODO: this should work on MS-Window
654 if has('unix')
655 let cmd = Get_cat_123_cmd()
656 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
657 call term_wait(buf)
658 call WaitFor('len(readfile("Xfile")) > 0')
659 call assert_match('123', readfile('Xfile')[0])
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200660 let g:job = term_getjob(buf)
661 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaar17833372017-09-04 22:23:19 +0200662 call delete('Xfile')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200663 bwipe
Bram Moolenaar17833372017-09-04 22:23:19 +0200664 endif
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200665
666 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200667 call writefile(['one line'], 'Xfile')
668 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
669 call term_wait(buf)
670 call WaitFor('term_getline(' . buf . ', 1) == "one line"')
671 call assert_equal('one line', term_getline(buf, 1))
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200672 let g:job = term_getjob(buf)
673 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200674 bwipe
675 call delete('Xfile')
676 endif
677endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200678
679func TerminalTmap(remap)
680 let buf = Run_shell_in_terminal({})
681 call assert_equal('t', mode())
682
683 if a:remap
684 tmap 123 456
685 else
686 tnoremap 123 456
687 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100688 " don't use abcde, it's an existing command
689 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200690 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100691 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200692 call feedkeys("123", 'tx')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100693 call WaitFor({-> term_getline(buf, term_getcursor(buf)[0]) =~ 'abxde\|456'})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200694 let lnum = term_getcursor(buf)[0]
695 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100696 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200697 else
698 call assert_match('456', term_getline(buf, lnum))
699 endif
700
701 call term_sendkeys(buf, "\r")
702 call Stop_shell_in_terminal(buf)
703 call term_wait(buf)
704
705 tunmap 123
706 tunmap 456
707 call assert_equal('', maparg('123', 't'))
708 close
709 unlet g:job
710endfunc
711
712func Test_terminal_tmap()
713 call TerminalTmap(1)
714 call TerminalTmap(0)
715endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200716
717func Test_terminal_wall()
718 let buf = Run_shell_in_terminal({})
719 wall
720 call Stop_shell_in_terminal(buf)
721 call term_wait(buf)
722 exe buf . 'bwipe'
723 unlet g:job
724endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200725
Bram Moolenaar7a760922018-02-19 23:10:02 +0100726func Test_terminal_wqall()
727 let buf = Run_shell_in_terminal({})
728 call assert_fails('wqall', 'E948')
729 call Stop_shell_in_terminal(buf)
730 call term_wait(buf)
731 exe buf . 'bwipe'
732 unlet g:job
733endfunc
734
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200735func Test_terminal_composing_unicode()
736 let save_enc = &encoding
737 set encoding=utf-8
738
739 if has('win32')
740 let cmd = "cmd /K chcp 65001"
741 let lnum = [3, 6, 9]
742 else
743 let cmd = &shell
744 let lnum = [1, 3, 5]
745 endif
746
747 enew
748 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100749 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200750 call term_wait(buf, 50)
751
752 " ascii + composing
753 let txt = "a\u0308bc"
754 call term_sendkeys(buf, "echo " . txt . "\r")
755 call term_wait(buf, 50)
756 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
757 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
758 let l = term_scrape(buf, lnum[0] + 1)
759 call assert_equal("a\u0308", l[0].chars)
760 call assert_equal("b", l[1].chars)
761 call assert_equal("c", l[2].chars)
762
763 " multibyte + composing
764 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
765 call term_sendkeys(buf, "echo " . txt . "\r")
766 call term_wait(buf, 50)
767 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
768 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
769 let l = term_scrape(buf, lnum[1] + 1)
770 call assert_equal("\u304b\u3099", l[0].chars)
771 call assert_equal("\u304e", l[1].chars)
772 call assert_equal("\u304f\u3099", l[2].chars)
773 call assert_equal("\u3052", l[3].chars)
774 call assert_equal("\u3053\u3099", l[4].chars)
775
776 " \u00a0 + composing
777 let txt = "abc\u00a0\u0308"
778 call term_sendkeys(buf, "echo " . txt . "\r")
779 call term_wait(buf, 50)
780 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
781 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
782 let l = term_scrape(buf, lnum[2] + 1)
783 call assert_equal("\u00a0\u0308", l[3].chars)
784
785 call term_sendkeys(buf, "exit\r")
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100786 call WaitFor('job_status(g:job) == "dead"')
787 call assert_equal('dead', job_status(g:job))
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200788 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100789 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200790 let &encoding = save_enc
791endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100792
793func Test_terminal_aucmd_on_close()
794 fun Nop()
795 let s:called = 1
796 endfun
797
798 aug repro
799 au!
800 au BufWinLeave * call Nop()
801 aug END
802
803 let [cmd, waittime] = s:get_sleep_cmd()
804
805 call assert_equal(1, winnr('$'))
806 new
807 call setline(1, ['one', 'two'])
808 exe 'term ++close ' . cmd
809 wincmd p
810 call WaitFor("winnr('$') == 2", waittime)
811 call assert_equal(1, s:called)
812 bwipe!
813
814 unlet s:called
815 au! repro
816 delfunc Nop
817endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100818
819func Test_terminal_term_start_empty_command()
820 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
821 call assert_fails(cmd, 'E474')
822 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
823 call assert_fails(cmd, 'E474')
824 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
825 call assert_fails(cmd, 'E474')
826 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
827 call assert_fails(cmd, 'E474')
828endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100829
830func Test_terminal_response_to_control_sequence()
831 if !has('unix')
832 return
833 endif
834
835 let buf = Run_shell_in_terminal({})
Bram Moolenaar086eb872018-03-25 21:24:12 +0200836 call WaitFor({-> term_getline(buf, 1) != ''})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100837
Bram Moolenaar086eb872018-03-25 21:24:12 +0200838 call term_sendkeys(buf, "cat\<CR>")
839 call WaitFor({-> term_getline(buf, 1) =~ 'cat'})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100840
Bram Moolenaar086eb872018-03-25 21:24:12 +0200841 " Request the cursor position.
842 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100843
844 " Wait for output from tty to display, below an empty line.
Bram Moolenaar086eb872018-03-25 21:24:12 +0200845 call WaitFor({-> term_getline(buf, 4) =~ '3;1R'})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100846
Bram Moolenaar086eb872018-03-25 21:24:12 +0200847 " End "cat" gently.
848 call term_sendkeys(buf, "\<CR>\<C-D>")
849
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100850 call Stop_shell_in_terminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100851 exe buf . 'bwipe'
852 unlet g:job
853endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100854
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100855" Run Vim, start a terminal in that Vim with the kill argument,
856" :qall works.
857func Run_terminal_qall_kill(line1, line2)
858 " 1. Open a terminal window and wait for the prompt to appear
859 " 2. set kill using term_setkill()
860 " 3. make Vim exit, it will kill the shell
861 let after = [
862 \ a:line1,
863 \ 'let buf = bufnr("%")',
864 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
865 \ ' sleep 10m',
866 \ 'endwhile',
867 \ a:line2,
868 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
869 \ 'qall',
870 \ ]
871 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100872 return
873 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100874 call assert_equal("done", readfile("Xdone")[0])
875 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100876endfunc
877
878" Run Vim in a terminal, then start a terminal in that Vim with a kill
879" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100880func Test_terminal_qall_kill_arg()
881 call Run_terminal_qall_kill('term ++kill=kill', '')
882endfunc
883
884" Run Vim, start a terminal in that Vim, set the kill argument with
885" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100886func Test_terminal_qall_kill_func()
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100887 call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")')
888endfunc
889
890" Run Vim, start a terminal in that Vim without the kill argument,
891" check that :qall does not exit, :qall! does.
892func Test_terminal_qall_exit()
893 let after = [
894 \ 'term',
895 \ 'let buf = bufnr("%")',
896 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
897 \ ' sleep 10m',
898 \ 'endwhile',
899 \ 'set nomore',
900 \ 'au VimLeavePre * call writefile(["too early"], "Xdone")',
901 \ 'qall',
902 \ 'au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")',
903 \ 'cquit',
904 \ ]
905 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100906 return
907 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100908 call assert_equal("done", readfile("Xdone")[0])
909 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100910endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100911
912" Run Vim in a terminal, then start a terminal in that Vim without a kill
913" argument, check that :confirm qall works.
914func Test_terminal_qall_prompt()
915 if !CanRunVimInTerminal()
916 return
917 endif
918 let buf = RunVimInTerminal('', {})
919
920 " Open a terminal window and wait for the prompt to appear
921 call term_sendkeys(buf, ":term\<CR>")
922 call WaitFor({-> term_getline(buf, 10) =~ '\[running]'})
923 call WaitFor({-> term_getline(buf, 1) !~ '^\s*$'})
924
925 " make Vim exit, it will prompt to kill the shell
926 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
927 call WaitFor({-> term_getline(buf, 20) =~ 'ancel:'})
928 call term_sendkeys(buf, "y")
929 call WaitFor({-> term_getstatus(buf) == "finished"})
930
931 " close the terminal window where Vim was running
932 quit
933endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100934
Bram Moolenaar012eb662018-03-13 17:55:27 +0100935func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100936 augroup repro
937 au!
938 au TerminalOpen * let s:called += 1
939 augroup END
940
941 let s:called = 0
942
943 " Open a terminal window with :terminal
944 terminal
945 call assert_equal(1, s:called)
946 bwipe!
947
948 " Open a terminal window with term_start()
949 call term_start(&shell)
950 call assert_equal(2, s:called)
951 bwipe!
952
953 " Open a hidden terminal buffer with :terminal
954 terminal ++hidden
955 call assert_equal(3, s:called)
956 for buf in term_list()
957 exe buf . "bwipe!"
958 endfor
959
960 " Open a hidden terminal buffer with term_start()
961 let buf = term_start(&shell, {'hidden': 1})
962 call assert_equal(4, s:called)
963 exe buf . "bwipe!"
964
965 unlet s:called
966 au! repro
967endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +0100968
969func Check_dump01(off)
970 call assert_equal('one two three four five', trim(getline(a:off + 1)))
971 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
972 call assert_equal(':popup PopUp :', trim(getline(a:off + 20)))
973endfunc
974
975" just testing basic functionality.
976func Test_terminal_dumpload()
977 call assert_equal(1, winnr('$'))
978 call term_dumpload('dumps/Test_popup_command_01.dump')
979 call assert_equal(2, winnr('$'))
980 call assert_equal(20, line('$'))
981 call Check_dump01(0)
982 quit
983endfunc
984
985func Test_terminal_dumpdiff()
986 call assert_equal(1, winnr('$'))
987 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump')
988 call assert_equal(2, winnr('$'))
989 call assert_equal(62, line('$'))
990 call Check_dump01(0)
991 call Check_dump01(42)
992 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
993 quit
994endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +0100995
996func Test_terminal_dumpdiff_options()
997 set laststatus=0
998 call assert_equal(1, winnr('$'))
999 let height = winheight(0)
1000 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1001 call assert_equal(2, winnr('$'))
1002 call assert_equal(height, winheight(winnr()))
1003 call assert_equal(33, winwidth(winnr()))
1004 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1005 quit
1006
1007 call assert_equal(1, winnr('$'))
1008 let width = winwidth(0)
1009 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1010 call assert_equal(2, winnr('$'))
1011 call assert_equal(width, winwidth(winnr()))
1012 call assert_equal(13, winheight(winnr()))
1013 call assert_equal('something else', bufname('%'))
1014 quit
1015
1016 call assert_equal(1, winnr('$'))
1017 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1018 call assert_equal(1, winnr('$'))
1019 bwipe
1020
1021 set laststatus&
1022endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001023
1024func Test_terminal_api_drop_newwin()
1025 if !CanRunVimInTerminal()
1026 return
1027 endif
1028 call assert_equal(1, winnr('$'))
1029
1030 " Use the title termcap entries to output the escape sequence.
1031 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001032 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001033 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1034 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1035 \ 'redraw',
1036 \ "set t_ts=",
1037 \ ], 'Xscript')
1038 let buf = RunVimInTerminal('-S Xscript', {})
1039 call WaitFor({-> bufnr('Xtextfile') > 0})
1040 call assert_equal('Xtextfile', expand('%:t'))
1041 call assert_true(winnr('$') >= 3)
1042
1043 call StopVimInTerminal(buf)
1044 call delete('Xscript')
1045 bwipe Xtextfile
1046endfunc
1047
1048func Test_terminal_api_drop_oldwin()
1049 if !CanRunVimInTerminal()
1050 return
1051 endif
1052 let firstwinid = win_getid()
1053 split Xtextfile
1054 let textfile_winid = win_getid()
1055 call assert_equal(2, winnr('$'))
1056 call win_gotoid(firstwinid)
1057
1058 " Use the title termcap entries to output the escape sequence.
1059 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001060 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001061 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1062 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1063 \ 'redraw',
1064 \ "set t_ts=",
1065 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001066 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001067 call WaitFor({-> expand('%:t') =='Xtextfile'})
1068 call assert_equal(textfile_winid, win_getid())
1069
1070 call StopVimInTerminal(buf)
1071 call delete('Xscript')
1072 bwipe Xtextfile
1073endfunc
1074
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001075func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001076 let g:called_bufnum = a:bufnum
1077 let g:called_arg = a:arg
1078endfunc
1079
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001080func WriteApiCall(funcname)
1081 " Use the title termcap entries to output the escape sequence.
1082 call writefile([
1083 \ 'set title',
1084 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1085 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1086 \ 'redraw',
1087 \ "set t_ts=",
1088 \ ], 'Xscript')
1089endfunc
1090
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001091func Test_terminal_api_call()
1092 if !CanRunVimInTerminal()
1093 return
1094 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001095
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001096 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001097 let buf = RunVimInTerminal('-S Xscript', {})
1098 call WaitFor({-> exists('g:called_bufnum')})
1099 call assert_equal(buf, g:called_bufnum)
1100 call assert_equal(['hello', 123], g:called_arg)
1101
1102 call StopVimInTerminal(buf)
1103 call delete('Xscript')
1104 unlet g:called_bufnum
1105 unlet g:called_arg
1106endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001107
1108func Test_terminal_api_call_fails()
1109 if !CanRunVimInTerminal()
1110 return
1111 endif
1112
1113 call WriteApiCall('TryThis')
1114 call ch_logfile('Xlog', 'w')
1115 let buf = RunVimInTerminal('-S Xscript', {})
1116 call WaitFor({-> string(readfile('Xlog')) =~ 'Invalid function name: TryThis'})
1117
1118 call StopVimInTerminal(buf)
1119 call delete('Xscript')
1120 call ch_logfile('', '')
1121 call delete('Xlog')
1122endfunc