Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 1 | " Tests for the +clientserver feature. |
| 2 | |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 3 | source check.vim |
| 4 | CheckFeature job |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 5 | |
| 6 | if !has('clientserver') |
| 7 | call assert_fails('call remote_startserver("local")', 'E942:') |
| 8 | endif |
| 9 | |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 10 | CheckFeature clientserver |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 11 | |
| 12 | source shared.vim |
| 13 | |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 14 | func Check_X11_Connection() |
Bram Moolenaar | a683ec4 | 2017-03-25 20:14:34 +0100 | [diff] [blame] | 15 | if has('x11') |
Bram Moolenaar | d6fa7bd | 2021-07-05 14:10:04 +0200 | [diff] [blame] | 16 | CheckEnv DISPLAY |
Bram Moolenaar | a2845b8 | 2017-03-25 15:20:06 +0100 | [diff] [blame] | 17 | try |
| 18 | call remote_send('xxx', '') |
| 19 | catch |
| 20 | if v:exception =~ 'E240:' |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 21 | throw 'Skipped: no connection to the X server' |
Bram Moolenaar | a2845b8 | 2017-03-25 15:20:06 +0100 | [diff] [blame] | 22 | endif |
| 23 | " ignore other errors |
| 24 | endtry |
| 25 | endif |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 26 | endfunc |
| 27 | |
| 28 | func Test_client_server() |
| 29 | let cmd = GetVimCommand() |
| 30 | if cmd == '' |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 31 | throw 'GetVimCommand() failed' |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 32 | endif |
| 33 | call Check_X11_Connection() |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 34 | |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 35 | let name = 'XVIMTEST' |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 36 | let cmd .= ' --servername ' . name |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 37 | let job = job_start(cmd, {'stoponexit': 'kill', 'out_io': 'null'}) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 38 | call WaitForAssert({-> assert_equal("run", job_status(job))}) |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 39 | |
| 40 | " Takes a short while for the server to be active. |
Bram Moolenaar | 9d18961 | 2017-09-09 18:11:00 +0200 | [diff] [blame] | 41 | " When using valgrind it takes much longer. |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 42 | call WaitForAssert({-> assert_match(name, serverlist())}) |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 43 | |
Bram Moolenaar | c5cf369 | 2021-03-20 22:16:56 +0100 | [diff] [blame] | 44 | if !has('win32') |
| 45 | if RunVim([], [], '--serverlist >Xtest_serverlist') |
| 46 | let lines = readfile('Xtest_serverlist') |
| 47 | call assert_true(index(lines, 'XVIMTEST') >= 0) |
| 48 | endif |
| 49 | call delete('Xtest_serverlist') |
| 50 | endif |
| 51 | |
Bram Moolenaar | a0d1fef | 2019-09-04 22:29:14 +0200 | [diff] [blame] | 52 | eval name->remote_foreground() |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 53 | |
| 54 | call remote_send(name, ":let testvar = 'yes'\<CR>") |
Bram Moolenaar | d97fbf1 | 2017-11-02 19:23:03 +0100 | [diff] [blame] | 55 | call WaitFor('remote_expr("' . name . '", "exists(\"testvar\") ? testvar : \"\"", "", 1) == "yes"') |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 56 | call assert_equal('yes', remote_expr(name, "testvar", "", 2)) |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 57 | call assert_fails("let x=remote_expr(name, '2+x')", 'E449:') |
| 58 | call assert_fails("let x=remote_expr('[], '2+2')", 'E116:') |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 59 | |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 60 | if has('unix') && has('gui') && !has('gui_running') |
Bram Moolenaar | 60964f6 | 2017-06-10 17:06:16 +0200 | [diff] [blame] | 61 | " Running in a terminal and the GUI is available: Tell the server to open |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 62 | " the GUI and check that the remote command still works. |
| 63 | " Need to wait for the GUI to start up, otherwise the send hangs in trying |
| 64 | " to send to the terminal window. |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 65 | if has('gui_athena') || has('gui_motif') |
| 66 | " For those GUIs, ignore the 'failed to create input context' error. |
| 67 | call remote_send(name, ":call test_ignore_error('E285') | gui -f\<CR>") |
| 68 | else |
| 69 | call remote_send(name, ":gui -f\<CR>") |
| 70 | endif |
| 71 | " Wait for the server to be up and answering requests. |
Bram Moolenaar | 18dc355 | 2020-11-22 14:24:00 +0100 | [diff] [blame] | 72 | " When using valgrind this can be very, very slow. |
| 73 | sleep 1 |
| 74 | call WaitForAssert({-> assert_match('\d', name->remote_expr("v:version", "", 1))}, 10000) |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 75 | |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 76 | call remote_send(name, ":let testvar = 'maybe'\<CR>") |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 77 | call WaitForAssert({-> assert_equal('maybe', remote_expr(name, "testvar", "", 2))}) |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 78 | endif |
| 79 | |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 80 | call assert_fails('call remote_send("XXX", ":let testvar = ''yes''\<CR>")', 'E241:') |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 81 | |
Bram Moolenaar | e4862a0 | 2020-12-27 19:00:24 +0100 | [diff] [blame] | 82 | call writefile(['one'], 'Xclientfile') |
| 83 | let cmd = GetVimProg() .. ' --servername ' .. name .. ' --remote Xclientfile' |
| 84 | call system(cmd) |
| 85 | call WaitForAssert({-> assert_equal('Xclientfile', remote_expr(name, "bufname()", "", 2))}) |
| 86 | call WaitForAssert({-> assert_equal('one', remote_expr(name, "getline(1)", "", 2))}) |
| 87 | call writefile(['one', 'two'], 'Xclientfile') |
| 88 | call system(cmd) |
| 89 | call WaitForAssert({-> assert_equal('two', remote_expr(name, "getline(2)", "", 2))}) |
Bram Moolenaar | 48e11c1 | 2021-01-11 18:47:00 +0100 | [diff] [blame] | 90 | call delete('Xclientfile') |
Bram Moolenaar | e4862a0 | 2020-12-27 19:00:24 +0100 | [diff] [blame] | 91 | |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 92 | " Expression evaluated locally. |
| 93 | if v:servername == '' |
Bram Moolenaar | a0d1fef | 2019-09-04 22:29:14 +0200 | [diff] [blame] | 94 | eval 'MYSELF'->remote_startserver() |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 95 | " May get MYSELF1 when running the test again. |
| 96 | call assert_match('MYSELF', v:servername) |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 97 | call assert_fails("call remote_startserver('MYSELF')", 'E941:') |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 98 | endif |
| 99 | let g:testvar = 'myself' |
| 100 | call assert_equal('myself', remote_expr(v:servername, 'testvar')) |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 101 | call remote_send(v:servername, ":let g:testvar2 = 75\<CR>") |
| 102 | call feedkeys('', 'x') |
| 103 | call assert_equal(75, g:testvar2) |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 104 | call assert_fails('let v = remote_expr(v:servername, "/2")', ['E15:.*/2']) |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 105 | |
| 106 | call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid') |
Bram Moolenaar | a0d1fef | 2019-09-04 22:29:14 +0200 | [diff] [blame] | 107 | call assert_equal('got it', g:myserverid->remote_read(2)) |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 108 | |
Bram Moolenaar | 196b466 | 2019-09-06 21:34:30 +0200 | [diff] [blame] | 109 | call remote_send(name, ":eval expand('<client>')->server2client('another')\<CR>", 'g:myserverid') |
Bram Moolenaar | 6caf606 | 2017-03-18 20:45:05 +0100 | [diff] [blame] | 110 | let peek_result = 'nothing' |
Bram Moolenaar | a0d1fef | 2019-09-04 22:29:14 +0200 | [diff] [blame] | 111 | let r = g:myserverid->remote_peek('peek_result') |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 112 | " unpredictable whether the result is already available. |
Bram Moolenaar | 6caf606 | 2017-03-18 20:45:05 +0100 | [diff] [blame] | 113 | if r > 0 |
| 114 | call assert_equal('another', peek_result) |
| 115 | elseif r == 0 |
| 116 | call assert_equal('nothing', peek_result) |
| 117 | else |
| 118 | call assert_report('remote_peek() failed') |
| 119 | endif |
| 120 | let g:peek_result = 'empty' |
| 121 | call WaitFor('remote_peek(g:myserverid, "g:peek_result") > 0') |
Bram Moolenaar | 6caf606 | 2017-03-18 20:45:05 +0100 | [diff] [blame] | 122 | call assert_equal('another', g:peek_result) |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 123 | call assert_equal('another', remote_read(g:myserverid, 2)) |
Bram Moolenaar | 6caf606 | 2017-03-18 20:45:05 +0100 | [diff] [blame] | 124 | |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 125 | if !has('gui_running') |
| 126 | " In GUI vim, the following tests display a dialog box |
| 127 | |
| 128 | let cmd = GetVimProg() .. ' --servername ' .. name |
| 129 | |
| 130 | " Run a separate instance to send a command to the server |
| 131 | call remote_expr(name, 'execute("only")') |
| 132 | call system(cmd .. ' --remote-send ":new Xfile<CR>"') |
| 133 | call assert_equal('2', remote_expr(name, 'winnr("$")')) |
| 134 | call assert_equal('Xfile', remote_expr(name, 'winbufnr(1)->bufname()')) |
| 135 | call remote_expr(name, 'execute("only")') |
| 136 | |
| 137 | " Invoke a remote-expr. On MS-Windows, the returned value has a carriage |
| 138 | " return. |
| 139 | let l = system(cmd .. ' --remote-expr "2 + 2"') |
| 140 | call assert_equal(['4'], split(l, "\n")) |
| 141 | |
| 142 | " Edit multiple files using --remote |
| 143 | call system(cmd .. ' --remote Xfile1 Xfile2 Xfile3') |
Bram Moolenaar | 41a834d | 2020-12-28 21:15:16 +0100 | [diff] [blame] | 144 | call assert_match(".*Xfile1\n.*Xfile2\n.*Xfile3\n", remote_expr(name, 'argv()')) |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 145 | eval name->remote_send(":%bw!\<CR>") |
| 146 | |
| 147 | " Edit files in separate tab pages |
| 148 | call system(cmd .. ' --remote-tab Xfile1 Xfile2 Xfile3') |
Bram Moolenaar | 25d5700 | 2020-04-08 22:56:34 +0200 | [diff] [blame] | 149 | call WaitForAssert({-> assert_equal('3', remote_expr(name, 'tabpagenr("$")'))}) |
Bram Moolenaar | 41a834d | 2020-12-28 21:15:16 +0100 | [diff] [blame] | 150 | call assert_match('.*\<Xfile2', remote_expr(name, 'bufname(tabpagebuflist(2)[0])')) |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 151 | eval name->remote_send(":%bw!\<CR>") |
| 152 | |
| 153 | " Edit a file using --remote-wait |
| 154 | eval name->remote_send(":source $VIMRUNTIME/plugin/rrhelper.vim\<CR>") |
| 155 | call system(cmd .. ' --remote-wait +enew Xfile1') |
Bram Moolenaar | 41a834d | 2020-12-28 21:15:16 +0100 | [diff] [blame] | 156 | call assert_match('.*\<Xfile1', remote_expr(name, 'bufname("#")')) |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 157 | eval name->remote_send(":%bw!\<CR>") |
| 158 | |
| 159 | " Edit files using --remote-tab-wait |
| 160 | call system(cmd .. ' --remote-tabwait +tabonly\|enew Xfile1 Xfile2') |
| 161 | call assert_equal('1', remote_expr(name, 'tabpagenr("$")')) |
| 162 | eval name->remote_send(":%bw!\<CR>") |
| 163 | |
| 164 | " Error cases |
| 165 | if v:lang == "C" || v:lang =~ '^[Ee]n' |
| 166 | let l = split(system(cmd .. ' --remote +pwd'), "\n") |
| 167 | call assert_equal("Argument missing after: \"+pwd\"", l[1]) |
| 168 | endif |
| 169 | let l = system(cmd .. ' --remote-expr "abcd"') |
| 170 | call assert_match('^E449: ', l) |
| 171 | endif |
| 172 | |
| 173 | eval name->remote_send(":%bw!\<CR>") |
Bram Moolenaar | a0d1fef | 2019-09-04 22:29:14 +0200 | [diff] [blame] | 174 | eval name->remote_send(":qa!\<CR>") |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 175 | try |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 176 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 177 | finally |
| 178 | if job_status(job) != 'dead' |
| 179 | call assert_report('Server did not exit') |
| 180 | call job_stop(job, 'kill') |
| 181 | endif |
| 182 | endtry |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 183 | |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 184 | call assert_fails('call remote_startserver([])', 'E730:') |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 185 | call assert_fails("let x = remote_peek([])", 'E730:') |
Bram Moolenaar | 4d57ba0 | 2020-07-12 20:00:24 +0200 | [diff] [blame] | 186 | call assert_fails("let x = remote_read('vim10')", |
Bram Moolenaar | c212dd0 | 2020-07-12 20:49:29 +0200 | [diff] [blame] | 187 | \ has('unix') ? ['E573:.*vim10'] : 'E277:') |
Bram Moolenaar | 4d57ba0 | 2020-07-12 20:00:24 +0200 | [diff] [blame] | 188 | call assert_fails("call server2client('abc', 'xyz')", |
Bram Moolenaar | c212dd0 | 2020-07-12 20:49:29 +0200 | [diff] [blame] | 189 | \ has('unix') ? ['E573:.*abc'] : 'E258:') |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 190 | endfunc |
| 191 | |
| 192 | " Uncomment this line to get a debugging log |
| 193 | " call ch_logfile('channellog', 'w') |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 194 | |
| 195 | " vim: shiftwidth=2 sts=2 expandtab |