blob: aebbc2f6ec5869a3a768bcf29b62d97f2a492b84 [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
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02005
6if !has('clientserver')
7 call assert_fails('call remote_startserver("local")', 'E942:')
8endif
9
Bram Moolenaar8c5a2782019-08-07 23:07:07 +020010CheckFeature clientserver
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010011
12source shared.vim
13
Bram Moolenaarcde0ff32020-04-04 14:00:39 +020014func Check_X11_Connection()
Bram Moolenaara683ec42017-03-25 20:14:34 +010015 if has('x11')
Bram Moolenaard6fa7bd2021-07-05 14:10:04 +020016 CheckEnv DISPLAY
Bram Moolenaara2845b82017-03-25 15:20:06 +010017 try
18 call remote_send('xxx', '')
19 catch
20 if v:exception =~ 'E240:'
Bram Moolenaarcde0ff32020-04-04 14:00:39 +020021 throw 'Skipped: no connection to the X server'
Bram Moolenaara2845b82017-03-25 15:20:06 +010022 endif
23 " ignore other errors
24 endtry
25 endif
Bram Moolenaarcde0ff32020-04-04 14:00:39 +020026endfunc
27
28func Test_client_server()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +010029 let g:test_is_flaky = 1
Bram Moolenaarcde0ff32020-04-04 14:00:39 +020030 let cmd = GetVimCommand()
31 if cmd == ''
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020032 throw 'GetVimCommand() failed'
Bram Moolenaarcde0ff32020-04-04 14:00:39 +020033 endif
34 call Check_X11_Connection()
Bram Moolenaar42205552017-03-18 19:42:22 +010035
Bram Moolenaar42205552017-03-18 19:42:22 +010036 let name = 'XVIMTEST'
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010037 let cmd .= ' --servername ' . name
Bram Moolenaarab8b1c12017-11-04 19:24:31 +010038 let job = job_start(cmd, {'stoponexit': 'kill', 'out_io': 'null'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +020039 call WaitForAssert({-> assert_equal("run", job_status(job))})
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010040
41 " Takes a short while for the server to be active.
Bram Moolenaar9d189612017-09-09 18:11:00 +020042 " When using valgrind it takes much longer.
Bram Moolenaar50182fa2018-04-28 21:34:40 +020043 call WaitForAssert({-> assert_match(name, serverlist())})
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010044
Bram Moolenaarc5cf3692021-03-20 22:16:56 +010045 if !has('win32')
46 if RunVim([], [], '--serverlist >Xtest_serverlist')
47 let lines = readfile('Xtest_serverlist')
48 call assert_true(index(lines, 'XVIMTEST') >= 0)
49 endif
50 call delete('Xtest_serverlist')
51 endif
52
Bram Moolenaara0d1fef2019-09-04 22:29:14 +020053 eval name->remote_foreground()
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010054
55 call remote_send(name, ":let testvar = 'yes'\<CR>")
Bram Moolenaard97fbf12017-11-02 19:23:03 +010056 call WaitFor('remote_expr("' . name . '", "exists(\"testvar\") ? testvar : \"\"", "", 1) == "yes"')
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010057 call assert_equal('yes', remote_expr(name, "testvar", "", 2))
Bram Moolenaarca68ae12020-03-30 19:32:53 +020058 call assert_fails("let x=remote_expr(name, '2+x')", 'E449:')
59 call assert_fails("let x=remote_expr('[], '2+2')", 'E116:')
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010060
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010061 if has('unix') && has('gui') && !has('gui_running')
Bram Moolenaar60964f62017-06-10 17:06:16 +020062 " Running in a terminal and the GUI is available: Tell the server to open
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010063 " the GUI and check that the remote command still works.
64 " Need to wait for the GUI to start up, otherwise the send hangs in trying
65 " to send to the terminal window.
Bram Moolenaar0b962e52022-04-03 18:02:37 +010066 if has('gui_motif')
67 " For this GUI ignore the 'failed to create input context' error.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010068 call remote_send(name, ":call test_ignore_error('E285') | gui -f\<CR>")
69 else
70 call remote_send(name, ":gui -f\<CR>")
71 endif
72 " Wait for the server to be up and answering requests.
Bram Moolenaar18dc3552020-11-22 14:24:00 +010073 " When using valgrind this can be very, very slow.
74 sleep 1
75 call WaitForAssert({-> assert_match('\d', name->remote_expr("v:version", "", 1))}, 10000)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010076
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010077 call remote_send(name, ":let testvar = 'maybe'\<CR>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +020078 call WaitForAssert({-> assert_equal('maybe', remote_expr(name, "testvar", "", 2))})
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010079 endif
80
Bram Moolenaare2e40752020-09-04 21:18:46 +020081 call assert_fails('call remote_send("XXX", ":let testvar = ''yes''\<CR>")', 'E241:')
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010082
Bram Moolenaare4862a02020-12-27 19:00:24 +010083 call writefile(['one'], 'Xclientfile')
84 let cmd = GetVimProg() .. ' --servername ' .. name .. ' --remote Xclientfile'
85 call system(cmd)
86 call WaitForAssert({-> assert_equal('Xclientfile', remote_expr(name, "bufname()", "", 2))})
87 call WaitForAssert({-> assert_equal('one', remote_expr(name, "getline(1)", "", 2))})
88 call writefile(['one', 'two'], 'Xclientfile')
89 call system(cmd)
90 call WaitForAssert({-> assert_equal('two', remote_expr(name, "getline(2)", "", 2))})
Bram Moolenaar48e11c12021-01-11 18:47:00 +010091 call delete('Xclientfile')
Bram Moolenaare4862a02020-12-27 19:00:24 +010092
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010093 " Expression evaluated locally.
94 if v:servername == ''
Bram Moolenaara0d1fef2019-09-04 22:29:14 +020095 eval 'MYSELF'->remote_startserver()
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010096 " May get MYSELF1 when running the test again.
97 call assert_match('MYSELF', v:servername)
Bram Moolenaarca68ae12020-03-30 19:32:53 +020098 call assert_fails("call remote_startserver('MYSELF')", 'E941:')
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010099 endif
100 let g:testvar = 'myself'
101 call assert_equal('myself', remote_expr(v:servername, 'testvar'))
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200102 call remote_send(v:servername, ":let g:testvar2 = 75\<CR>")
103 call feedkeys('', 'x')
104 call assert_equal(75, g:testvar2)
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200105 call assert_fails('let v = remote_expr(v:servername, "/2")', ['E15:.*/2'])
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100106
107 call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid')
Bram Moolenaara0d1fef2019-09-04 22:29:14 +0200108 call assert_equal('got it', g:myserverid->remote_read(2))
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100109
Bram Moolenaar196b4662019-09-06 21:34:30 +0200110 call remote_send(name, ":eval expand('<client>')->server2client('another')\<CR>", 'g:myserverid')
Bram Moolenaar6caf6062017-03-18 20:45:05 +0100111 let peek_result = 'nothing'
Bram Moolenaara0d1fef2019-09-04 22:29:14 +0200112 let r = g:myserverid->remote_peek('peek_result')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100113 " unpredictable whether the result is already available.
Bram Moolenaar6caf6062017-03-18 20:45:05 +0100114 if r > 0
115 call assert_equal('another', peek_result)
116 elseif r == 0
117 call assert_equal('nothing', peek_result)
118 else
119 call assert_report('remote_peek() failed')
120 endif
121 let g:peek_result = 'empty'
122 call WaitFor('remote_peek(g:myserverid, "g:peek_result") > 0')
Bram Moolenaar6caf6062017-03-18 20:45:05 +0100123 call assert_equal('another', g:peek_result)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100124 call assert_equal('another', remote_read(g:myserverid, 2))
Bram Moolenaar6caf6062017-03-18 20:45:05 +0100125
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200126 if !has('gui_running')
127 " In GUI vim, the following tests display a dialog box
128
129 let cmd = GetVimProg() .. ' --servername ' .. name
130
131 " Run a separate instance to send a command to the server
132 call remote_expr(name, 'execute("only")')
133 call system(cmd .. ' --remote-send ":new Xfile<CR>"')
134 call assert_equal('2', remote_expr(name, 'winnr("$")'))
135 call assert_equal('Xfile', remote_expr(name, 'winbufnr(1)->bufname()'))
136 call remote_expr(name, 'execute("only")')
137
138 " Invoke a remote-expr. On MS-Windows, the returned value has a carriage
139 " return.
140 let l = system(cmd .. ' --remote-expr "2 + 2"')
141 call assert_equal(['4'], split(l, "\n"))
142
143 " Edit multiple files using --remote
144 call system(cmd .. ' --remote Xfile1 Xfile2 Xfile3')
Bram Moolenaar41a834d2020-12-28 21:15:16 +0100145 call assert_match(".*Xfile1\n.*Xfile2\n.*Xfile3\n", remote_expr(name, 'argv()'))
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200146 eval name->remote_send(":%bw!\<CR>")
147
148 " Edit files in separate tab pages
149 call system(cmd .. ' --remote-tab Xfile1 Xfile2 Xfile3')
Bram Moolenaar25d57002020-04-08 22:56:34 +0200150 call WaitForAssert({-> assert_equal('3', remote_expr(name, 'tabpagenr("$")'))})
Bram Moolenaar41a834d2020-12-28 21:15:16 +0100151 call assert_match('.*\<Xfile2', remote_expr(name, 'bufname(tabpagebuflist(2)[0])'))
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200152 eval name->remote_send(":%bw!\<CR>")
153
154 " Edit a file using --remote-wait
155 eval name->remote_send(":source $VIMRUNTIME/plugin/rrhelper.vim\<CR>")
156 call system(cmd .. ' --remote-wait +enew Xfile1')
Bram Moolenaar41a834d2020-12-28 21:15:16 +0100157 call assert_match('.*\<Xfile1', remote_expr(name, 'bufname("#")'))
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200158 eval name->remote_send(":%bw!\<CR>")
159
160 " Edit files using --remote-tab-wait
161 call system(cmd .. ' --remote-tabwait +tabonly\|enew Xfile1 Xfile2')
162 call assert_equal('1', remote_expr(name, 'tabpagenr("$")'))
163 eval name->remote_send(":%bw!\<CR>")
164
165 " Error cases
166 if v:lang == "C" || v:lang =~ '^[Ee]n'
167 let l = split(system(cmd .. ' --remote +pwd'), "\n")
168 call assert_equal("Argument missing after: \"+pwd\"", l[1])
169 endif
170 let l = system(cmd .. ' --remote-expr "abcd"')
171 call assert_match('^E449: ', l)
172 endif
173
174 eval name->remote_send(":%bw!\<CR>")
Bram Moolenaara0d1fef2019-09-04 22:29:14 +0200175 eval name->remote_send(":qa!\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100176 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200177 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100178 finally
179 if job_status(job) != 'dead'
180 call assert_report('Server did not exit')
181 call job_stop(job, 'kill')
182 endif
183 endtry
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100184
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200185 call assert_fails('call remote_startserver([])', 'E730:')
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200186 call assert_fails("let x = remote_peek([])", 'E730:')
Bram Moolenaar4d57ba02020-07-12 20:00:24 +0200187 call assert_fails("let x = remote_read('vim10')",
Bram Moolenaarc212dd02020-07-12 20:49:29 +0200188 \ has('unix') ? ['E573:.*vim10'] : 'E277:')
Bram Moolenaar4d57ba02020-07-12 20:00:24 +0200189 call assert_fails("call server2client('abc', 'xyz')",
Bram Moolenaarc212dd02020-07-12 20:49:29 +0200190 \ has('unix') ? ['E573:.*abc'] : 'E258:')
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100191endfunc
192
193" Uncomment this line to get a debugging log
194" call ch_logfile('channellog', 'w')
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100195
196" vim: shiftwidth=2 sts=2 expandtab