blob: b24dbc997403d7f462e13961b3fc6ce0b0cbda32 [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 Moolenaar606cb8b2018-05-03 20:40:20 +020035 au TerminalOpen * let b:done = 'yes'
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 Moolenaaraa5df7e2019-02-03 14:53:10 +010042 " ConPTY works on anonymous pipe.
43 if !has('conpty')
44 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
45 call assert_match('^\\\\.\\pipe\\', term_gettty(''))
46 endif
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020047 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020048 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020049 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020050 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020051 call assert_match('%aR[^\n]*running]', execute('ls R'))
52 call assert_notmatch('%[^\n]*running]', execute('ls F'))
53 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020054
Bram Moolenaar94053a52017-08-01 21:44:33 +020055 call Stop_shell_in_terminal(buf)
56 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020057 call assert_equal('n', mode())
58 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020059 call assert_match('%aF[^\n]*finished]', execute('ls F'))
60 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
61 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020062
Bram Moolenaar94053a52017-08-01 21:44:33 +020063 " closing window wipes out the terminal buffer a with finished job
64 close
65 call assert_equal("", bufname(buf))
66
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020067 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020068 unlet g:job
69endfunc
70
71func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020072 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020073 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020074 call term_wait(buf)
75
76 setlocal modifiable
77 exe "normal Axxx\<Esc>"
78 call assert_fails(buf . 'bwipe', 'E517')
79 undo
80
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020081 exe buf . 'bwipe'
82 unlet g:job
83endfunc
84
Bram Moolenaar94053a52017-08-01 21:44:33 +020085func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020086 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +020087 call assert_fails(buf . 'bwipe', 'E517')
88 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +020089 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +020090 call assert_equal("", bufname(buf))
91
92 unlet g:job
93endfunc
94
Bram Moolenaar8adb0d02017-09-17 19:08:02 +020095func Test_terminal_split_quit()
96 let buf = Run_shell_in_terminal({})
97 call term_wait(buf)
98 split
99 quit!
100 call term_wait(buf)
101 sleep 50m
102 call assert_equal('run', job_status(g:job))
103
104 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200105 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200106
107 exe buf . 'bwipe'
108 unlet g:job
109endfunc
110
Bram Moolenaar94053a52017-08-01 21:44:33 +0200111func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200112 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200113 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200114 quit
115 for nr in range(1, winnr('$'))
116 call assert_notequal(winbufnr(nr), buf)
117 endfor
118 call assert_true(bufloaded(buf))
119 call assert_true(buflisted(buf))
120
121 exe 'split ' . buf . 'buf'
122 call Stop_shell_in_terminal(buf)
123 exe buf . 'bwipe'
124
125 unlet g:job
126endfunc
127
Bram Moolenaar1e115362019-01-09 23:01:02 +0100128func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200129 exe g:buf . 'bwipe!'
130 let g:buf = 0
131endfunc
132
Bram Moolenaar9d189612017-09-09 18:11:00 +0200133func Get_cat_123_cmd()
134 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100135 if !has('conpty')
136 return 'cmd /c "cls && color 2 && echo 123"'
137 else
138 " When clearing twice, extra sequence is not output.
139 return 'cmd /c "cls && cls && color 2 && echo 123"'
140 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200141 else
142 call writefile(["\<Esc>[32m123"], 'Xtext')
143 return "cat Xtext"
144 endif
145endfunc
146
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200147func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200148 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200149 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
150 let g:job = term_getjob(g:buf)
151
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200152 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
153 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200154 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100155 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200156 call delete('Xtext')
157endfunc
158
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200159func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200160 let l = term_scrape(a:buf, 0)
161 call assert_true(len(l) == 0)
162 let l = term_scrape(a:buf, 999)
163 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200164 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200165 call assert_true(len(l) > 0)
166 call assert_equal('1', l[0].chars)
167 call assert_equal('2', l[1].chars)
168 call assert_equal('3', l[2].chars)
169 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar81df6352018-12-22 18:25:30 +0100170 if has('win32')
171 " On Windows 'background' always defaults to dark, even though the terminal
172 " may use a light background. Therefore accept both white and black.
173 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200174 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100175 if &background == 'light'
176 call assert_equal('#ffffff', l[0].bg)
177 else
178 call assert_equal('#000000', l[0].bg)
179 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200180 endif
181
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200182 let l = term_getline(a:buf, -1)
183 call assert_equal('', l)
184 let l = term_getline(a:buf, 0)
185 call assert_equal('', l)
186 let l = term_getline(a:buf, 999)
187 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200188 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200189 call assert_equal('123', l)
190endfunc
191
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200192func Test_terminal_scrape_123()
193 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200194 let buf = term_start(cmd)
195
196 let termlist = term_list()
197 call assert_equal(1, len(termlist))
198 call assert_equal(buf, termlist[0])
199
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200200 " Nothing happens with invalid buffer number
201 call term_wait(1234)
202
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200203 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200204 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200205 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200206 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200207 call Check_123(buf)
208
209 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100210 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200211 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200212 call term_wait(buf)
213 call Check_123(buf)
214
215 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200216 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200217endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200218
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200219func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200220 call writefile(["léttまrs"], 'Xtext')
221 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200222 " Run cmd with UTF-8 codepage to make the type command print the expected
223 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100224 let buf = term_start("cmd /K chcp 65001")
225 call term_sendkeys(buf, "type Xtext\<CR>")
226 call term_sendkeys(buf, "exit\<CR>")
227 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200228 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100229 let buf = term_start("cat Xtext")
230 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200231 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200232
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100233 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
234 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200235 call assert_true(len(l) >= 7)
236 call assert_equal('l', l[0].chars)
237 call assert_equal('é', l[1].chars)
238 call assert_equal(1, l[1].width)
239 call assert_equal('t', l[2].chars)
240 call assert_equal('t', l[3].chars)
241 call assert_equal('ま', l[4].chars)
242 call assert_equal(2, l[4].width)
243 call assert_equal('r', l[5].chars)
244 call assert_equal('s', l[6].chars)
245
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100246 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200247 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100248 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200249
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100250 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200251 call delete('Xtext')
252endfunc
253
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200254func Test_terminal_scroll()
255 call writefile(range(1, 200), 'Xtext')
256 if has('win32')
257 let cmd = 'cmd /c "type Xtext"'
258 else
259 let cmd = "cat Xtext"
260 endif
261 let buf = term_start(cmd)
262
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100263 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200264 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200265 call term_wait(buf)
266 if has('win32')
267 " TODO: this should not be needed
268 sleep 100m
269 endif
270
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200271 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200272 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200273 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200274 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200275 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200276 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200277 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200278
279 exe buf . 'bwipe'
280 call delete('Xtext')
281endfunc
282
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200283func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200284 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200285 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200286 call writefile(range(150), 'Xtext')
287 if has('win32')
288 call term_sendkeys(buf, "type Xtext\<CR>")
289 else
290 call term_sendkeys(buf, "cat Xtext\<CR>")
291 endif
292 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200293 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200294 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200295 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200296 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200297
298 call Stop_shell_in_terminal(buf)
299 call term_wait(buf)
300 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200301 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100302 call delete('Xtext')
303endfunc
304
305func Test_terminal_postponed_scrollback()
306 if !has('unix')
307 " tail -f only works on Unix
308 return
309 endif
310
311 call writefile(range(50), 'Xtext')
312 call writefile([
313 \ 'terminal',
314 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
315 \ 'sleep 100m',
316 \ 'call feedkeys("\<C-W>N", "xt")',
317 \ ], 'XTest_postponed')
318 let buf = RunVimInTerminal('-S XTest_postponed', {})
319 " Check that the Xtext lines are displayed and in Terminal-Normal mode
320 call VerifyScreenDump(buf, 'Test_terminal_01', {})
321
322 silent !echo 'one more line' >>Xtext
323 " Sceen will not change, move cursor to get a different dump
324 call term_sendkeys(buf, "k")
325 call VerifyScreenDump(buf, 'Test_terminal_02', {})
326
327 " Back to Terminal-Job mode, text will scroll and show the extra line.
328 call term_sendkeys(buf, "a")
329 call VerifyScreenDump(buf, 'Test_terminal_03', {})
330
331 call term_wait(buf)
332 call term_sendkeys(buf, "\<C-C>")
333 call term_wait(buf)
334 call term_sendkeys(buf, "exit\<CR>")
335 call term_wait(buf)
336 call term_sendkeys(buf, ":q\<CR>")
337 call StopVimInTerminal(buf)
338 call delete('XTest_postponed')
339 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200340endfunc
341
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200342func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200343 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200344
Bram Moolenaarb2412082017-08-20 18:09:14 +0200345 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200346 let size = term_getsize('')
347 bwipe!
348 call assert_equal(5, size[0])
349
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200350 call term_start(cmd, {'term_rows': 6})
351 let size = term_getsize('')
352 bwipe!
353 call assert_equal(6, size[0])
354
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200355 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200356 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaara42d3632018-04-14 17:05:38 +0200357 call assert_equal([5, 33], term_getsize(''))
358
359 call term_setsize('', 6, 0)
360 call assert_equal([6, 33], term_getsize(''))
361
362 call term_setsize('', 0, 35)
363 call assert_equal([6, 35], term_getsize(''))
364
365 call term_setsize('', 7, 30)
366 call assert_equal([7, 30], term_getsize(''))
367
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200368 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200369 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200370
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200371 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
372 let size = term_getsize('')
373 bwipe!
374 call assert_equal([6, 36], size)
375
Bram Moolenaarb2412082017-08-20 18:09:14 +0200376 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200377 let size = term_getsize('')
378 bwipe!
379 call assert_equal(20, size[1])
380
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200381 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
382 let size = term_getsize('')
383 bwipe!
384 call assert_equal(26, size[1])
385
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200386 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200387 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200388 let size = term_getsize('')
389 bwipe!
390 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200391
392 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
393 let size = term_getsize('')
394 bwipe!
395 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200396
397 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200398endfunc
399
400func Test_terminal_curwin()
401 let cmd = Get_cat_123_cmd()
402 call assert_equal(1, winnr('$'))
403
404 split dummy
405 exe 'terminal ++curwin ' . cmd
406 call assert_equal(2, winnr('$'))
407 bwipe!
408
409 split dummy
410 call term_start(cmd, {'curwin': 1})
411 call assert_equal(2, winnr('$'))
412 bwipe!
413
414 split dummy
415 call setline(1, 'change')
416 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
417 call assert_equal(2, winnr('$'))
418 exe 'terminal! ++curwin ' . cmd
419 call assert_equal(2, winnr('$'))
420 bwipe!
421
422 split dummy
423 call setline(1, 'change')
424 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
425 call assert_equal(2, winnr('$'))
426 bwipe!
427
428 split dummy
429 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200430 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200431endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200432
Bram Moolenaarff546792017-11-21 14:47:57 +0100433func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200434 if s:python != ''
435 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200436 " 500 was not enough for Travis
437 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200438 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200439 echo 'This will take five seconds...'
440 let waittime = 2000
441 if has('win32')
442 let cmd = $windir . '\system32\timeout.exe 1'
443 else
444 let cmd = 'sleep 1'
445 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200446 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100447 return [cmd, waittime]
448endfunc
449
450func Test_terminal_finish_open_close()
451 call assert_equal(1, winnr('$'))
452
453 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200454
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100455 " shell terminal closes automatically
456 terminal
457 let buf = bufnr('%')
458 call assert_equal(2, winnr('$'))
459 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200460 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100461 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200462 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100463
464 " shell terminal that does not close automatically
465 terminal ++noclose
466 let buf = bufnr('%')
467 call assert_equal(2, winnr('$'))
468 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200469 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100470 call Stop_shell_in_terminal(buf)
471 call assert_equal(2, winnr('$'))
472 quit
473 call assert_equal(1, winnr('$'))
474
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200475 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200476 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200477 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200478 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200479
480 call term_start(cmd, {'term_finish': 'close'})
481 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200482 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200483 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200484 call assert_equal(1, winnr('$'))
485
486 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200487 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200488 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200489 bwipe
490
491 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200492 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200493 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200494 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200495
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200496 exe 'terminal ++hidden ++open ' . cmd
497 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200498 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200499 bwipe
500
501 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
502 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200503 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200504 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200505
506 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
507 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
508 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
509 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
510
511 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200512 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200513 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200514 call assert_equal(4, winheight(0))
515 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200516endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200517
518func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200519 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200520 return
521 endif
522 call mkdir('Xdir')
523 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200524 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200525
526 exe buf . 'bwipe'
527 call delete('Xdir', 'rf')
528endfunc
529
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200530func Test_terminal_cwd_failure()
531 " Case 1: Provided directory is not actually a directory. Attempt to make
532 " the file executable as well.
533 call writefile([], 'Xfile')
534 call setfperm('Xfile', 'rwx------')
535 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
536 call delete('Xfile')
537
538 " Case 2: Directory does not exist.
539 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
540
541 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100542 " Skip this for root, it will be accessible anyway.
543 if $USER != 'root'
544 call mkdir('XdirNoAccess', '', '0600')
545 " return early if the directory permissions could not be set properly
546 if getfperm('XdirNoAccess')[2] == 'x'
547 call delete('XdirNoAccess', 'rf')
548 return
549 endif
550 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
551 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200552 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200553endfunc
554
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100555func Test_terminal_servername()
556 if !has('clientserver')
557 return
558 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200559 call s:test_environment("VIM_SERVERNAME", v:servername)
560endfunc
561
562func Test_terminal_version()
563 call s:test_environment("VIM_TERMINAL", string(v:version))
564endfunc
565
566func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100567 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100568 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200569 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100570 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200571 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100572 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200573 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100574 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100575 call term_wait(buf)
576 call Stop_shell_in_terminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200577 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100578
Bram Moolenaar012eb662018-03-13 17:55:27 +0100579 exe buf . 'bwipe'
580 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100581endfunc
582
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200583func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100584 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200585 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200586 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100587 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100588 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100589 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100590 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100591 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100592 call term_wait(buf)
593 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200594 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200595
Bram Moolenaar012eb662018-03-13 17:55:27 +0100596 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200597endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200598
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200599func Test_terminal_list_args()
600 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
601 call assert_fails(buf . 'bwipe', 'E517')
602 exe buf . 'bwipe!'
603 call assert_equal("", bufname(buf))
604endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200605
606func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100607 let buf = term_start(&shell)
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100608 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200609 " The shell or something else has a problem dealing with more than 1000
610 " characters at the same time.
611 let len = 1000
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100612 " NPFS is used in Windows, nonblocking mode does not work properly.
613 elseif has('win32')
614 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200615 else
616 let len = 5000
617 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200618
619 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100620 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200621 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100622 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200623
624 " On MS-Windows there is an extra empty line below "done". Find "done" in
625 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100626 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100627 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100628 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200629 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100630 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200631 endif
632 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200633
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100634 let g:job = term_getjob(buf)
635 call Stop_shell_in_terminal(buf)
636 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200637 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200638 bwipe
639endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200640
641func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200642 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200643 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200644 endif
Bram Moolenaar1580f752018-06-03 15:26:36 +0200645 if has('win32')
646 " TODO: enable once writing to stdin works on MS-Windows
647 return
648 endif
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200649 new
650 call setline(1, ['one', 'two', 'three'])
651 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200652 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200653 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200654 call assert_equal(['3', '3', '14'], nrs)
655 bwipe
656
Bram Moolenaardada6d22017-09-02 17:18:35 +0200657 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200658 call setline(1, ['one', 'two', 'three', 'four'])
659 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200660 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200661 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200662 call assert_equal(['2', '2', '10'], nrs)
663 bwipe
664
Bram Moolenaardada6d22017-09-02 17:18:35 +0200665 if executable('python')
666 new
667 call setline(1, ['print("hello")'])
668 1term ++eof=exit() python
669 " MS-Windows echoes the input, Unix doesn't.
670 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
671 if getline(1) =~ 'hello'
672 call assert_equal('hello', getline(1))
673 else
674 call assert_equal('hello', getline(line('$') - 1))
675 endif
676 bwipe
677
678 if has('win32')
679 new
680 call setline(1, ['print("hello")'])
681 1term ++eof=<C-Z> python
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200682 call WaitForAssert({-> assert_match('Z', getline("$"))})
Bram Moolenaardada6d22017-09-02 17:18:35 +0200683 call assert_equal('hello', getline(line('$') - 1))
684 bwipe
685 endif
686 endif
687
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200688 bwipe!
689endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200690
691func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200692 let buf = term_start('NONE', {})
693 call assert_notequal(0, buf)
694
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200695 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200696 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100697 if has('gui_running') && !has('win32')
698 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200699 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100700 else
701 " Otherwise using a job works on all systems.
702 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200703 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200704 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200705
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200706 bwipe!
707endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200708
709func Test_terminal_special_chars()
710 " this file name only works on Unix
711 if !has('unix')
712 return
713 endif
714 call mkdir('Xdir with spaces')
715 call writefile(['x'], 'Xdir with spaces/quoted"file')
716 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200717 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200718 call term_wait('')
719
720 call delete('Xdir with spaces', 'rf')
721 bwipe
722endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200723
724func Test_terminal_wrong_options()
725 call assert_fails('call term_start(&shell, {
726 \ "in_io": "file",
727 \ "in_name": "xxx",
728 \ "out_io": "file",
729 \ "out_name": "xxx",
730 \ "err_io": "file",
731 \ "err_name": "xxx"
732 \ })', 'E474:')
733 call assert_fails('call term_start(&shell, {
734 \ "out_buf": bufnr("%")
735 \ })', 'E474:')
736 call assert_fails('call term_start(&shell, {
737 \ "err_buf": bufnr("%")
738 \ })', 'E474:')
739endfunc
740
741func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200742 let cmd = Get_cat_123_cmd()
743 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
744 call term_wait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100745 " ConPTY may precede escape sequence. There are things that are not so.
746 if !has('conpty')
747 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
748 call assert_match('123', readfile('Xfile')[0])
749 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200750 let g:job = term_getjob(buf)
751 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
752 call delete('Xfile')
753 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200754
755 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200756 call writefile(['one line'], 'Xfile')
757 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
758 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200759 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200760 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200761 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200762 bwipe
763 call delete('Xfile')
764 endif
765endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200766
767func TerminalTmap(remap)
768 let buf = Run_shell_in_terminal({})
769 call assert_equal('t', mode())
770
771 if a:remap
772 tmap 123 456
773 else
774 tnoremap 123 456
775 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100776 " don't use abcde, it's an existing command
777 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200778 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100779 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200780 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200781 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200782 let lnum = term_getcursor(buf)[0]
783 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100784 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200785 else
786 call assert_match('456', term_getline(buf, lnum))
787 endif
788
789 call term_sendkeys(buf, "\r")
790 call Stop_shell_in_terminal(buf)
791 call term_wait(buf)
792
793 tunmap 123
794 tunmap 456
795 call assert_equal('', maparg('123', 't'))
796 close
797 unlet g:job
798endfunc
799
800func Test_terminal_tmap()
801 call TerminalTmap(1)
802 call TerminalTmap(0)
803endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200804
805func Test_terminal_wall()
806 let buf = Run_shell_in_terminal({})
807 wall
808 call Stop_shell_in_terminal(buf)
809 call term_wait(buf)
810 exe buf . 'bwipe'
811 unlet g:job
812endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200813
Bram Moolenaar7a760922018-02-19 23:10:02 +0100814func Test_terminal_wqall()
815 let buf = Run_shell_in_terminal({})
816 call assert_fails('wqall', 'E948')
817 call Stop_shell_in_terminal(buf)
818 call term_wait(buf)
819 exe buf . 'bwipe'
820 unlet g:job
821endfunc
822
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200823func Test_terminal_composing_unicode()
824 let save_enc = &encoding
825 set encoding=utf-8
826
827 if has('win32')
828 let cmd = "cmd /K chcp 65001"
829 let lnum = [3, 6, 9]
830 else
831 let cmd = &shell
832 let lnum = [1, 3, 5]
833 endif
834
835 enew
836 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100837 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200838 call term_wait(buf, 50)
839
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200840 if has('win32')
841 call assert_equal('cmd', job_info(g:job).cmd[0])
842 else
843 call assert_equal(&shell, job_info(g:job).cmd[0])
844 endif
845
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200846 " ascii + composing
847 let txt = "a\u0308bc"
848 call term_sendkeys(buf, "echo " . txt . "\r")
849 call term_wait(buf, 50)
850 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
851 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
852 let l = term_scrape(buf, lnum[0] + 1)
853 call assert_equal("a\u0308", l[0].chars)
854 call assert_equal("b", l[1].chars)
855 call assert_equal("c", l[2].chars)
856
857 " multibyte + composing
858 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
859 call term_sendkeys(buf, "echo " . txt . "\r")
860 call term_wait(buf, 50)
861 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
862 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
863 let l = term_scrape(buf, lnum[1] + 1)
864 call assert_equal("\u304b\u3099", l[0].chars)
865 call assert_equal("\u304e", l[1].chars)
866 call assert_equal("\u304f\u3099", l[2].chars)
867 call assert_equal("\u3052", l[3].chars)
868 call assert_equal("\u3053\u3099", l[4].chars)
869
870 " \u00a0 + composing
871 let txt = "abc\u00a0\u0308"
872 call term_sendkeys(buf, "echo " . txt . "\r")
873 call term_wait(buf, 50)
874 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
875 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
876 let l = term_scrape(buf, lnum[2] + 1)
877 call assert_equal("\u00a0\u0308", l[3].chars)
878
879 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200880 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200881 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100882 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200883 let &encoding = save_enc
884endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100885
886func Test_terminal_aucmd_on_close()
887 fun Nop()
888 let s:called = 1
889 endfun
890
891 aug repro
892 au!
893 au BufWinLeave * call Nop()
894 aug END
895
896 let [cmd, waittime] = s:get_sleep_cmd()
897
898 call assert_equal(1, winnr('$'))
899 new
900 call setline(1, ['one', 'two'])
901 exe 'term ++close ' . cmd
902 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200903 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100904 call assert_equal(1, s:called)
905 bwipe!
906
907 unlet s:called
908 au! repro
909 delfunc Nop
910endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100911
912func Test_terminal_term_start_empty_command()
913 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
914 call assert_fails(cmd, 'E474')
915 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
916 call assert_fails(cmd, 'E474')
917 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
918 call assert_fails(cmd, 'E474')
919 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
920 call assert_fails(cmd, 'E474')
921endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100922
923func Test_terminal_response_to_control_sequence()
924 if !has('unix')
925 return
926 endif
927
928 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200929 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100930
Bram Moolenaar086eb872018-03-25 21:24:12 +0200931 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200932 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100933
Bram Moolenaar086eb872018-03-25 21:24:12 +0200934 " Request the cursor position.
935 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100936
937 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200938 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100939
Bram Moolenaar086eb872018-03-25 21:24:12 +0200940 " End "cat" gently.
941 call term_sendkeys(buf, "\<CR>\<C-D>")
942
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100943 call Stop_shell_in_terminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100944 exe buf . 'bwipe'
945 unlet g:job
946endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100947
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100948" Run Vim, start a terminal in that Vim with the kill argument,
949" :qall works.
950func Run_terminal_qall_kill(line1, line2)
951 " 1. Open a terminal window and wait for the prompt to appear
952 " 2. set kill using term_setkill()
953 " 3. make Vim exit, it will kill the shell
954 let after = [
955 \ a:line1,
956 \ 'let buf = bufnr("%")',
957 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
958 \ ' sleep 10m',
959 \ 'endwhile',
960 \ a:line2,
961 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
962 \ 'qall',
963 \ ]
964 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100965 return
966 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100967 call assert_equal("done", readfile("Xdone")[0])
968 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100969endfunc
970
971" Run Vim in a terminal, then start a terminal in that Vim with a kill
972" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100973func Test_terminal_qall_kill_arg()
974 call Run_terminal_qall_kill('term ++kill=kill', '')
975endfunc
976
977" Run Vim, start a terminal in that Vim, set the kill argument with
978" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100979func Test_terminal_qall_kill_func()
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100980 call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")')
981endfunc
982
983" Run Vim, start a terminal in that Vim without the kill argument,
984" check that :qall does not exit, :qall! does.
985func Test_terminal_qall_exit()
986 let after = [
987 \ 'term',
988 \ 'let buf = bufnr("%")',
989 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
990 \ ' sleep 10m',
991 \ 'endwhile',
992 \ 'set nomore',
993 \ 'au VimLeavePre * call writefile(["too early"], "Xdone")',
994 \ 'qall',
995 \ 'au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")',
996 \ 'cquit',
997 \ ]
998 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100999 return
1000 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001001 call assert_equal("done", readfile("Xdone")[0])
1002 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001003endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001004
1005" Run Vim in a terminal, then start a terminal in that Vim without a kill
1006" argument, check that :confirm qall works.
1007func Test_terminal_qall_prompt()
1008 if !CanRunVimInTerminal()
1009 return
1010 endif
1011 let buf = RunVimInTerminal('', {})
1012
1013 " Open a terminal window and wait for the prompt to appear
1014 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001015 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1016 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001017
1018 " make Vim exit, it will prompt to kill the shell
1019 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001020 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001021 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001022 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001023
1024 " close the terminal window where Vim was running
1025 quit
1026endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001027
Bram Moolenaar012eb662018-03-13 17:55:27 +01001028func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001029 augroup repro
1030 au!
1031 au TerminalOpen * let s:called += 1
1032 augroup END
1033
1034 let s:called = 0
1035
1036 " Open a terminal window with :terminal
1037 terminal
1038 call assert_equal(1, s:called)
1039 bwipe!
1040
1041 " Open a terminal window with term_start()
1042 call term_start(&shell)
1043 call assert_equal(2, s:called)
1044 bwipe!
1045
1046 " Open a hidden terminal buffer with :terminal
1047 terminal ++hidden
1048 call assert_equal(3, s:called)
1049 for buf in term_list()
1050 exe buf . "bwipe!"
1051 endfor
1052
1053 " Open a hidden terminal buffer with term_start()
1054 let buf = term_start(&shell, {'hidden': 1})
1055 call assert_equal(4, s:called)
1056 exe buf . "bwipe!"
1057
1058 unlet s:called
1059 au! repro
1060endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001061
1062func Check_dump01(off)
1063 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1064 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001065 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001066endfunc
1067
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001068func Test_terminal_dumpwrite_composing()
1069 if !CanRunVimInTerminal()
1070 return
1071 endif
1072 let save_enc = &encoding
1073 set encoding=utf-8
1074 call assert_equal(1, winnr('$'))
1075
1076 let text = " a\u0300 e\u0302 o\u0308"
1077 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001078 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001079 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001080 call term_dumpwrite(buf, 'Xdump')
1081 let dumpline = readfile('Xdump')[0]
1082 call assert_match('|à| |ê| |ö', dumpline)
1083
1084 call StopVimInTerminal(buf)
1085 call delete('Xcomposing')
1086 call delete('Xdump')
1087 let &encoding = save_enc
1088endfunc
1089
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001090" just testing basic functionality.
1091func Test_terminal_dumpload()
1092 call assert_equal(1, winnr('$'))
1093 call term_dumpload('dumps/Test_popup_command_01.dump')
1094 call assert_equal(2, winnr('$'))
1095 call assert_equal(20, line('$'))
1096 call Check_dump01(0)
1097 quit
1098endfunc
1099
1100func Test_terminal_dumpdiff()
1101 call assert_equal(1, winnr('$'))
1102 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump')
1103 call assert_equal(2, winnr('$'))
1104 call assert_equal(62, line('$'))
1105 call Check_dump01(0)
1106 call Check_dump01(42)
1107 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1108 quit
1109endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001110
1111func Test_terminal_dumpdiff_options()
1112 set laststatus=0
1113 call assert_equal(1, winnr('$'))
1114 let height = winheight(0)
1115 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1116 call assert_equal(2, winnr('$'))
1117 call assert_equal(height, winheight(winnr()))
1118 call assert_equal(33, winwidth(winnr()))
1119 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1120 quit
1121
1122 call assert_equal(1, winnr('$'))
1123 let width = winwidth(0)
1124 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1125 call assert_equal(2, winnr('$'))
1126 call assert_equal(width, winwidth(winnr()))
1127 call assert_equal(13, winheight(winnr()))
1128 call assert_equal('something else', bufname('%'))
1129 quit
1130
1131 call assert_equal(1, winnr('$'))
1132 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1133 call assert_equal(1, winnr('$'))
1134 bwipe
1135
1136 set laststatus&
1137endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001138
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001139func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001140 call assert_equal(1, winnr('$'))
1141
1142 " Use the title termcap entries to output the escape sequence.
1143 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001144 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001145 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001146 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001147 \ 'redraw',
1148 \ "set t_ts=",
1149 \ ], 'Xscript')
1150 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001151 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001152 call assert_equal('Xtextfile', expand('%:t'))
1153 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001154 return buf
1155endfunc
1156
1157func Test_terminal_api_drop_newwin()
1158 if !CanRunVimInTerminal()
1159 return
1160 endif
1161 let buf = Api_drop_common('')
1162 call assert_equal(0, &bin)
1163 call assert_equal('', &fenc)
1164
1165 call StopVimInTerminal(buf)
1166 call delete('Xscript')
1167 bwipe Xtextfile
1168endfunc
1169
1170func Test_terminal_api_drop_newwin_bin()
1171 if !CanRunVimInTerminal()
1172 return
1173 endif
1174 let buf = Api_drop_common(',{"bin":1}')
1175 call assert_equal(1, &bin)
1176
1177 call StopVimInTerminal(buf)
1178 call delete('Xscript')
1179 bwipe Xtextfile
1180endfunc
1181
1182func Test_terminal_api_drop_newwin_binary()
1183 if !CanRunVimInTerminal()
1184 return
1185 endif
1186 let buf = Api_drop_common(',{"binary":1}')
1187 call assert_equal(1, &bin)
1188
1189 call StopVimInTerminal(buf)
1190 call delete('Xscript')
1191 bwipe Xtextfile
1192endfunc
1193
1194func Test_terminal_api_drop_newwin_nobin()
1195 if !CanRunVimInTerminal()
1196 return
1197 endif
1198 set binary
1199 let buf = Api_drop_common(',{"nobin":1}')
1200 call assert_equal(0, &bin)
1201
1202 call StopVimInTerminal(buf)
1203 call delete('Xscript')
1204 bwipe Xtextfile
1205 set nobinary
1206endfunc
1207
1208func Test_terminal_api_drop_newwin_nobinary()
1209 if !CanRunVimInTerminal()
1210 return
1211 endif
1212 set binary
1213 let buf = Api_drop_common(',{"nobinary":1}')
1214 call assert_equal(0, &bin)
1215
1216 call StopVimInTerminal(buf)
1217 call delete('Xscript')
1218 bwipe Xtextfile
1219 set nobinary
1220endfunc
1221
1222func Test_terminal_api_drop_newwin_ff()
1223 if !CanRunVimInTerminal()
1224 return
1225 endif
1226 let buf = Api_drop_common(',{"ff":"dos"}')
1227 call assert_equal("dos", &ff)
1228
1229 call StopVimInTerminal(buf)
1230 call delete('Xscript')
1231 bwipe Xtextfile
1232endfunc
1233
1234func Test_terminal_api_drop_newwin_fileformat()
1235 if !CanRunVimInTerminal()
1236 return
1237 endif
1238 let buf = Api_drop_common(',{"fileformat":"dos"}')
1239 call assert_equal("dos", &ff)
1240
1241 call StopVimInTerminal(buf)
1242 call delete('Xscript')
1243 bwipe Xtextfile
1244endfunc
1245
1246func Test_terminal_api_drop_newwin_enc()
1247 if !CanRunVimInTerminal()
1248 return
1249 endif
1250 let buf = Api_drop_common(',{"enc":"utf-16"}')
1251 call assert_equal("utf-16", &fenc)
1252
1253 call StopVimInTerminal(buf)
1254 call delete('Xscript')
1255 bwipe Xtextfile
1256endfunc
1257
1258func Test_terminal_api_drop_newwin_encoding()
1259 if !CanRunVimInTerminal()
1260 return
1261 endif
1262 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1263 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001264
1265 call StopVimInTerminal(buf)
1266 call delete('Xscript')
1267 bwipe Xtextfile
1268endfunc
1269
1270func Test_terminal_api_drop_oldwin()
1271 if !CanRunVimInTerminal()
1272 return
1273 endif
1274 let firstwinid = win_getid()
1275 split Xtextfile
1276 let textfile_winid = win_getid()
1277 call assert_equal(2, winnr('$'))
1278 call win_gotoid(firstwinid)
1279
1280 " Use the title termcap entries to output the escape sequence.
1281 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001282 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001283 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1284 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1285 \ 'redraw',
1286 \ "set t_ts=",
1287 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001288 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001289 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001290 call assert_equal(textfile_winid, win_getid())
1291
1292 call StopVimInTerminal(buf)
1293 call delete('Xscript')
1294 bwipe Xtextfile
1295endfunc
1296
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001297func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001298 let g:called_bufnum = a:bufnum
1299 let g:called_arg = a:arg
1300endfunc
1301
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001302func WriteApiCall(funcname)
1303 " Use the title termcap entries to output the escape sequence.
1304 call writefile([
1305 \ 'set title',
1306 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1307 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1308 \ 'redraw',
1309 \ "set t_ts=",
1310 \ ], 'Xscript')
1311endfunc
1312
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001313func Test_terminal_api_call()
1314 if !CanRunVimInTerminal()
1315 return
1316 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001317
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001318 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001319 let buf = RunVimInTerminal('-S Xscript', {})
1320 call WaitFor({-> exists('g:called_bufnum')})
1321 call assert_equal(buf, g:called_bufnum)
1322 call assert_equal(['hello', 123], g:called_arg)
1323
1324 call StopVimInTerminal(buf)
1325 call delete('Xscript')
1326 unlet g:called_bufnum
1327 unlet g:called_arg
1328endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001329
1330func Test_terminal_api_call_fails()
1331 if !CanRunVimInTerminal()
1332 return
1333 endif
1334
1335 call WriteApiCall('TryThis')
1336 call ch_logfile('Xlog', 'w')
1337 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001338 call WaitForAssert({-> assert_match('Invalid function name: TryThis', string(readfile('Xlog')))})
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001339
1340 call StopVimInTerminal(buf)
1341 call delete('Xscript')
1342 call ch_logfile('', '')
1343 call delete('Xlog')
1344endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001345
Bram Moolenaara997b452018-04-17 23:24:06 +02001346let s:caught_e937 = 0
1347
1348func Tapi_Delete(bufnum, arg)
1349 try
1350 execute 'bdelete!' a:bufnum
1351 catch /E937:/
1352 let s:caught_e937 = 1
1353 endtry
1354endfunc
1355
1356func Test_terminal_api_call_fail_delete()
1357 if !CanRunVimInTerminal()
1358 return
1359 endif
1360
1361 call WriteApiCall('Tapi_Delete')
1362 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001363 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001364
1365 call StopVimInTerminal(buf)
1366 call delete('Xscript')
1367 call ch_logfile('', '')
1368endfunc
1369
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001370func Test_terminal_ansicolors_default()
1371 let colors = [
1372 \ '#000000', '#e00000',
1373 \ '#00e000', '#e0e000',
1374 \ '#0000e0', '#e000e0',
1375 \ '#00e0e0', '#e0e0e0',
1376 \ '#808080', '#ff4040',
1377 \ '#40ff40', '#ffff40',
1378 \ '#4040ff', '#ff40ff',
1379 \ '#40ffff', '#ffffff',
1380 \]
1381
1382 let buf = Run_shell_in_terminal({})
1383 call assert_equal(colors, term_getansicolors(buf))
1384 call Stop_shell_in_terminal(buf)
1385 call term_wait(buf)
1386
1387 exe buf . 'bwipe'
1388endfunc
1389
1390let s:test_colors = [
1391 \ '#616e64', '#0d0a79',
1392 \ '#6d610d', '#0a7373',
1393 \ '#690d0a', '#6d696e',
1394 \ '#0d0a6f', '#616e0d',
1395 \ '#0a6479', '#6d0d0a',
1396 \ '#617373', '#0d0a69',
1397 \ '#6d690d', '#0a6e6f',
1398 \ '#610d0a', '#6e6479',
1399 \]
1400
1401func Test_terminal_ansicolors_global()
1402 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1403 let buf = Run_shell_in_terminal({})
1404 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1405 call Stop_shell_in_terminal(buf)
1406 call term_wait(buf)
1407
1408 exe buf . 'bwipe'
1409 unlet g:terminal_ansi_colors
1410endfunc
1411
1412func Test_terminal_ansicolors_func()
1413 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1414 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1415 call assert_equal(s:test_colors, term_getansicolors(buf))
1416
1417 call term_setansicolors(buf, g:terminal_ansi_colors)
1418 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1419
1420 let colors = [
1421 \ 'ivory', 'AliceBlue',
1422 \ 'grey67', 'dark goldenrod',
1423 \ 'SteelBlue3', 'PaleVioletRed4',
1424 \ 'MediumPurple2', 'yellow2',
1425 \ 'RosyBrown3', 'OrangeRed2',
1426 \ 'white smoke', 'navy blue',
1427 \ 'grey47', 'gray97',
1428 \ 'MistyRose2', 'DodgerBlue4',
1429 \]
1430 call term_setansicolors(buf, colors)
1431
1432 let colors[4] = 'Invalid'
1433 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1434
1435 call Stop_shell_in_terminal(buf)
1436 call term_wait(buf)
1437 exe buf . 'bwipe'
1438endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001439
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001440func Test_terminal_termwinsize_option_fixed()
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001441 if !CanRunVimInTerminal()
1442 return
1443 endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001444 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001445 let text = []
1446 for n in range(10)
1447 call add(text, repeat(n, 50))
1448 endfor
1449 call writefile(text, 'Xwinsize')
1450 let buf = RunVimInTerminal('Xwinsize', {})
1451 let win = bufwinid(buf)
1452 call assert_equal([6, 40], term_getsize(buf))
1453 call assert_equal(6, winheight(win))
1454 call assert_equal(40, winwidth(win))
1455
1456 " resizing the window doesn't resize the terminal.
1457 resize 10
1458 vertical resize 60
1459 call assert_equal([6, 40], term_getsize(buf))
1460 call assert_equal(10, winheight(win))
1461 call assert_equal(60, winwidth(win))
1462
1463 call StopVimInTerminal(buf)
1464 call delete('Xwinsize')
1465
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001466 call assert_fails('set termwinsize=40', 'E474')
1467 call assert_fails('set termwinsize=10+40', 'E474')
1468 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001469
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001470 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001471endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001472
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001473func Test_terminal_termwinsize_option_zero()
1474 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001475 let buf = Run_shell_in_terminal({})
1476 let win = bufwinid(buf)
1477 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1478 call Stop_shell_in_terminal(buf)
1479 call term_wait(buf)
1480 exe buf . 'bwipe'
1481
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001482 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001483 let buf = Run_shell_in_terminal({})
1484 let win = bufwinid(buf)
1485 call assert_equal([7, winwidth(win)], term_getsize(buf))
1486 call Stop_shell_in_terminal(buf)
1487 call term_wait(buf)
1488 exe buf . 'bwipe'
1489
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001490 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001491 let buf = Run_shell_in_terminal({})
1492 let win = bufwinid(buf)
1493 call assert_equal([winheight(win), 33], term_getsize(buf))
1494 call Stop_shell_in_terminal(buf)
1495 call term_wait(buf)
1496 exe buf . 'bwipe'
1497
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001498 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001499endfunc
1500
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001501func Test_terminal_termwinsize_mininmum()
1502 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001503 vsplit
1504 let buf = Run_shell_in_terminal({})
1505 let win = bufwinid(buf)
1506 call assert_inrange(10, 1000, winheight(win))
1507 call assert_inrange(50, 1000, winwidth(win))
1508 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1509
1510 resize 15
1511 vertical resize 60
1512 redraw
1513 call assert_equal([15, 60], term_getsize(buf))
1514 call assert_equal(15, winheight(win))
1515 call assert_equal(60, winwidth(win))
1516
1517 resize 7
1518 vertical resize 30
1519 redraw
1520 call assert_equal([10, 50], term_getsize(buf))
1521 call assert_equal(7, winheight(win))
1522 call assert_equal(30, winwidth(win))
1523
1524 call Stop_shell_in_terminal(buf)
1525 call term_wait(buf)
1526 exe buf . 'bwipe'
1527
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001528 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001529 let buf = Run_shell_in_terminal({})
1530 let win = bufwinid(buf)
1531 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1532 call Stop_shell_in_terminal(buf)
1533 call term_wait(buf)
1534 exe buf . 'bwipe'
1535
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001536 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001537endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001538
1539func Test_terminal_termwinkey()
1540 call assert_equal(1, winnr('$'))
1541 let thiswin = win_getid()
1542
1543 let buf = Run_shell_in_terminal({})
1544 let termwin = bufwinid(buf)
1545 set termwinkey=<C-L>
1546 call feedkeys("\<C-L>w", 'tx')
1547 call assert_equal(thiswin, win_getid())
1548 call feedkeys("\<C-W>w", 'tx')
1549
1550 let job = term_getjob(buf)
1551 call feedkeys("\<C-L>\<C-C>", 'tx')
1552 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001553
1554 set termwinkey&
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001555endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001556
1557func Test_terminal_out_err()
1558 if !has('unix')
1559 return
1560 endif
1561 call writefile([
1562 \ '#!/bin/sh',
1563 \ 'echo "this is standard error" >&2',
1564 \ 'echo "this is standard out" >&1',
1565 \ ], 'Xechoerrout.sh')
1566 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1567
1568 let outfile = 'Xtermstdout'
1569 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001570
1571 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1572 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001573 call assert_equal('this is standard error', term_getline(buf, 1))
1574
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001575 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001576 exe buf . 'bwipe'
1577 call delete('Xechoerrout.sh')
1578 call delete(outfile)
1579endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001580
1581func Test_terminwinscroll()
1582 if !has('unix')
1583 return
1584 endif
1585
1586 " Let the terminal output more than 'termwinscroll' lines, some at the start
1587 " will be dropped.
1588 exe 'set termwinscroll=' . &lines
1589 let buf = term_start('/bin/sh')
1590 for i in range(1, &lines)
1591 call feedkeys("echo " . i . "\<CR>", 'xt')
1592 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1593 endfor
1594 " Go to Terminal-Normal mode to update the buffer.
1595 call feedkeys("\<C-W>N", 'xt')
1596 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1597
1598 " Every "echo nr" must only appear once
1599 let lines = getline(1, line('$'))
1600 for i in range(&lines - len(lines) / 2 + 2, &lines)
1601 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1602 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1603 endfor
1604
1605 exe buf . 'bwipe!'
1606endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001607
Bram Moolenaar875cf872018-07-08 20:49:07 +02001608" Resizing the terminal window caused an ml_get error.
1609" TODO: This does not reproduce the original problem.
1610func Test_terminal_resize()
1611 set statusline=x
1612 terminal
1613 call assert_equal(2, winnr('$'))
1614
1615 " Fill the terminal with text.
1616 if has('win32')
1617 call feedkeys("dir\<CR>", 'xt')
1618 else
1619 call feedkeys("ls\<CR>", 'xt')
1620 endif
1621 " Go to Terminal-Normal mode for a moment.
1622 call feedkeys("\<C-W>N", 'xt')
1623 " Open a new window
1624 call feedkeys("i\<C-W>n", 'xt')
1625 call assert_equal(3, winnr('$'))
1626 redraw
1627
1628 close
1629 call assert_equal(2, winnr('$'))
1630 call feedkeys("exit\<CR>", 'xt')
1631 set statusline&
1632endfunc
1633
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001634" must be nearly the last, we can't go back from GUI to terminal
1635func Test_zz1_terminal_in_gui()
1636 if !CanRunGui()
1637 return
1638 endif
1639
1640 " Ignore the "failed to create input context" error.
1641 call test_ignore_error('E285:')
1642
1643 gui -f
1644
1645 call assert_equal(1, winnr('$'))
1646 let buf = Run_shell_in_terminal({'term_finish': 'close'})
1647 call Stop_shell_in_terminal(buf)
1648 call term_wait(buf)
1649
1650 " closing window wipes out the terminal buffer a with finished job
1651 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1652 call assert_equal("", bufname(buf))
1653
1654 unlet g:job
1655endfunc
1656
1657func Test_zz2_terminal_guioptions_bang()
1658 if !has('gui_running')
1659 return
1660 endif
1661 set guioptions+=!
1662
1663 let filename = 'Xtestscript'
1664 if has('win32')
1665 let filename .= '.bat'
1666 let prefix = ''
1667 let contents = ['@echo off', 'exit %1']
1668 else
1669 let filename .= '.sh'
1670 let prefix = './'
1671 let contents = ['#!/bin/sh', 'exit $1']
1672 endif
1673 call writefile(contents, filename)
1674 call setfperm(filename, 'rwxrwx---')
1675
1676 " Check if v:shell_error is equal to the exit status.
1677 let exitval = 0
1678 execute printf(':!%s%s %d', prefix, filename, exitval)
1679 call assert_equal(exitval, v:shell_error)
1680
1681 let exitval = 9
1682 execute printf(':!%s%s %d', prefix, filename, exitval)
1683 call assert_equal(exitval, v:shell_error)
1684
1685 set guioptions&
1686 call delete(filename)
1687endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001688
1689func Test_terminal_hidden()
1690 if !has('unix')
1691 return
1692 endif
1693 term ++hidden cat
1694 let bnr = bufnr('$')
1695 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1696 exe 'sbuf ' . bnr
1697 call assert_equal('terminal', &buftype)
1698 call term_sendkeys(bnr, "asdf\<CR>")
1699 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1700 call term_sendkeys(bnr, "\<C-D>")
1701 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
1702 bwipe!
1703endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001704
1705func Test_terminal_hidden_and_close()
1706 if !has('unix')
1707 return
1708 endif
1709 call assert_equal(1, winnr('$'))
1710 term ++hidden ++close ls
1711 let bnr = bufnr('$')
1712 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1713 call WaitForAssert({-> assert_false(bufexists(bnr))})
1714 call assert_equal(1, winnr('$'))
1715endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001716
1717func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001718 " This test does not pass through ConPTY.
1719 if has('conpty')
1720 return
1721 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001722 let contents = [
1723 \ [ 'One', '', 'X' ],
1724 \ [ 'Two', '', '' ],
1725 \ [ 'Three' ] + repeat([''], 30)
1726 \ ]
1727
1728 for c in contents
1729 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01001730 if has('win32')
1731 term cmd /c type Xfile
1732 else
1733 term cat Xfile
1734 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001735 let bnr = bufnr('$')
1736 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1737 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01001738 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001739 call assert_equal(c, getline(1, line('$')))
1740 quit
1741 endfor
1742
1743 call delete('Xfile')
1744endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01001745
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01001746func Test_terminal_no_job()
1747 let term = term_start('false', {'term_finish': 'close'})
1748 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
1749endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01001750
1751func Test_term_gettitle()
1752 if !has('title') || empty(&t_ts)
1753 return
1754 endif
1755 " TODO: this fails on Travis
1756 return
1757
1758 " term_gettitle() returns an empty string for a non-terminal buffer
1759 " or for a non-existing buffer.
1760 call assert_equal('', term_gettitle(bufnr('%')))
1761 call assert_equal('', term_gettitle(bufnr('$') + 1))
1762
1763 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
1764 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
1765
1766 call term_sendkeys(term, ":e Xfoo\r")
1767 call WaitForAssert({-> assert_match('Xfoo (.*[/\\]testdir) - VIM', term_gettitle(term)) })
1768
1769 call term_sendkeys(term, ":set titlestring=foo\r")
1770 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
1771
1772 exe term . 'bwipe!'
1773endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01001774
1775" When drawing the statusline the cursor position may not have been updated
1776" yet.
1777" 1. create a terminal, make it show 2 lines
1778" 2. 0.5 sec later: leave terminal window, execute "i"
1779" 3. 0.5 sec later: clear terminal window, now it's 1 line
1780" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1781" 4. 0.5 sec later: should be done, clean up
1782func Test_terminal_statusline()
1783 if !has('unix')
1784 return
1785 endif
1786 set statusline=x
1787 terminal
1788 let tbuf = bufnr('')
1789 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1790 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1791 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1792 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1793
1794 sleep 2
1795 exe tbuf . 'bwipe!'
1796 au! BufLeave
1797 set statusline=
1798endfunc