blob: c4d3e528c668480054e916d192903a79cde5136f [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 Moolenaar7ee80f72019-09-08 20:55:06 +020028 let string = string({'job': buf->term_getjob()})
Bram Moolenaar35422f42017-08-05 16:33:56 +020029 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)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020045 call assert_match('^\\\\.\\pipe\\', ''->term_gettty())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010046 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))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020094 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +010095
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 Moolenaar7ee80f72019-09-08 20:55:06 +0200179 let l = a:buf->term_scrape(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 Moolenaar7ee80f72019-09-08 20:55:06 +0200185 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
186 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100187 if has('win32')
188 " On Windows 'background' always defaults to dark, even though the terminal
189 " may use a light background. Therefore accept both white and black.
190 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200191 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100192 if &background == 'light'
193 call assert_equal('#ffffff', l[0].bg)
194 else
195 call assert_equal('#000000', l[0].bg)
196 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200197 endif
198
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200199 let l = term_getline(a:buf, -1)
200 call assert_equal('', l)
201 let l = term_getline(a:buf, 0)
202 call assert_equal('', l)
203 let l = term_getline(a:buf, 999)
204 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200205 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200206 call assert_equal('123', l)
207endfunc
208
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200209func Test_terminal_scrape_123()
210 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200211 let buf = term_start(cmd)
212
213 let termlist = term_list()
214 call assert_equal(1, len(termlist))
215 call assert_equal(buf, termlist[0])
216
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200217 " Nothing happens with invalid buffer number
218 call term_wait(1234)
219
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200220 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200221 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200222 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200223 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200224 call Check_123(buf)
225
226 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100227 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200228 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200229 call term_wait(buf)
230 call Check_123(buf)
231
232 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200233 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200234endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200235
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200236func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200237 call writefile(["léttまrs"], 'Xtext')
238 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200239 " Run cmd with UTF-8 codepage to make the type command print the expected
240 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100241 let buf = term_start("cmd /K chcp 65001")
242 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200243 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100244 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200245 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100246 let buf = term_start("cat Xtext")
247 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200248 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200249
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100250 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
251 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200252 call assert_true(len(l) >= 7)
253 call assert_equal('l', l[0].chars)
254 call assert_equal('é', l[1].chars)
255 call assert_equal(1, l[1].width)
256 call assert_equal('t', l[2].chars)
257 call assert_equal('t', l[3].chars)
258 call assert_equal('ま', l[4].chars)
259 call assert_equal(2, l[4].width)
260 call assert_equal('r', l[5].chars)
261 call assert_equal('s', l[6].chars)
262
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100263 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200264 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100265 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200266
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100267 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200268 call delete('Xtext')
269endfunc
270
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200271func Test_terminal_scroll()
272 call writefile(range(1, 200), 'Xtext')
273 if has('win32')
274 let cmd = 'cmd /c "type Xtext"'
275 else
276 let cmd = "cat Xtext"
277 endif
278 let buf = term_start(cmd)
279
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100280 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200281 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200282 call term_wait(buf)
283 if has('win32')
284 " TODO: this should not be needed
285 sleep 100m
286 endif
287
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200288 let scrolled = buf->term_getscrolled()
289 call assert_equal(scrolled, term_getscrolled(buf))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200290 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200291 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200292 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200293 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200294 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200295 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200296
297 exe buf . 'bwipe'
298 call delete('Xtext')
299endfunc
300
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200301func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200302 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200303 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200304 call writefile(range(150), 'Xtext')
305 if has('win32')
306 call term_sendkeys(buf, "type Xtext\<CR>")
307 else
308 call term_sendkeys(buf, "cat Xtext\<CR>")
309 endif
310 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200311 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200312 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200313 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200314 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200315
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200316 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200317 call term_wait(buf)
318 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200319 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100320 call delete('Xtext')
321endfunc
322
323func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200324 " tail -f only works on Unix
325 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100326
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 Moolenaar7ee80f72019-09-08 20:55:06 +0200387 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200388
389 call term_setsize('', 6, 0)
390 call assert_equal([6, 33], term_getsize(''))
391
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200392 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200393 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 Moolenaar7ee80f72019-09-08 20:55:06 +0200411 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200412 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 Moolenaar7a39dd72019-06-23 00:50:15 +0200491 call StopShellInTerminal(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 Moolenaar7a39dd72019-06-23 00:50:15 +0200500 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100501 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.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200573 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100574 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)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200606 call StopShellInTerminal(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 Moolenaar7ee80f72019-09-08 20:55:06 +0200622 eval buf->term_wait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200623 call StopShellInTerminal(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)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200665 call StopShellInTerminal(buf)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100666 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
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200741 CheckUnix
742
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200743 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()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200953 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100954
955 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200956 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100957
Bram Moolenaar086eb872018-03-25 21:24:12 +0200958 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200959 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100960
Bram Moolenaar086eb872018-03-25 21:24:12 +0200961 " Request the cursor position.
962 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100963
964 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200965 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100966
Bram Moolenaar086eb872018-03-25 21:24:12 +0200967 " End "cat" gently.
968 call term_sendkeys(buf, "\<CR>\<C-D>")
969
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200970 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100971 exe buf . 'bwipe'
972 unlet g:job
973endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100974
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100975" Run Vim, start a terminal in that Vim with the kill argument,
976" :qall works.
977func Run_terminal_qall_kill(line1, line2)
978 " 1. Open a terminal window and wait for the prompt to appear
979 " 2. set kill using term_setkill()
980 " 3. make Vim exit, it will kill the shell
981 let after = [
982 \ a:line1,
983 \ 'let buf = bufnr("%")',
984 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
985 \ ' sleep 10m',
986 \ 'endwhile',
987 \ a:line2,
988 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
989 \ 'qall',
990 \ ]
991 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100992 return
993 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100994 call assert_equal("done", readfile("Xdone")[0])
995 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100996endfunc
997
998" Run Vim in a terminal, then start a terminal in that Vim with a kill
999" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001000func Test_terminal_qall_kill_arg()
1001 call Run_terminal_qall_kill('term ++kill=kill', '')
1002endfunc
1003
1004" Run Vim, start a terminal in that Vim, set the kill argument with
1005" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001006func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001007 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001008endfunc
1009
1010" Run Vim, start a terminal in that Vim without the kill argument,
1011" check that :qall does not exit, :qall! does.
1012func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001013 let after =<< trim [CODE]
1014 term
1015 let buf = bufnr("%")
1016 while term_getline(buf, 1) =~ "^\\s*$"
1017 sleep 10m
1018 endwhile
1019 set nomore
1020 au VimLeavePre * call writefile(["too early"], "Xdone")
1021 qall
1022 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1023 cquit
1024 [CODE]
1025
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001026 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001027 return
1028 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001029 call assert_equal("done", readfile("Xdone")[0])
1030 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001031endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001032
1033" Run Vim in a terminal, then start a terminal in that Vim without a kill
1034" argument, check that :confirm qall works.
1035func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001036 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001037 let buf = RunVimInTerminal('', {})
1038
1039 " Open a terminal window and wait for the prompt to appear
1040 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001041 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1042 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001043
1044 " make Vim exit, it will prompt to kill the shell
1045 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001046 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001047 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001048 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001049
1050 " close the terminal window where Vim was running
1051 quit
1052endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001053
Bram Moolenaar012eb662018-03-13 17:55:27 +01001054func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001055 augroup repro
1056 au!
1057 au TerminalOpen * let s:called += 1
1058 augroup END
1059
1060 let s:called = 0
1061
1062 " Open a terminal window with :terminal
1063 terminal
1064 call assert_equal(1, s:called)
1065 bwipe!
1066
1067 " Open a terminal window with term_start()
1068 call term_start(&shell)
1069 call assert_equal(2, s:called)
1070 bwipe!
1071
1072 " Open a hidden terminal buffer with :terminal
1073 terminal ++hidden
1074 call assert_equal(3, s:called)
1075 for buf in term_list()
1076 exe buf . "bwipe!"
1077 endfor
1078
1079 " Open a hidden terminal buffer with term_start()
1080 let buf = term_start(&shell, {'hidden': 1})
1081 call assert_equal(4, s:called)
1082 exe buf . "bwipe!"
1083
1084 unlet s:called
1085 au! repro
1086endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001087
1088func Check_dump01(off)
1089 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1090 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001091 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001092endfunc
1093
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001094func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001095 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001096 let save_enc = &encoding
1097 set encoding=utf-8
1098 call assert_equal(1, winnr('$'))
1099
1100 let text = " a\u0300 e\u0302 o\u0308"
1101 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001102 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001103 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001104 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001105 let dumpline = readfile('Xdump')[0]
1106 call assert_match('|à| |ê| |ö', dumpline)
1107
1108 call StopVimInTerminal(buf)
1109 call delete('Xcomposing')
1110 call delete('Xdump')
1111 let &encoding = save_enc
1112endfunc
1113
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001114" just testing basic functionality.
1115func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001116 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001117 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001118 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001119 call assert_equal(2, winnr('$'))
1120 call assert_equal(20, line('$'))
1121 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001122
1123 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001124 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001125 call assert_equal(buf, buf2)
1126 call assert_notequal('one two three four five', trim(getline(1)))
1127
1128 " Load the first dump again in the same window
1129 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1130 call assert_equal(buf, buf2)
1131 call Check_dump01(0)
1132
1133 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1134 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1135 new
1136 let closedbuf = winbufnr('')
1137 quit
1138 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
1139
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001140 quit
1141endfunc
1142
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001143func Test_terminal_dumpload_dump()
1144 CheckRunVimInTerminal
1145
1146 let lines =<< trim END
1147 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1148 END
1149 call writefile(lines, 'XtermDumpload')
1150 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1151 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1152
1153 call StopVimInTerminal(buf)
1154 call delete('XtermDumpload')
1155endfunc
1156
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001157func Test_terminal_dumpdiff()
1158 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001159 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001160 call assert_equal(2, winnr('$'))
1161 call assert_equal(62, line('$'))
1162 call Check_dump01(0)
1163 call Check_dump01(42)
1164 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1165 quit
1166endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001167
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001168func Test_terminal_dumpdiff_swap()
1169 call assert_equal(1, winnr('$'))
1170 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1171 call assert_equal(2, winnr('$'))
1172 call assert_equal(62, line('$'))
1173 call assert_match('Test_popup_command_01.dump', getline(21))
1174 call assert_match('Test_popup_command_03.dump', getline(42))
1175 call assert_match('Undo', getline(3))
1176 call assert_match('three four five', getline(45))
1177
1178 normal s
1179 call assert_match('Test_popup_command_03.dump', getline(21))
1180 call assert_match('Test_popup_command_01.dump', getline(42))
1181 call assert_match('three four five', getline(3))
1182 call assert_match('Undo', getline(45))
1183 quit
1184endfunc
1185
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001186func Test_terminal_dumpdiff_options()
1187 set laststatus=0
1188 call assert_equal(1, winnr('$'))
1189 let height = winheight(0)
1190 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1191 call assert_equal(2, winnr('$'))
1192 call assert_equal(height, winheight(winnr()))
1193 call assert_equal(33, winwidth(winnr()))
1194 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1195 quit
1196
1197 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001198 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1199 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001200 call assert_equal(&columns, winwidth(0))
1201 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001202 call assert_equal('something else', bufname('%'))
1203 quit
1204
1205 call assert_equal(1, winnr('$'))
1206 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1207 call assert_equal(1, winnr('$'))
1208 bwipe
1209
1210 set laststatus&
1211endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001212
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001213func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001214 call assert_equal(1, winnr('$'))
1215
1216 " Use the title termcap entries to output the escape sequence.
1217 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001218 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001219 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001220 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001221 \ 'redraw',
1222 \ "set t_ts=",
1223 \ ], 'Xscript')
1224 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001225 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001226 call assert_equal('Xtextfile', expand('%:t'))
1227 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001228 return buf
1229endfunc
1230
1231func Test_terminal_api_drop_newwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001232 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001233 let buf = Api_drop_common('')
1234 call assert_equal(0, &bin)
1235 call assert_equal('', &fenc)
1236
1237 call StopVimInTerminal(buf)
1238 call delete('Xscript')
1239 bwipe Xtextfile
1240endfunc
1241
1242func Test_terminal_api_drop_newwin_bin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001243 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001244 let buf = Api_drop_common(',{"bin":1}')
1245 call assert_equal(1, &bin)
1246
1247 call StopVimInTerminal(buf)
1248 call delete('Xscript')
1249 bwipe Xtextfile
1250endfunc
1251
1252func Test_terminal_api_drop_newwin_binary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001253 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001254 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()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001263 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001264 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()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001275 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001276 set binary
1277 let buf = Api_drop_common(',{"nobinary":1}')
1278 call assert_equal(0, &bin)
1279
1280 call StopVimInTerminal(buf)
1281 call delete('Xscript')
1282 bwipe Xtextfile
1283 set nobinary
1284endfunc
1285
1286func Test_terminal_api_drop_newwin_ff()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001287 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001288 let buf = Api_drop_common(',{"ff":"dos"}')
1289 call assert_equal("dos", &ff)
1290
1291 call StopVimInTerminal(buf)
1292 call delete('Xscript')
1293 bwipe Xtextfile
1294endfunc
1295
1296func Test_terminal_api_drop_newwin_fileformat()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001297 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001298 let buf = Api_drop_common(',{"fileformat":"dos"}')
1299 call assert_equal("dos", &ff)
1300
1301 call StopVimInTerminal(buf)
1302 call delete('Xscript')
1303 bwipe Xtextfile
1304endfunc
1305
1306func Test_terminal_api_drop_newwin_enc()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001307 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001308 let buf = Api_drop_common(',{"enc":"utf-16"}')
1309 call assert_equal("utf-16", &fenc)
1310
1311 call StopVimInTerminal(buf)
1312 call delete('Xscript')
1313 bwipe Xtextfile
1314endfunc
1315
1316func Test_terminal_api_drop_newwin_encoding()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001317 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001318 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1319 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001320
1321 call StopVimInTerminal(buf)
1322 call delete('Xscript')
1323 bwipe Xtextfile
1324endfunc
1325
1326func Test_terminal_api_drop_oldwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001327 CheckRunVimInTerminal
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001328 let firstwinid = win_getid()
1329 split Xtextfile
1330 let textfile_winid = win_getid()
1331 call assert_equal(2, winnr('$'))
1332 call win_gotoid(firstwinid)
1333
1334 " Use the title termcap entries to output the escape sequence.
1335 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001336 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001337 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1338 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1339 \ 'redraw',
1340 \ "set t_ts=",
1341 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001342 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001343 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001344 call assert_equal(textfile_winid, win_getid())
1345
1346 call StopVimInTerminal(buf)
1347 call delete('Xscript')
1348 bwipe Xtextfile
1349endfunc
1350
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001351func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001352 let g:called_bufnum = a:bufnum
1353 let g:called_arg = a:arg
1354endfunc
1355
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001356func WriteApiCall(funcname)
1357 " Use the title termcap entries to output the escape sequence.
1358 call writefile([
1359 \ 'set title',
1360 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1361 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1362 \ 'redraw',
1363 \ "set t_ts=",
1364 \ ], 'Xscript')
1365endfunc
1366
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001367func Test_terminal_api_call()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001368 CheckRunVimInTerminal
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001369
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001370 unlet! g:called_bufnum
1371 unlet! g:called_arg
1372
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001373 call WriteApiCall('Tapi_TryThis')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001374
1375 " Default
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001376 let buf = RunVimInTerminal('-S Xscript', {})
1377 call WaitFor({-> exists('g:called_bufnum')})
1378 call assert_equal(buf, g:called_bufnum)
1379 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001380 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001381
1382 unlet! g:called_bufnum
1383 unlet! g:called_arg
1384
1385 " Enable explicitly
1386 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1387 call WaitFor({-> exists('g:called_bufnum')})
1388 call assert_equal(buf, g:called_bufnum)
1389 call assert_equal(['hello', 123], g:called_arg)
1390 call StopVimInTerminal(buf)
1391
1392 unlet! g:called_bufnum
1393 unlet! g:called_arg
1394
1395 func! ApiCall_TryThis(bufnum, arg)
1396 let g:called_bufnum2 = a:bufnum
1397 let g:called_arg2 = a:arg
1398 endfunc
1399
1400 call WriteApiCall('ApiCall_TryThis')
1401
1402 " Use prefix match
1403 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1404 call WaitFor({-> exists('g:called_bufnum2')})
1405 call assert_equal(buf, g:called_bufnum2)
1406 call assert_equal(['hello', 123], g:called_arg2)
1407 call StopVimInTerminal(buf)
1408
1409 unlet! g:called_bufnum2
1410 unlet! g:called_arg2
1411
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001412 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001413 delfunction! ApiCall_TryThis
1414 unlet! g:called_bufnum2
1415 unlet! g:called_arg2
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001416endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001417
1418func Test_terminal_api_call_fails()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001419 CheckRunVimInTerminal
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001420
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001421 func! TryThis(bufnum, arg)
1422 let g:called_bufnum3 = a:bufnum
1423 let g:called_arg3 = a:arg
1424 endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001425
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001426 call WriteApiCall('TryThis')
1427
1428 unlet! g:called_bufnum3
1429 unlet! g:called_arg3
1430
1431 " Not permitted
1432 call ch_logfile('Xlog', 'w')
1433 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1434 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1435 call assert_false(exists('g:called_bufnum3'))
1436 call assert_false(exists('g:called_arg3'))
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001437 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001438
1439 " No match
1440 call ch_logfile('Xlog', 'w')
1441 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1442 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1443 call assert_false(exists('g:called_bufnum3'))
1444 call assert_false(exists('g:called_arg3'))
1445 call StopVimInTerminal(buf)
1446
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001447 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001448 call ch_logfile('')
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001449 call delete('Xlog')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001450 delfunction! TryThis
1451 unlet! g:called_bufnum3
1452 unlet! g:called_arg3
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001453endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001454
Bram Moolenaara997b452018-04-17 23:24:06 +02001455let s:caught_e937 = 0
1456
1457func Tapi_Delete(bufnum, arg)
1458 try
1459 execute 'bdelete!' a:bufnum
1460 catch /E937:/
1461 let s:caught_e937 = 1
1462 endtry
1463endfunc
1464
1465func Test_terminal_api_call_fail_delete()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001466 CheckRunVimInTerminal
Bram Moolenaara997b452018-04-17 23:24:06 +02001467
1468 call WriteApiCall('Tapi_Delete')
1469 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001470 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001471
1472 call StopVimInTerminal(buf)
1473 call delete('Xscript')
1474 call ch_logfile('', '')
1475endfunc
1476
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001477func Test_terminal_ansicolors_default()
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001478 if !exists('*term_getansicolors')
1479 throw 'Skipped: term_getansicolors() not supported'
1480 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001481 let colors = [
1482 \ '#000000', '#e00000',
1483 \ '#00e000', '#e0e000',
1484 \ '#0000e0', '#e000e0',
1485 \ '#00e0e0', '#e0e0e0',
1486 \ '#808080', '#ff4040',
1487 \ '#40ff40', '#ffff40',
1488 \ '#4040ff', '#ff40ff',
1489 \ '#40ffff', '#ffffff',
1490 \]
1491
1492 let buf = Run_shell_in_terminal({})
1493 call assert_equal(colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001494 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001495 call term_wait(buf)
1496
1497 exe buf . 'bwipe'
1498endfunc
1499
1500let s:test_colors = [
1501 \ '#616e64', '#0d0a79',
1502 \ '#6d610d', '#0a7373',
1503 \ '#690d0a', '#6d696e',
1504 \ '#0d0a6f', '#616e0d',
1505 \ '#0a6479', '#6d0d0a',
1506 \ '#617373', '#0d0a69',
1507 \ '#6d690d', '#0a6e6f',
1508 \ '#610d0a', '#6e6479',
1509 \]
1510
1511func Test_terminal_ansicolors_global()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001512 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001513 if !exists('*term_getansicolors')
1514 throw 'Skipped: term_getansicolors() not supported'
1515 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001516 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1517 let buf = Run_shell_in_terminal({})
1518 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001519 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001520 call term_wait(buf)
1521
1522 exe buf . 'bwipe'
1523 unlet g:terminal_ansi_colors
1524endfunc
1525
1526func Test_terminal_ansicolors_func()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001527 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001528 if !exists('*term_getansicolors')
1529 throw 'Skipped: term_getansicolors() not supported'
1530 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001531 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1532 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1533 call assert_equal(s:test_colors, term_getansicolors(buf))
1534
1535 call term_setansicolors(buf, g:terminal_ansi_colors)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001536 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001537
1538 let colors = [
1539 \ 'ivory', 'AliceBlue',
1540 \ 'grey67', 'dark goldenrod',
1541 \ 'SteelBlue3', 'PaleVioletRed4',
1542 \ 'MediumPurple2', 'yellow2',
1543 \ 'RosyBrown3', 'OrangeRed2',
1544 \ 'white smoke', 'navy blue',
1545 \ 'grey47', 'gray97',
1546 \ 'MistyRose2', 'DodgerBlue4',
1547 \]
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001548 eval buf->term_setansicolors(colors)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001549
1550 let colors[4] = 'Invalid'
1551 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1552
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001553 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001554 call term_wait(buf)
1555 exe buf . 'bwipe'
1556endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001557
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001558func Test_terminal_all_ansi_colors()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001559 CheckRunVimInTerminal
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001560
1561 " Use all the ANSI colors.
1562 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001563 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001564 \ 'hi Tblack ctermfg=0 ctermbg=8',
1565 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1566 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1567 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1568 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1569 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1570 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1571 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1572 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1573 \ 'hi Tred ctermfg=9 ctermbg=1',
1574 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1575 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1576 \ 'hi Tblue ctermfg=12 ctermbg=4',
1577 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1578 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1579 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001580 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1581 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1582 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001583 \ '',
1584 \ 'call matchadd("Tblack", "A")',
1585 \ 'call matchadd("Tdarkred", "B")',
1586 \ 'call matchadd("Tdarkgreen", "C")',
1587 \ 'call matchadd("Tbrown", "D")',
1588 \ 'call matchadd("Tdarkblue", "E")',
1589 \ 'call matchadd("Tdarkmagenta", "F")',
1590 \ 'call matchadd("Tdarkcyan", "G")',
1591 \ 'call matchadd("Tlightgrey", "H")',
1592 \ 'call matchadd("Tdarkgrey", "I")',
1593 \ 'call matchadd("Tred", "J")',
1594 \ 'call matchadd("Tgreen", "K")',
1595 \ 'call matchadd("Tyellow", "L")',
1596 \ 'call matchadd("Tblue", "M")',
1597 \ 'call matchadd("Tmagenta", "N")',
1598 \ 'call matchadd("Tcyan", "O")',
1599 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001600 \ 'call matchadd("TdarkredBold", "X")',
1601 \ 'call matchadd("TgreenBold", "Y")',
1602 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001603 \ 'redraw',
1604 \ ], 'Xcolorscript')
1605 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1606 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1607
1608 call term_sendkeys(buf, ":q\<CR>")
1609 call StopVimInTerminal(buf)
1610 call delete('Xcolorscript')
1611endfunc
1612
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001613func Test_terminal_termwinsize_option_fixed()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001614 CheckRunVimInTerminal
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001615 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001616 let text = []
1617 for n in range(10)
1618 call add(text, repeat(n, 50))
1619 endfor
1620 call writefile(text, 'Xwinsize')
1621 let buf = RunVimInTerminal('Xwinsize', {})
1622 let win = bufwinid(buf)
1623 call assert_equal([6, 40], term_getsize(buf))
1624 call assert_equal(6, winheight(win))
1625 call assert_equal(40, winwidth(win))
1626
1627 " resizing the window doesn't resize the terminal.
1628 resize 10
1629 vertical resize 60
1630 call assert_equal([6, 40], term_getsize(buf))
1631 call assert_equal(10, winheight(win))
1632 call assert_equal(60, winwidth(win))
1633
1634 call StopVimInTerminal(buf)
1635 call delete('Xwinsize')
1636
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001637 call assert_fails('set termwinsize=40', 'E474')
1638 call assert_fails('set termwinsize=10+40', 'E474')
1639 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001640
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001641 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001642endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001643
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001644func Test_terminal_termwinsize_option_zero()
1645 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001646 let buf = Run_shell_in_terminal({})
1647 let win = bufwinid(buf)
1648 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001649 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001650 call term_wait(buf)
1651 exe buf . 'bwipe'
1652
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001653 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001654 let buf = Run_shell_in_terminal({})
1655 let win = bufwinid(buf)
1656 call assert_equal([7, winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001657 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001658 call term_wait(buf)
1659 exe buf . 'bwipe'
1660
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001661 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001662 let buf = Run_shell_in_terminal({})
1663 let win = bufwinid(buf)
1664 call assert_equal([winheight(win), 33], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001665 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001666 call term_wait(buf)
1667 exe buf . 'bwipe'
1668
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001669 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001670endfunc
1671
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001672func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001673 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001674 vsplit
1675 let buf = Run_shell_in_terminal({})
1676 let win = bufwinid(buf)
1677 call assert_inrange(10, 1000, winheight(win))
1678 call assert_inrange(50, 1000, winwidth(win))
1679 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1680
1681 resize 15
1682 vertical resize 60
1683 redraw
1684 call assert_equal([15, 60], term_getsize(buf))
1685 call assert_equal(15, winheight(win))
1686 call assert_equal(60, winwidth(win))
1687
1688 resize 7
1689 vertical resize 30
1690 redraw
1691 call assert_equal([10, 50], term_getsize(buf))
1692 call assert_equal(7, winheight(win))
1693 call assert_equal(30, winwidth(win))
1694
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001695 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001696 call term_wait(buf)
1697 exe buf . 'bwipe'
1698
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001699 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001700 let buf = Run_shell_in_terminal({})
1701 let win = bufwinid(buf)
1702 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001703 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001704 call term_wait(buf)
1705 exe buf . 'bwipe'
1706
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001707 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001708endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001709
1710func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001711 " make three tabpages, terminal in the middle
1712 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001713 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001714 tabnew
1715 tabprev
1716 call assert_equal(1, winnr('$'))
1717 call assert_equal(2, tabpagenr())
1718 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001719
1720 let buf = Run_shell_in_terminal({})
1721 let termwin = bufwinid(buf)
1722 set termwinkey=<C-L>
1723 call feedkeys("\<C-L>w", 'tx')
1724 call assert_equal(thiswin, win_getid())
1725 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001726 call assert_equal(termwin, win_getid())
1727
Bram Moolenaar997d4242019-09-14 21:23:40 +02001728 if has('langmap')
1729 set langmap=xjyk
1730 call feedkeys("\<C-L>x", 'tx')
1731 call assert_equal(thiswin, win_getid())
1732 call feedkeys("\<C-W>y", 'tx')
1733 call assert_equal(termwin, win_getid())
1734 set langmap=
1735 endif
Bram Moolenaara4b26992019-08-15 20:58:54 +02001736
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001737 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001738 call assert_equal(3, tabpagenr())
1739 tabprev
1740 call assert_equal(2, tabpagenr())
1741 call assert_equal(termwin, win_getid())
1742
1743 call feedkeys("\<C-L>gT", "xt")
1744 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001745 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001746 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001747 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001748
1749 let job = term_getjob(buf)
1750 call feedkeys("\<C-L>\<C-C>", 'tx')
1751 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001752
1753 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001754 tabnext
1755 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001756 tabprev
1757 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001758endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001759
1760func Test_terminal_out_err()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001761 CheckUnix
1762
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001763 call writefile([
1764 \ '#!/bin/sh',
1765 \ 'echo "this is standard error" >&2',
1766 \ 'echo "this is standard out" >&1',
1767 \ ], 'Xechoerrout.sh')
1768 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1769
1770 let outfile = 'Xtermstdout'
1771 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001772
1773 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1774 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001775 call assert_equal('this is standard error', term_getline(buf, 1))
1776
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001777 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001778 exe buf . 'bwipe'
1779 call delete('Xechoerrout.sh')
1780 call delete(outfile)
1781endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001782
1783func Test_terminwinscroll()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001784 CheckUnix
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001785
1786 " Let the terminal output more than 'termwinscroll' lines, some at the start
1787 " will be dropped.
1788 exe 'set termwinscroll=' . &lines
1789 let buf = term_start('/bin/sh')
1790 for i in range(1, &lines)
1791 call feedkeys("echo " . i . "\<CR>", 'xt')
1792 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1793 endfor
1794 " Go to Terminal-Normal mode to update the buffer.
1795 call feedkeys("\<C-W>N", 'xt')
1796 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1797
1798 " Every "echo nr" must only appear once
1799 let lines = getline(1, line('$'))
1800 for i in range(&lines - len(lines) / 2 + 2, &lines)
1801 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1802 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1803 endfor
1804
1805 exe buf . 'bwipe!'
1806endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001807
Bram Moolenaar875cf872018-07-08 20:49:07 +02001808" Resizing the terminal window caused an ml_get error.
1809" TODO: This does not reproduce the original problem.
1810func Test_terminal_resize()
1811 set statusline=x
1812 terminal
1813 call assert_equal(2, winnr('$'))
1814
1815 " Fill the terminal with text.
1816 if has('win32')
1817 call feedkeys("dir\<CR>", 'xt')
1818 else
1819 call feedkeys("ls\<CR>", 'xt')
1820 endif
1821 " Go to Terminal-Normal mode for a moment.
1822 call feedkeys("\<C-W>N", 'xt')
1823 " Open a new window
1824 call feedkeys("i\<C-W>n", 'xt')
1825 call assert_equal(3, winnr('$'))
1826 redraw
1827
1828 close
1829 call assert_equal(2, winnr('$'))
1830 call feedkeys("exit\<CR>", 'xt')
1831 set statusline&
1832endfunc
1833
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001834" must be nearly the last, we can't go back from GUI to terminal
1835func Test_zz1_terminal_in_gui()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001836 CheckCanRunGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001837
1838 " Ignore the "failed to create input context" error.
1839 call test_ignore_error('E285:')
1840
1841 gui -f
1842
1843 call assert_equal(1, winnr('$'))
1844 let buf = Run_shell_in_terminal({'term_finish': 'close'})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001845 call StopShellInTerminal(buf)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001846 call term_wait(buf)
1847
1848 " closing window wipes out the terminal buffer a with finished job
1849 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1850 call assert_equal("", bufname(buf))
1851
1852 unlet g:job
1853endfunc
1854
1855func Test_zz2_terminal_guioptions_bang()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001856 CheckGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001857 set guioptions+=!
1858
1859 let filename = 'Xtestscript'
1860 if has('win32')
1861 let filename .= '.bat'
1862 let prefix = ''
1863 let contents = ['@echo off', 'exit %1']
1864 else
1865 let filename .= '.sh'
1866 let prefix = './'
1867 let contents = ['#!/bin/sh', 'exit $1']
1868 endif
1869 call writefile(contents, filename)
1870 call setfperm(filename, 'rwxrwx---')
1871
1872 " Check if v:shell_error is equal to the exit status.
1873 let exitval = 0
1874 execute printf(':!%s%s %d', prefix, filename, exitval)
1875 call assert_equal(exitval, v:shell_error)
1876
1877 let exitval = 9
1878 execute printf(':!%s%s %d', prefix, filename, exitval)
1879 call assert_equal(exitval, v:shell_error)
1880
1881 set guioptions&
1882 call delete(filename)
1883endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001884
1885func Test_terminal_hidden()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001886 CheckUnix
1887
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001888 term ++hidden cat
1889 let bnr = bufnr('$')
1890 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1891 exe 'sbuf ' . bnr
1892 call assert_equal('terminal', &buftype)
1893 call term_sendkeys(bnr, "asdf\<CR>")
1894 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1895 call term_sendkeys(bnr, "\<C-D>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001896 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001897 bwipe!
1898endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001899
Bram Moolenaara4c2a242019-03-25 23:01:38 +01001900func Test_terminal_switch_mode()
1901 term
1902 let bnr = bufnr('$')
1903 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1904 call feedkeys("\<C-W>N", 'xt')
1905 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1906 call feedkeys("A", 'xt')
1907 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1908 call feedkeys("\<C-W>N", 'xt')
1909 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1910 call feedkeys("I", 'xt')
1911 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1912 call feedkeys("\<C-W>Nv", 'xt')
1913 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1914 call feedkeys("I", 'xt')
1915 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1916 call feedkeys("\<C-W>Nv", 'xt')
1917 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1918 call feedkeys("A", 'xt')
1919 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1920 bwipe!
1921endfunc
1922
Bram Moolenaara3817732019-10-16 16:31:44 +02001923func Test_terminal_normal_mode()
1924 CheckRunVimInTerminal
1925
1926 " Run Vim in a terminal and open a terminal window to run Vim in.
1927 let lines =<< trim END
1928 call setline(1, range(11111, 11122))
1929 3
1930 END
1931 call writefile(lines, 'XtermNormal')
1932 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
1933 call term_wait(buf)
1934
1935 call term_sendkeys(buf, "\<C-W>N")
1936 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
1937 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
1938
1939 call term_sendkeys(buf, ":set culopt=number\r")
1940 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
1941
1942 call term_sendkeys(buf, ":set culopt=line\r")
1943 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
1944
1945 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
1946 call StopVimInTerminal(buf)
1947 call delete('XtermNormal')
1948endfunc
1949
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001950func Test_terminal_hidden_and_close()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001951 CheckUnix
1952
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001953 call assert_equal(1, winnr('$'))
1954 term ++hidden ++close ls
1955 let bnr = bufnr('$')
1956 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1957 call WaitForAssert({-> assert_false(bufexists(bnr))})
1958 call assert_equal(1, winnr('$'))
1959endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001960
1961func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001962 " This test does not pass through ConPTY.
1963 if has('conpty')
1964 return
1965 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001966 let contents = [
1967 \ [ 'One', '', 'X' ],
1968 \ [ 'Two', '', '' ],
1969 \ [ 'Three' ] + repeat([''], 30)
1970 \ ]
1971
1972 for c in contents
1973 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01001974 if has('win32')
1975 term cmd /c type Xfile
1976 else
1977 term cat Xfile
1978 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001979 let bnr = bufnr('$')
1980 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1981 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01001982 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001983 call assert_equal(c, getline(1, line('$')))
1984 quit
1985 endfor
1986
1987 call delete('Xfile')
1988endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01001989
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01001990func Test_terminal_no_job()
1991 let term = term_start('false', {'term_finish': 'close'})
1992 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
1993endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01001994
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02001995func Test_term_getcursor()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001996 CheckUnix
1997
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02001998 let buf = Run_shell_in_terminal({})
1999
2000 " Wait for the shell to display a prompt.
2001 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2002
2003 " Hide the cursor.
2004 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
2005 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
2006
2007 " Show the cursor.
2008 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002009 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002010
2011 " Change color of cursor.
2012 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
2013 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
2014 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
2015 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
2016 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
2017
2018 " Make cursor a blinking block.
2019 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
2020 call WaitForAssert({-> assert_equal([1, 1],
2021 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2022
2023 " Make cursor a steady block.
2024 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
2025 call WaitForAssert({-> assert_equal([0, 1],
2026 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2027
2028 " Make cursor a blinking underline.
2029 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
2030 call WaitForAssert({-> assert_equal([1, 2],
2031 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2032
2033 " Make cursor a steady underline.
2034 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
2035 call WaitForAssert({-> assert_equal([0, 2],
2036 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2037
2038 " Make cursor a blinking vertical bar.
2039 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
2040 call WaitForAssert({-> assert_equal([1, 3],
2041 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2042
2043 " Make cursor a steady vertical bar.
2044 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
2045 call WaitForAssert({-> assert_equal([0, 3],
2046 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2047
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02002048 call StopShellInTerminal(buf)
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002049endfunc
2050
Bram Moolenaar1e115362019-01-09 23:01:02 +01002051func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01002052 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002053 " and for a non-existing buffer.
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002054 call assert_equal('', bufnr('%')->term_gettitle())
Bram Moolenaar1e115362019-01-09 23:01:02 +01002055 call assert_equal('', term_gettitle(bufnr('$') + 1))
2056
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002057 if !has('title') || &title == 0 || empty(&t_ts)
2058 throw "Skipped: can't get/set title"
2059 endif
2060
Bram Moolenaar1e115362019-01-09 23:01:02 +01002061 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002062 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002063 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
2064 call term_sendkeys(term, ":e Xfoo\r")
2065 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002066 else
2067 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002068 call term_sendkeys(term, ":e Xfoo\r")
2069 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002070 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01002071
Bram Moolenaar1e115362019-01-09 23:01:02 +01002072 call term_sendkeys(term, ":set titlestring=foo\r")
2073 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2074
2075 exe term . 'bwipe!'
2076endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002077
2078" When drawing the statusline the cursor position may not have been updated
2079" yet.
2080" 1. create a terminal, make it show 2 lines
2081" 2. 0.5 sec later: leave terminal window, execute "i"
2082" 3. 0.5 sec later: clear terminal window, now it's 1 line
2083" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2084" 4. 0.5 sec later: should be done, clean up
2085func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002086 CheckUnix
2087
Bram Moolenaar10772302019-01-20 18:25:54 +01002088 set statusline=x
2089 terminal
2090 let tbuf = bufnr('')
2091 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2092 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2093 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2094 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2095
2096 sleep 2
2097 exe tbuf . 'bwipe!'
2098 au! BufLeave
2099 set statusline=
2100endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002101
2102func Test_terminal_getwinpos()
Bram Moolenaarc2585492019-09-22 21:29:53 +02002103 CheckRunVimInTerminal
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002104
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002105 " split, go to the bottom-right window
2106 split
2107 wincmd j
2108 set splitright
2109
2110 call writefile([
2111 \ 'echo getwinpos()',
2112 \ ], 'XTest_getwinpos')
2113 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
2114 call term_wait(buf)
2115
2116 " Find the output of getwinpos() in the bottom line.
2117 let rows = term_getsize(buf)[0]
2118 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2119 let line = term_getline(buf, rows)
2120 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2121 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2122
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002123 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002124 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002125 " In the GUI it can be more, let's assume a 20 x 14 cell.
2126 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002127 let [xroot, yroot] = getwinpos()
Bram Moolenaar5d69fdb2019-08-31 19:13:58 +02002128 let winpos = 50->getwinpos()
2129 call assert_equal(xroot, winpos[0])
2130 call assert_equal(yroot, winpos[1])
Bram Moolenaar1b557972019-04-09 21:17:32 +02002131 let [winrow, wincol] = win_screenpos('.')
2132 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2133 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2134 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2135 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002136
2137 call term_wait(buf)
2138 call term_sendkeys(buf, ":q\<CR>")
2139 call StopVimInTerminal(buf)
2140 call delete('XTest_getwinpos')
2141 exe buf . 'bwipe!'
2142 set splitright&
2143 only!
2144endfunc
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002145
2146func Test_terminal_altscreen()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002147 " somehow doesn't work on MS-Windows
2148 CheckUnix
2149 let cmd = "cat Xtext\<CR>"
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002150
2151 let buf = term_start(&shell, {})
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002152 call writefile(["\<Esc>[?1047h"], 'Xtext')
2153 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002154 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
2155
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002156 call writefile(["\<Esc>[?1047l"], 'Xtext')
2157 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002158 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002159
2160 call term_sendkeys(buf, "exit\r")
2161 exe buf . "bwipe!"
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002162 call delete('Xtext')
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002163endfunc
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002164
2165func Test_terminal_setapi_and_call()
2166 if !CanRunVimInTerminal()
2167 return
2168 endif
2169
2170 call WriteApiCall('Tapi_TryThis')
2171 call ch_logfile('Xlog', 'w')
2172
2173 unlet! g:called_bufnum
2174 unlet! g:called_arg
2175
2176 let buf = RunVimInTerminal('-S Xscript', {'term_api': 0})
2177 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2178 call assert_false(exists('g:called_bufnum'))
2179 call assert_false(exists('g:called_arg'))
2180
2181 call term_setapi(buf, 'Tapi_TryThis')
2182 call term_sendkeys(buf, ":set notitle\<CR>")
2183 call term_sendkeys(buf, ":source Xscript\<CR>")
2184 call WaitFor({-> exists('g:called_bufnum')})
2185 call assert_equal(buf, g:called_bufnum)
2186 call assert_equal(['hello', 123], g:called_arg)
2187 call StopVimInTerminal(buf)
2188
2189 call delete('Xscript')
2190 call ch_logfile('')
2191 call delete('Xlog')
2192 unlet! g:called_bufnum
2193 unlet! g:called_arg
2194endfunc