blob: 5496b6325e86da3a9c416d8cae427ee7dc072cbe [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature terminal
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02005
6source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01007source screendump.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02008
Bram Moolenaarb81bc772017-08-11 22:45:01 +02009let s:python = PythonProg()
Bram Moolenaarddd33082019-06-03 23:07:25 +020010let $PROMPT_COMMAND=''
Bram Moolenaarb81bc772017-08-11 22:45:01 +020011
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 Moolenaar7a39dd72019-06-23 00:50:15 +020055 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +020056 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 Moolenaar7a39dd72019-06-23 00:50:15 +020073 call StopShellInTerminal(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 Moolenaar5b868a82019-02-25 06:11:53 +010085func Test_terminal_paste_register()
86 let @" = "text to paste"
87
88 let buf = Run_shell_in_terminal({})
89 " Wait for the shell to display a prompt
90 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
91
92 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
93 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
94 call WaitForAssert({-> assert_equal('text to paste 42', getline(2))})
95
96 exe buf . 'bwipe!'
97 unlet g:job
98endfunc
99
Bram Moolenaar94053a52017-08-01 21:44:33 +0200100func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200101 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +0200102 call assert_fails(buf . 'bwipe', 'E517')
103 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200104 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200105 call assert_equal("", bufname(buf))
106
107 unlet g:job
108endfunc
109
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200110func Test_terminal_split_quit()
111 let buf = Run_shell_in_terminal({})
112 call term_wait(buf)
113 split
114 quit!
115 call term_wait(buf)
116 sleep 50m
117 call assert_equal('run', job_status(g:job))
118
119 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200120 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200121
122 exe buf . 'bwipe'
123 unlet g:job
124endfunc
125
Bram Moolenaar94053a52017-08-01 21:44:33 +0200126func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200127 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200128 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200129 quit
130 for nr in range(1, winnr('$'))
131 call assert_notequal(winbufnr(nr), buf)
132 endfor
133 call assert_true(bufloaded(buf))
134 call assert_true(buflisted(buf))
135
136 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200137 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200138 exe buf . 'bwipe'
139
140 unlet g:job
141endfunc
142
Bram Moolenaar1e115362019-01-09 23:01:02 +0100143func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200144 exe g:buf . 'bwipe!'
145 let g:buf = 0
146endfunc
147
Bram Moolenaar9d189612017-09-09 18:11:00 +0200148func Get_cat_123_cmd()
149 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100150 if !has('conpty')
151 return 'cmd /c "cls && color 2 && echo 123"'
152 else
153 " When clearing twice, extra sequence is not output.
154 return 'cmd /c "cls && cls && color 2 && echo 123"'
155 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200156 else
157 call writefile(["\<Esc>[32m123"], 'Xtext')
158 return "cat Xtext"
159 endif
160endfunc
161
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200162func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200163 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200164 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
165 let g:job = term_getjob(g:buf)
166
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200167 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
168 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200169 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100170 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200171 call delete('Xtext')
172endfunc
173
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200174func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200175 let l = term_scrape(a:buf, 0)
176 call assert_true(len(l) == 0)
177 let l = term_scrape(a:buf, 999)
178 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200179 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200180 call assert_true(len(l) > 0)
181 call assert_equal('1', l[0].chars)
182 call assert_equal('2', l[1].chars)
183 call assert_equal('3', l[2].chars)
184 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar81df6352018-12-22 18:25:30 +0100185 if has('win32')
186 " On Windows 'background' always defaults to dark, even though the terminal
187 " may use a light background. Therefore accept both white and black.
188 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200189 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100190 if &background == 'light'
191 call assert_equal('#ffffff', l[0].bg)
192 else
193 call assert_equal('#000000', l[0].bg)
194 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200195 endif
196
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200197 let l = term_getline(a:buf, -1)
198 call assert_equal('', l)
199 let l = term_getline(a:buf, 0)
200 call assert_equal('', l)
201 let l = term_getline(a:buf, 999)
202 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200203 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200204 call assert_equal('123', l)
205endfunc
206
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200207func Test_terminal_scrape_123()
208 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200209 let buf = term_start(cmd)
210
211 let termlist = term_list()
212 call assert_equal(1, len(termlist))
213 call assert_equal(buf, termlist[0])
214
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200215 " Nothing happens with invalid buffer number
216 call term_wait(1234)
217
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200218 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200219 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200220 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200221 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200222 call Check_123(buf)
223
224 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100225 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200226 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200227 call term_wait(buf)
228 call Check_123(buf)
229
230 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200231 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200232endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200233
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200234func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200235 call writefile(["léttまrs"], 'Xtext')
236 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200237 " Run cmd with UTF-8 codepage to make the type command print the expected
238 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100239 let buf = term_start("cmd /K chcp 65001")
240 call term_sendkeys(buf, "type Xtext\<CR>")
241 call term_sendkeys(buf, "exit\<CR>")
242 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200243 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100244 let buf = term_start("cat Xtext")
245 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200246 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200247
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100248 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
249 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200250 call assert_true(len(l) >= 7)
251 call assert_equal('l', l[0].chars)
252 call assert_equal('é', l[1].chars)
253 call assert_equal(1, l[1].width)
254 call assert_equal('t', l[2].chars)
255 call assert_equal('t', l[3].chars)
256 call assert_equal('ま', l[4].chars)
257 call assert_equal(2, l[4].width)
258 call assert_equal('r', l[5].chars)
259 call assert_equal('s', l[6].chars)
260
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100261 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200262 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100263 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200264
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100265 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200266 call delete('Xtext')
267endfunc
268
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200269func Test_terminal_scroll()
270 call writefile(range(1, 200), 'Xtext')
271 if has('win32')
272 let cmd = 'cmd /c "type Xtext"'
273 else
274 let cmd = "cat Xtext"
275 endif
276 let buf = term_start(cmd)
277
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100278 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200279 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200280 call term_wait(buf)
281 if has('win32')
282 " TODO: this should not be needed
283 sleep 100m
284 endif
285
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200286 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200287 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200288 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200289 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200290 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200291 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200292 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200293
294 exe buf . 'bwipe'
295 call delete('Xtext')
296endfunc
297
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200298func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200299 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200300 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200301 call writefile(range(150), 'Xtext')
302 if has('win32')
303 call term_sendkeys(buf, "type Xtext\<CR>")
304 else
305 call term_sendkeys(buf, "cat Xtext\<CR>")
306 endif
307 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200308 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200309 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200310 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200311 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200312
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200313 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200314 call term_wait(buf)
315 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200316 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100317 call delete('Xtext')
318endfunc
319
320func Test_terminal_postponed_scrollback()
321 if !has('unix')
322 " tail -f only works on Unix
323 return
324 endif
325
326 call writefile(range(50), 'Xtext')
327 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100328 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100329 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100330 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100331 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
332 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100333 \ 'call feedkeys("\<C-W>N", "xt")',
334 \ ], 'XTest_postponed')
335 let buf = RunVimInTerminal('-S XTest_postponed', {})
336 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaar96baf022019-02-14 23:49:38 +0100337 call VerifyScreenDump(buf, 'Test_terminal_01', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100338
339 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200340 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100341 call term_sendkeys(buf, "k")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100342 call VerifyScreenDump(buf, 'Test_terminal_02', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100343
344 " Back to Terminal-Job mode, text will scroll and show the extra line.
345 call term_sendkeys(buf, "a")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100346 call VerifyScreenDump(buf, 'Test_terminal_03', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100347
348 call term_wait(buf)
349 call term_sendkeys(buf, "\<C-C>")
350 call term_wait(buf)
351 call term_sendkeys(buf, "exit\<CR>")
352 call term_wait(buf)
353 call term_sendkeys(buf, ":q\<CR>")
354 call StopVimInTerminal(buf)
355 call delete('XTest_postponed')
356 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200357endfunc
358
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100359" Run diff on two dumps with different size.
360func Test_terminal_dumpdiff_size()
361 call assert_equal(1, winnr('$'))
362 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
363 call assert_equal(2, winnr('$'))
364 call assert_match('Test_incsearch_search_01.dump', getline(10))
365 call assert_match(' +++++$', getline(11))
366 call assert_match('Test_popup_command_01.dump', getline(31))
367 call assert_equal(repeat('+', 75), getline(30))
368 quit
369endfunc
370
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200371func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200372 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200373
Bram Moolenaarb2412082017-08-20 18:09:14 +0200374 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200375 let size = term_getsize('')
376 bwipe!
377 call assert_equal(5, size[0])
378
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200379 call term_start(cmd, {'term_rows': 6})
380 let size = term_getsize('')
381 bwipe!
382 call assert_equal(6, size[0])
383
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200384 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200385 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaara42d3632018-04-14 17:05:38 +0200386 call assert_equal([5, 33], term_getsize(''))
387
388 call term_setsize('', 6, 0)
389 call assert_equal([6, 33], term_getsize(''))
390
391 call term_setsize('', 0, 35)
392 call assert_equal([6, 35], term_getsize(''))
393
394 call term_setsize('', 7, 30)
395 call assert_equal([7, 30], term_getsize(''))
396
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200397 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200398 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200399
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200400 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
401 let size = term_getsize('')
402 bwipe!
403 call assert_equal([6, 36], size)
404
Bram Moolenaarb2412082017-08-20 18:09:14 +0200405 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200406 let size = term_getsize('')
407 bwipe!
408 call assert_equal(20, size[1])
409
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200410 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
411 let size = term_getsize('')
412 bwipe!
413 call assert_equal(26, size[1])
414
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200415 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200416 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200417 let size = term_getsize('')
418 bwipe!
419 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200420
421 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
422 let size = term_getsize('')
423 bwipe!
424 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200425
426 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200427endfunc
428
429func Test_terminal_curwin()
430 let cmd = Get_cat_123_cmd()
431 call assert_equal(1, winnr('$'))
432
433 split dummy
434 exe 'terminal ++curwin ' . cmd
435 call assert_equal(2, winnr('$'))
436 bwipe!
437
438 split dummy
439 call term_start(cmd, {'curwin': 1})
440 call assert_equal(2, winnr('$'))
441 bwipe!
442
443 split dummy
444 call setline(1, 'change')
445 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
446 call assert_equal(2, winnr('$'))
447 exe 'terminal! ++curwin ' . cmd
448 call assert_equal(2, winnr('$'))
449 bwipe!
450
451 split dummy
452 call setline(1, 'change')
453 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
454 call assert_equal(2, winnr('$'))
455 bwipe!
456
457 split dummy
458 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200459 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200460endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200461
Bram Moolenaarff546792017-11-21 14:47:57 +0100462func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200463 if s:python != ''
464 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200465 " 500 was not enough for Travis
466 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200467 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200468 echo 'This will take five seconds...'
469 let waittime = 2000
470 if has('win32')
471 let cmd = $windir . '\system32\timeout.exe 1'
472 else
473 let cmd = 'sleep 1'
474 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200475 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100476 return [cmd, waittime]
477endfunc
478
479func Test_terminal_finish_open_close()
480 call assert_equal(1, winnr('$'))
481
482 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200483
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100484 " shell terminal closes automatically
485 terminal
486 let buf = bufnr('%')
487 call assert_equal(2, winnr('$'))
488 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200489 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200490 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200491 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100492
493 " shell terminal that does not close automatically
494 terminal ++noclose
495 let buf = bufnr('%')
496 call assert_equal(2, winnr('$'))
497 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200498 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200499 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100500 call assert_equal(2, winnr('$'))
501 quit
502 call assert_equal(1, winnr('$'))
503
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200504 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200505 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200506 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200507 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200508
509 call term_start(cmd, {'term_finish': 'close'})
510 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200511 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200512 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200513 call assert_equal(1, winnr('$'))
514
515 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200516 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200517 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200518 bwipe
519
520 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200521 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200522 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200523 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200524
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200525 exe 'terminal ++hidden ++open ' . cmd
526 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200527 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200528 bwipe
529
530 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
531 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200532 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200533 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200534
535 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
536 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
537 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
538 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
539
540 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200541 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200542 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200543 call assert_equal(4, winheight(0))
544 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200545endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200546
547func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200548 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200549 return
550 endif
551 call mkdir('Xdir')
552 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200553 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200554
555 exe buf . 'bwipe'
556 call delete('Xdir', 'rf')
557endfunc
558
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200559func Test_terminal_cwd_failure()
560 " Case 1: Provided directory is not actually a directory. Attempt to make
561 " the file executable as well.
562 call writefile([], 'Xfile')
563 call setfperm('Xfile', 'rwx------')
564 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
565 call delete('Xfile')
566
567 " Case 2: Directory does not exist.
568 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
569
570 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100571 " Skip this for root, it will be accessible anyway.
572 if $USER != 'root'
573 call mkdir('XdirNoAccess', '', '0600')
574 " return early if the directory permissions could not be set properly
575 if getfperm('XdirNoAccess')[2] == 'x'
576 call delete('XdirNoAccess', 'rf')
577 return
578 endif
579 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
580 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200581 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200582endfunc
583
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100584func Test_terminal_servername()
585 if !has('clientserver')
586 return
587 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200588 call s:test_environment("VIM_SERVERNAME", v:servername)
589endfunc
590
591func Test_terminal_version()
592 call s:test_environment("VIM_TERMINAL", string(v:version))
593endfunc
594
595func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100596 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100597 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200598 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100599 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200600 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100601 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200602 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100603 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100604 call term_wait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200605 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200606 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100607
Bram Moolenaar012eb662018-03-13 17:55:27 +0100608 exe buf . 'bwipe'
609 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100610endfunc
611
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200612func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100613 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200614 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200615 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100616 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100617 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100618 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100619 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100620 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100621 call term_wait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200622 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200623 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200624
Bram Moolenaar012eb662018-03-13 17:55:27 +0100625 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200626endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200627
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200628func Test_terminal_list_args()
629 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
630 call assert_fails(buf . 'bwipe', 'E517')
631 exe buf . 'bwipe!'
632 call assert_equal("", bufname(buf))
633endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200634
635func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100636 let buf = term_start(&shell)
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100637 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200638 " The shell or something else has a problem dealing with more than 1000
639 " characters at the same time.
640 let len = 1000
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100641 " NPFS is used in Windows, nonblocking mode does not work properly.
642 elseif has('win32')
643 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200644 else
645 let len = 5000
646 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200647
648 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100649 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200650 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100651 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200652
653 " On MS-Windows there is an extra empty line below "done". Find "done" in
654 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100655 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100656 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100657 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200658 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100659 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200660 endif
661 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200662
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100663 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200664 call StopShellInTerminal(buf)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100665 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200666 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200667 bwipe
668endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200669
670func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200671 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200672 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200673 endif
Bram Moolenaar1580f752018-06-03 15:26:36 +0200674 if has('win32')
675 " TODO: enable once writing to stdin works on MS-Windows
676 return
677 endif
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200678 new
679 call setline(1, ['one', 'two', 'three'])
680 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200681 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200682 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200683 call assert_equal(['3', '3', '14'], nrs)
684 bwipe
685
Bram Moolenaardada6d22017-09-02 17:18:35 +0200686 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200687 call setline(1, ['one', 'two', 'three', 'four'])
688 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200689 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200690 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200691 call assert_equal(['2', '2', '10'], nrs)
692 bwipe
693
Bram Moolenaardada6d22017-09-02 17:18:35 +0200694 if executable('python')
695 new
696 call setline(1, ['print("hello")'])
697 1term ++eof=exit() python
698 " MS-Windows echoes the input, Unix doesn't.
699 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
700 if getline(1) =~ 'hello'
701 call assert_equal('hello', getline(1))
702 else
703 call assert_equal('hello', getline(line('$') - 1))
704 endif
705 bwipe
706
707 if has('win32')
708 new
709 call setline(1, ['print("hello")'])
710 1term ++eof=<C-Z> python
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200711 call WaitForAssert({-> assert_match('Z', getline("$"))})
Bram Moolenaardada6d22017-09-02 17:18:35 +0200712 call assert_equal('hello', getline(line('$') - 1))
713 bwipe
714 endif
715 endif
716
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200717 bwipe!
718endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200719
720func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200721 let buf = term_start('NONE', {})
722 call assert_notequal(0, buf)
723
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200724 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200725 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100726 if has('gui_running') && !has('win32')
727 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200728 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100729 else
730 " Otherwise using a job works on all systems.
731 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200732 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200733 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200734
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200735 bwipe!
736endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200737
738func Test_terminal_special_chars()
739 " this file name only works on Unix
740 if !has('unix')
741 return
742 endif
743 call mkdir('Xdir with spaces')
744 call writefile(['x'], 'Xdir with spaces/quoted"file')
745 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200746 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200747 call term_wait('')
748
749 call delete('Xdir with spaces', 'rf')
750 bwipe
751endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200752
753func Test_terminal_wrong_options()
754 call assert_fails('call term_start(&shell, {
755 \ "in_io": "file",
756 \ "in_name": "xxx",
757 \ "out_io": "file",
758 \ "out_name": "xxx",
759 \ "err_io": "file",
760 \ "err_name": "xxx"
761 \ })', 'E474:')
762 call assert_fails('call term_start(&shell, {
763 \ "out_buf": bufnr("%")
764 \ })', 'E474:')
765 call assert_fails('call term_start(&shell, {
766 \ "err_buf": bufnr("%")
767 \ })', 'E474:')
768endfunc
769
770func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200771 let cmd = Get_cat_123_cmd()
772 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
773 call term_wait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100774 " ConPTY may precede escape sequence. There are things that are not so.
775 if !has('conpty')
776 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
777 call assert_match('123', readfile('Xfile')[0])
778 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200779 let g:job = term_getjob(buf)
780 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
781 call delete('Xfile')
782 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200783
784 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200785 call writefile(['one line'], 'Xfile')
786 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
787 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200788 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200789 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200790 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200791 bwipe
792 call delete('Xfile')
793 endif
794endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200795
796func TerminalTmap(remap)
797 let buf = Run_shell_in_terminal({})
798 call assert_equal('t', mode())
799
800 if a:remap
801 tmap 123 456
802 else
803 tnoremap 123 456
804 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100805 " don't use abcde, it's an existing command
806 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200807 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100808 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200809 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200810 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200811 let lnum = term_getcursor(buf)[0]
812 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100813 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200814 else
815 call assert_match('456', term_getline(buf, lnum))
816 endif
817
818 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200819 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200820 call term_wait(buf)
821
822 tunmap 123
823 tunmap 456
824 call assert_equal('', maparg('123', 't'))
825 close
826 unlet g:job
827endfunc
828
829func Test_terminal_tmap()
830 call TerminalTmap(1)
831 call TerminalTmap(0)
832endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200833
834func Test_terminal_wall()
835 let buf = Run_shell_in_terminal({})
836 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200837 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200838 call term_wait(buf)
839 exe buf . 'bwipe'
840 unlet g:job
841endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200842
Bram Moolenaar7a760922018-02-19 23:10:02 +0100843func Test_terminal_wqall()
844 let buf = Run_shell_in_terminal({})
845 call assert_fails('wqall', 'E948')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200846 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100847 call term_wait(buf)
848 exe buf . 'bwipe'
849 unlet g:job
850endfunc
851
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200852func Test_terminal_composing_unicode()
853 let save_enc = &encoding
854 set encoding=utf-8
855
856 if has('win32')
857 let cmd = "cmd /K chcp 65001"
858 let lnum = [3, 6, 9]
859 else
860 let cmd = &shell
861 let lnum = [1, 3, 5]
862 endif
863
864 enew
865 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100866 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200867 call term_wait(buf, 50)
868
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200869 if has('win32')
870 call assert_equal('cmd', job_info(g:job).cmd[0])
871 else
872 call assert_equal(&shell, job_info(g:job).cmd[0])
873 endif
874
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200875 " ascii + composing
876 let txt = "a\u0308bc"
877 call term_sendkeys(buf, "echo " . txt . "\r")
878 call term_wait(buf, 50)
879 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
880 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
881 let l = term_scrape(buf, lnum[0] + 1)
882 call assert_equal("a\u0308", l[0].chars)
883 call assert_equal("b", l[1].chars)
884 call assert_equal("c", l[2].chars)
885
886 " multibyte + composing
887 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
888 call term_sendkeys(buf, "echo " . txt . "\r")
889 call term_wait(buf, 50)
890 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
891 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
892 let l = term_scrape(buf, lnum[1] + 1)
893 call assert_equal("\u304b\u3099", l[0].chars)
894 call assert_equal("\u304e", l[1].chars)
895 call assert_equal("\u304f\u3099", l[2].chars)
896 call assert_equal("\u3052", l[3].chars)
897 call assert_equal("\u3053\u3099", l[4].chars)
898
899 " \u00a0 + composing
900 let txt = "abc\u00a0\u0308"
901 call term_sendkeys(buf, "echo " . txt . "\r")
902 call term_wait(buf, 50)
903 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
904 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
905 let l = term_scrape(buf, lnum[2] + 1)
906 call assert_equal("\u00a0\u0308", l[3].chars)
907
908 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200909 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200910 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100911 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200912 let &encoding = save_enc
913endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100914
915func Test_terminal_aucmd_on_close()
916 fun Nop()
917 let s:called = 1
918 endfun
919
920 aug repro
921 au!
922 au BufWinLeave * call Nop()
923 aug END
924
925 let [cmd, waittime] = s:get_sleep_cmd()
926
927 call assert_equal(1, winnr('$'))
928 new
929 call setline(1, ['one', 'two'])
930 exe 'term ++close ' . cmd
931 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200932 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100933 call assert_equal(1, s:called)
934 bwipe!
935
936 unlet s:called
937 au! repro
938 delfunc Nop
939endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100940
941func Test_terminal_term_start_empty_command()
942 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
943 call assert_fails(cmd, 'E474')
944 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
945 call assert_fails(cmd, 'E474')
946 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
947 call assert_fails(cmd, 'E474')
948 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
949 call assert_fails(cmd, 'E474')
950endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100951
952func Test_terminal_response_to_control_sequence()
953 if !has('unix')
954 return
955 endif
956
957 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200958 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100959
Bram Moolenaar086eb872018-03-25 21:24:12 +0200960 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200961 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100962
Bram Moolenaar086eb872018-03-25 21:24:12 +0200963 " Request the cursor position.
964 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100965
966 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200967 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100968
Bram Moolenaar086eb872018-03-25 21:24:12 +0200969 " End "cat" gently.
970 call term_sendkeys(buf, "\<CR>\<C-D>")
971
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200972 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100973 exe buf . 'bwipe'
974 unlet g:job
975endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100976
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100977" Run Vim, start a terminal in that Vim with the kill argument,
978" :qall works.
979func Run_terminal_qall_kill(line1, line2)
980 " 1. Open a terminal window and wait for the prompt to appear
981 " 2. set kill using term_setkill()
982 " 3. make Vim exit, it will kill the shell
983 let after = [
984 \ a:line1,
985 \ 'let buf = bufnr("%")',
986 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
987 \ ' sleep 10m',
988 \ 'endwhile',
989 \ a:line2,
990 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
991 \ 'qall',
992 \ ]
993 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100994 return
995 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100996 call assert_equal("done", readfile("Xdone")[0])
997 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100998endfunc
999
1000" Run Vim in a terminal, then start a terminal in that Vim with a kill
1001" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001002func Test_terminal_qall_kill_arg()
1003 call Run_terminal_qall_kill('term ++kill=kill', '')
1004endfunc
1005
1006" Run Vim, start a terminal in that Vim, set the kill argument with
1007" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001008func Test_terminal_qall_kill_func()
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001009 call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")')
1010endfunc
1011
1012" Run Vim, start a terminal in that Vim without the kill argument,
1013" check that :qall does not exit, :qall! does.
1014func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001015 let after =<< trim [CODE]
1016 term
1017 let buf = bufnr("%")
1018 while term_getline(buf, 1) =~ "^\\s*$"
1019 sleep 10m
1020 endwhile
1021 set nomore
1022 au VimLeavePre * call writefile(["too early"], "Xdone")
1023 qall
1024 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1025 cquit
1026 [CODE]
1027
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001028 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001029 return
1030 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001031 call assert_equal("done", readfile("Xdone")[0])
1032 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001033endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001034
1035" Run Vim in a terminal, then start a terminal in that Vim without a kill
1036" argument, check that :confirm qall works.
1037func Test_terminal_qall_prompt()
1038 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001039 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001040 endif
1041 let buf = RunVimInTerminal('', {})
1042
1043 " Open a terminal window and wait for the prompt to appear
1044 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001045 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1046 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001047
1048 " make Vim exit, it will prompt to kill the shell
1049 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001050 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001051 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001052 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001053
1054 " close the terminal window where Vim was running
1055 quit
1056endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001057
Bram Moolenaar012eb662018-03-13 17:55:27 +01001058func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001059 augroup repro
1060 au!
1061 au TerminalOpen * let s:called += 1
1062 augroup END
1063
1064 let s:called = 0
1065
1066 " Open a terminal window with :terminal
1067 terminal
1068 call assert_equal(1, s:called)
1069 bwipe!
1070
1071 " Open a terminal window with term_start()
1072 call term_start(&shell)
1073 call assert_equal(2, s:called)
1074 bwipe!
1075
1076 " Open a hidden terminal buffer with :terminal
1077 terminal ++hidden
1078 call assert_equal(3, s:called)
1079 for buf in term_list()
1080 exe buf . "bwipe!"
1081 endfor
1082
1083 " Open a hidden terminal buffer with term_start()
1084 let buf = term_start(&shell, {'hidden': 1})
1085 call assert_equal(4, s:called)
1086 exe buf . "bwipe!"
1087
1088 unlet s:called
1089 au! repro
1090endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001091
1092func Check_dump01(off)
1093 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1094 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001095 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001096endfunc
1097
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001098func Test_terminal_dumpwrite_composing()
1099 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001100 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001101 endif
1102 let save_enc = &encoding
1103 set encoding=utf-8
1104 call assert_equal(1, winnr('$'))
1105
1106 let text = " a\u0300 e\u0302 o\u0308"
1107 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001108 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001109 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001110 call term_dumpwrite(buf, 'Xdump')
1111 let dumpline = readfile('Xdump')[0]
1112 call assert_match('|à| |ê| |ö', dumpline)
1113
1114 call StopVimInTerminal(buf)
1115 call delete('Xcomposing')
1116 call delete('Xdump')
1117 let &encoding = save_enc
1118endfunc
1119
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001120" just testing basic functionality.
1121func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001122 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001123 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001124 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001125 call assert_equal(2, winnr('$'))
1126 call assert_equal(20, line('$'))
1127 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001128
1129 " Load another dump in the same window
1130 let buf2 = term_dumpload('dumps/Test_diff_01.dump', {'bufnr': buf})
1131 call assert_equal(buf, buf2)
1132 call assert_notequal('one two three four five', trim(getline(1)))
1133
1134 " Load the first dump again in the same window
1135 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1136 call assert_equal(buf, buf2)
1137 call Check_dump01(0)
1138
1139 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1140 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1141 new
1142 let closedbuf = winbufnr('')
1143 quit
1144 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
1145
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001146 quit
1147endfunc
1148
1149func Test_terminal_dumpdiff()
1150 call assert_equal(1, winnr('$'))
1151 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump')
1152 call assert_equal(2, winnr('$'))
1153 call assert_equal(62, line('$'))
1154 call Check_dump01(0)
1155 call Check_dump01(42)
1156 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1157 quit
1158endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001159
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001160func Test_terminal_dumpdiff_swap()
1161 call assert_equal(1, winnr('$'))
1162 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1163 call assert_equal(2, winnr('$'))
1164 call assert_equal(62, line('$'))
1165 call assert_match('Test_popup_command_01.dump', getline(21))
1166 call assert_match('Test_popup_command_03.dump', getline(42))
1167 call assert_match('Undo', getline(3))
1168 call assert_match('three four five', getline(45))
1169
1170 normal s
1171 call assert_match('Test_popup_command_03.dump', getline(21))
1172 call assert_match('Test_popup_command_01.dump', getline(42))
1173 call assert_match('three four five', getline(3))
1174 call assert_match('Undo', getline(45))
1175 quit
1176endfunc
1177
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001178func Test_terminal_dumpdiff_options()
1179 set laststatus=0
1180 call assert_equal(1, winnr('$'))
1181 let height = winheight(0)
1182 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1183 call assert_equal(2, winnr('$'))
1184 call assert_equal(height, winheight(winnr()))
1185 call assert_equal(33, winwidth(winnr()))
1186 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1187 quit
1188
1189 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001190 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1191 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001192 call assert_equal(&columns, winwidth(0))
1193 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001194 call assert_equal('something else', bufname('%'))
1195 quit
1196
1197 call assert_equal(1, winnr('$'))
1198 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1199 call assert_equal(1, winnr('$'))
1200 bwipe
1201
1202 set laststatus&
1203endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001204
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001205func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001206 call assert_equal(1, winnr('$'))
1207
1208 " Use the title termcap entries to output the escape sequence.
1209 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001210 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001211 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001212 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001213 \ 'redraw',
1214 \ "set t_ts=",
1215 \ ], 'Xscript')
1216 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001217 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001218 call assert_equal('Xtextfile', expand('%:t'))
1219 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001220 return buf
1221endfunc
1222
1223func Test_terminal_api_drop_newwin()
1224 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001225 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001226 endif
1227 let buf = Api_drop_common('')
1228 call assert_equal(0, &bin)
1229 call assert_equal('', &fenc)
1230
1231 call StopVimInTerminal(buf)
1232 call delete('Xscript')
1233 bwipe Xtextfile
1234endfunc
1235
1236func Test_terminal_api_drop_newwin_bin()
1237 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001238 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001239 endif
1240 let buf = Api_drop_common(',{"bin":1}')
1241 call assert_equal(1, &bin)
1242
1243 call StopVimInTerminal(buf)
1244 call delete('Xscript')
1245 bwipe Xtextfile
1246endfunc
1247
1248func Test_terminal_api_drop_newwin_binary()
1249 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001250 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001251 endif
1252 let buf = Api_drop_common(',{"binary":1}')
1253 call assert_equal(1, &bin)
1254
1255 call StopVimInTerminal(buf)
1256 call delete('Xscript')
1257 bwipe Xtextfile
1258endfunc
1259
1260func Test_terminal_api_drop_newwin_nobin()
1261 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001262 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001263 endif
1264 set binary
1265 let buf = Api_drop_common(',{"nobin":1}')
1266 call assert_equal(0, &bin)
1267
1268 call StopVimInTerminal(buf)
1269 call delete('Xscript')
1270 bwipe Xtextfile
1271 set nobinary
1272endfunc
1273
1274func Test_terminal_api_drop_newwin_nobinary()
1275 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001276 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001277 endif
1278 set binary
1279 let buf = Api_drop_common(',{"nobinary":1}')
1280 call assert_equal(0, &bin)
1281
1282 call StopVimInTerminal(buf)
1283 call delete('Xscript')
1284 bwipe Xtextfile
1285 set nobinary
1286endfunc
1287
1288func Test_terminal_api_drop_newwin_ff()
1289 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001290 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001291 endif
1292 let buf = Api_drop_common(',{"ff":"dos"}')
1293 call assert_equal("dos", &ff)
1294
1295 call StopVimInTerminal(buf)
1296 call delete('Xscript')
1297 bwipe Xtextfile
1298endfunc
1299
1300func Test_terminal_api_drop_newwin_fileformat()
1301 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001302 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001303 endif
1304 let buf = Api_drop_common(',{"fileformat":"dos"}')
1305 call assert_equal("dos", &ff)
1306
1307 call StopVimInTerminal(buf)
1308 call delete('Xscript')
1309 bwipe Xtextfile
1310endfunc
1311
1312func Test_terminal_api_drop_newwin_enc()
1313 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001314 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001315 endif
1316 let buf = Api_drop_common(',{"enc":"utf-16"}')
1317 call assert_equal("utf-16", &fenc)
1318
1319 call StopVimInTerminal(buf)
1320 call delete('Xscript')
1321 bwipe Xtextfile
1322endfunc
1323
1324func Test_terminal_api_drop_newwin_encoding()
1325 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001326 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001327 endif
1328 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1329 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001330
1331 call StopVimInTerminal(buf)
1332 call delete('Xscript')
1333 bwipe Xtextfile
1334endfunc
1335
1336func Test_terminal_api_drop_oldwin()
1337 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001338 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001339 endif
1340 let firstwinid = win_getid()
1341 split Xtextfile
1342 let textfile_winid = win_getid()
1343 call assert_equal(2, winnr('$'))
1344 call win_gotoid(firstwinid)
1345
1346 " Use the title termcap entries to output the escape sequence.
1347 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001348 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001349 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1350 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1351 \ 'redraw',
1352 \ "set t_ts=",
1353 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001354 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001355 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001356 call assert_equal(textfile_winid, win_getid())
1357
1358 call StopVimInTerminal(buf)
1359 call delete('Xscript')
1360 bwipe Xtextfile
1361endfunc
1362
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001363func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001364 let g:called_bufnum = a:bufnum
1365 let g:called_arg = a:arg
1366endfunc
1367
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001368func WriteApiCall(funcname)
1369 " Use the title termcap entries to output the escape sequence.
1370 call writefile([
1371 \ 'set title',
1372 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1373 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1374 \ 'redraw',
1375 \ "set t_ts=",
1376 \ ], 'Xscript')
1377endfunc
1378
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001379func Test_terminal_api_call()
1380 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001381 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001382 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001383
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001384 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001385 let buf = RunVimInTerminal('-S Xscript', {})
1386 call WaitFor({-> exists('g:called_bufnum')})
1387 call assert_equal(buf, g:called_bufnum)
1388 call assert_equal(['hello', 123], g:called_arg)
1389
1390 call StopVimInTerminal(buf)
1391 call delete('Xscript')
1392 unlet g:called_bufnum
1393 unlet g:called_arg
1394endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001395
1396func Test_terminal_api_call_fails()
1397 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001398 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001399 endif
1400
1401 call WriteApiCall('TryThis')
1402 call ch_logfile('Xlog', 'w')
1403 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001404 call WaitForAssert({-> assert_match('Invalid function name: TryThis', string(readfile('Xlog')))})
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001405
1406 call StopVimInTerminal(buf)
1407 call delete('Xscript')
1408 call ch_logfile('', '')
1409 call delete('Xlog')
1410endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001411
Bram Moolenaara997b452018-04-17 23:24:06 +02001412let s:caught_e937 = 0
1413
1414func Tapi_Delete(bufnum, arg)
1415 try
1416 execute 'bdelete!' a:bufnum
1417 catch /E937:/
1418 let s:caught_e937 = 1
1419 endtry
1420endfunc
1421
1422func Test_terminal_api_call_fail_delete()
1423 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001424 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaara997b452018-04-17 23:24:06 +02001425 endif
1426
1427 call WriteApiCall('Tapi_Delete')
1428 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001429 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001430
1431 call StopVimInTerminal(buf)
1432 call delete('Xscript')
1433 call ch_logfile('', '')
1434endfunc
1435
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001436func Test_terminal_ansicolors_default()
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001437 if !exists('*term_getansicolors')
1438 throw 'Skipped: term_getansicolors() not supported'
1439 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001440 let colors = [
1441 \ '#000000', '#e00000',
1442 \ '#00e000', '#e0e000',
1443 \ '#0000e0', '#e000e0',
1444 \ '#00e0e0', '#e0e0e0',
1445 \ '#808080', '#ff4040',
1446 \ '#40ff40', '#ffff40',
1447 \ '#4040ff', '#ff40ff',
1448 \ '#40ffff', '#ffffff',
1449 \]
1450
1451 let buf = Run_shell_in_terminal({})
1452 call assert_equal(colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001453 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001454 call term_wait(buf)
1455
1456 exe buf . 'bwipe'
1457endfunc
1458
1459let s:test_colors = [
1460 \ '#616e64', '#0d0a79',
1461 \ '#6d610d', '#0a7373',
1462 \ '#690d0a', '#6d696e',
1463 \ '#0d0a6f', '#616e0d',
1464 \ '#0a6479', '#6d0d0a',
1465 \ '#617373', '#0d0a69',
1466 \ '#6d690d', '#0a6e6f',
1467 \ '#610d0a', '#6e6479',
1468 \]
1469
1470func Test_terminal_ansicolors_global()
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001471 if !exists('*term_getansicolors')
1472 throw 'Skipped: term_getansicolors() not supported'
1473 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001474 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1475 let buf = Run_shell_in_terminal({})
1476 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001477 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001478 call term_wait(buf)
1479
1480 exe buf . 'bwipe'
1481 unlet g:terminal_ansi_colors
1482endfunc
1483
1484func Test_terminal_ansicolors_func()
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001485 if !exists('*term_getansicolors')
1486 throw 'Skipped: term_getansicolors() not supported'
1487 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001488 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1489 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1490 call assert_equal(s:test_colors, term_getansicolors(buf))
1491
1492 call term_setansicolors(buf, g:terminal_ansi_colors)
1493 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1494
1495 let colors = [
1496 \ 'ivory', 'AliceBlue',
1497 \ 'grey67', 'dark goldenrod',
1498 \ 'SteelBlue3', 'PaleVioletRed4',
1499 \ 'MediumPurple2', 'yellow2',
1500 \ 'RosyBrown3', 'OrangeRed2',
1501 \ 'white smoke', 'navy blue',
1502 \ 'grey47', 'gray97',
1503 \ 'MistyRose2', 'DodgerBlue4',
1504 \]
1505 call term_setansicolors(buf, colors)
1506
1507 let colors[4] = 'Invalid'
1508 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1509
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001510 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001511 call term_wait(buf)
1512 exe buf . 'bwipe'
1513endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001514
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001515func Test_terminal_all_ansi_colors()
1516 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001517 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001518 endif
1519
1520 " Use all the ANSI colors.
1521 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001522 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001523 \ 'hi Tblack ctermfg=0 ctermbg=8',
1524 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1525 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1526 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1527 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1528 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1529 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1530 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1531 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1532 \ 'hi Tred ctermfg=9 ctermbg=1',
1533 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1534 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1535 \ 'hi Tblue ctermfg=12 ctermbg=4',
1536 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1537 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1538 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001539 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1540 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1541 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001542 \ '',
1543 \ 'call matchadd("Tblack", "A")',
1544 \ 'call matchadd("Tdarkred", "B")',
1545 \ 'call matchadd("Tdarkgreen", "C")',
1546 \ 'call matchadd("Tbrown", "D")',
1547 \ 'call matchadd("Tdarkblue", "E")',
1548 \ 'call matchadd("Tdarkmagenta", "F")',
1549 \ 'call matchadd("Tdarkcyan", "G")',
1550 \ 'call matchadd("Tlightgrey", "H")',
1551 \ 'call matchadd("Tdarkgrey", "I")',
1552 \ 'call matchadd("Tred", "J")',
1553 \ 'call matchadd("Tgreen", "K")',
1554 \ 'call matchadd("Tyellow", "L")',
1555 \ 'call matchadd("Tblue", "M")',
1556 \ 'call matchadd("Tmagenta", "N")',
1557 \ 'call matchadd("Tcyan", "O")',
1558 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001559 \ 'call matchadd("TdarkredBold", "X")',
1560 \ 'call matchadd("TgreenBold", "Y")',
1561 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001562 \ 'redraw',
1563 \ ], 'Xcolorscript')
1564 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1565 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1566
1567 call term_sendkeys(buf, ":q\<CR>")
1568 call StopVimInTerminal(buf)
1569 call delete('Xcolorscript')
1570endfunc
1571
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001572func Test_terminal_termwinsize_option_fixed()
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001573 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001574 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001575 endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001576 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001577 let text = []
1578 for n in range(10)
1579 call add(text, repeat(n, 50))
1580 endfor
1581 call writefile(text, 'Xwinsize')
1582 let buf = RunVimInTerminal('Xwinsize', {})
1583 let win = bufwinid(buf)
1584 call assert_equal([6, 40], term_getsize(buf))
1585 call assert_equal(6, winheight(win))
1586 call assert_equal(40, winwidth(win))
1587
1588 " resizing the window doesn't resize the terminal.
1589 resize 10
1590 vertical resize 60
1591 call assert_equal([6, 40], term_getsize(buf))
1592 call assert_equal(10, winheight(win))
1593 call assert_equal(60, winwidth(win))
1594
1595 call StopVimInTerminal(buf)
1596 call delete('Xwinsize')
1597
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001598 call assert_fails('set termwinsize=40', 'E474')
1599 call assert_fails('set termwinsize=10+40', 'E474')
1600 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001601
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001602 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001603endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001604
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001605func Test_terminal_termwinsize_option_zero()
1606 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001607 let buf = Run_shell_in_terminal({})
1608 let win = bufwinid(buf)
1609 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001610 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001611 call term_wait(buf)
1612 exe buf . 'bwipe'
1613
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001614 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001615 let buf = Run_shell_in_terminal({})
1616 let win = bufwinid(buf)
1617 call assert_equal([7, winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001618 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001619 call term_wait(buf)
1620 exe buf . 'bwipe'
1621
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001622 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001623 let buf = Run_shell_in_terminal({})
1624 let win = bufwinid(buf)
1625 call assert_equal([winheight(win), 33], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001626 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001627 call term_wait(buf)
1628 exe buf . 'bwipe'
1629
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001630 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001631endfunc
1632
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001633func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001634 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001635 vsplit
1636 let buf = Run_shell_in_terminal({})
1637 let win = bufwinid(buf)
1638 call assert_inrange(10, 1000, winheight(win))
1639 call assert_inrange(50, 1000, winwidth(win))
1640 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1641
1642 resize 15
1643 vertical resize 60
1644 redraw
1645 call assert_equal([15, 60], term_getsize(buf))
1646 call assert_equal(15, winheight(win))
1647 call assert_equal(60, winwidth(win))
1648
1649 resize 7
1650 vertical resize 30
1651 redraw
1652 call assert_equal([10, 50], term_getsize(buf))
1653 call assert_equal(7, winheight(win))
1654 call assert_equal(30, winwidth(win))
1655
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001656 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001657 call term_wait(buf)
1658 exe buf . 'bwipe'
1659
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001660 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001661 let buf = Run_shell_in_terminal({})
1662 let win = bufwinid(buf)
1663 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001664 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001665 call term_wait(buf)
1666 exe buf . 'bwipe'
1667
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001668 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001669endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001670
1671func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001672 " make three tabpages, terminal in the middle
1673 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001674 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001675 tabnew
1676 tabprev
1677 call assert_equal(1, winnr('$'))
1678 call assert_equal(2, tabpagenr())
1679 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001680
1681 let buf = Run_shell_in_terminal({})
1682 let termwin = bufwinid(buf)
1683 set termwinkey=<C-L>
1684 call feedkeys("\<C-L>w", 'tx')
1685 call assert_equal(thiswin, win_getid())
1686 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001687 call assert_equal(termwin, win_getid())
1688
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001689 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001690 call assert_equal(3, tabpagenr())
1691 tabprev
1692 call assert_equal(2, tabpagenr())
1693 call assert_equal(termwin, win_getid())
1694
1695 call feedkeys("\<C-L>gT", "xt")
1696 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001697 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001698 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001699 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001700
1701 let job = term_getjob(buf)
1702 call feedkeys("\<C-L>\<C-C>", 'tx')
1703 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001704
1705 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001706 tabnext
1707 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001708 tabprev
1709 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001710endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001711
1712func Test_terminal_out_err()
1713 if !has('unix')
1714 return
1715 endif
1716 call writefile([
1717 \ '#!/bin/sh',
1718 \ 'echo "this is standard error" >&2',
1719 \ 'echo "this is standard out" >&1',
1720 \ ], 'Xechoerrout.sh')
1721 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1722
1723 let outfile = 'Xtermstdout'
1724 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001725
1726 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1727 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001728 call assert_equal('this is standard error', term_getline(buf, 1))
1729
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001730 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001731 exe buf . 'bwipe'
1732 call delete('Xechoerrout.sh')
1733 call delete(outfile)
1734endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001735
1736func Test_terminwinscroll()
1737 if !has('unix')
1738 return
1739 endif
1740
1741 " Let the terminal output more than 'termwinscroll' lines, some at the start
1742 " will be dropped.
1743 exe 'set termwinscroll=' . &lines
1744 let buf = term_start('/bin/sh')
1745 for i in range(1, &lines)
1746 call feedkeys("echo " . i . "\<CR>", 'xt')
1747 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1748 endfor
1749 " Go to Terminal-Normal mode to update the buffer.
1750 call feedkeys("\<C-W>N", 'xt')
1751 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1752
1753 " Every "echo nr" must only appear once
1754 let lines = getline(1, line('$'))
1755 for i in range(&lines - len(lines) / 2 + 2, &lines)
1756 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1757 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1758 endfor
1759
1760 exe buf . 'bwipe!'
1761endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001762
Bram Moolenaar875cf872018-07-08 20:49:07 +02001763" Resizing the terminal window caused an ml_get error.
1764" TODO: This does not reproduce the original problem.
1765func Test_terminal_resize()
1766 set statusline=x
1767 terminal
1768 call assert_equal(2, winnr('$'))
1769
1770 " Fill the terminal with text.
1771 if has('win32')
1772 call feedkeys("dir\<CR>", 'xt')
1773 else
1774 call feedkeys("ls\<CR>", 'xt')
1775 endif
1776 " Go to Terminal-Normal mode for a moment.
1777 call feedkeys("\<C-W>N", 'xt')
1778 " Open a new window
1779 call feedkeys("i\<C-W>n", 'xt')
1780 call assert_equal(3, winnr('$'))
1781 redraw
1782
1783 close
1784 call assert_equal(2, winnr('$'))
1785 call feedkeys("exit\<CR>", 'xt')
1786 set statusline&
1787endfunc
1788
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001789" must be nearly the last, we can't go back from GUI to terminal
1790func Test_zz1_terminal_in_gui()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001791 CheckCanRunGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001792
1793 " Ignore the "failed to create input context" error.
1794 call test_ignore_error('E285:')
1795
1796 gui -f
1797
1798 call assert_equal(1, winnr('$'))
1799 let buf = Run_shell_in_terminal({'term_finish': 'close'})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001800 call StopShellInTerminal(buf)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001801 call term_wait(buf)
1802
1803 " closing window wipes out the terminal buffer a with finished job
1804 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1805 call assert_equal("", bufname(buf))
1806
1807 unlet g:job
1808endfunc
1809
1810func Test_zz2_terminal_guioptions_bang()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001811 CheckGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001812 set guioptions+=!
1813
1814 let filename = 'Xtestscript'
1815 if has('win32')
1816 let filename .= '.bat'
1817 let prefix = ''
1818 let contents = ['@echo off', 'exit %1']
1819 else
1820 let filename .= '.sh'
1821 let prefix = './'
1822 let contents = ['#!/bin/sh', 'exit $1']
1823 endif
1824 call writefile(contents, filename)
1825 call setfperm(filename, 'rwxrwx---')
1826
1827 " Check if v:shell_error is equal to the exit status.
1828 let exitval = 0
1829 execute printf(':!%s%s %d', prefix, filename, exitval)
1830 call assert_equal(exitval, v:shell_error)
1831
1832 let exitval = 9
1833 execute printf(':!%s%s %d', prefix, filename, exitval)
1834 call assert_equal(exitval, v:shell_error)
1835
1836 set guioptions&
1837 call delete(filename)
1838endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001839
1840func Test_terminal_hidden()
1841 if !has('unix')
1842 return
1843 endif
1844 term ++hidden cat
1845 let bnr = bufnr('$')
1846 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1847 exe 'sbuf ' . bnr
1848 call assert_equal('terminal', &buftype)
1849 call term_sendkeys(bnr, "asdf\<CR>")
1850 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1851 call term_sendkeys(bnr, "\<C-D>")
1852 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
1853 bwipe!
1854endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001855
Bram Moolenaara4c2a242019-03-25 23:01:38 +01001856func Test_terminal_switch_mode()
1857 term
1858 let bnr = bufnr('$')
1859 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1860 call feedkeys("\<C-W>N", 'xt')
1861 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1862 call feedkeys("A", 'xt')
1863 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1864 call feedkeys("\<C-W>N", 'xt')
1865 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1866 call feedkeys("I", 'xt')
1867 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1868 call feedkeys("\<C-W>Nv", 'xt')
1869 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1870 call feedkeys("I", 'xt')
1871 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1872 call feedkeys("\<C-W>Nv", 'xt')
1873 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1874 call feedkeys("A", 'xt')
1875 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1876 bwipe!
1877endfunc
1878
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001879func Test_terminal_hidden_and_close()
1880 if !has('unix')
1881 return
1882 endif
1883 call assert_equal(1, winnr('$'))
1884 term ++hidden ++close ls
1885 let bnr = bufnr('$')
1886 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1887 call WaitForAssert({-> assert_false(bufexists(bnr))})
1888 call assert_equal(1, winnr('$'))
1889endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001890
1891func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001892 " This test does not pass through ConPTY.
1893 if has('conpty')
1894 return
1895 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001896 let contents = [
1897 \ [ 'One', '', 'X' ],
1898 \ [ 'Two', '', '' ],
1899 \ [ 'Three' ] + repeat([''], 30)
1900 \ ]
1901
1902 for c in contents
1903 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01001904 if has('win32')
1905 term cmd /c type Xfile
1906 else
1907 term cat Xfile
1908 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001909 let bnr = bufnr('$')
1910 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1911 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01001912 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001913 call assert_equal(c, getline(1, line('$')))
1914 quit
1915 endfor
1916
1917 call delete('Xfile')
1918endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01001919
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01001920func Test_terminal_no_job()
1921 let term = term_start('false', {'term_finish': 'close'})
1922 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
1923endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01001924
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02001925func Test_term_getcursor()
1926 if !has('unix')
1927 return
1928 endif
1929 let buf = Run_shell_in_terminal({})
1930
1931 " Wait for the shell to display a prompt.
1932 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
1933
1934 " Hide the cursor.
1935 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
1936 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
1937
1938 " Show the cursor.
1939 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
1940 call WaitForAssert({-> assert_equal(1, term_getcursor(buf)[2].visible)})
1941
1942 " Change color of cursor.
1943 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
1944 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
1945 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
1946 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
1947 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
1948
1949 " Make cursor a blinking block.
1950 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
1951 call WaitForAssert({-> assert_equal([1, 1],
1952 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1953
1954 " Make cursor a steady block.
1955 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
1956 call WaitForAssert({-> assert_equal([0, 1],
1957 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1958
1959 " Make cursor a blinking underline.
1960 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
1961 call WaitForAssert({-> assert_equal([1, 2],
1962 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1963
1964 " Make cursor a steady underline.
1965 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
1966 call WaitForAssert({-> assert_equal([0, 2],
1967 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1968
1969 " Make cursor a blinking vertical bar.
1970 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
1971 call WaitForAssert({-> assert_equal([1, 3],
1972 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1973
1974 " Make cursor a steady vertical bar.
1975 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
1976 call WaitForAssert({-> assert_equal([0, 3],
1977 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1978
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001979 call StopShellInTerminal(buf)
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02001980endfunc
1981
Bram Moolenaar1e115362019-01-09 23:01:02 +01001982func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01001983 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02001984 " and for a non-existing buffer.
Bram Moolenaar1e115362019-01-09 23:01:02 +01001985 call assert_equal('', term_gettitle(bufnr('%')))
1986 call assert_equal('', term_gettitle(bufnr('$') + 1))
1987
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02001988 if !has('title') || &title == 0 || empty(&t_ts)
1989 throw "Skipped: can't get/set title"
1990 endif
1991
Bram Moolenaar1e115362019-01-09 23:01:02 +01001992 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001993 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02001994 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
1995 call term_sendkeys(term, ":e Xfoo\r")
1996 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001997 else
1998 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02001999 call term_sendkeys(term, ":e Xfoo\r")
2000 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002001 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01002002
Bram Moolenaar1e115362019-01-09 23:01:02 +01002003 call term_sendkeys(term, ":set titlestring=foo\r")
2004 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2005
2006 exe term . 'bwipe!'
2007endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002008
2009" When drawing the statusline the cursor position may not have been updated
2010" yet.
2011" 1. create a terminal, make it show 2 lines
2012" 2. 0.5 sec later: leave terminal window, execute "i"
2013" 3. 0.5 sec later: clear terminal window, now it's 1 line
2014" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2015" 4. 0.5 sec later: should be done, clean up
2016func Test_terminal_statusline()
2017 if !has('unix')
2018 return
2019 endif
2020 set statusline=x
2021 terminal
2022 let tbuf = bufnr('')
2023 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2024 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2025 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2026 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2027
2028 sleep 2
2029 exe tbuf . 'bwipe!'
2030 au! BufLeave
2031 set statusline=
2032endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002033
2034func Test_terminal_getwinpos()
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002035 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02002036 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002037 endif
2038
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002039 " split, go to the bottom-right window
2040 split
2041 wincmd j
2042 set splitright
2043
2044 call writefile([
2045 \ 'echo getwinpos()',
2046 \ ], 'XTest_getwinpos')
2047 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
2048 call term_wait(buf)
2049
2050 " Find the output of getwinpos() in the bottom line.
2051 let rows = term_getsize(buf)[0]
2052 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2053 let line = term_getline(buf, rows)
2054 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2055 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2056
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002057 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002058 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002059 " In the GUI it can be more, let's assume a 20 x 14 cell.
2060 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002061 let [xroot, yroot] = getwinpos()
Bram Moolenaar1b557972019-04-09 21:17:32 +02002062 let [winrow, wincol] = win_screenpos('.')
2063 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2064 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2065 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2066 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002067
2068 call term_wait(buf)
2069 call term_sendkeys(buf, ":q\<CR>")
2070 call StopVimInTerminal(buf)
2071 call delete('XTest_getwinpos')
2072 exe buf . 'bwipe!'
2073 set splitright&
2074 only!
2075endfunc