blob: 09c050aada259d1b6f0253de249158f339b56e48 [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
Bram Moolenaarea5d6fa2017-08-18 21:07:11 +02003if !has('terminal')
Bram Moolenaarb0f94c12019-06-13 22:19:53 +02004 throw 'Skipped, terminal feature missing'
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02005endif
6
7source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01008source screendump.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02009
Bram Moolenaarb81bc772017-08-11 22:45:01 +020010let s:python = PythonProg()
Bram Moolenaarddd33082019-06-03 23:07:25 +020011let $PROMPT_COMMAND=''
Bram Moolenaarb81bc772017-08-11 22:45:01 +020012
Bram Moolenaar94053a52017-08-01 21:44:33 +020013" Open a terminal with a shell, assign the job to g:job and return the buffer
14" number.
Bram Moolenaar05aafed2017-08-11 19:12:11 +020015func Run_shell_in_terminal(options)
Bram Moolenaarba6febd2017-10-30 21:56:23 +010016 if has('win32')
17 let buf = term_start([&shell,'/k'], a:options)
18 else
19 let buf = term_start(&shell, a:options)
20 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020021
22 let termlist = term_list()
23 call assert_equal(1, len(termlist))
24 call assert_equal(buf, termlist[0])
25
26 let g:job = term_getjob(buf)
27 call assert_equal(v:t_job, type(g:job))
28
Bram Moolenaar35422f42017-08-05 16:33:56 +020029 let string = string({'job': term_getjob(buf)})
30 call assert_match("{'job': 'process \\d\\+ run'}", string)
31
Bram Moolenaar94053a52017-08-01 21:44:33 +020032 return buf
33endfunc
34
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020035func Test_terminal_basic()
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020036 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020037 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020038
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020039 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020040 call assert_match('^/dev/', job_info(g:job).tty_out)
41 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020042 else
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010043 " ConPTY works on anonymous pipe.
44 if !has('conpty')
45 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
46 call assert_match('^\\\\.\\pipe\\', term_gettty(''))
47 endif
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020048 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020049 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020050 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020051 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020052 call assert_match('%aR[^\n]*running]', execute('ls R'))
53 call assert_notmatch('%[^\n]*running]', execute('ls F'))
54 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020055
Bram Moolenaar94053a52017-08-01 21:44:33 +020056 call Stop_shell_in_terminal(buf)
57 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020058 call assert_equal('n', mode())
59 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020060 call assert_match('%aF[^\n]*finished]', execute('ls F'))
61 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
62 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020063
Bram Moolenaar94053a52017-08-01 21:44:33 +020064 " closing window wipes out the terminal buffer a with finished job
65 close
66 call assert_equal("", bufname(buf))
67
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020068 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020069 unlet g:job
70endfunc
71
72func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020073 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020074 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020075 call term_wait(buf)
76
77 setlocal modifiable
78 exe "normal Axxx\<Esc>"
79 call assert_fails(buf . 'bwipe', 'E517')
80 undo
81
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020082 exe buf . 'bwipe'
83 unlet g:job
84endfunc
85
Bram Moolenaar5b868a82019-02-25 06:11:53 +010086func Test_terminal_paste_register()
87 let @" = "text to paste"
88
89 let buf = Run_shell_in_terminal({})
90 " Wait for the shell to display a prompt
91 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
92
93 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
94 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
95 call WaitForAssert({-> assert_equal('text to paste 42', getline(2))})
96
97 exe buf . 'bwipe!'
98 unlet g:job
99endfunc
100
Bram Moolenaar94053a52017-08-01 21:44:33 +0200101func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200102 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +0200103 call assert_fails(buf . 'bwipe', 'E517')
104 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200105 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200106 call assert_equal("", bufname(buf))
107
108 unlet g:job
109endfunc
110
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200111func Test_terminal_split_quit()
112 let buf = Run_shell_in_terminal({})
113 call term_wait(buf)
114 split
115 quit!
116 call term_wait(buf)
117 sleep 50m
118 call assert_equal('run', job_status(g:job))
119
120 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200121 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200122
123 exe buf . 'bwipe'
124 unlet g:job
125endfunc
126
Bram Moolenaar94053a52017-08-01 21:44:33 +0200127func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200128 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200129 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200130 quit
131 for nr in range(1, winnr('$'))
132 call assert_notequal(winbufnr(nr), buf)
133 endfor
134 call assert_true(bufloaded(buf))
135 call assert_true(buflisted(buf))
136
137 exe 'split ' . buf . 'buf'
138 call Stop_shell_in_terminal(buf)
139 exe buf . 'bwipe'
140
141 unlet g:job
142endfunc
143
Bram Moolenaar1e115362019-01-09 23:01:02 +0100144func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200145 exe g:buf . 'bwipe!'
146 let g:buf = 0
147endfunc
148
Bram Moolenaar9d189612017-09-09 18:11:00 +0200149func Get_cat_123_cmd()
150 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100151 if !has('conpty')
152 return 'cmd /c "cls && color 2 && echo 123"'
153 else
154 " When clearing twice, extra sequence is not output.
155 return 'cmd /c "cls && cls && color 2 && echo 123"'
156 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200157 else
158 call writefile(["\<Esc>[32m123"], 'Xtext')
159 return "cat Xtext"
160 endif
161endfunc
162
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200163func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200164 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200165 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
166 let g:job = term_getjob(g:buf)
167
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200168 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
169 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200170 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100171 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200172 call delete('Xtext')
173endfunc
174
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200175func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200176 let l = term_scrape(a:buf, 0)
177 call assert_true(len(l) == 0)
178 let l = term_scrape(a:buf, 999)
179 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200180 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200181 call assert_true(len(l) > 0)
182 call assert_equal('1', l[0].chars)
183 call assert_equal('2', l[1].chars)
184 call assert_equal('3', l[2].chars)
185 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar81df6352018-12-22 18:25:30 +0100186 if has('win32')
187 " On Windows 'background' always defaults to dark, even though the terminal
188 " may use a light background. Therefore accept both white and black.
189 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200190 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100191 if &background == 'light'
192 call assert_equal('#ffffff', l[0].bg)
193 else
194 call assert_equal('#000000', l[0].bg)
195 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200196 endif
197
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200198 let l = term_getline(a:buf, -1)
199 call assert_equal('', l)
200 let l = term_getline(a:buf, 0)
201 call assert_equal('', l)
202 let l = term_getline(a:buf, 999)
203 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200204 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200205 call assert_equal('123', l)
206endfunc
207
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200208func Test_terminal_scrape_123()
209 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200210 let buf = term_start(cmd)
211
212 let termlist = term_list()
213 call assert_equal(1, len(termlist))
214 call assert_equal(buf, termlist[0])
215
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200216 " Nothing happens with invalid buffer number
217 call term_wait(1234)
218
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200219 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200220 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200221 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200222 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200223 call Check_123(buf)
224
225 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100226 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200227 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200228 call term_wait(buf)
229 call Check_123(buf)
230
231 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200232 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200233endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200234
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200235func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200236 call writefile(["léttまrs"], 'Xtext')
237 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200238 " Run cmd with UTF-8 codepage to make the type command print the expected
239 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100240 let buf = term_start("cmd /K chcp 65001")
241 call term_sendkeys(buf, "type Xtext\<CR>")
242 call term_sendkeys(buf, "exit\<CR>")
243 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200244 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100245 let buf = term_start("cat Xtext")
246 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200247 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200248
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100249 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
250 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200251 call assert_true(len(l) >= 7)
252 call assert_equal('l', l[0].chars)
253 call assert_equal('é', l[1].chars)
254 call assert_equal(1, l[1].width)
255 call assert_equal('t', l[2].chars)
256 call assert_equal('t', l[3].chars)
257 call assert_equal('ま', l[4].chars)
258 call assert_equal(2, l[4].width)
259 call assert_equal('r', l[5].chars)
260 call assert_equal('s', l[6].chars)
261
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100262 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200263 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100264 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200265
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100266 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200267 call delete('Xtext')
268endfunc
269
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200270func Test_terminal_scroll()
271 call writefile(range(1, 200), 'Xtext')
272 if has('win32')
273 let cmd = 'cmd /c "type Xtext"'
274 else
275 let cmd = "cat Xtext"
276 endif
277 let buf = term_start(cmd)
278
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100279 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200280 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200281 call term_wait(buf)
282 if has('win32')
283 " TODO: this should not be needed
284 sleep 100m
285 endif
286
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200287 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200288 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200289 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200290 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200291 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200292 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200293 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200294
295 exe buf . 'bwipe'
296 call delete('Xtext')
297endfunc
298
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200299func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200300 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200301 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200302 call writefile(range(150), 'Xtext')
303 if has('win32')
304 call term_sendkeys(buf, "type Xtext\<CR>")
305 else
306 call term_sendkeys(buf, "cat Xtext\<CR>")
307 endif
308 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200309 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200310 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200311 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200312 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200313
314 call Stop_shell_in_terminal(buf)
315 call term_wait(buf)
316 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200317 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100318 call delete('Xtext')
319endfunc
320
321func Test_terminal_postponed_scrollback()
322 if !has('unix')
323 " tail -f only works on Unix
324 return
325 endif
326
327 call writefile(range(50), 'Xtext')
328 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100329 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100330 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100331 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100332 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
333 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100334 \ 'call feedkeys("\<C-W>N", "xt")',
335 \ ], 'XTest_postponed')
336 let buf = RunVimInTerminal('-S XTest_postponed', {})
337 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaar96baf022019-02-14 23:49:38 +0100338 call VerifyScreenDump(buf, 'Test_terminal_01', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100339
340 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200341 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100342 call term_sendkeys(buf, "k")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100343 call VerifyScreenDump(buf, 'Test_terminal_02', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100344
345 " Back to Terminal-Job mode, text will scroll and show the extra line.
346 call term_sendkeys(buf, "a")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100347 call VerifyScreenDump(buf, 'Test_terminal_03', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100348
349 call term_wait(buf)
350 call term_sendkeys(buf, "\<C-C>")
351 call term_wait(buf)
352 call term_sendkeys(buf, "exit\<CR>")
353 call term_wait(buf)
354 call term_sendkeys(buf, ":q\<CR>")
355 call StopVimInTerminal(buf)
356 call delete('XTest_postponed')
357 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200358endfunc
359
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100360" Run diff on two dumps with different size.
361func Test_terminal_dumpdiff_size()
362 call assert_equal(1, winnr('$'))
363 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
364 call assert_equal(2, winnr('$'))
365 call assert_match('Test_incsearch_search_01.dump', getline(10))
366 call assert_match(' +++++$', getline(11))
367 call assert_match('Test_popup_command_01.dump', getline(31))
368 call assert_equal(repeat('+', 75), getline(30))
369 quit
370endfunc
371
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200372func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200373 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200374
Bram Moolenaarb2412082017-08-20 18:09:14 +0200375 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200376 let size = term_getsize('')
377 bwipe!
378 call assert_equal(5, size[0])
379
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200380 call term_start(cmd, {'term_rows': 6})
381 let size = term_getsize('')
382 bwipe!
383 call assert_equal(6, size[0])
384
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200385 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200386 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaara42d3632018-04-14 17:05:38 +0200387 call assert_equal([5, 33], term_getsize(''))
388
389 call term_setsize('', 6, 0)
390 call assert_equal([6, 33], term_getsize(''))
391
392 call term_setsize('', 0, 35)
393 call assert_equal([6, 35], term_getsize(''))
394
395 call term_setsize('', 7, 30)
396 call assert_equal([7, 30], term_getsize(''))
397
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200398 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200399 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200400
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200401 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
402 let size = term_getsize('')
403 bwipe!
404 call assert_equal([6, 36], size)
405
Bram Moolenaarb2412082017-08-20 18:09:14 +0200406 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200407 let size = term_getsize('')
408 bwipe!
409 call assert_equal(20, size[1])
410
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200411 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
412 let size = term_getsize('')
413 bwipe!
414 call assert_equal(26, size[1])
415
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200416 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200417 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200418 let size = term_getsize('')
419 bwipe!
420 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200421
422 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
423 let size = term_getsize('')
424 bwipe!
425 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200426
427 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200428endfunc
429
430func Test_terminal_curwin()
431 let cmd = Get_cat_123_cmd()
432 call assert_equal(1, winnr('$'))
433
434 split dummy
435 exe 'terminal ++curwin ' . cmd
436 call assert_equal(2, winnr('$'))
437 bwipe!
438
439 split dummy
440 call term_start(cmd, {'curwin': 1})
441 call assert_equal(2, winnr('$'))
442 bwipe!
443
444 split dummy
445 call setline(1, 'change')
446 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
447 call assert_equal(2, winnr('$'))
448 exe 'terminal! ++curwin ' . cmd
449 call assert_equal(2, winnr('$'))
450 bwipe!
451
452 split dummy
453 call setline(1, 'change')
454 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
455 call assert_equal(2, winnr('$'))
456 bwipe!
457
458 split dummy
459 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200460 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200461endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200462
Bram Moolenaarff546792017-11-21 14:47:57 +0100463func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200464 if s:python != ''
465 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200466 " 500 was not enough for Travis
467 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200468 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200469 echo 'This will take five seconds...'
470 let waittime = 2000
471 if has('win32')
472 let cmd = $windir . '\system32\timeout.exe 1'
473 else
474 let cmd = 'sleep 1'
475 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200476 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100477 return [cmd, waittime]
478endfunc
479
480func Test_terminal_finish_open_close()
481 call assert_equal(1, winnr('$'))
482
483 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200484
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100485 " shell terminal closes automatically
486 terminal
487 let buf = bufnr('%')
488 call assert_equal(2, winnr('$'))
489 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200490 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100491 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200492 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100493
494 " shell terminal that does not close automatically
495 terminal ++noclose
496 let buf = bufnr('%')
497 call assert_equal(2, winnr('$'))
498 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200499 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100500 call Stop_shell_in_terminal(buf)
501 call assert_equal(2, winnr('$'))
502 quit
503 call assert_equal(1, winnr('$'))
504
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200505 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200506 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200507 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200508 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200509
510 call term_start(cmd, {'term_finish': 'close'})
511 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200512 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200513 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200514 call assert_equal(1, winnr('$'))
515
516 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200517 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200518 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200519 bwipe
520
521 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200522 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200523 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200524 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200525
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200526 exe 'terminal ++hidden ++open ' . cmd
527 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200528 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200529 bwipe
530
531 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
532 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200533 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200534 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200535
536 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
537 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
538 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
539 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
540
541 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200542 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200543 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200544 call assert_equal(4, winheight(0))
545 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200546endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200547
548func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200549 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200550 return
551 endif
552 call mkdir('Xdir')
553 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200554 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200555
556 exe buf . 'bwipe'
557 call delete('Xdir', 'rf')
558endfunc
559
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200560func Test_terminal_cwd_failure()
561 " Case 1: Provided directory is not actually a directory. Attempt to make
562 " the file executable as well.
563 call writefile([], 'Xfile')
564 call setfperm('Xfile', 'rwx------')
565 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
566 call delete('Xfile')
567
568 " Case 2: Directory does not exist.
569 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
570
571 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100572 " Skip this for root, it will be accessible anyway.
573 if $USER != 'root'
574 call mkdir('XdirNoAccess', '', '0600')
575 " return early if the directory permissions could not be set properly
576 if getfperm('XdirNoAccess')[2] == 'x'
577 call delete('XdirNoAccess', 'rf')
578 return
579 endif
580 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
581 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200582 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200583endfunc
584
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100585func Test_terminal_servername()
586 if !has('clientserver')
587 return
588 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200589 call s:test_environment("VIM_SERVERNAME", v:servername)
590endfunc
591
592func Test_terminal_version()
593 call s:test_environment("VIM_TERMINAL", string(v:version))
594endfunc
595
596func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100597 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100598 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200599 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100600 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200601 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100602 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200603 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100604 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100605 call term_wait(buf)
606 call Stop_shell_in_terminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200607 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100608
Bram Moolenaar012eb662018-03-13 17:55:27 +0100609 exe buf . 'bwipe'
610 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100611endfunc
612
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200613func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100614 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200615 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200616 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100617 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100618 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100619 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100620 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100621 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100622 call term_wait(buf)
623 call Stop_shell_in_terminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200624 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200625
Bram Moolenaar012eb662018-03-13 17:55:27 +0100626 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200627endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200628
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200629func Test_terminal_list_args()
630 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
631 call assert_fails(buf . 'bwipe', 'E517')
632 exe buf . 'bwipe!'
633 call assert_equal("", bufname(buf))
634endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200635
636func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100637 let buf = term_start(&shell)
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100638 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200639 " The shell or something else has a problem dealing with more than 1000
640 " characters at the same time.
641 let len = 1000
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100642 " NPFS is used in Windows, nonblocking mode does not work properly.
643 elseif has('win32')
644 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200645 else
646 let len = 5000
647 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200648
649 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100650 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200651 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100652 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200653
654 " On MS-Windows there is an extra empty line below "done". Find "done" in
655 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100656 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100657 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100658 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200659 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100660 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200661 endif
662 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200663
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100664 let g:job = term_getjob(buf)
665 call Stop_shell_in_terminal(buf)
666 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200667 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200668 bwipe
669endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200670
671func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200672 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200673 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200674 endif
Bram Moolenaar1580f752018-06-03 15:26:36 +0200675 if has('win32')
676 " TODO: enable once writing to stdin works on MS-Windows
677 return
678 endif
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200679 new
680 call setline(1, ['one', 'two', 'three'])
681 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200682 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200683 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200684 call assert_equal(['3', '3', '14'], nrs)
685 bwipe
686
Bram Moolenaardada6d22017-09-02 17:18:35 +0200687 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200688 call setline(1, ['one', 'two', 'three', 'four'])
689 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200690 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200691 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200692 call assert_equal(['2', '2', '10'], nrs)
693 bwipe
694
Bram Moolenaardada6d22017-09-02 17:18:35 +0200695 if executable('python')
696 new
697 call setline(1, ['print("hello")'])
698 1term ++eof=exit() python
699 " MS-Windows echoes the input, Unix doesn't.
700 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
701 if getline(1) =~ 'hello'
702 call assert_equal('hello', getline(1))
703 else
704 call assert_equal('hello', getline(line('$') - 1))
705 endif
706 bwipe
707
708 if has('win32')
709 new
710 call setline(1, ['print("hello")'])
711 1term ++eof=<C-Z> python
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200712 call WaitForAssert({-> assert_match('Z', getline("$"))})
Bram Moolenaardada6d22017-09-02 17:18:35 +0200713 call assert_equal('hello', getline(line('$') - 1))
714 bwipe
715 endif
716 endif
717
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200718 bwipe!
719endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200720
721func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200722 let buf = term_start('NONE', {})
723 call assert_notequal(0, buf)
724
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200725 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200726 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100727 if has('gui_running') && !has('win32')
728 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200729 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100730 else
731 " Otherwise using a job works on all systems.
732 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200733 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200734 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200735
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200736 bwipe!
737endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200738
739func Test_terminal_special_chars()
740 " this file name only works on Unix
741 if !has('unix')
742 return
743 endif
744 call mkdir('Xdir with spaces')
745 call writefile(['x'], 'Xdir with spaces/quoted"file')
746 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200747 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200748 call term_wait('')
749
750 call delete('Xdir with spaces', 'rf')
751 bwipe
752endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200753
754func Test_terminal_wrong_options()
755 call assert_fails('call term_start(&shell, {
756 \ "in_io": "file",
757 \ "in_name": "xxx",
758 \ "out_io": "file",
759 \ "out_name": "xxx",
760 \ "err_io": "file",
761 \ "err_name": "xxx"
762 \ })', 'E474:')
763 call assert_fails('call term_start(&shell, {
764 \ "out_buf": bufnr("%")
765 \ })', 'E474:')
766 call assert_fails('call term_start(&shell, {
767 \ "err_buf": bufnr("%")
768 \ })', 'E474:')
769endfunc
770
771func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200772 let cmd = Get_cat_123_cmd()
773 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
774 call term_wait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100775 " ConPTY may precede escape sequence. There are things that are not so.
776 if !has('conpty')
777 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
778 call assert_match('123', readfile('Xfile')[0])
779 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200780 let g:job = term_getjob(buf)
781 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
782 call delete('Xfile')
783 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200784
785 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200786 call writefile(['one line'], 'Xfile')
787 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
788 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200789 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200790 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200791 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200792 bwipe
793 call delete('Xfile')
794 endif
795endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200796
797func TerminalTmap(remap)
798 let buf = Run_shell_in_terminal({})
799 call assert_equal('t', mode())
800
801 if a:remap
802 tmap 123 456
803 else
804 tnoremap 123 456
805 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100806 " don't use abcde, it's an existing command
807 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200808 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100809 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200810 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200811 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200812 let lnum = term_getcursor(buf)[0]
813 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100814 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200815 else
816 call assert_match('456', term_getline(buf, lnum))
817 endif
818
819 call term_sendkeys(buf, "\r")
820 call Stop_shell_in_terminal(buf)
821 call term_wait(buf)
822
823 tunmap 123
824 tunmap 456
825 call assert_equal('', maparg('123', 't'))
826 close
827 unlet g:job
828endfunc
829
830func Test_terminal_tmap()
831 call TerminalTmap(1)
832 call TerminalTmap(0)
833endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200834
835func Test_terminal_wall()
836 let buf = Run_shell_in_terminal({})
837 wall
838 call Stop_shell_in_terminal(buf)
839 call term_wait(buf)
840 exe buf . 'bwipe'
841 unlet g:job
842endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200843
Bram Moolenaar7a760922018-02-19 23:10:02 +0100844func Test_terminal_wqall()
845 let buf = Run_shell_in_terminal({})
846 call assert_fails('wqall', 'E948')
847 call Stop_shell_in_terminal(buf)
848 call term_wait(buf)
849 exe buf . 'bwipe'
850 unlet g:job
851endfunc
852
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200853func Test_terminal_composing_unicode()
854 let save_enc = &encoding
855 set encoding=utf-8
856
857 if has('win32')
858 let cmd = "cmd /K chcp 65001"
859 let lnum = [3, 6, 9]
860 else
861 let cmd = &shell
862 let lnum = [1, 3, 5]
863 endif
864
865 enew
866 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100867 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200868 call term_wait(buf, 50)
869
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200870 if has('win32')
871 call assert_equal('cmd', job_info(g:job).cmd[0])
872 else
873 call assert_equal(&shell, job_info(g:job).cmd[0])
874 endif
875
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200876 " ascii + composing
877 let txt = "a\u0308bc"
878 call term_sendkeys(buf, "echo " . txt . "\r")
879 call term_wait(buf, 50)
880 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
881 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
882 let l = term_scrape(buf, lnum[0] + 1)
883 call assert_equal("a\u0308", l[0].chars)
884 call assert_equal("b", l[1].chars)
885 call assert_equal("c", l[2].chars)
886
887 " multibyte + composing
888 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
889 call term_sendkeys(buf, "echo " . txt . "\r")
890 call term_wait(buf, 50)
891 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
892 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
893 let l = term_scrape(buf, lnum[1] + 1)
894 call assert_equal("\u304b\u3099", l[0].chars)
895 call assert_equal("\u304e", l[1].chars)
896 call assert_equal("\u304f\u3099", l[2].chars)
897 call assert_equal("\u3052", l[3].chars)
898 call assert_equal("\u3053\u3099", l[4].chars)
899
900 " \u00a0 + composing
901 let txt = "abc\u00a0\u0308"
902 call term_sendkeys(buf, "echo " . txt . "\r")
903 call term_wait(buf, 50)
904 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
905 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
906 let l = term_scrape(buf, lnum[2] + 1)
907 call assert_equal("\u00a0\u0308", l[3].chars)
908
909 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200910 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200911 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100912 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200913 let &encoding = save_enc
914endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100915
916func Test_terminal_aucmd_on_close()
917 fun Nop()
918 let s:called = 1
919 endfun
920
921 aug repro
922 au!
923 au BufWinLeave * call Nop()
924 aug END
925
926 let [cmd, waittime] = s:get_sleep_cmd()
927
928 call assert_equal(1, winnr('$'))
929 new
930 call setline(1, ['one', 'two'])
931 exe 'term ++close ' . cmd
932 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200933 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100934 call assert_equal(1, s:called)
935 bwipe!
936
937 unlet s:called
938 au! repro
939 delfunc Nop
940endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100941
942func Test_terminal_term_start_empty_command()
943 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
944 call assert_fails(cmd, 'E474')
945 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
946 call assert_fails(cmd, 'E474')
947 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
948 call assert_fails(cmd, 'E474')
949 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
950 call assert_fails(cmd, 'E474')
951endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100952
953func Test_terminal_response_to_control_sequence()
954 if !has('unix')
955 return
956 endif
957
958 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200959 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100960
Bram Moolenaar086eb872018-03-25 21:24:12 +0200961 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200962 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100963
Bram Moolenaar086eb872018-03-25 21:24:12 +0200964 " Request the cursor position.
965 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100966
967 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200968 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100969
Bram Moolenaar086eb872018-03-25 21:24:12 +0200970 " End "cat" gently.
971 call term_sendkeys(buf, "\<CR>\<C-D>")
972
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100973 call Stop_shell_in_terminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100974 exe buf . 'bwipe'
975 unlet g:job
976endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100977
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100978" Run Vim, start a terminal in that Vim with the kill argument,
979" :qall works.
980func Run_terminal_qall_kill(line1, line2)
981 " 1. Open a terminal window and wait for the prompt to appear
982 " 2. set kill using term_setkill()
983 " 3. make Vim exit, it will kill the shell
984 let after = [
985 \ a:line1,
986 \ 'let buf = bufnr("%")',
987 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
988 \ ' sleep 10m',
989 \ 'endwhile',
990 \ a:line2,
991 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
992 \ 'qall',
993 \ ]
994 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100995 return
996 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100997 call assert_equal("done", readfile("Xdone")[0])
998 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100999endfunc
1000
1001" Run Vim in a terminal, then start a terminal in that Vim with a kill
1002" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001003func Test_terminal_qall_kill_arg()
1004 call Run_terminal_qall_kill('term ++kill=kill', '')
1005endfunc
1006
1007" Run Vim, start a terminal in that Vim, set the kill argument with
1008" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001009func Test_terminal_qall_kill_func()
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001010 call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")')
1011endfunc
1012
1013" Run Vim, start a terminal in that Vim without the kill argument,
1014" check that :qall does not exit, :qall! does.
1015func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001016 let after =<< trim [CODE]
1017 term
1018 let buf = bufnr("%")
1019 while term_getline(buf, 1) =~ "^\\s*$"
1020 sleep 10m
1021 endwhile
1022 set nomore
1023 au VimLeavePre * call writefile(["too early"], "Xdone")
1024 qall
1025 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1026 cquit
1027 [CODE]
1028
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001029 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001030 return
1031 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001032 call assert_equal("done", readfile("Xdone")[0])
1033 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001034endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001035
1036" Run Vim in a terminal, then start a terminal in that Vim without a kill
1037" argument, check that :confirm qall works.
1038func Test_terminal_qall_prompt()
1039 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001040 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001041 endif
1042 let buf = RunVimInTerminal('', {})
1043
1044 " Open a terminal window and wait for the prompt to appear
1045 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001046 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1047 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001048
1049 " make Vim exit, it will prompt to kill the shell
1050 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001051 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001052 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001053 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001054
1055 " close the terminal window where Vim was running
1056 quit
1057endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001058
Bram Moolenaar012eb662018-03-13 17:55:27 +01001059func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001060 augroup repro
1061 au!
1062 au TerminalOpen * let s:called += 1
1063 augroup END
1064
1065 let s:called = 0
1066
1067 " Open a terminal window with :terminal
1068 terminal
1069 call assert_equal(1, s:called)
1070 bwipe!
1071
1072 " Open a terminal window with term_start()
1073 call term_start(&shell)
1074 call assert_equal(2, s:called)
1075 bwipe!
1076
1077 " Open a hidden terminal buffer with :terminal
1078 terminal ++hidden
1079 call assert_equal(3, s:called)
1080 for buf in term_list()
1081 exe buf . "bwipe!"
1082 endfor
1083
1084 " Open a hidden terminal buffer with term_start()
1085 let buf = term_start(&shell, {'hidden': 1})
1086 call assert_equal(4, s:called)
1087 exe buf . "bwipe!"
1088
1089 unlet s:called
1090 au! repro
1091endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001092
1093func Check_dump01(off)
1094 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1095 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001096 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001097endfunc
1098
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001099func Test_terminal_dumpwrite_composing()
1100 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001101 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001102 endif
1103 let save_enc = &encoding
1104 set encoding=utf-8
1105 call assert_equal(1, winnr('$'))
1106
1107 let text = " a\u0300 e\u0302 o\u0308"
1108 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001109 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001110 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001111 call term_dumpwrite(buf, 'Xdump')
1112 let dumpline = readfile('Xdump')[0]
1113 call assert_match('|à| |ê| |ö', dumpline)
1114
1115 call StopVimInTerminal(buf)
1116 call delete('Xcomposing')
1117 call delete('Xdump')
1118 let &encoding = save_enc
1119endfunc
1120
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001121" just testing basic functionality.
1122func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001123 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001124 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001125 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001126 call assert_equal(2, winnr('$'))
1127 call assert_equal(20, line('$'))
1128 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001129
1130 " Load another dump in the same window
1131 let buf2 = term_dumpload('dumps/Test_diff_01.dump', {'bufnr': buf})
1132 call assert_equal(buf, buf2)
1133 call assert_notequal('one two three four five', trim(getline(1)))
1134
1135 " Load the first dump again in the same window
1136 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1137 call assert_equal(buf, buf2)
1138 call Check_dump01(0)
1139
1140 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1141 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1142 new
1143 let closedbuf = winbufnr('')
1144 quit
1145 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
1146
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001147 quit
1148endfunc
1149
1150func Test_terminal_dumpdiff()
1151 call assert_equal(1, winnr('$'))
1152 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump')
1153 call assert_equal(2, winnr('$'))
1154 call assert_equal(62, line('$'))
1155 call Check_dump01(0)
1156 call Check_dump01(42)
1157 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1158 quit
1159endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001160
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001161func Test_terminal_dumpdiff_swap()
1162 call assert_equal(1, winnr('$'))
1163 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1164 call assert_equal(2, winnr('$'))
1165 call assert_equal(62, line('$'))
1166 call assert_match('Test_popup_command_01.dump', getline(21))
1167 call assert_match('Test_popup_command_03.dump', getline(42))
1168 call assert_match('Undo', getline(3))
1169 call assert_match('three four five', getline(45))
1170
1171 normal s
1172 call assert_match('Test_popup_command_03.dump', getline(21))
1173 call assert_match('Test_popup_command_01.dump', getline(42))
1174 call assert_match('three four five', getline(3))
1175 call assert_match('Undo', getline(45))
1176 quit
1177endfunc
1178
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001179func Test_terminal_dumpdiff_options()
1180 set laststatus=0
1181 call assert_equal(1, winnr('$'))
1182 let height = winheight(0)
1183 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1184 call assert_equal(2, winnr('$'))
1185 call assert_equal(height, winheight(winnr()))
1186 call assert_equal(33, winwidth(winnr()))
1187 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1188 quit
1189
1190 call assert_equal(1, winnr('$'))
1191 let width = winwidth(0)
1192 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1193 call assert_equal(2, winnr('$'))
1194 call assert_equal(width, winwidth(winnr()))
1195 call assert_equal(13, winheight(winnr()))
1196 call assert_equal('something else', bufname('%'))
1197 quit
1198
1199 call assert_equal(1, winnr('$'))
1200 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1201 call assert_equal(1, winnr('$'))
1202 bwipe
1203
1204 set laststatus&
1205endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001206
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001207func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001208 call assert_equal(1, winnr('$'))
1209
1210 " Use the title termcap entries to output the escape sequence.
1211 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001212 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001213 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001214 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001215 \ 'redraw',
1216 \ "set t_ts=",
1217 \ ], 'Xscript')
1218 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001219 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001220 call assert_equal('Xtextfile', expand('%:t'))
1221 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001222 return buf
1223endfunc
1224
1225func Test_terminal_api_drop_newwin()
1226 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001227 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001228 endif
1229 let buf = Api_drop_common('')
1230 call assert_equal(0, &bin)
1231 call assert_equal('', &fenc)
1232
1233 call StopVimInTerminal(buf)
1234 call delete('Xscript')
1235 bwipe Xtextfile
1236endfunc
1237
1238func Test_terminal_api_drop_newwin_bin()
1239 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001240 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001241 endif
1242 let buf = Api_drop_common(',{"bin":1}')
1243 call assert_equal(1, &bin)
1244
1245 call StopVimInTerminal(buf)
1246 call delete('Xscript')
1247 bwipe Xtextfile
1248endfunc
1249
1250func Test_terminal_api_drop_newwin_binary()
1251 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001252 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001253 endif
1254 let buf = Api_drop_common(',{"binary":1}')
1255 call assert_equal(1, &bin)
1256
1257 call StopVimInTerminal(buf)
1258 call delete('Xscript')
1259 bwipe Xtextfile
1260endfunc
1261
1262func Test_terminal_api_drop_newwin_nobin()
1263 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001264 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001265 endif
1266 set binary
1267 let buf = Api_drop_common(',{"nobin":1}')
1268 call assert_equal(0, &bin)
1269
1270 call StopVimInTerminal(buf)
1271 call delete('Xscript')
1272 bwipe Xtextfile
1273 set nobinary
1274endfunc
1275
1276func Test_terminal_api_drop_newwin_nobinary()
1277 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001278 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001279 endif
1280 set binary
1281 let buf = Api_drop_common(',{"nobinary":1}')
1282 call assert_equal(0, &bin)
1283
1284 call StopVimInTerminal(buf)
1285 call delete('Xscript')
1286 bwipe Xtextfile
1287 set nobinary
1288endfunc
1289
1290func Test_terminal_api_drop_newwin_ff()
1291 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001292 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001293 endif
1294 let buf = Api_drop_common(',{"ff":"dos"}')
1295 call assert_equal("dos", &ff)
1296
1297 call StopVimInTerminal(buf)
1298 call delete('Xscript')
1299 bwipe Xtextfile
1300endfunc
1301
1302func Test_terminal_api_drop_newwin_fileformat()
1303 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001304 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001305 endif
1306 let buf = Api_drop_common(',{"fileformat":"dos"}')
1307 call assert_equal("dos", &ff)
1308
1309 call StopVimInTerminal(buf)
1310 call delete('Xscript')
1311 bwipe Xtextfile
1312endfunc
1313
1314func Test_terminal_api_drop_newwin_enc()
1315 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001316 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001317 endif
1318 let buf = Api_drop_common(',{"enc":"utf-16"}')
1319 call assert_equal("utf-16", &fenc)
1320
1321 call StopVimInTerminal(buf)
1322 call delete('Xscript')
1323 bwipe Xtextfile
1324endfunc
1325
1326func Test_terminal_api_drop_newwin_encoding()
1327 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001328 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001329 endif
1330 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1331 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001332
1333 call StopVimInTerminal(buf)
1334 call delete('Xscript')
1335 bwipe Xtextfile
1336endfunc
1337
1338func Test_terminal_api_drop_oldwin()
1339 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001340 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001341 endif
1342 let firstwinid = win_getid()
1343 split Xtextfile
1344 let textfile_winid = win_getid()
1345 call assert_equal(2, winnr('$'))
1346 call win_gotoid(firstwinid)
1347
1348 " Use the title termcap entries to output the escape sequence.
1349 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001350 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001351 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1352 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1353 \ 'redraw',
1354 \ "set t_ts=",
1355 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001356 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001357 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001358 call assert_equal(textfile_winid, win_getid())
1359
1360 call StopVimInTerminal(buf)
1361 call delete('Xscript')
1362 bwipe Xtextfile
1363endfunc
1364
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001365func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001366 let g:called_bufnum = a:bufnum
1367 let g:called_arg = a:arg
1368endfunc
1369
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001370func WriteApiCall(funcname)
1371 " Use the title termcap entries to output the escape sequence.
1372 call writefile([
1373 \ 'set title',
1374 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1375 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1376 \ 'redraw',
1377 \ "set t_ts=",
1378 \ ], 'Xscript')
1379endfunc
1380
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001381func Test_terminal_api_call()
1382 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001383 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001384 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001385
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001386 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001387 let buf = RunVimInTerminal('-S Xscript', {})
1388 call WaitFor({-> exists('g:called_bufnum')})
1389 call assert_equal(buf, g:called_bufnum)
1390 call assert_equal(['hello', 123], g:called_arg)
1391
1392 call StopVimInTerminal(buf)
1393 call delete('Xscript')
1394 unlet g:called_bufnum
1395 unlet g:called_arg
1396endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001397
1398func Test_terminal_api_call_fails()
1399 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001400 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001401 endif
1402
1403 call WriteApiCall('TryThis')
1404 call ch_logfile('Xlog', 'w')
1405 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001406 call WaitForAssert({-> assert_match('Invalid function name: TryThis', string(readfile('Xlog')))})
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001407
1408 call StopVimInTerminal(buf)
1409 call delete('Xscript')
1410 call ch_logfile('', '')
1411 call delete('Xlog')
1412endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001413
Bram Moolenaara997b452018-04-17 23:24:06 +02001414let s:caught_e937 = 0
1415
1416func Tapi_Delete(bufnum, arg)
1417 try
1418 execute 'bdelete!' a:bufnum
1419 catch /E937:/
1420 let s:caught_e937 = 1
1421 endtry
1422endfunc
1423
1424func Test_terminal_api_call_fail_delete()
1425 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001426 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaara997b452018-04-17 23:24:06 +02001427 endif
1428
1429 call WriteApiCall('Tapi_Delete')
1430 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001431 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001432
1433 call StopVimInTerminal(buf)
1434 call delete('Xscript')
1435 call ch_logfile('', '')
1436endfunc
1437
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001438func Test_terminal_ansicolors_default()
1439 let colors = [
1440 \ '#000000', '#e00000',
1441 \ '#00e000', '#e0e000',
1442 \ '#0000e0', '#e000e0',
1443 \ '#00e0e0', '#e0e0e0',
1444 \ '#808080', '#ff4040',
1445 \ '#40ff40', '#ffff40',
1446 \ '#4040ff', '#ff40ff',
1447 \ '#40ffff', '#ffffff',
1448 \]
1449
1450 let buf = Run_shell_in_terminal({})
1451 call assert_equal(colors, term_getansicolors(buf))
1452 call Stop_shell_in_terminal(buf)
1453 call term_wait(buf)
1454
1455 exe buf . 'bwipe'
1456endfunc
1457
1458let s:test_colors = [
1459 \ '#616e64', '#0d0a79',
1460 \ '#6d610d', '#0a7373',
1461 \ '#690d0a', '#6d696e',
1462 \ '#0d0a6f', '#616e0d',
1463 \ '#0a6479', '#6d0d0a',
1464 \ '#617373', '#0d0a69',
1465 \ '#6d690d', '#0a6e6f',
1466 \ '#610d0a', '#6e6479',
1467 \]
1468
1469func Test_terminal_ansicolors_global()
1470 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1471 let buf = Run_shell_in_terminal({})
1472 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1473 call Stop_shell_in_terminal(buf)
1474 call term_wait(buf)
1475
1476 exe buf . 'bwipe'
1477 unlet g:terminal_ansi_colors
1478endfunc
1479
1480func Test_terminal_ansicolors_func()
1481 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1482 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1483 call assert_equal(s:test_colors, term_getansicolors(buf))
1484
1485 call term_setansicolors(buf, g:terminal_ansi_colors)
1486 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1487
1488 let colors = [
1489 \ 'ivory', 'AliceBlue',
1490 \ 'grey67', 'dark goldenrod',
1491 \ 'SteelBlue3', 'PaleVioletRed4',
1492 \ 'MediumPurple2', 'yellow2',
1493 \ 'RosyBrown3', 'OrangeRed2',
1494 \ 'white smoke', 'navy blue',
1495 \ 'grey47', 'gray97',
1496 \ 'MistyRose2', 'DodgerBlue4',
1497 \]
1498 call term_setansicolors(buf, colors)
1499
1500 let colors[4] = 'Invalid'
1501 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1502
1503 call Stop_shell_in_terminal(buf)
1504 call term_wait(buf)
1505 exe buf . 'bwipe'
1506endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001507
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001508func Test_terminal_all_ansi_colors()
1509 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001510 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001511 endif
1512
1513 " Use all the ANSI colors.
1514 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001515 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001516 \ 'hi Tblack ctermfg=0 ctermbg=8',
1517 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1518 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1519 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1520 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1521 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1522 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1523 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1524 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1525 \ 'hi Tred ctermfg=9 ctermbg=1',
1526 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1527 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1528 \ 'hi Tblue ctermfg=12 ctermbg=4',
1529 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1530 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1531 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001532 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1533 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1534 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001535 \ '',
1536 \ 'call matchadd("Tblack", "A")',
1537 \ 'call matchadd("Tdarkred", "B")',
1538 \ 'call matchadd("Tdarkgreen", "C")',
1539 \ 'call matchadd("Tbrown", "D")',
1540 \ 'call matchadd("Tdarkblue", "E")',
1541 \ 'call matchadd("Tdarkmagenta", "F")',
1542 \ 'call matchadd("Tdarkcyan", "G")',
1543 \ 'call matchadd("Tlightgrey", "H")',
1544 \ 'call matchadd("Tdarkgrey", "I")',
1545 \ 'call matchadd("Tred", "J")',
1546 \ 'call matchadd("Tgreen", "K")',
1547 \ 'call matchadd("Tyellow", "L")',
1548 \ 'call matchadd("Tblue", "M")',
1549 \ 'call matchadd("Tmagenta", "N")',
1550 \ 'call matchadd("Tcyan", "O")',
1551 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001552 \ 'call matchadd("TdarkredBold", "X")',
1553 \ 'call matchadd("TgreenBold", "Y")',
1554 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001555 \ 'redraw',
1556 \ ], 'Xcolorscript')
1557 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1558 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1559
1560 call term_sendkeys(buf, ":q\<CR>")
1561 call StopVimInTerminal(buf)
1562 call delete('Xcolorscript')
1563endfunc
1564
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001565func Test_terminal_termwinsize_option_fixed()
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001566 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001567 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001568 endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001569 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001570 let text = []
1571 for n in range(10)
1572 call add(text, repeat(n, 50))
1573 endfor
1574 call writefile(text, 'Xwinsize')
1575 let buf = RunVimInTerminal('Xwinsize', {})
1576 let win = bufwinid(buf)
1577 call assert_equal([6, 40], term_getsize(buf))
1578 call assert_equal(6, winheight(win))
1579 call assert_equal(40, winwidth(win))
1580
1581 " resizing the window doesn't resize the terminal.
1582 resize 10
1583 vertical resize 60
1584 call assert_equal([6, 40], term_getsize(buf))
1585 call assert_equal(10, winheight(win))
1586 call assert_equal(60, winwidth(win))
1587
1588 call StopVimInTerminal(buf)
1589 call delete('Xwinsize')
1590
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001591 call assert_fails('set termwinsize=40', 'E474')
1592 call assert_fails('set termwinsize=10+40', 'E474')
1593 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001594
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001595 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001596endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001597
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001598func Test_terminal_termwinsize_option_zero()
1599 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001600 let buf = Run_shell_in_terminal({})
1601 let win = bufwinid(buf)
1602 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1603 call Stop_shell_in_terminal(buf)
1604 call term_wait(buf)
1605 exe buf . 'bwipe'
1606
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001607 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001608 let buf = Run_shell_in_terminal({})
1609 let win = bufwinid(buf)
1610 call assert_equal([7, winwidth(win)], term_getsize(buf))
1611 call Stop_shell_in_terminal(buf)
1612 call term_wait(buf)
1613 exe buf . 'bwipe'
1614
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001615 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001616 let buf = Run_shell_in_terminal({})
1617 let win = bufwinid(buf)
1618 call assert_equal([winheight(win), 33], term_getsize(buf))
1619 call Stop_shell_in_terminal(buf)
1620 call term_wait(buf)
1621 exe buf . 'bwipe'
1622
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001623 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001624endfunc
1625
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001626func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001627 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001628 vsplit
1629 let buf = Run_shell_in_terminal({})
1630 let win = bufwinid(buf)
1631 call assert_inrange(10, 1000, winheight(win))
1632 call assert_inrange(50, 1000, winwidth(win))
1633 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1634
1635 resize 15
1636 vertical resize 60
1637 redraw
1638 call assert_equal([15, 60], term_getsize(buf))
1639 call assert_equal(15, winheight(win))
1640 call assert_equal(60, winwidth(win))
1641
1642 resize 7
1643 vertical resize 30
1644 redraw
1645 call assert_equal([10, 50], term_getsize(buf))
1646 call assert_equal(7, winheight(win))
1647 call assert_equal(30, winwidth(win))
1648
1649 call Stop_shell_in_terminal(buf)
1650 call term_wait(buf)
1651 exe buf . 'bwipe'
1652
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001653 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001654 let buf = Run_shell_in_terminal({})
1655 let win = bufwinid(buf)
1656 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1657 call Stop_shell_in_terminal(buf)
1658 call term_wait(buf)
1659 exe buf . 'bwipe'
1660
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001661 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001662endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001663
1664func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001665 " make three tabpages, terminal in the middle
1666 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001667 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001668 tabnew
1669 tabprev
1670 call assert_equal(1, winnr('$'))
1671 call assert_equal(2, tabpagenr())
1672 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001673
1674 let buf = Run_shell_in_terminal({})
1675 let termwin = bufwinid(buf)
1676 set termwinkey=<C-L>
1677 call feedkeys("\<C-L>w", 'tx')
1678 call assert_equal(thiswin, win_getid())
1679 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001680 call assert_equal(termwin, win_getid())
1681
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001682 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001683 call assert_equal(3, tabpagenr())
1684 tabprev
1685 call assert_equal(2, tabpagenr())
1686 call assert_equal(termwin, win_getid())
1687
1688 call feedkeys("\<C-L>gT", "xt")
1689 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001690 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001691 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001692 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001693
1694 let job = term_getjob(buf)
1695 call feedkeys("\<C-L>\<C-C>", 'tx')
1696 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001697
1698 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001699 tabnext
1700 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001701 tabprev
1702 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001703endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001704
1705func Test_terminal_out_err()
1706 if !has('unix')
1707 return
1708 endif
1709 call writefile([
1710 \ '#!/bin/sh',
1711 \ 'echo "this is standard error" >&2',
1712 \ 'echo "this is standard out" >&1',
1713 \ ], 'Xechoerrout.sh')
1714 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1715
1716 let outfile = 'Xtermstdout'
1717 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001718
1719 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1720 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001721 call assert_equal('this is standard error', term_getline(buf, 1))
1722
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001723 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001724 exe buf . 'bwipe'
1725 call delete('Xechoerrout.sh')
1726 call delete(outfile)
1727endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001728
1729func Test_terminwinscroll()
1730 if !has('unix')
1731 return
1732 endif
1733
1734 " Let the terminal output more than 'termwinscroll' lines, some at the start
1735 " will be dropped.
1736 exe 'set termwinscroll=' . &lines
1737 let buf = term_start('/bin/sh')
1738 for i in range(1, &lines)
1739 call feedkeys("echo " . i . "\<CR>", 'xt')
1740 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1741 endfor
1742 " Go to Terminal-Normal mode to update the buffer.
1743 call feedkeys("\<C-W>N", 'xt')
1744 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1745
1746 " Every "echo nr" must only appear once
1747 let lines = getline(1, line('$'))
1748 for i in range(&lines - len(lines) / 2 + 2, &lines)
1749 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1750 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1751 endfor
1752
1753 exe buf . 'bwipe!'
1754endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001755
Bram Moolenaar875cf872018-07-08 20:49:07 +02001756" Resizing the terminal window caused an ml_get error.
1757" TODO: This does not reproduce the original problem.
1758func Test_terminal_resize()
1759 set statusline=x
1760 terminal
1761 call assert_equal(2, winnr('$'))
1762
1763 " Fill the terminal with text.
1764 if has('win32')
1765 call feedkeys("dir\<CR>", 'xt')
1766 else
1767 call feedkeys("ls\<CR>", 'xt')
1768 endif
1769 " Go to Terminal-Normal mode for a moment.
1770 call feedkeys("\<C-W>N", 'xt')
1771 " Open a new window
1772 call feedkeys("i\<C-W>n", 'xt')
1773 call assert_equal(3, winnr('$'))
1774 redraw
1775
1776 close
1777 call assert_equal(2, winnr('$'))
1778 call feedkeys("exit\<CR>", 'xt')
1779 set statusline&
1780endfunc
1781
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001782" must be nearly the last, we can't go back from GUI to terminal
1783func Test_zz1_terminal_in_gui()
1784 if !CanRunGui()
1785 return
1786 endif
1787
1788 " Ignore the "failed to create input context" error.
1789 call test_ignore_error('E285:')
1790
1791 gui -f
1792
1793 call assert_equal(1, winnr('$'))
1794 let buf = Run_shell_in_terminal({'term_finish': 'close'})
1795 call Stop_shell_in_terminal(buf)
1796 call term_wait(buf)
1797
1798 " closing window wipes out the terminal buffer a with finished job
1799 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1800 call assert_equal("", bufname(buf))
1801
1802 unlet g:job
1803endfunc
1804
1805func Test_zz2_terminal_guioptions_bang()
1806 if !has('gui_running')
1807 return
1808 endif
1809 set guioptions+=!
1810
1811 let filename = 'Xtestscript'
1812 if has('win32')
1813 let filename .= '.bat'
1814 let prefix = ''
1815 let contents = ['@echo off', 'exit %1']
1816 else
1817 let filename .= '.sh'
1818 let prefix = './'
1819 let contents = ['#!/bin/sh', 'exit $1']
1820 endif
1821 call writefile(contents, filename)
1822 call setfperm(filename, 'rwxrwx---')
1823
1824 " Check if v:shell_error is equal to the exit status.
1825 let exitval = 0
1826 execute printf(':!%s%s %d', prefix, filename, exitval)
1827 call assert_equal(exitval, v:shell_error)
1828
1829 let exitval = 9
1830 execute printf(':!%s%s %d', prefix, filename, exitval)
1831 call assert_equal(exitval, v:shell_error)
1832
1833 set guioptions&
1834 call delete(filename)
1835endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001836
1837func Test_terminal_hidden()
1838 if !has('unix')
1839 return
1840 endif
1841 term ++hidden cat
1842 let bnr = bufnr('$')
1843 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1844 exe 'sbuf ' . bnr
1845 call assert_equal('terminal', &buftype)
1846 call term_sendkeys(bnr, "asdf\<CR>")
1847 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1848 call term_sendkeys(bnr, "\<C-D>")
1849 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
1850 bwipe!
1851endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001852
Bram Moolenaara4c2a242019-03-25 23:01:38 +01001853func Test_terminal_switch_mode()
1854 term
1855 let bnr = bufnr('$')
1856 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1857 call feedkeys("\<C-W>N", 'xt')
1858 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1859 call feedkeys("A", 'xt')
1860 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1861 call feedkeys("\<C-W>N", 'xt')
1862 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1863 call feedkeys("I", 'xt')
1864 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1865 call feedkeys("\<C-W>Nv", 'xt')
1866 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1867 call feedkeys("I", 'xt')
1868 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1869 call feedkeys("\<C-W>Nv", 'xt')
1870 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1871 call feedkeys("A", 'xt')
1872 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1873 bwipe!
1874endfunc
1875
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001876func Test_terminal_hidden_and_close()
1877 if !has('unix')
1878 return
1879 endif
1880 call assert_equal(1, winnr('$'))
1881 term ++hidden ++close ls
1882 let bnr = bufnr('$')
1883 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1884 call WaitForAssert({-> assert_false(bufexists(bnr))})
1885 call assert_equal(1, winnr('$'))
1886endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001887
1888func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001889 " This test does not pass through ConPTY.
1890 if has('conpty')
1891 return
1892 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001893 let contents = [
1894 \ [ 'One', '', 'X' ],
1895 \ [ 'Two', '', '' ],
1896 \ [ 'Three' ] + repeat([''], 30)
1897 \ ]
1898
1899 for c in contents
1900 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01001901 if has('win32')
1902 term cmd /c type Xfile
1903 else
1904 term cat Xfile
1905 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001906 let bnr = bufnr('$')
1907 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1908 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01001909 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001910 call assert_equal(c, getline(1, line('$')))
1911 quit
1912 endfor
1913
1914 call delete('Xfile')
1915endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01001916
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01001917func Test_terminal_no_job()
1918 let term = term_start('false', {'term_finish': 'close'})
1919 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
1920endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01001921
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02001922func Test_term_getcursor()
1923 if !has('unix')
1924 return
1925 endif
1926 let buf = Run_shell_in_terminal({})
1927
1928 " Wait for the shell to display a prompt.
1929 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
1930
1931 " Hide the cursor.
1932 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
1933 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
1934
1935 " Show the cursor.
1936 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
1937 call WaitForAssert({-> assert_equal(1, term_getcursor(buf)[2].visible)})
1938
1939 " Change color of cursor.
1940 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
1941 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
1942 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
1943 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
1944 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
1945
1946 " Make cursor a blinking block.
1947 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
1948 call WaitForAssert({-> assert_equal([1, 1],
1949 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1950
1951 " Make cursor a steady block.
1952 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
1953 call WaitForAssert({-> assert_equal([0, 1],
1954 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1955
1956 " Make cursor a blinking underline.
1957 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
1958 call WaitForAssert({-> assert_equal([1, 2],
1959 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1960
1961 " Make cursor a steady underline.
1962 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
1963 call WaitForAssert({-> assert_equal([0, 2],
1964 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1965
1966 " Make cursor a blinking vertical bar.
1967 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
1968 call WaitForAssert({-> assert_equal([1, 3],
1969 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1970
1971 " Make cursor a steady vertical bar.
1972 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
1973 call WaitForAssert({-> assert_equal([0, 3],
1974 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1975
1976 call Stop_shell_in_terminal(buf)
1977endfunc
1978
Bram Moolenaar1e115362019-01-09 23:01:02 +01001979func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01001980 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02001981 " and for a non-existing buffer.
Bram Moolenaar1e115362019-01-09 23:01:02 +01001982 call assert_equal('', term_gettitle(bufnr('%')))
1983 call assert_equal('', term_gettitle(bufnr('$') + 1))
1984
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02001985 if !has('title') || &title == 0 || empty(&t_ts)
1986 throw "Skipped: can't get/set title"
1987 endif
1988
Bram Moolenaar1e115362019-01-09 23:01:02 +01001989 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001990 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02001991 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
1992 call term_sendkeys(term, ":e Xfoo\r")
1993 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001994 else
1995 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02001996 call term_sendkeys(term, ":e Xfoo\r")
1997 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001998 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01001999
Bram Moolenaar1e115362019-01-09 23:01:02 +01002000 call term_sendkeys(term, ":set titlestring=foo\r")
2001 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2002
2003 exe term . 'bwipe!'
2004endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002005
2006" When drawing the statusline the cursor position may not have been updated
2007" yet.
2008" 1. create a terminal, make it show 2 lines
2009" 2. 0.5 sec later: leave terminal window, execute "i"
2010" 3. 0.5 sec later: clear terminal window, now it's 1 line
2011" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2012" 4. 0.5 sec later: should be done, clean up
2013func Test_terminal_statusline()
2014 if !has('unix')
2015 return
2016 endif
2017 set statusline=x
2018 terminal
2019 let tbuf = bufnr('')
2020 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2021 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2022 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2023 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2024
2025 sleep 2
2026 exe tbuf . 'bwipe!'
2027 au! BufLeave
2028 set statusline=
2029endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002030
2031func Test_terminal_getwinpos()
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002032 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02002033 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002034 endif
2035
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002036 " split, go to the bottom-right window
2037 split
2038 wincmd j
2039 set splitright
2040
2041 call writefile([
2042 \ 'echo getwinpos()',
2043 \ ], 'XTest_getwinpos')
2044 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
2045 call term_wait(buf)
2046
2047 " Find the output of getwinpos() in the bottom line.
2048 let rows = term_getsize(buf)[0]
2049 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2050 let line = term_getline(buf, rows)
2051 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2052 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2053
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002054 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002055 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002056 " In the GUI it can be more, let's assume a 20 x 14 cell.
2057 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002058 let [xroot, yroot] = getwinpos()
Bram Moolenaar1b557972019-04-09 21:17:32 +02002059 let [winrow, wincol] = win_screenpos('.')
2060 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2061 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2062 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2063 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002064
2065 call term_wait(buf)
2066 call term_sendkeys(buf, ":q\<CR>")
2067 call StopVimInTerminal(buf)
2068 call delete('XTest_getwinpos')
2069 exe buf . 'bwipe!'
2070 set splitright&
2071 only!
2072endfunc