blob: 8eb43a0cc75c849ac04cc6c4e7a7595c70926b47 [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 Moolenaar6e72cd02018-04-14 21:31:35 +0200302endfunc
303
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200304func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200305 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200306
Bram Moolenaarb2412082017-08-20 18:09:14 +0200307 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200308 let size = term_getsize('')
309 bwipe!
310 call assert_equal(5, size[0])
311
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200312 call term_start(cmd, {'term_rows': 6})
313 let size = term_getsize('')
314 bwipe!
315 call assert_equal(6, size[0])
316
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200317 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200318 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaara42d3632018-04-14 17:05:38 +0200319 call assert_equal([5, 33], term_getsize(''))
320
321 call term_setsize('', 6, 0)
322 call assert_equal([6, 33], term_getsize(''))
323
324 call term_setsize('', 0, 35)
325 call assert_equal([6, 35], term_getsize(''))
326
327 call term_setsize('', 7, 30)
328 call assert_equal([7, 30], term_getsize(''))
329
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200330 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200331 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200332
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200333 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
334 let size = term_getsize('')
335 bwipe!
336 call assert_equal([6, 36], size)
337
Bram Moolenaarb2412082017-08-20 18:09:14 +0200338 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200339 let size = term_getsize('')
340 bwipe!
341 call assert_equal(20, size[1])
342
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200343 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
344 let size = term_getsize('')
345 bwipe!
346 call assert_equal(26, size[1])
347
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200348 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200349 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200350 let size = term_getsize('')
351 bwipe!
352 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200353
354 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
355 let size = term_getsize('')
356 bwipe!
357 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200358
359 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200360endfunc
361
362func Test_terminal_curwin()
363 let cmd = Get_cat_123_cmd()
364 call assert_equal(1, winnr('$'))
365
366 split dummy
367 exe 'terminal ++curwin ' . cmd
368 call assert_equal(2, winnr('$'))
369 bwipe!
370
371 split dummy
372 call term_start(cmd, {'curwin': 1})
373 call assert_equal(2, winnr('$'))
374 bwipe!
375
376 split dummy
377 call setline(1, 'change')
378 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
379 call assert_equal(2, winnr('$'))
380 exe 'terminal! ++curwin ' . cmd
381 call assert_equal(2, winnr('$'))
382 bwipe!
383
384 split dummy
385 call setline(1, 'change')
386 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
387 call assert_equal(2, winnr('$'))
388 bwipe!
389
390 split dummy
391 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200392 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200393endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200394
Bram Moolenaarff546792017-11-21 14:47:57 +0100395func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200396 if s:python != ''
397 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200398 " 500 was not enough for Travis
399 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200400 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200401 echo 'This will take five seconds...'
402 let waittime = 2000
403 if has('win32')
404 let cmd = $windir . '\system32\timeout.exe 1'
405 else
406 let cmd = 'sleep 1'
407 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200408 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100409 return [cmd, waittime]
410endfunc
411
412func Test_terminal_finish_open_close()
413 call assert_equal(1, winnr('$'))
414
415 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200416
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100417 " shell terminal closes automatically
418 terminal
419 let buf = bufnr('%')
420 call assert_equal(2, winnr('$'))
421 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200422 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100423 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200424 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100425
426 " shell terminal that does not close automatically
427 terminal ++noclose
428 let buf = bufnr('%')
429 call assert_equal(2, winnr('$'))
430 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200431 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100432 call Stop_shell_in_terminal(buf)
433 call assert_equal(2, winnr('$'))
434 quit
435 call assert_equal(1, winnr('$'))
436
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200437 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200438 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
442 call term_start(cmd, {'term_finish': 'close'})
443 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200444 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200445 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200446 call assert_equal(1, winnr('$'))
447
448 exe 'terminal ++open ' . cmd
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 Moolenaardd693ce2017-08-10 23:15:19 +0200451 bwipe
452
453 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200454 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200455 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200456 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200457
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200458 exe 'terminal ++hidden ++open ' . cmd
459 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200460 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200461 bwipe
462
463 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
464 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200465 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200466 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200467
468 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
469 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
470 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
471 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
472
473 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200474 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200475 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200476 call assert_equal(4, winheight(0))
477 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200478endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200479
480func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200481 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200482 return
483 endif
484 call mkdir('Xdir')
485 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200486 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200487
488 exe buf . 'bwipe'
489 call delete('Xdir', 'rf')
490endfunc
491
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200492func Test_terminal_cwd_failure()
493 " Case 1: Provided directory is not actually a directory. Attempt to make
494 " the file executable as well.
495 call writefile([], 'Xfile')
496 call setfperm('Xfile', 'rwx------')
497 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
498 call delete('Xfile')
499
500 " Case 2: Directory does not exist.
501 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
502
503 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100504 " Skip this for root, it will be accessible anyway.
505 if $USER != 'root'
506 call mkdir('XdirNoAccess', '', '0600')
507 " return early if the directory permissions could not be set properly
508 if getfperm('XdirNoAccess')[2] == 'x'
509 call delete('XdirNoAccess', 'rf')
510 return
511 endif
512 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
513 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200514 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200515endfunc
516
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100517func Test_terminal_servername()
518 if !has('clientserver')
519 return
520 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200521 call s:test_environment("VIM_SERVERNAME", v:servername)
522endfunc
523
524func Test_terminal_version()
525 call s:test_environment("VIM_TERMINAL", string(v:version))
526endfunc
527
528func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100529 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100530 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200531 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100532 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200533 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100534 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200535 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100536 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100537 call term_wait(buf)
538 call Stop_shell_in_terminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200539 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100540
Bram Moolenaar012eb662018-03-13 17:55:27 +0100541 exe buf . 'bwipe'
542 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100543endfunc
544
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200545func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100546 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200547 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200548 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100549 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100550 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100551 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100552 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100553 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100554 call term_wait(buf)
555 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200556 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200557
Bram Moolenaar012eb662018-03-13 17:55:27 +0100558 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200559endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200560
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200561func Test_terminal_list_args()
562 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
563 call assert_fails(buf . 'bwipe', 'E517')
564 exe buf . 'bwipe!'
565 call assert_equal("", bufname(buf))
566endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200567
568func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100569 let buf = term_start(&shell)
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100570 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200571 " The shell or something else has a problem dealing with more than 1000
572 " characters at the same time.
573 let len = 1000
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100574 " NPFS is used in Windows, nonblocking mode does not work properly.
575 elseif has('win32')
576 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200577 else
578 let len = 5000
579 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200580
581 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100582 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200583 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100584 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200585
586 " On MS-Windows there is an extra empty line below "done". Find "done" in
587 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100588 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100589 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100590 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200591 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100592 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200593 endif
594 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200595
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100596 let g:job = term_getjob(buf)
597 call Stop_shell_in_terminal(buf)
598 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200599 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200600 bwipe
601endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200602
603func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200604 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200605 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200606 endif
Bram Moolenaar1580f752018-06-03 15:26:36 +0200607 if has('win32')
608 " TODO: enable once writing to stdin works on MS-Windows
609 return
610 endif
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200611 new
612 call setline(1, ['one', 'two', 'three'])
613 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200614 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200615 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200616 call assert_equal(['3', '3', '14'], nrs)
617 bwipe
618
Bram Moolenaardada6d22017-09-02 17:18:35 +0200619 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200620 call setline(1, ['one', 'two', 'three', 'four'])
621 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200622 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200623 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200624 call assert_equal(['2', '2', '10'], nrs)
625 bwipe
626
Bram Moolenaardada6d22017-09-02 17:18:35 +0200627 if executable('python')
628 new
629 call setline(1, ['print("hello")'])
630 1term ++eof=exit() python
631 " MS-Windows echoes the input, Unix doesn't.
632 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
633 if getline(1) =~ 'hello'
634 call assert_equal('hello', getline(1))
635 else
636 call assert_equal('hello', getline(line('$') - 1))
637 endif
638 bwipe
639
640 if has('win32')
641 new
642 call setline(1, ['print("hello")'])
643 1term ++eof=<C-Z> python
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200644 call WaitForAssert({-> assert_match('Z', getline("$"))})
Bram Moolenaardada6d22017-09-02 17:18:35 +0200645 call assert_equal('hello', getline(line('$') - 1))
646 bwipe
647 endif
648 endif
649
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200650 bwipe!
651endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200652
653func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200654 let buf = term_start('NONE', {})
655 call assert_notequal(0, buf)
656
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200657 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200658 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100659 if has('gui_running') && !has('win32')
660 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200661 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100662 else
663 " Otherwise using a job works on all systems.
664 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200665 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200666 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200667
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200668 bwipe!
669endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200670
671func Test_terminal_special_chars()
672 " this file name only works on Unix
673 if !has('unix')
674 return
675 endif
676 call mkdir('Xdir with spaces')
677 call writefile(['x'], 'Xdir with spaces/quoted"file')
678 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200679 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200680 call term_wait('')
681
682 call delete('Xdir with spaces', 'rf')
683 bwipe
684endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200685
686func Test_terminal_wrong_options()
687 call assert_fails('call term_start(&shell, {
688 \ "in_io": "file",
689 \ "in_name": "xxx",
690 \ "out_io": "file",
691 \ "out_name": "xxx",
692 \ "err_io": "file",
693 \ "err_name": "xxx"
694 \ })', 'E474:')
695 call assert_fails('call term_start(&shell, {
696 \ "out_buf": bufnr("%")
697 \ })', 'E474:')
698 call assert_fails('call term_start(&shell, {
699 \ "err_buf": bufnr("%")
700 \ })', 'E474:')
701endfunc
702
703func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200704 let cmd = Get_cat_123_cmd()
705 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
706 call term_wait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100707 " ConPTY may precede escape sequence. There are things that are not so.
708 if !has('conpty')
709 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
710 call assert_match('123', readfile('Xfile')[0])
711 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200712 let g:job = term_getjob(buf)
713 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
714 call delete('Xfile')
715 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200716
717 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200718 call writefile(['one line'], 'Xfile')
719 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
720 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200721 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200722 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200723 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200724 bwipe
725 call delete('Xfile')
726 endif
727endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200728
729func TerminalTmap(remap)
730 let buf = Run_shell_in_terminal({})
731 call assert_equal('t', mode())
732
733 if a:remap
734 tmap 123 456
735 else
736 tnoremap 123 456
737 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100738 " don't use abcde, it's an existing command
739 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200740 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100741 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200742 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200743 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200744 let lnum = term_getcursor(buf)[0]
745 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100746 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200747 else
748 call assert_match('456', term_getline(buf, lnum))
749 endif
750
751 call term_sendkeys(buf, "\r")
752 call Stop_shell_in_terminal(buf)
753 call term_wait(buf)
754
755 tunmap 123
756 tunmap 456
757 call assert_equal('', maparg('123', 't'))
758 close
759 unlet g:job
760endfunc
761
762func Test_terminal_tmap()
763 call TerminalTmap(1)
764 call TerminalTmap(0)
765endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200766
767func Test_terminal_wall()
768 let buf = Run_shell_in_terminal({})
769 wall
770 call Stop_shell_in_terminal(buf)
771 call term_wait(buf)
772 exe buf . 'bwipe'
773 unlet g:job
774endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200775
Bram Moolenaar7a760922018-02-19 23:10:02 +0100776func Test_terminal_wqall()
777 let buf = Run_shell_in_terminal({})
778 call assert_fails('wqall', 'E948')
779 call Stop_shell_in_terminal(buf)
780 call term_wait(buf)
781 exe buf . 'bwipe'
782 unlet g:job
783endfunc
784
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200785func Test_terminal_composing_unicode()
786 let save_enc = &encoding
787 set encoding=utf-8
788
789 if has('win32')
790 let cmd = "cmd /K chcp 65001"
791 let lnum = [3, 6, 9]
792 else
793 let cmd = &shell
794 let lnum = [1, 3, 5]
795 endif
796
797 enew
798 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100799 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200800 call term_wait(buf, 50)
801
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200802 if has('win32')
803 call assert_equal('cmd', job_info(g:job).cmd[0])
804 else
805 call assert_equal(&shell, job_info(g:job).cmd[0])
806 endif
807
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200808 " ascii + composing
809 let txt = "a\u0308bc"
810 call term_sendkeys(buf, "echo " . txt . "\r")
811 call term_wait(buf, 50)
812 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
813 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
814 let l = term_scrape(buf, lnum[0] + 1)
815 call assert_equal("a\u0308", l[0].chars)
816 call assert_equal("b", l[1].chars)
817 call assert_equal("c", l[2].chars)
818
819 " multibyte + composing
820 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
821 call term_sendkeys(buf, "echo " . txt . "\r")
822 call term_wait(buf, 50)
823 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
824 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
825 let l = term_scrape(buf, lnum[1] + 1)
826 call assert_equal("\u304b\u3099", l[0].chars)
827 call assert_equal("\u304e", l[1].chars)
828 call assert_equal("\u304f\u3099", l[2].chars)
829 call assert_equal("\u3052", l[3].chars)
830 call assert_equal("\u3053\u3099", l[4].chars)
831
832 " \u00a0 + composing
833 let txt = "abc\u00a0\u0308"
834 call term_sendkeys(buf, "echo " . txt . "\r")
835 call term_wait(buf, 50)
836 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
837 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
838 let l = term_scrape(buf, lnum[2] + 1)
839 call assert_equal("\u00a0\u0308", l[3].chars)
840
841 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200842 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200843 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100844 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200845 let &encoding = save_enc
846endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100847
848func Test_terminal_aucmd_on_close()
849 fun Nop()
850 let s:called = 1
851 endfun
852
853 aug repro
854 au!
855 au BufWinLeave * call Nop()
856 aug END
857
858 let [cmd, waittime] = s:get_sleep_cmd()
859
860 call assert_equal(1, winnr('$'))
861 new
862 call setline(1, ['one', 'two'])
863 exe 'term ++close ' . cmd
864 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200865 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100866 call assert_equal(1, s:called)
867 bwipe!
868
869 unlet s:called
870 au! repro
871 delfunc Nop
872endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100873
874func Test_terminal_term_start_empty_command()
875 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
876 call assert_fails(cmd, 'E474')
877 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
878 call assert_fails(cmd, 'E474')
879 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
880 call assert_fails(cmd, 'E474')
881 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
882 call assert_fails(cmd, 'E474')
883endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100884
885func Test_terminal_response_to_control_sequence()
886 if !has('unix')
887 return
888 endif
889
890 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200891 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100892
Bram Moolenaar086eb872018-03-25 21:24:12 +0200893 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200894 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100895
Bram Moolenaar086eb872018-03-25 21:24:12 +0200896 " Request the cursor position.
897 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100898
899 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200900 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100901
Bram Moolenaar086eb872018-03-25 21:24:12 +0200902 " End "cat" gently.
903 call term_sendkeys(buf, "\<CR>\<C-D>")
904
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100905 call Stop_shell_in_terminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100906 exe buf . 'bwipe'
907 unlet g:job
908endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100909
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100910" Run Vim, start a terminal in that Vim with the kill argument,
911" :qall works.
912func Run_terminal_qall_kill(line1, line2)
913 " 1. Open a terminal window and wait for the prompt to appear
914 " 2. set kill using term_setkill()
915 " 3. make Vim exit, it will kill the shell
916 let after = [
917 \ a:line1,
918 \ 'let buf = bufnr("%")',
919 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
920 \ ' sleep 10m',
921 \ 'endwhile',
922 \ a:line2,
923 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
924 \ 'qall',
925 \ ]
926 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100927 return
928 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100929 call assert_equal("done", readfile("Xdone")[0])
930 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100931endfunc
932
933" Run Vim in a terminal, then start a terminal in that Vim with a kill
934" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100935func Test_terminal_qall_kill_arg()
936 call Run_terminal_qall_kill('term ++kill=kill', '')
937endfunc
938
939" Run Vim, start a terminal in that Vim, set the kill argument with
940" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100941func Test_terminal_qall_kill_func()
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100942 call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")')
943endfunc
944
945" Run Vim, start a terminal in that Vim without the kill argument,
946" check that :qall does not exit, :qall! does.
947func Test_terminal_qall_exit()
948 let after = [
949 \ 'term',
950 \ 'let buf = bufnr("%")',
951 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
952 \ ' sleep 10m',
953 \ 'endwhile',
954 \ 'set nomore',
955 \ 'au VimLeavePre * call writefile(["too early"], "Xdone")',
956 \ 'qall',
957 \ 'au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")',
958 \ 'cquit',
959 \ ]
960 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100961 return
962 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100963 call assert_equal("done", readfile("Xdone")[0])
964 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100965endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100966
967" Run Vim in a terminal, then start a terminal in that Vim without a kill
968" argument, check that :confirm qall works.
969func Test_terminal_qall_prompt()
970 if !CanRunVimInTerminal()
971 return
972 endif
973 let buf = RunVimInTerminal('', {})
974
975 " Open a terminal window and wait for the prompt to appear
976 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200977 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
978 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100979
980 " make Vim exit, it will prompt to kill the shell
981 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200982 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100983 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200984 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100985
986 " close the terminal window where Vim was running
987 quit
988endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100989
Bram Moolenaar012eb662018-03-13 17:55:27 +0100990func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100991 augroup repro
992 au!
993 au TerminalOpen * let s:called += 1
994 augroup END
995
996 let s:called = 0
997
998 " Open a terminal window with :terminal
999 terminal
1000 call assert_equal(1, s:called)
1001 bwipe!
1002
1003 " Open a terminal window with term_start()
1004 call term_start(&shell)
1005 call assert_equal(2, s:called)
1006 bwipe!
1007
1008 " Open a hidden terminal buffer with :terminal
1009 terminal ++hidden
1010 call assert_equal(3, s:called)
1011 for buf in term_list()
1012 exe buf . "bwipe!"
1013 endfor
1014
1015 " Open a hidden terminal buffer with term_start()
1016 let buf = term_start(&shell, {'hidden': 1})
1017 call assert_equal(4, s:called)
1018 exe buf . "bwipe!"
1019
1020 unlet s:called
1021 au! repro
1022endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001023
1024func Check_dump01(off)
1025 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1026 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001027 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001028endfunc
1029
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001030func Test_terminal_dumpwrite_composing()
1031 if !CanRunVimInTerminal()
1032 return
1033 endif
1034 let save_enc = &encoding
1035 set encoding=utf-8
1036 call assert_equal(1, winnr('$'))
1037
1038 let text = " a\u0300 e\u0302 o\u0308"
1039 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001040 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001041 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001042 call term_dumpwrite(buf, 'Xdump')
1043 let dumpline = readfile('Xdump')[0]
1044 call assert_match('|à| |ê| |ö', dumpline)
1045
1046 call StopVimInTerminal(buf)
1047 call delete('Xcomposing')
1048 call delete('Xdump')
1049 let &encoding = save_enc
1050endfunc
1051
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001052" just testing basic functionality.
1053func Test_terminal_dumpload()
1054 call assert_equal(1, winnr('$'))
1055 call term_dumpload('dumps/Test_popup_command_01.dump')
1056 call assert_equal(2, winnr('$'))
1057 call assert_equal(20, line('$'))
1058 call Check_dump01(0)
1059 quit
1060endfunc
1061
1062func Test_terminal_dumpdiff()
1063 call assert_equal(1, winnr('$'))
1064 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump')
1065 call assert_equal(2, winnr('$'))
1066 call assert_equal(62, line('$'))
1067 call Check_dump01(0)
1068 call Check_dump01(42)
1069 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1070 quit
1071endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001072
1073func Test_terminal_dumpdiff_options()
1074 set laststatus=0
1075 call assert_equal(1, winnr('$'))
1076 let height = winheight(0)
1077 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1078 call assert_equal(2, winnr('$'))
1079 call assert_equal(height, winheight(winnr()))
1080 call assert_equal(33, winwidth(winnr()))
1081 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1082 quit
1083
1084 call assert_equal(1, winnr('$'))
1085 let width = winwidth(0)
1086 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1087 call assert_equal(2, winnr('$'))
1088 call assert_equal(width, winwidth(winnr()))
1089 call assert_equal(13, winheight(winnr()))
1090 call assert_equal('something else', bufname('%'))
1091 quit
1092
1093 call assert_equal(1, winnr('$'))
1094 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1095 call assert_equal(1, winnr('$'))
1096 bwipe
1097
1098 set laststatus&
1099endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001100
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001101func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001102 call assert_equal(1, winnr('$'))
1103
1104 " Use the title termcap entries to output the escape sequence.
1105 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001106 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001107 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001108 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001109 \ 'redraw',
1110 \ "set t_ts=",
1111 \ ], 'Xscript')
1112 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001113 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001114 call assert_equal('Xtextfile', expand('%:t'))
1115 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001116 return buf
1117endfunc
1118
1119func Test_terminal_api_drop_newwin()
1120 if !CanRunVimInTerminal()
1121 return
1122 endif
1123 let buf = Api_drop_common('')
1124 call assert_equal(0, &bin)
1125 call assert_equal('', &fenc)
1126
1127 call StopVimInTerminal(buf)
1128 call delete('Xscript')
1129 bwipe Xtextfile
1130endfunc
1131
1132func Test_terminal_api_drop_newwin_bin()
1133 if !CanRunVimInTerminal()
1134 return
1135 endif
1136 let buf = Api_drop_common(',{"bin":1}')
1137 call assert_equal(1, &bin)
1138
1139 call StopVimInTerminal(buf)
1140 call delete('Xscript')
1141 bwipe Xtextfile
1142endfunc
1143
1144func Test_terminal_api_drop_newwin_binary()
1145 if !CanRunVimInTerminal()
1146 return
1147 endif
1148 let buf = Api_drop_common(',{"binary":1}')
1149 call assert_equal(1, &bin)
1150
1151 call StopVimInTerminal(buf)
1152 call delete('Xscript')
1153 bwipe Xtextfile
1154endfunc
1155
1156func Test_terminal_api_drop_newwin_nobin()
1157 if !CanRunVimInTerminal()
1158 return
1159 endif
1160 set binary
1161 let buf = Api_drop_common(',{"nobin":1}')
1162 call assert_equal(0, &bin)
1163
1164 call StopVimInTerminal(buf)
1165 call delete('Xscript')
1166 bwipe Xtextfile
1167 set nobinary
1168endfunc
1169
1170func Test_terminal_api_drop_newwin_nobinary()
1171 if !CanRunVimInTerminal()
1172 return
1173 endif
1174 set binary
1175 let buf = Api_drop_common(',{"nobinary":1}')
1176 call assert_equal(0, &bin)
1177
1178 call StopVimInTerminal(buf)
1179 call delete('Xscript')
1180 bwipe Xtextfile
1181 set nobinary
1182endfunc
1183
1184func Test_terminal_api_drop_newwin_ff()
1185 if !CanRunVimInTerminal()
1186 return
1187 endif
1188 let buf = Api_drop_common(',{"ff":"dos"}')
1189 call assert_equal("dos", &ff)
1190
1191 call StopVimInTerminal(buf)
1192 call delete('Xscript')
1193 bwipe Xtextfile
1194endfunc
1195
1196func Test_terminal_api_drop_newwin_fileformat()
1197 if !CanRunVimInTerminal()
1198 return
1199 endif
1200 let buf = Api_drop_common(',{"fileformat":"dos"}')
1201 call assert_equal("dos", &ff)
1202
1203 call StopVimInTerminal(buf)
1204 call delete('Xscript')
1205 bwipe Xtextfile
1206endfunc
1207
1208func Test_terminal_api_drop_newwin_enc()
1209 if !CanRunVimInTerminal()
1210 return
1211 endif
1212 let buf = Api_drop_common(',{"enc":"utf-16"}')
1213 call assert_equal("utf-16", &fenc)
1214
1215 call StopVimInTerminal(buf)
1216 call delete('Xscript')
1217 bwipe Xtextfile
1218endfunc
1219
1220func Test_terminal_api_drop_newwin_encoding()
1221 if !CanRunVimInTerminal()
1222 return
1223 endif
1224 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1225 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001226
1227 call StopVimInTerminal(buf)
1228 call delete('Xscript')
1229 bwipe Xtextfile
1230endfunc
1231
1232func Test_terminal_api_drop_oldwin()
1233 if !CanRunVimInTerminal()
1234 return
1235 endif
1236 let firstwinid = win_getid()
1237 split Xtextfile
1238 let textfile_winid = win_getid()
1239 call assert_equal(2, winnr('$'))
1240 call win_gotoid(firstwinid)
1241
1242 " Use the title termcap entries to output the escape sequence.
1243 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001244 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001245 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1246 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1247 \ 'redraw',
1248 \ "set t_ts=",
1249 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001250 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001251 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001252 call assert_equal(textfile_winid, win_getid())
1253
1254 call StopVimInTerminal(buf)
1255 call delete('Xscript')
1256 bwipe Xtextfile
1257endfunc
1258
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001259func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001260 let g:called_bufnum = a:bufnum
1261 let g:called_arg = a:arg
1262endfunc
1263
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001264func WriteApiCall(funcname)
1265 " Use the title termcap entries to output the escape sequence.
1266 call writefile([
1267 \ 'set title',
1268 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1269 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1270 \ 'redraw',
1271 \ "set t_ts=",
1272 \ ], 'Xscript')
1273endfunc
1274
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001275func Test_terminal_api_call()
1276 if !CanRunVimInTerminal()
1277 return
1278 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001279
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001280 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001281 let buf = RunVimInTerminal('-S Xscript', {})
1282 call WaitFor({-> exists('g:called_bufnum')})
1283 call assert_equal(buf, g:called_bufnum)
1284 call assert_equal(['hello', 123], g:called_arg)
1285
1286 call StopVimInTerminal(buf)
1287 call delete('Xscript')
1288 unlet g:called_bufnum
1289 unlet g:called_arg
1290endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001291
1292func Test_terminal_api_call_fails()
1293 if !CanRunVimInTerminal()
1294 return
1295 endif
1296
1297 call WriteApiCall('TryThis')
1298 call ch_logfile('Xlog', 'w')
1299 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001300 call WaitForAssert({-> assert_match('Invalid function name: TryThis', string(readfile('Xlog')))})
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001301
1302 call StopVimInTerminal(buf)
1303 call delete('Xscript')
1304 call ch_logfile('', '')
1305 call delete('Xlog')
1306endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001307
Bram Moolenaara997b452018-04-17 23:24:06 +02001308let s:caught_e937 = 0
1309
1310func Tapi_Delete(bufnum, arg)
1311 try
1312 execute 'bdelete!' a:bufnum
1313 catch /E937:/
1314 let s:caught_e937 = 1
1315 endtry
1316endfunc
1317
1318func Test_terminal_api_call_fail_delete()
1319 if !CanRunVimInTerminal()
1320 return
1321 endif
1322
1323 call WriteApiCall('Tapi_Delete')
1324 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001325 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001326
1327 call StopVimInTerminal(buf)
1328 call delete('Xscript')
1329 call ch_logfile('', '')
1330endfunc
1331
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001332func Test_terminal_ansicolors_default()
1333 let colors = [
1334 \ '#000000', '#e00000',
1335 \ '#00e000', '#e0e000',
1336 \ '#0000e0', '#e000e0',
1337 \ '#00e0e0', '#e0e0e0',
1338 \ '#808080', '#ff4040',
1339 \ '#40ff40', '#ffff40',
1340 \ '#4040ff', '#ff40ff',
1341 \ '#40ffff', '#ffffff',
1342 \]
1343
1344 let buf = Run_shell_in_terminal({})
1345 call assert_equal(colors, term_getansicolors(buf))
1346 call Stop_shell_in_terminal(buf)
1347 call term_wait(buf)
1348
1349 exe buf . 'bwipe'
1350endfunc
1351
1352let s:test_colors = [
1353 \ '#616e64', '#0d0a79',
1354 \ '#6d610d', '#0a7373',
1355 \ '#690d0a', '#6d696e',
1356 \ '#0d0a6f', '#616e0d',
1357 \ '#0a6479', '#6d0d0a',
1358 \ '#617373', '#0d0a69',
1359 \ '#6d690d', '#0a6e6f',
1360 \ '#610d0a', '#6e6479',
1361 \]
1362
1363func Test_terminal_ansicolors_global()
1364 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1365 let buf = Run_shell_in_terminal({})
1366 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1367 call Stop_shell_in_terminal(buf)
1368 call term_wait(buf)
1369
1370 exe buf . 'bwipe'
1371 unlet g:terminal_ansi_colors
1372endfunc
1373
1374func Test_terminal_ansicolors_func()
1375 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1376 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1377 call assert_equal(s:test_colors, term_getansicolors(buf))
1378
1379 call term_setansicolors(buf, g:terminal_ansi_colors)
1380 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1381
1382 let colors = [
1383 \ 'ivory', 'AliceBlue',
1384 \ 'grey67', 'dark goldenrod',
1385 \ 'SteelBlue3', 'PaleVioletRed4',
1386 \ 'MediumPurple2', 'yellow2',
1387 \ 'RosyBrown3', 'OrangeRed2',
1388 \ 'white smoke', 'navy blue',
1389 \ 'grey47', 'gray97',
1390 \ 'MistyRose2', 'DodgerBlue4',
1391 \]
1392 call term_setansicolors(buf, colors)
1393
1394 let colors[4] = 'Invalid'
1395 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1396
1397 call Stop_shell_in_terminal(buf)
1398 call term_wait(buf)
1399 exe buf . 'bwipe'
1400endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001401
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001402func Test_terminal_termwinsize_option_fixed()
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001403 if !CanRunVimInTerminal()
1404 return
1405 endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001406 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001407 let text = []
1408 for n in range(10)
1409 call add(text, repeat(n, 50))
1410 endfor
1411 call writefile(text, 'Xwinsize')
1412 let buf = RunVimInTerminal('Xwinsize', {})
1413 let win = bufwinid(buf)
1414 call assert_equal([6, 40], term_getsize(buf))
1415 call assert_equal(6, winheight(win))
1416 call assert_equal(40, winwidth(win))
1417
1418 " resizing the window doesn't resize the terminal.
1419 resize 10
1420 vertical resize 60
1421 call assert_equal([6, 40], term_getsize(buf))
1422 call assert_equal(10, winheight(win))
1423 call assert_equal(60, winwidth(win))
1424
1425 call StopVimInTerminal(buf)
1426 call delete('Xwinsize')
1427
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001428 call assert_fails('set termwinsize=40', 'E474')
1429 call assert_fails('set termwinsize=10+40', 'E474')
1430 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001431
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001432 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001433endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001434
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001435func Test_terminal_termwinsize_option_zero()
1436 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001437 let buf = Run_shell_in_terminal({})
1438 let win = bufwinid(buf)
1439 call assert_equal([winheight(win), 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=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001445 let buf = Run_shell_in_terminal({})
1446 let win = bufwinid(buf)
1447 call assert_equal([7, winwidth(win)], 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=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001453 let buf = Run_shell_in_terminal({})
1454 let win = bufwinid(buf)
1455 call assert_equal([winheight(win), 33], term_getsize(buf))
1456 call Stop_shell_in_terminal(buf)
1457 call term_wait(buf)
1458 exe buf . 'bwipe'
1459
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001460 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001461endfunc
1462
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001463func Test_terminal_termwinsize_mininmum()
1464 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001465 vsplit
1466 let buf = Run_shell_in_terminal({})
1467 let win = bufwinid(buf)
1468 call assert_inrange(10, 1000, winheight(win))
1469 call assert_inrange(50, 1000, winwidth(win))
1470 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1471
1472 resize 15
1473 vertical resize 60
1474 redraw
1475 call assert_equal([15, 60], term_getsize(buf))
1476 call assert_equal(15, winheight(win))
1477 call assert_equal(60, winwidth(win))
1478
1479 resize 7
1480 vertical resize 30
1481 redraw
1482 call assert_equal([10, 50], term_getsize(buf))
1483 call assert_equal(7, winheight(win))
1484 call assert_equal(30, winwidth(win))
1485
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=0*0
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), winwidth(win)], 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
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001500
1501func Test_terminal_termwinkey()
1502 call assert_equal(1, winnr('$'))
1503 let thiswin = win_getid()
1504
1505 let buf = Run_shell_in_terminal({})
1506 let termwin = bufwinid(buf)
1507 set termwinkey=<C-L>
1508 call feedkeys("\<C-L>w", 'tx')
1509 call assert_equal(thiswin, win_getid())
1510 call feedkeys("\<C-W>w", 'tx')
1511
1512 let job = term_getjob(buf)
1513 call feedkeys("\<C-L>\<C-C>", 'tx')
1514 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1515endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001516
1517func Test_terminal_out_err()
1518 if !has('unix')
1519 return
1520 endif
1521 call writefile([
1522 \ '#!/bin/sh',
1523 \ 'echo "this is standard error" >&2',
1524 \ 'echo "this is standard out" >&1',
1525 \ ], 'Xechoerrout.sh')
1526 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1527
1528 let outfile = 'Xtermstdout'
1529 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001530
1531 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1532 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001533 call assert_equal('this is standard error', term_getline(buf, 1))
1534
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001535 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001536 exe buf . 'bwipe'
1537 call delete('Xechoerrout.sh')
1538 call delete(outfile)
1539endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001540
1541func Test_terminwinscroll()
1542 if !has('unix')
1543 return
1544 endif
1545
1546 " Let the terminal output more than 'termwinscroll' lines, some at the start
1547 " will be dropped.
1548 exe 'set termwinscroll=' . &lines
1549 let buf = term_start('/bin/sh')
1550 for i in range(1, &lines)
1551 call feedkeys("echo " . i . "\<CR>", 'xt')
1552 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1553 endfor
1554 " Go to Terminal-Normal mode to update the buffer.
1555 call feedkeys("\<C-W>N", 'xt')
1556 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1557
1558 " Every "echo nr" must only appear once
1559 let lines = getline(1, line('$'))
1560 for i in range(&lines - len(lines) / 2 + 2, &lines)
1561 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1562 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1563 endfor
1564
1565 exe buf . 'bwipe!'
1566endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001567
Bram Moolenaar875cf872018-07-08 20:49:07 +02001568" Resizing the terminal window caused an ml_get error.
1569" TODO: This does not reproduce the original problem.
1570func Test_terminal_resize()
1571 set statusline=x
1572 terminal
1573 call assert_equal(2, winnr('$'))
1574
1575 " Fill the terminal with text.
1576 if has('win32')
1577 call feedkeys("dir\<CR>", 'xt')
1578 else
1579 call feedkeys("ls\<CR>", 'xt')
1580 endif
1581 " Go to Terminal-Normal mode for a moment.
1582 call feedkeys("\<C-W>N", 'xt')
1583 " Open a new window
1584 call feedkeys("i\<C-W>n", 'xt')
1585 call assert_equal(3, winnr('$'))
1586 redraw
1587
1588 close
1589 call assert_equal(2, winnr('$'))
1590 call feedkeys("exit\<CR>", 'xt')
1591 set statusline&
1592endfunc
1593
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001594" must be nearly the last, we can't go back from GUI to terminal
1595func Test_zz1_terminal_in_gui()
1596 if !CanRunGui()
1597 return
1598 endif
1599
1600 " Ignore the "failed to create input context" error.
1601 call test_ignore_error('E285:')
1602
1603 gui -f
1604
1605 call assert_equal(1, winnr('$'))
1606 let buf = Run_shell_in_terminal({'term_finish': 'close'})
1607 call Stop_shell_in_terminal(buf)
1608 call term_wait(buf)
1609
1610 " closing window wipes out the terminal buffer a with finished job
1611 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1612 call assert_equal("", bufname(buf))
1613
1614 unlet g:job
1615endfunc
1616
1617func Test_zz2_terminal_guioptions_bang()
1618 if !has('gui_running')
1619 return
1620 endif
1621 set guioptions+=!
1622
1623 let filename = 'Xtestscript'
1624 if has('win32')
1625 let filename .= '.bat'
1626 let prefix = ''
1627 let contents = ['@echo off', 'exit %1']
1628 else
1629 let filename .= '.sh'
1630 let prefix = './'
1631 let contents = ['#!/bin/sh', 'exit $1']
1632 endif
1633 call writefile(contents, filename)
1634 call setfperm(filename, 'rwxrwx---')
1635
1636 " Check if v:shell_error is equal to the exit status.
1637 let exitval = 0
1638 execute printf(':!%s%s %d', prefix, filename, exitval)
1639 call assert_equal(exitval, v:shell_error)
1640
1641 let exitval = 9
1642 execute printf(':!%s%s %d', prefix, filename, exitval)
1643 call assert_equal(exitval, v:shell_error)
1644
1645 set guioptions&
1646 call delete(filename)
1647endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001648
1649func Test_terminal_hidden()
1650 if !has('unix')
1651 return
1652 endif
1653 term ++hidden cat
1654 let bnr = bufnr('$')
1655 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1656 exe 'sbuf ' . bnr
1657 call assert_equal('terminal', &buftype)
1658 call term_sendkeys(bnr, "asdf\<CR>")
1659 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1660 call term_sendkeys(bnr, "\<C-D>")
1661 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
1662 bwipe!
1663endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001664
1665func Test_terminal_hidden_and_close()
1666 if !has('unix')
1667 return
1668 endif
1669 call assert_equal(1, winnr('$'))
1670 term ++hidden ++close ls
1671 let bnr = bufnr('$')
1672 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1673 call WaitForAssert({-> assert_false(bufexists(bnr))})
1674 call assert_equal(1, winnr('$'))
1675endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001676
1677func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001678 " This test does not pass through ConPTY.
1679 if has('conpty')
1680 return
1681 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001682 let contents = [
1683 \ [ 'One', '', 'X' ],
1684 \ [ 'Two', '', '' ],
1685 \ [ 'Three' ] + repeat([''], 30)
1686 \ ]
1687
1688 for c in contents
1689 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01001690 if has('win32')
1691 term cmd /c type Xfile
1692 else
1693 term cat Xfile
1694 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001695 let bnr = bufnr('$')
1696 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1697 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01001698 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001699 call assert_equal(c, getline(1, line('$')))
1700 quit
1701 endfor
1702
1703 call delete('Xfile')
1704endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01001705
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01001706func Test_terminal_no_job()
1707 let term = term_start('false', {'term_finish': 'close'})
1708 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
1709endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01001710
1711func Test_term_gettitle()
1712 if !has('title') || empty(&t_ts)
1713 return
1714 endif
1715 " TODO: this fails on Travis
1716 return
1717
1718 " term_gettitle() returns an empty string for a non-terminal buffer
1719 " or for a non-existing buffer.
1720 call assert_equal('', term_gettitle(bufnr('%')))
1721 call assert_equal('', term_gettitle(bufnr('$') + 1))
1722
1723 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
1724 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
1725
1726 call term_sendkeys(term, ":e Xfoo\r")
1727 call WaitForAssert({-> assert_match('Xfoo (.*[/\\]testdir) - VIM', term_gettitle(term)) })
1728
1729 call term_sendkeys(term, ":set titlestring=foo\r")
1730 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
1731
1732 exe term . 'bwipe!'
1733endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01001734
1735" When drawing the statusline the cursor position may not have been updated
1736" yet.
1737" 1. create a terminal, make it show 2 lines
1738" 2. 0.5 sec later: leave terminal window, execute "i"
1739" 3. 0.5 sec later: clear terminal window, now it's 1 line
1740" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1741" 4. 0.5 sec later: should be done, clean up
1742func Test_terminal_statusline()
1743 if !has('unix')
1744 return
1745 endif
1746 set statusline=x
1747 terminal
1748 let tbuf = bufnr('')
1749 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1750 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1751 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1752 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1753
1754 sleep 2
1755 exe tbuf . 'bwipe!'
1756 au! BufLeave
1757 set statusline=
1758endfunc