blob: 960a85e978c4af07a7aface04d2aa3f1c3954bae [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 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'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020048 call assert_match('%aR[^\n]*running]', execute('ls R'))
49 call assert_notmatch('%[^\n]*running]', execute('ls F'))
50 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020051
Bram Moolenaar94053a52017-08-01 21:44:33 +020052 call Stop_shell_in_terminal(buf)
53 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020054 call assert_equal('n', mode())
55 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020056 call assert_match('%aF[^\n]*finished]', execute('ls F'))
57 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
58 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020059
Bram Moolenaar94053a52017-08-01 21:44:33 +020060 " closing window wipes out the terminal buffer a with finished job
61 close
62 call assert_equal("", bufname(buf))
63
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020064 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020065 unlet g:job
66endfunc
67
68func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020069 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020070 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020071 call term_wait(buf)
72
73 setlocal modifiable
74 exe "normal Axxx\<Esc>"
75 call assert_fails(buf . 'bwipe', 'E517')
76 undo
77
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020078 exe buf . 'bwipe'
79 unlet g:job
80endfunc
81
Bram Moolenaar94053a52017-08-01 21:44:33 +020082func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020083 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +020084 call assert_fails(buf . 'bwipe', 'E517')
85 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +020086 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +020087 call assert_equal("", bufname(buf))
88
89 unlet g:job
90endfunc
91
Bram Moolenaar8adb0d02017-09-17 19:08:02 +020092func Test_terminal_split_quit()
93 let buf = Run_shell_in_terminal({})
94 call term_wait(buf)
95 split
96 quit!
97 call term_wait(buf)
98 sleep 50m
99 call assert_equal('run', job_status(g:job))
100
101 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200102 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200103
104 exe buf . 'bwipe'
105 unlet g:job
106endfunc
107
Bram Moolenaar94053a52017-08-01 21:44:33 +0200108func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200109 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200110 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200111 quit
112 for nr in range(1, winnr('$'))
113 call assert_notequal(winbufnr(nr), buf)
114 endfor
115 call assert_true(bufloaded(buf))
116 call assert_true(buflisted(buf))
117
118 exe 'split ' . buf . 'buf'
119 call Stop_shell_in_terminal(buf)
120 exe buf . 'bwipe'
121
122 unlet g:job
123endfunc
124
Bram Moolenaar1e115362019-01-09 23:01:02 +0100125func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200126 exe g:buf . 'bwipe!'
127 let g:buf = 0
128endfunc
129
Bram Moolenaar9d189612017-09-09 18:11:00 +0200130func Get_cat_123_cmd()
131 if has('win32')
132 return 'cmd /c "cls && color 2 && echo 123"'
133 else
134 call writefile(["\<Esc>[32m123"], 'Xtext')
135 return "cat Xtext"
136 endif
137endfunc
138
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200139func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200140 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200141 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
142 let g:job = term_getjob(g:buf)
143
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200144 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
145 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200146 unlet g:buf
147 unlet g:job
148 call delete('Xtext')
149endfunc
150
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200151func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200152 let l = term_scrape(a:buf, 0)
153 call assert_true(len(l) == 0)
154 let l = term_scrape(a:buf, 999)
155 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200156 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200157 call assert_true(len(l) > 0)
158 call assert_equal('1', l[0].chars)
159 call assert_equal('2', l[1].chars)
160 call assert_equal('3', l[2].chars)
161 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar81df6352018-12-22 18:25:30 +0100162 if has('win32')
163 " On Windows 'background' always defaults to dark, even though the terminal
164 " may use a light background. Therefore accept both white and black.
165 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200166 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100167 if &background == 'light'
168 call assert_equal('#ffffff', l[0].bg)
169 else
170 call assert_equal('#000000', l[0].bg)
171 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200172 endif
173
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200174 let l = term_getline(a:buf, -1)
175 call assert_equal('', l)
176 let l = term_getline(a:buf, 0)
177 call assert_equal('', l)
178 let l = term_getline(a:buf, 999)
179 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200180 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200181 call assert_equal('123', l)
182endfunc
183
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200184func Test_terminal_scrape_123()
185 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200186 let buf = term_start(cmd)
187
188 let termlist = term_list()
189 call assert_equal(1, len(termlist))
190 call assert_equal(buf, termlist[0])
191
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200192 " Nothing happens with invalid buffer number
193 call term_wait(1234)
194
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200195 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200196 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200197 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200198 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200199 call Check_123(buf)
200
201 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100202 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200203 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200204 call term_wait(buf)
205 call Check_123(buf)
206
207 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200208 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200209endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200210
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200211func Test_terminal_scrape_multibyte()
212 if !has('multi_byte')
213 return
214 endif
215 call writefile(["léttまrs"], 'Xtext')
216 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200217 " Run cmd with UTF-8 codepage to make the type command print the expected
218 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100219 let buf = term_start("cmd /K chcp 65001")
220 call term_sendkeys(buf, "type Xtext\<CR>")
221 call term_sendkeys(buf, "exit\<CR>")
222 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200223 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100224 let buf = term_start("cat Xtext")
225 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200226 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200227
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100228 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
229 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200230 call assert_true(len(l) >= 7)
231 call assert_equal('l', l[0].chars)
232 call assert_equal('é', l[1].chars)
233 call assert_equal(1, l[1].width)
234 call assert_equal('t', l[2].chars)
235 call assert_equal('t', l[3].chars)
236 call assert_equal('ま', l[4].chars)
237 call assert_equal(2, l[4].width)
238 call assert_equal('r', l[5].chars)
239 call assert_equal('s', l[6].chars)
240
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100241 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200242 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100243 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200244
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100245 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200246 call delete('Xtext')
247endfunc
248
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200249func Test_terminal_scroll()
250 call writefile(range(1, 200), 'Xtext')
251 if has('win32')
252 let cmd = 'cmd /c "type Xtext"'
253 else
254 let cmd = "cat Xtext"
255 endif
256 let buf = term_start(cmd)
257
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100258 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200259 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200260 call term_wait(buf)
261 if has('win32')
262 " TODO: this should not be needed
263 sleep 100m
264 endif
265
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200266 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200267 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200268 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200269 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200270 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200271 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200272 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200273
274 exe buf . 'bwipe'
275 call delete('Xtext')
276endfunc
277
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200278func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200279 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200280 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200281 call writefile(range(150), 'Xtext')
282 if has('win32')
283 call term_sendkeys(buf, "type Xtext\<CR>")
284 else
285 call term_sendkeys(buf, "cat Xtext\<CR>")
286 endif
287 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200288 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200289 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200290 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200291 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200292
293 call Stop_shell_in_terminal(buf)
294 call term_wait(buf)
295 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200296 set termwinscroll&
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200297endfunc
298
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200299func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200300 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200301
Bram Moolenaarb2412082017-08-20 18:09:14 +0200302 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200303 let size = term_getsize('')
304 bwipe!
305 call assert_equal(5, size[0])
306
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200307 call term_start(cmd, {'term_rows': 6})
308 let size = term_getsize('')
309 bwipe!
310 call assert_equal(6, size[0])
311
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200312 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200313 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaara42d3632018-04-14 17:05:38 +0200314 call assert_equal([5, 33], term_getsize(''))
315
316 call term_setsize('', 6, 0)
317 call assert_equal([6, 33], term_getsize(''))
318
319 call term_setsize('', 0, 35)
320 call assert_equal([6, 35], term_getsize(''))
321
322 call term_setsize('', 7, 30)
323 call assert_equal([7, 30], term_getsize(''))
324
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200325 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200326 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200327
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200328 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
329 let size = term_getsize('')
330 bwipe!
331 call assert_equal([6, 36], size)
332
Bram Moolenaarb2412082017-08-20 18:09:14 +0200333 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200334 let size = term_getsize('')
335 bwipe!
336 call assert_equal(20, size[1])
337
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200338 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
339 let size = term_getsize('')
340 bwipe!
341 call assert_equal(26, size[1])
342
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200343 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200344 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200345 let size = term_getsize('')
346 bwipe!
347 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200348
349 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
350 let size = term_getsize('')
351 bwipe!
352 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200353
354 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200355endfunc
356
357func Test_terminal_curwin()
358 let cmd = Get_cat_123_cmd()
359 call assert_equal(1, winnr('$'))
360
361 split dummy
362 exe 'terminal ++curwin ' . cmd
363 call assert_equal(2, winnr('$'))
364 bwipe!
365
366 split dummy
367 call term_start(cmd, {'curwin': 1})
368 call assert_equal(2, winnr('$'))
369 bwipe!
370
371 split dummy
372 call setline(1, 'change')
373 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
374 call assert_equal(2, winnr('$'))
375 exe 'terminal! ++curwin ' . cmd
376 call assert_equal(2, winnr('$'))
377 bwipe!
378
379 split dummy
380 call setline(1, 'change')
381 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
382 call assert_equal(2, winnr('$'))
383 bwipe!
384
385 split dummy
386 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200387 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200388endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200389
Bram Moolenaarff546792017-11-21 14:47:57 +0100390func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200391 if s:python != ''
392 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200393 " 500 was not enough for Travis
394 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200395 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200396 echo 'This will take five seconds...'
397 let waittime = 2000
398 if has('win32')
399 let cmd = $windir . '\system32\timeout.exe 1'
400 else
401 let cmd = 'sleep 1'
402 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200403 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100404 return [cmd, waittime]
405endfunc
406
407func Test_terminal_finish_open_close()
408 call assert_equal(1, winnr('$'))
409
410 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200411
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100412 " shell terminal closes automatically
413 terminal
414 let buf = bufnr('%')
415 call assert_equal(2, winnr('$'))
416 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200417 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100418 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200419 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100420
421 " shell terminal that does not close automatically
422 terminal ++noclose
423 let buf = bufnr('%')
424 call assert_equal(2, winnr('$'))
425 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200426 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100427 call Stop_shell_in_terminal(buf)
428 call assert_equal(2, winnr('$'))
429 quit
430 call assert_equal(1, winnr('$'))
431
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200432 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200433 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200434 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200435 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200436
437 call term_start(cmd, {'term_finish': 'close'})
438 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200439 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200440 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200441 call assert_equal(1, winnr('$'))
442
443 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200444 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200445 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200446 bwipe
447
448 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200449 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200450 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200451 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200452
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200453 exe 'terminal ++hidden ++open ' . cmd
454 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200455 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200456 bwipe
457
458 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
459 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200460 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200461 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200462
463 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
464 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
465 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
466 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
467
468 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200469 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200470 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200471 call assert_equal(4, winheight(0))
472 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200473endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200474
475func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200476 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200477 return
478 endif
479 call mkdir('Xdir')
480 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200481 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200482
483 exe buf . 'bwipe'
484 call delete('Xdir', 'rf')
485endfunc
486
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200487func Test_terminal_cwd_failure()
488 " Case 1: Provided directory is not actually a directory. Attempt to make
489 " the file executable as well.
490 call writefile([], 'Xfile')
491 call setfperm('Xfile', 'rwx------')
492 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
493 call delete('Xfile')
494
495 " Case 2: Directory does not exist.
496 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
497
498 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100499 " Skip this for root, it will be accessible anyway.
500 if $USER != 'root'
501 call mkdir('XdirNoAccess', '', '0600')
502 " return early if the directory permissions could not be set properly
503 if getfperm('XdirNoAccess')[2] == 'x'
504 call delete('XdirNoAccess', 'rf')
505 return
506 endif
507 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
508 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200509 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200510endfunc
511
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100512func Test_terminal_servername()
513 if !has('clientserver')
514 return
515 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200516 call s:test_environment("VIM_SERVERNAME", v:servername)
517endfunc
518
519func Test_terminal_version()
520 call s:test_environment("VIM_TERMINAL", string(v:version))
521endfunc
522
523func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100524 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100525 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200526 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100527 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200528 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100529 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200530 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100531 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100532 call term_wait(buf)
533 call Stop_shell_in_terminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200534 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100535
Bram Moolenaar012eb662018-03-13 17:55:27 +0100536 exe buf . 'bwipe'
537 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100538endfunc
539
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200540func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100541 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200542 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200543 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100544 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100545 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100546 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100547 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100548 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100549 call term_wait(buf)
550 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200551 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200552
Bram Moolenaar012eb662018-03-13 17:55:27 +0100553 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200554endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200555
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200556func Test_terminal_list_args()
557 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
558 call assert_fails(buf . 'bwipe', 'E517')
559 exe buf . 'bwipe!'
560 call assert_equal("", bufname(buf))
561endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200562
563func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100564 let buf = term_start(&shell)
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200565 if has('mac')
566 " The shell or something else has a problem dealing with more than 1000
567 " characters at the same time.
568 let len = 1000
569 else
570 let len = 5000
571 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200572
573 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100574 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200575 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100576 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200577
578 " On MS-Windows there is an extra empty line below "done". Find "done" in
579 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100580 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100581 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100582 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200583 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100584 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200585 endif
586 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200587
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100588 let g:job = term_getjob(buf)
589 call Stop_shell_in_terminal(buf)
590 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200591 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200592 bwipe
593endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200594
595func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200596 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200597 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200598 endif
Bram Moolenaar1580f752018-06-03 15:26:36 +0200599 if has('win32')
600 " TODO: enable once writing to stdin works on MS-Windows
601 return
602 endif
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200603 new
604 call setline(1, ['one', 'two', 'three'])
605 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200606 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200607 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200608 call assert_equal(['3', '3', '14'], nrs)
609 bwipe
610
Bram Moolenaardada6d22017-09-02 17:18:35 +0200611 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200612 call setline(1, ['one', 'two', 'three', 'four'])
613 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200614 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200615 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200616 call assert_equal(['2', '2', '10'], nrs)
617 bwipe
618
Bram Moolenaardada6d22017-09-02 17:18:35 +0200619 if executable('python')
620 new
621 call setline(1, ['print("hello")'])
622 1term ++eof=exit() python
623 " MS-Windows echoes the input, Unix doesn't.
624 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
625 if getline(1) =~ 'hello'
626 call assert_equal('hello', getline(1))
627 else
628 call assert_equal('hello', getline(line('$') - 1))
629 endif
630 bwipe
631
632 if has('win32')
633 new
634 call setline(1, ['print("hello")'])
635 1term ++eof=<C-Z> python
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200636 call WaitForAssert({-> assert_match('Z', getline("$"))})
Bram Moolenaardada6d22017-09-02 17:18:35 +0200637 call assert_equal('hello', getline(line('$') - 1))
638 bwipe
639 endif
640 endif
641
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200642 bwipe!
643endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200644
645func Test_terminal_no_cmd()
Bram Moolenaare25bbc32019-01-19 18:20:45 +0100646 " Does not work on Mac.
Bram Moolenaard383c922019-01-19 15:27:08 +0100647 " Todo: make this work on Win32 again
Bram Moolenaare25bbc32019-01-19 18:20:45 +0100648 if has('mac') || has('win32')
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200649 return
650 endif
651 let buf = term_start('NONE', {})
652 call assert_notequal(0, buf)
653
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200654 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200655 call assert_notequal('', pty)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200656 if has('win32')
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200657 silent exe '!start cmd /c "echo look here > ' . pty . '"'
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200658 else
659 call system('echo "look here" > ' . pty)
660 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200661 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200662
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200663 bwipe!
664endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200665
666func Test_terminal_special_chars()
667 " this file name only works on Unix
668 if !has('unix')
669 return
670 endif
671 call mkdir('Xdir with spaces')
672 call writefile(['x'], 'Xdir with spaces/quoted"file')
673 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200674 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200675 call term_wait('')
676
677 call delete('Xdir with spaces', 'rf')
678 bwipe
679endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200680
681func Test_terminal_wrong_options()
682 call assert_fails('call term_start(&shell, {
683 \ "in_io": "file",
684 \ "in_name": "xxx",
685 \ "out_io": "file",
686 \ "out_name": "xxx",
687 \ "err_io": "file",
688 \ "err_name": "xxx"
689 \ })', 'E474:')
690 call assert_fails('call term_start(&shell, {
691 \ "out_buf": bufnr("%")
692 \ })', 'E474:')
693 call assert_fails('call term_start(&shell, {
694 \ "err_buf": bufnr("%")
695 \ })', 'E474:')
696endfunc
697
698func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200699 let cmd = Get_cat_123_cmd()
700 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
701 call term_wait(buf)
702 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
703 call assert_match('123', readfile('Xfile')[0])
704 let g:job = term_getjob(buf)
705 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
706 call delete('Xfile')
707 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200708
709 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200710 call writefile(['one line'], 'Xfile')
711 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
712 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200713 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200714 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200715 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200716 bwipe
717 call delete('Xfile')
718 endif
719endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200720
721func TerminalTmap(remap)
722 let buf = Run_shell_in_terminal({})
723 call assert_equal('t', mode())
724
725 if a:remap
726 tmap 123 456
727 else
728 tnoremap 123 456
729 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100730 " don't use abcde, it's an existing command
731 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200732 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100733 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200734 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200735 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200736 let lnum = term_getcursor(buf)[0]
737 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100738 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200739 else
740 call assert_match('456', term_getline(buf, lnum))
741 endif
742
743 call term_sendkeys(buf, "\r")
744 call Stop_shell_in_terminal(buf)
745 call term_wait(buf)
746
747 tunmap 123
748 tunmap 456
749 call assert_equal('', maparg('123', 't'))
750 close
751 unlet g:job
752endfunc
753
754func Test_terminal_tmap()
755 call TerminalTmap(1)
756 call TerminalTmap(0)
757endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200758
759func Test_terminal_wall()
760 let buf = Run_shell_in_terminal({})
761 wall
762 call Stop_shell_in_terminal(buf)
763 call term_wait(buf)
764 exe buf . 'bwipe'
765 unlet g:job
766endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200767
Bram Moolenaar7a760922018-02-19 23:10:02 +0100768func Test_terminal_wqall()
769 let buf = Run_shell_in_terminal({})
770 call assert_fails('wqall', 'E948')
771 call Stop_shell_in_terminal(buf)
772 call term_wait(buf)
773 exe buf . 'bwipe'
774 unlet g:job
775endfunc
776
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200777func Test_terminal_composing_unicode()
778 let save_enc = &encoding
779 set encoding=utf-8
780
781 if has('win32')
782 let cmd = "cmd /K chcp 65001"
783 let lnum = [3, 6, 9]
784 else
785 let cmd = &shell
786 let lnum = [1, 3, 5]
787 endif
788
789 enew
790 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100791 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200792 call term_wait(buf, 50)
793
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200794 if has('win32')
795 call assert_equal('cmd', job_info(g:job).cmd[0])
796 else
797 call assert_equal(&shell, job_info(g:job).cmd[0])
798 endif
799
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200800 " ascii + composing
801 let txt = "a\u0308bc"
802 call term_sendkeys(buf, "echo " . txt . "\r")
803 call term_wait(buf, 50)
804 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
805 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
806 let l = term_scrape(buf, lnum[0] + 1)
807 call assert_equal("a\u0308", l[0].chars)
808 call assert_equal("b", l[1].chars)
809 call assert_equal("c", l[2].chars)
810
811 " multibyte + composing
812 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
813 call term_sendkeys(buf, "echo " . txt . "\r")
814 call term_wait(buf, 50)
815 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
816 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
817 let l = term_scrape(buf, lnum[1] + 1)
818 call assert_equal("\u304b\u3099", l[0].chars)
819 call assert_equal("\u304e", l[1].chars)
820 call assert_equal("\u304f\u3099", l[2].chars)
821 call assert_equal("\u3052", l[3].chars)
822 call assert_equal("\u3053\u3099", l[4].chars)
823
824 " \u00a0 + composing
825 let txt = "abc\u00a0\u0308"
826 call term_sendkeys(buf, "echo " . txt . "\r")
827 call term_wait(buf, 50)
828 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
829 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
830 let l = term_scrape(buf, lnum[2] + 1)
831 call assert_equal("\u00a0\u0308", l[3].chars)
832
833 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200834 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200835 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100836 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200837 let &encoding = save_enc
838endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100839
840func Test_terminal_aucmd_on_close()
841 fun Nop()
842 let s:called = 1
843 endfun
844
845 aug repro
846 au!
847 au BufWinLeave * call Nop()
848 aug END
849
850 let [cmd, waittime] = s:get_sleep_cmd()
851
852 call assert_equal(1, winnr('$'))
853 new
854 call setline(1, ['one', 'two'])
855 exe 'term ++close ' . cmd
856 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200857 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100858 call assert_equal(1, s:called)
859 bwipe!
860
861 unlet s:called
862 au! repro
863 delfunc Nop
864endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100865
866func Test_terminal_term_start_empty_command()
867 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
868 call assert_fails(cmd, 'E474')
869 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
870 call assert_fails(cmd, 'E474')
871 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
872 call assert_fails(cmd, 'E474')
873 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
874 call assert_fails(cmd, 'E474')
875endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100876
877func Test_terminal_response_to_control_sequence()
878 if !has('unix')
879 return
880 endif
881
882 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200883 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100884
Bram Moolenaar086eb872018-03-25 21:24:12 +0200885 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200886 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100887
Bram Moolenaar086eb872018-03-25 21:24:12 +0200888 " Request the cursor position.
889 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100890
891 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200892 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100893
Bram Moolenaar086eb872018-03-25 21:24:12 +0200894 " End "cat" gently.
895 call term_sendkeys(buf, "\<CR>\<C-D>")
896
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100897 call Stop_shell_in_terminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100898 exe buf . 'bwipe'
899 unlet g:job
900endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100901
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100902" Run Vim, start a terminal in that Vim with the kill argument,
903" :qall works.
904func Run_terminal_qall_kill(line1, line2)
905 " 1. Open a terminal window and wait for the prompt to appear
906 " 2. set kill using term_setkill()
907 " 3. make Vim exit, it will kill the shell
908 let after = [
909 \ a:line1,
910 \ 'let buf = bufnr("%")',
911 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
912 \ ' sleep 10m',
913 \ 'endwhile',
914 \ a:line2,
915 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
916 \ 'qall',
917 \ ]
918 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100919 return
920 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100921 call assert_equal("done", readfile("Xdone")[0])
922 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100923endfunc
924
925" Run Vim in a terminal, then start a terminal in that Vim with a kill
926" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100927func Test_terminal_qall_kill_arg()
928 call Run_terminal_qall_kill('term ++kill=kill', '')
929endfunc
930
931" Run Vim, start a terminal in that Vim, set the kill argument with
932" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100933func Test_terminal_qall_kill_func()
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100934 call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")')
935endfunc
936
937" Run Vim, start a terminal in that Vim without the kill argument,
938" check that :qall does not exit, :qall! does.
939func Test_terminal_qall_exit()
940 let after = [
941 \ 'term',
942 \ 'let buf = bufnr("%")',
943 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
944 \ ' sleep 10m',
945 \ 'endwhile',
946 \ 'set nomore',
947 \ 'au VimLeavePre * call writefile(["too early"], "Xdone")',
948 \ 'qall',
949 \ 'au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")',
950 \ 'cquit',
951 \ ]
952 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100953 return
954 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100955 call assert_equal("done", readfile("Xdone")[0])
956 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100957endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100958
959" Run Vim in a terminal, then start a terminal in that Vim without a kill
960" argument, check that :confirm qall works.
961func Test_terminal_qall_prompt()
962 if !CanRunVimInTerminal()
963 return
964 endif
965 let buf = RunVimInTerminal('', {})
966
967 " Open a terminal window and wait for the prompt to appear
968 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200969 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
970 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100971
972 " make Vim exit, it will prompt to kill the shell
973 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200974 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100975 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200976 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100977
978 " close the terminal window where Vim was running
979 quit
980endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100981
Bram Moolenaar012eb662018-03-13 17:55:27 +0100982func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100983 augroup repro
984 au!
985 au TerminalOpen * let s:called += 1
986 augroup END
987
988 let s:called = 0
989
990 " Open a terminal window with :terminal
991 terminal
992 call assert_equal(1, s:called)
993 bwipe!
994
995 " Open a terminal window with term_start()
996 call term_start(&shell)
997 call assert_equal(2, s:called)
998 bwipe!
999
1000 " Open a hidden terminal buffer with :terminal
1001 terminal ++hidden
1002 call assert_equal(3, s:called)
1003 for buf in term_list()
1004 exe buf . "bwipe!"
1005 endfor
1006
1007 " Open a hidden terminal buffer with term_start()
1008 let buf = term_start(&shell, {'hidden': 1})
1009 call assert_equal(4, s:called)
1010 exe buf . "bwipe!"
1011
1012 unlet s:called
1013 au! repro
1014endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001015
1016func Check_dump01(off)
1017 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1018 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001019 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001020endfunc
1021
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001022func Test_terminal_dumpwrite_composing()
1023 if !CanRunVimInTerminal()
1024 return
1025 endif
1026 let save_enc = &encoding
1027 set encoding=utf-8
1028 call assert_equal(1, winnr('$'))
1029
1030 let text = " a\u0300 e\u0302 o\u0308"
1031 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001032 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001033 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001034 call term_dumpwrite(buf, 'Xdump')
1035 let dumpline = readfile('Xdump')[0]
1036 call assert_match('|à| |ê| |ö', dumpline)
1037
1038 call StopVimInTerminal(buf)
1039 call delete('Xcomposing')
1040 call delete('Xdump')
1041 let &encoding = save_enc
1042endfunc
1043
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001044" just testing basic functionality.
1045func Test_terminal_dumpload()
1046 call assert_equal(1, winnr('$'))
1047 call term_dumpload('dumps/Test_popup_command_01.dump')
1048 call assert_equal(2, winnr('$'))
1049 call assert_equal(20, line('$'))
1050 call Check_dump01(0)
1051 quit
1052endfunc
1053
1054func Test_terminal_dumpdiff()
1055 call assert_equal(1, winnr('$'))
1056 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump')
1057 call assert_equal(2, winnr('$'))
1058 call assert_equal(62, line('$'))
1059 call Check_dump01(0)
1060 call Check_dump01(42)
1061 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1062 quit
1063endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001064
1065func Test_terminal_dumpdiff_options()
1066 set laststatus=0
1067 call assert_equal(1, winnr('$'))
1068 let height = winheight(0)
1069 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1070 call assert_equal(2, winnr('$'))
1071 call assert_equal(height, winheight(winnr()))
1072 call assert_equal(33, winwidth(winnr()))
1073 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1074 quit
1075
1076 call assert_equal(1, winnr('$'))
1077 let width = winwidth(0)
1078 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1079 call assert_equal(2, winnr('$'))
1080 call assert_equal(width, winwidth(winnr()))
1081 call assert_equal(13, winheight(winnr()))
1082 call assert_equal('something else', bufname('%'))
1083 quit
1084
1085 call assert_equal(1, winnr('$'))
1086 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1087 call assert_equal(1, winnr('$'))
1088 bwipe
1089
1090 set laststatus&
1091endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001092
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001093func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001094 call assert_equal(1, winnr('$'))
1095
1096 " Use the title termcap entries to output the escape sequence.
1097 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001098 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001099 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001100 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001101 \ 'redraw',
1102 \ "set t_ts=",
1103 \ ], 'Xscript')
1104 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001105 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001106 call assert_equal('Xtextfile', expand('%:t'))
1107 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001108 return buf
1109endfunc
1110
1111func Test_terminal_api_drop_newwin()
1112 if !CanRunVimInTerminal()
1113 return
1114 endif
1115 let buf = Api_drop_common('')
1116 call assert_equal(0, &bin)
1117 call assert_equal('', &fenc)
1118
1119 call StopVimInTerminal(buf)
1120 call delete('Xscript')
1121 bwipe Xtextfile
1122endfunc
1123
1124func Test_terminal_api_drop_newwin_bin()
1125 if !CanRunVimInTerminal()
1126 return
1127 endif
1128 let buf = Api_drop_common(',{"bin":1}')
1129 call assert_equal(1, &bin)
1130
1131 call StopVimInTerminal(buf)
1132 call delete('Xscript')
1133 bwipe Xtextfile
1134endfunc
1135
1136func Test_terminal_api_drop_newwin_binary()
1137 if !CanRunVimInTerminal()
1138 return
1139 endif
1140 let buf = Api_drop_common(',{"binary":1}')
1141 call assert_equal(1, &bin)
1142
1143 call StopVimInTerminal(buf)
1144 call delete('Xscript')
1145 bwipe Xtextfile
1146endfunc
1147
1148func Test_terminal_api_drop_newwin_nobin()
1149 if !CanRunVimInTerminal()
1150 return
1151 endif
1152 set binary
1153 let buf = Api_drop_common(',{"nobin":1}')
1154 call assert_equal(0, &bin)
1155
1156 call StopVimInTerminal(buf)
1157 call delete('Xscript')
1158 bwipe Xtextfile
1159 set nobinary
1160endfunc
1161
1162func Test_terminal_api_drop_newwin_nobinary()
1163 if !CanRunVimInTerminal()
1164 return
1165 endif
1166 set binary
1167 let buf = Api_drop_common(',{"nobinary":1}')
1168 call assert_equal(0, &bin)
1169
1170 call StopVimInTerminal(buf)
1171 call delete('Xscript')
1172 bwipe Xtextfile
1173 set nobinary
1174endfunc
1175
1176func Test_terminal_api_drop_newwin_ff()
1177 if !CanRunVimInTerminal()
1178 return
1179 endif
1180 let buf = Api_drop_common(',{"ff":"dos"}')
1181 call assert_equal("dos", &ff)
1182
1183 call StopVimInTerminal(buf)
1184 call delete('Xscript')
1185 bwipe Xtextfile
1186endfunc
1187
1188func Test_terminal_api_drop_newwin_fileformat()
1189 if !CanRunVimInTerminal()
1190 return
1191 endif
1192 let buf = Api_drop_common(',{"fileformat":"dos"}')
1193 call assert_equal("dos", &ff)
1194
1195 call StopVimInTerminal(buf)
1196 call delete('Xscript')
1197 bwipe Xtextfile
1198endfunc
1199
1200func Test_terminal_api_drop_newwin_enc()
1201 if !CanRunVimInTerminal()
1202 return
1203 endif
1204 let buf = Api_drop_common(',{"enc":"utf-16"}')
1205 call assert_equal("utf-16", &fenc)
1206
1207 call StopVimInTerminal(buf)
1208 call delete('Xscript')
1209 bwipe Xtextfile
1210endfunc
1211
1212func Test_terminal_api_drop_newwin_encoding()
1213 if !CanRunVimInTerminal()
1214 return
1215 endif
1216 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1217 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001218
1219 call StopVimInTerminal(buf)
1220 call delete('Xscript')
1221 bwipe Xtextfile
1222endfunc
1223
1224func Test_terminal_api_drop_oldwin()
1225 if !CanRunVimInTerminal()
1226 return
1227 endif
1228 let firstwinid = win_getid()
1229 split Xtextfile
1230 let textfile_winid = win_getid()
1231 call assert_equal(2, winnr('$'))
1232 call win_gotoid(firstwinid)
1233
1234 " Use the title termcap entries to output the escape sequence.
1235 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001236 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001237 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1238 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1239 \ 'redraw',
1240 \ "set t_ts=",
1241 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001242 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001243 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001244 call assert_equal(textfile_winid, win_getid())
1245
1246 call StopVimInTerminal(buf)
1247 call delete('Xscript')
1248 bwipe Xtextfile
1249endfunc
1250
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001251func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001252 let g:called_bufnum = a:bufnum
1253 let g:called_arg = a:arg
1254endfunc
1255
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001256func WriteApiCall(funcname)
1257 " Use the title termcap entries to output the escape sequence.
1258 call writefile([
1259 \ 'set title',
1260 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1261 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1262 \ 'redraw',
1263 \ "set t_ts=",
1264 \ ], 'Xscript')
1265endfunc
1266
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001267func Test_terminal_api_call()
1268 if !CanRunVimInTerminal()
1269 return
1270 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001271
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001272 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001273 let buf = RunVimInTerminal('-S Xscript', {})
1274 call WaitFor({-> exists('g:called_bufnum')})
1275 call assert_equal(buf, g:called_bufnum)
1276 call assert_equal(['hello', 123], g:called_arg)
1277
1278 call StopVimInTerminal(buf)
1279 call delete('Xscript')
1280 unlet g:called_bufnum
1281 unlet g:called_arg
1282endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001283
1284func Test_terminal_api_call_fails()
1285 if !CanRunVimInTerminal()
1286 return
1287 endif
1288
1289 call WriteApiCall('TryThis')
1290 call ch_logfile('Xlog', 'w')
1291 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001292 call WaitForAssert({-> assert_match('Invalid function name: TryThis', string(readfile('Xlog')))})
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001293
1294 call StopVimInTerminal(buf)
1295 call delete('Xscript')
1296 call ch_logfile('', '')
1297 call delete('Xlog')
1298endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001299
Bram Moolenaara997b452018-04-17 23:24:06 +02001300let s:caught_e937 = 0
1301
1302func Tapi_Delete(bufnum, arg)
1303 try
1304 execute 'bdelete!' a:bufnum
1305 catch /E937:/
1306 let s:caught_e937 = 1
1307 endtry
1308endfunc
1309
1310func Test_terminal_api_call_fail_delete()
1311 if !CanRunVimInTerminal()
1312 return
1313 endif
1314
1315 call WriteApiCall('Tapi_Delete')
1316 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001317 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001318
1319 call StopVimInTerminal(buf)
1320 call delete('Xscript')
1321 call ch_logfile('', '')
1322endfunc
1323
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001324func Test_terminal_ansicolors_default()
1325 let colors = [
1326 \ '#000000', '#e00000',
1327 \ '#00e000', '#e0e000',
1328 \ '#0000e0', '#e000e0',
1329 \ '#00e0e0', '#e0e0e0',
1330 \ '#808080', '#ff4040',
1331 \ '#40ff40', '#ffff40',
1332 \ '#4040ff', '#ff40ff',
1333 \ '#40ffff', '#ffffff',
1334 \]
1335
1336 let buf = Run_shell_in_terminal({})
1337 call assert_equal(colors, term_getansicolors(buf))
1338 call Stop_shell_in_terminal(buf)
1339 call term_wait(buf)
1340
1341 exe buf . 'bwipe'
1342endfunc
1343
1344let s:test_colors = [
1345 \ '#616e64', '#0d0a79',
1346 \ '#6d610d', '#0a7373',
1347 \ '#690d0a', '#6d696e',
1348 \ '#0d0a6f', '#616e0d',
1349 \ '#0a6479', '#6d0d0a',
1350 \ '#617373', '#0d0a69',
1351 \ '#6d690d', '#0a6e6f',
1352 \ '#610d0a', '#6e6479',
1353 \]
1354
1355func Test_terminal_ansicolors_global()
1356 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1357 let buf = Run_shell_in_terminal({})
1358 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1359 call Stop_shell_in_terminal(buf)
1360 call term_wait(buf)
1361
1362 exe buf . 'bwipe'
1363 unlet g:terminal_ansi_colors
1364endfunc
1365
1366func Test_terminal_ansicolors_func()
1367 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1368 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1369 call assert_equal(s:test_colors, term_getansicolors(buf))
1370
1371 call term_setansicolors(buf, g:terminal_ansi_colors)
1372 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1373
1374 let colors = [
1375 \ 'ivory', 'AliceBlue',
1376 \ 'grey67', 'dark goldenrod',
1377 \ 'SteelBlue3', 'PaleVioletRed4',
1378 \ 'MediumPurple2', 'yellow2',
1379 \ 'RosyBrown3', 'OrangeRed2',
1380 \ 'white smoke', 'navy blue',
1381 \ 'grey47', 'gray97',
1382 \ 'MistyRose2', 'DodgerBlue4',
1383 \]
1384 call term_setansicolors(buf, colors)
1385
1386 let colors[4] = 'Invalid'
1387 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1388
1389 call Stop_shell_in_terminal(buf)
1390 call term_wait(buf)
1391 exe buf . 'bwipe'
1392endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001393
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001394func Test_terminal_termwinsize_option_fixed()
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001395 if !CanRunVimInTerminal()
1396 return
1397 endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001398 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001399 let text = []
1400 for n in range(10)
1401 call add(text, repeat(n, 50))
1402 endfor
1403 call writefile(text, 'Xwinsize')
1404 let buf = RunVimInTerminal('Xwinsize', {})
1405 let win = bufwinid(buf)
1406 call assert_equal([6, 40], term_getsize(buf))
1407 call assert_equal(6, winheight(win))
1408 call assert_equal(40, winwidth(win))
1409
1410 " resizing the window doesn't resize the terminal.
1411 resize 10
1412 vertical resize 60
1413 call assert_equal([6, 40], term_getsize(buf))
1414 call assert_equal(10, winheight(win))
1415 call assert_equal(60, winwidth(win))
1416
1417 call StopVimInTerminal(buf)
1418 call delete('Xwinsize')
1419
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001420 call assert_fails('set termwinsize=40', 'E474')
1421 call assert_fails('set termwinsize=10+40', 'E474')
1422 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001423
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001424 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001425endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001426
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001427func Test_terminal_termwinsize_option_zero()
1428 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001429 let buf = Run_shell_in_terminal({})
1430 let win = bufwinid(buf)
1431 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1432 call Stop_shell_in_terminal(buf)
1433 call term_wait(buf)
1434 exe buf . 'bwipe'
1435
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001436 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001437 let buf = Run_shell_in_terminal({})
1438 let win = bufwinid(buf)
1439 call assert_equal([7, winwidth(win)], term_getsize(buf))
1440 call Stop_shell_in_terminal(buf)
1441 call term_wait(buf)
1442 exe buf . 'bwipe'
1443
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001444 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001445 let buf = Run_shell_in_terminal({})
1446 let win = bufwinid(buf)
1447 call assert_equal([winheight(win), 33], term_getsize(buf))
1448 call Stop_shell_in_terminal(buf)
1449 call term_wait(buf)
1450 exe buf . 'bwipe'
1451
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001452 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001453endfunc
1454
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001455func Test_terminal_termwinsize_mininmum()
1456 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001457 vsplit
1458 let buf = Run_shell_in_terminal({})
1459 let win = bufwinid(buf)
1460 call assert_inrange(10, 1000, winheight(win))
1461 call assert_inrange(50, 1000, winwidth(win))
1462 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1463
1464 resize 15
1465 vertical resize 60
1466 redraw
1467 call assert_equal([15, 60], term_getsize(buf))
1468 call assert_equal(15, winheight(win))
1469 call assert_equal(60, winwidth(win))
1470
1471 resize 7
1472 vertical resize 30
1473 redraw
1474 call assert_equal([10, 50], term_getsize(buf))
1475 call assert_equal(7, winheight(win))
1476 call assert_equal(30, winwidth(win))
1477
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=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001483 let buf = Run_shell_in_terminal({})
1484 let win = bufwinid(buf)
1485 call assert_equal([winheight(win), 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=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001491endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001492
1493func Test_terminal_termwinkey()
1494 call assert_equal(1, winnr('$'))
1495 let thiswin = win_getid()
1496
1497 let buf = Run_shell_in_terminal({})
1498 let termwin = bufwinid(buf)
1499 set termwinkey=<C-L>
1500 call feedkeys("\<C-L>w", 'tx')
1501 call assert_equal(thiswin, win_getid())
1502 call feedkeys("\<C-W>w", 'tx')
1503
1504 let job = term_getjob(buf)
1505 call feedkeys("\<C-L>\<C-C>", 'tx')
1506 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1507endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001508
1509func Test_terminal_out_err()
1510 if !has('unix')
1511 return
1512 endif
1513 call writefile([
1514 \ '#!/bin/sh',
1515 \ 'echo "this is standard error" >&2',
1516 \ 'echo "this is standard out" >&1',
1517 \ ], 'Xechoerrout.sh')
1518 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1519
1520 let outfile = 'Xtermstdout'
1521 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001522
1523 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1524 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001525 call assert_equal('this is standard error', term_getline(buf, 1))
1526
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001527 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001528 exe buf . 'bwipe'
1529 call delete('Xechoerrout.sh')
1530 call delete(outfile)
1531endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001532
1533func Test_terminwinscroll()
1534 if !has('unix')
1535 return
1536 endif
1537
1538 " Let the terminal output more than 'termwinscroll' lines, some at the start
1539 " will be dropped.
1540 exe 'set termwinscroll=' . &lines
1541 let buf = term_start('/bin/sh')
1542 for i in range(1, &lines)
1543 call feedkeys("echo " . i . "\<CR>", 'xt')
1544 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1545 endfor
1546 " Go to Terminal-Normal mode to update the buffer.
1547 call feedkeys("\<C-W>N", 'xt')
1548 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1549
1550 " Every "echo nr" must only appear once
1551 let lines = getline(1, line('$'))
1552 for i in range(&lines - len(lines) / 2 + 2, &lines)
1553 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1554 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1555 endfor
1556
1557 exe buf . 'bwipe!'
1558endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001559
Bram Moolenaar875cf872018-07-08 20:49:07 +02001560" Resizing the terminal window caused an ml_get error.
1561" TODO: This does not reproduce the original problem.
1562func Test_terminal_resize()
1563 set statusline=x
1564 terminal
1565 call assert_equal(2, winnr('$'))
1566
1567 " Fill the terminal with text.
1568 if has('win32')
1569 call feedkeys("dir\<CR>", 'xt')
1570 else
1571 call feedkeys("ls\<CR>", 'xt')
1572 endif
1573 " Go to Terminal-Normal mode for a moment.
1574 call feedkeys("\<C-W>N", 'xt')
1575 " Open a new window
1576 call feedkeys("i\<C-W>n", 'xt')
1577 call assert_equal(3, winnr('$'))
1578 redraw
1579
1580 close
1581 call assert_equal(2, winnr('$'))
1582 call feedkeys("exit\<CR>", 'xt')
1583 set statusline&
1584endfunc
1585
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001586" must be nearly the last, we can't go back from GUI to terminal
1587func Test_zz1_terminal_in_gui()
1588 if !CanRunGui()
1589 return
1590 endif
1591
1592 " Ignore the "failed to create input context" error.
1593 call test_ignore_error('E285:')
1594
1595 gui -f
1596
1597 call assert_equal(1, winnr('$'))
1598 let buf = Run_shell_in_terminal({'term_finish': 'close'})
1599 call Stop_shell_in_terminal(buf)
1600 call term_wait(buf)
1601
1602 " closing window wipes out the terminal buffer a with finished job
1603 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1604 call assert_equal("", bufname(buf))
1605
1606 unlet g:job
1607endfunc
1608
1609func Test_zz2_terminal_guioptions_bang()
1610 if !has('gui_running')
1611 return
1612 endif
1613 set guioptions+=!
1614
1615 let filename = 'Xtestscript'
1616 if has('win32')
1617 let filename .= '.bat'
1618 let prefix = ''
1619 let contents = ['@echo off', 'exit %1']
1620 else
1621 let filename .= '.sh'
1622 let prefix = './'
1623 let contents = ['#!/bin/sh', 'exit $1']
1624 endif
1625 call writefile(contents, filename)
1626 call setfperm(filename, 'rwxrwx---')
1627
1628 " Check if v:shell_error is equal to the exit status.
1629 let exitval = 0
1630 execute printf(':!%s%s %d', prefix, filename, exitval)
1631 call assert_equal(exitval, v:shell_error)
1632
1633 let exitval = 9
1634 execute printf(':!%s%s %d', prefix, filename, exitval)
1635 call assert_equal(exitval, v:shell_error)
1636
1637 set guioptions&
1638 call delete(filename)
1639endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001640
1641func Test_terminal_hidden()
1642 if !has('unix')
1643 return
1644 endif
1645 term ++hidden cat
1646 let bnr = bufnr('$')
1647 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1648 exe 'sbuf ' . bnr
1649 call assert_equal('terminal', &buftype)
1650 call term_sendkeys(bnr, "asdf\<CR>")
1651 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1652 call term_sendkeys(bnr, "\<C-D>")
1653 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
1654 bwipe!
1655endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001656
1657func Test_terminal_hidden_and_close()
1658 if !has('unix')
1659 return
1660 endif
1661 call assert_equal(1, winnr('$'))
1662 term ++hidden ++close ls
1663 let bnr = bufnr('$')
1664 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1665 call WaitForAssert({-> assert_false(bufexists(bnr))})
1666 call assert_equal(1, winnr('$'))
1667endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001668
1669func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001670 let contents = [
1671 \ [ 'One', '', 'X' ],
1672 \ [ 'Two', '', '' ],
1673 \ [ 'Three' ] + repeat([''], 30)
1674 \ ]
1675
1676 for c in contents
1677 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01001678 if has('win32')
1679 term cmd /c type Xfile
1680 else
1681 term cat Xfile
1682 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001683 let bnr = bufnr('$')
1684 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1685 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01001686 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001687 call assert_equal(c, getline(1, line('$')))
1688 quit
1689 endfor
1690
1691 call delete('Xfile')
1692endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01001693
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01001694func Test_terminal_no_job()
1695 let term = term_start('false', {'term_finish': 'close'})
1696 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
1697endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01001698
1699func Test_term_gettitle()
1700 if !has('title') || empty(&t_ts)
1701 return
1702 endif
1703 " TODO: this fails on Travis
1704 return
1705
1706 " term_gettitle() returns an empty string for a non-terminal buffer
1707 " or for a non-existing buffer.
1708 call assert_equal('', term_gettitle(bufnr('%')))
1709 call assert_equal('', term_gettitle(bufnr('$') + 1))
1710
1711 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
1712 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
1713
1714 call term_sendkeys(term, ":e Xfoo\r")
1715 call WaitForAssert({-> assert_match('Xfoo (.*[/\\]testdir) - VIM', term_gettitle(term)) })
1716
1717 call term_sendkeys(term, ":set titlestring=foo\r")
1718 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
1719
1720 exe term . 'bwipe!'
1721endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01001722
1723" When drawing the statusline the cursor position may not have been updated
1724" yet.
1725" 1. create a terminal, make it show 2 lines
1726" 2. 0.5 sec later: leave terminal window, execute "i"
1727" 3. 0.5 sec later: clear terminal window, now it's 1 line
1728" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1729" 4. 0.5 sec later: should be done, clean up
1730func Test_terminal_statusline()
1731 if !has('unix')
1732 return
1733 endif
1734 set statusline=x
1735 terminal
1736 let tbuf = bufnr('')
1737 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1738 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1739 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1740 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1741
1742 sleep 2
1743 exe tbuf . 'bwipe!'
1744 au! BufLeave
1745 set statusline=
1746endfunc