blob: 8a333aa9083567a246e3b9fd8325b5557cf136b3 [file] [log] [blame]
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01001" Tests for the +clientserver feature.
2
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02003source check.vim
4CheckFeature job
5CheckFeature clientserver
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01006
7source shared.vim
8
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02009func Check_X11_Connection()
Bram Moolenaara683ec42017-03-25 20:14:34 +010010 if has('x11')
11 if empty($DISPLAY)
12 throw 'Skipped: $DISPLAY is not set'
13 endif
Bram Moolenaara2845b82017-03-25 15:20:06 +010014 try
15 call remote_send('xxx', '')
16 catch
17 if v:exception =~ 'E240:'
Bram Moolenaarcde0ff32020-04-04 14:00:39 +020018 throw 'Skipped: no connection to the X server'
Bram Moolenaara2845b82017-03-25 15:20:06 +010019 endif
20 " ignore other errors
21 endtry
22 endif
Bram Moolenaarcde0ff32020-04-04 14:00:39 +020023endfunc
24
25func Test_client_server()
26 let cmd = GetVimCommand()
27 if cmd == ''
28 return
29 endif
30 call Check_X11_Connection()
Bram Moolenaar42205552017-03-18 19:42:22 +010031
Bram Moolenaar42205552017-03-18 19:42:22 +010032 let name = 'XVIMTEST'
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010033 let cmd .= ' --servername ' . name
Bram Moolenaarab8b1c12017-11-04 19:24:31 +010034 let job = job_start(cmd, {'stoponexit': 'kill', 'out_io': 'null'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +020035 call WaitForAssert({-> assert_equal("run", job_status(job))})
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010036
37 " Takes a short while for the server to be active.
Bram Moolenaar9d189612017-09-09 18:11:00 +020038 " When using valgrind it takes much longer.
Bram Moolenaar50182fa2018-04-28 21:34:40 +020039 call WaitForAssert({-> assert_match(name, serverlist())})
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010040
Bram Moolenaara0d1fef2019-09-04 22:29:14 +020041 eval name->remote_foreground()
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010042
43 call remote_send(name, ":let testvar = 'yes'\<CR>")
Bram Moolenaard97fbf12017-11-02 19:23:03 +010044 call WaitFor('remote_expr("' . name . '", "exists(\"testvar\") ? testvar : \"\"", "", 1) == "yes"')
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010045 call assert_equal('yes', remote_expr(name, "testvar", "", 2))
Bram Moolenaarca68ae12020-03-30 19:32:53 +020046 call assert_fails("let x=remote_expr(name, '2+x')", 'E449:')
47 call assert_fails("let x=remote_expr('[], '2+2')", 'E116:')
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010048
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010049 if has('unix') && has('gui') && !has('gui_running')
Bram Moolenaar60964f62017-06-10 17:06:16 +020050 " Running in a terminal and the GUI is available: Tell the server to open
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010051 " the GUI and check that the remote command still works.
52 " Need to wait for the GUI to start up, otherwise the send hangs in trying
53 " to send to the terminal window.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010054 if has('gui_athena') || has('gui_motif')
55 " For those GUIs, ignore the 'failed to create input context' error.
56 call remote_send(name, ":call test_ignore_error('E285') | gui -f\<CR>")
57 else
58 call remote_send(name, ":gui -f\<CR>")
59 endif
60 " Wait for the server to be up and answering requests.
Bram Moolenaar60964f62017-06-10 17:06:16 +020061 sleep 100m
Bram Moolenaara0d1fef2019-09-04 22:29:14 +020062 call WaitForAssert({-> assert_true(name->remote_expr("v:version", "", 1) != "")})
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010063
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010064 call remote_send(name, ":let testvar = 'maybe'\<CR>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +020065 call WaitForAssert({-> assert_equal('maybe', remote_expr(name, "testvar", "", 2))})
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010066 endif
67
68 call assert_fails('call remote_send("XXX", ":let testvar = ''yes''\<CR>")', 'E241')
69
70 " Expression evaluated locally.
71 if v:servername == ''
Bram Moolenaara0d1fef2019-09-04 22:29:14 +020072 eval 'MYSELF'->remote_startserver()
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010073 " May get MYSELF1 when running the test again.
74 call assert_match('MYSELF', v:servername)
Bram Moolenaarca68ae12020-03-30 19:32:53 +020075 call assert_fails("call remote_startserver('MYSELF')", 'E941:')
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010076 endif
77 let g:testvar = 'myself'
78 call assert_equal('myself', remote_expr(v:servername, 'testvar'))
Bram Moolenaarcde0ff32020-04-04 14:00:39 +020079 call remote_send(v:servername, ":let g:testvar2 = 75\<CR>")
80 call feedkeys('', 'x')
81 call assert_equal(75, g:testvar2)
82 call assert_fails('let v = remote_expr(v:servername, "/2")', 'E449:')
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010083
84 call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid')
Bram Moolenaara0d1fef2019-09-04 22:29:14 +020085 call assert_equal('got it', g:myserverid->remote_read(2))
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010086
Bram Moolenaar196b4662019-09-06 21:34:30 +020087 call remote_send(name, ":eval expand('<client>')->server2client('another')\<CR>", 'g:myserverid')
Bram Moolenaar6caf6062017-03-18 20:45:05 +010088 let peek_result = 'nothing'
Bram Moolenaara0d1fef2019-09-04 22:29:14 +020089 let r = g:myserverid->remote_peek('peek_result')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +010090 " unpredictable whether the result is already available.
Bram Moolenaar6caf6062017-03-18 20:45:05 +010091 if r > 0
92 call assert_equal('another', peek_result)
93 elseif r == 0
94 call assert_equal('nothing', peek_result)
95 else
96 call assert_report('remote_peek() failed')
97 endif
98 let g:peek_result = 'empty'
99 call WaitFor('remote_peek(g:myserverid, "g:peek_result") > 0')
Bram Moolenaar6caf6062017-03-18 20:45:05 +0100100 call assert_equal('another', g:peek_result)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100101 call assert_equal('another', remote_read(g:myserverid, 2))
Bram Moolenaar6caf6062017-03-18 20:45:05 +0100102
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200103 if !has('gui_running')
104 " In GUI vim, the following tests display a dialog box
105
106 let cmd = GetVimProg() .. ' --servername ' .. name
107
108 " Run a separate instance to send a command to the server
109 call remote_expr(name, 'execute("only")')
110 call system(cmd .. ' --remote-send ":new Xfile<CR>"')
111 call assert_equal('2', remote_expr(name, 'winnr("$")'))
112 call assert_equal('Xfile', remote_expr(name, 'winbufnr(1)->bufname()'))
113 call remote_expr(name, 'execute("only")')
114
115 " Invoke a remote-expr. On MS-Windows, the returned value has a carriage
116 " return.
117 let l = system(cmd .. ' --remote-expr "2 + 2"')
118 call assert_equal(['4'], split(l, "\n"))
119
120 " Edit multiple files using --remote
121 call system(cmd .. ' --remote Xfile1 Xfile2 Xfile3')
122 call assert_equal("Xfile1\nXfile2\nXfile3\n", remote_expr(name, 'argv()'))
123 eval name->remote_send(":%bw!\<CR>")
124
125 " Edit files in separate tab pages
126 call system(cmd .. ' --remote-tab Xfile1 Xfile2 Xfile3')
127 call assert_equal('3', remote_expr(name, 'tabpagenr("$")'))
128 call assert_equal('Xfile2', remote_expr(name, 'bufname(tabpagebuflist(2)[0])'))
129 eval name->remote_send(":%bw!\<CR>")
130
131 " Edit a file using --remote-wait
132 eval name->remote_send(":source $VIMRUNTIME/plugin/rrhelper.vim\<CR>")
133 call system(cmd .. ' --remote-wait +enew Xfile1')
134 call assert_equal("Xfile1", remote_expr(name, 'bufname("#")'))
135 eval name->remote_send(":%bw!\<CR>")
136
137 " Edit files using --remote-tab-wait
138 call system(cmd .. ' --remote-tabwait +tabonly\|enew Xfile1 Xfile2')
139 call assert_equal('1', remote_expr(name, 'tabpagenr("$")'))
140 eval name->remote_send(":%bw!\<CR>")
141
142 " Error cases
143 if v:lang == "C" || v:lang =~ '^[Ee]n'
144 let l = split(system(cmd .. ' --remote +pwd'), "\n")
145 call assert_equal("Argument missing after: \"+pwd\"", l[1])
146 endif
147 let l = system(cmd .. ' --remote-expr "abcd"')
148 call assert_match('^E449: ', l)
149 endif
150
151 eval name->remote_send(":%bw!\<CR>")
Bram Moolenaara0d1fef2019-09-04 22:29:14 +0200152 eval name->remote_send(":qa!\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100153 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200154 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100155 finally
156 if job_status(job) != 'dead'
157 call assert_report('Server did not exit')
158 call job_stop(job, 'kill')
159 endif
160 endtry
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100161
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200162 call assert_fails("let x = remote_peek([])", 'E730:')
163 call assert_fails("let x = remote_read('vim10')", 'E277:')
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100164endfunc
165
166" Uncomment this line to get a debugging log
167" call ch_logfile('channellog', 'w')
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100168
169" vim: shiftwidth=2 sts=2 expandtab