2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local assert_log = t.assert_log
|
2024-04-20 08:44:13 -07:00
|
|
|
local eq, neq, eval = t.eq, t.neq, n.eval
|
|
|
|
local clear, fn, api = n.clear, n.fn, n.api
|
2024-04-08 02:03:20 -07:00
|
|
|
local matches = t.matches
|
|
|
|
local pcall_err = t.pcall_err
|
2024-04-20 08:44:13 -07:00
|
|
|
local check_close = n.check_close
|
2024-04-08 02:03:20 -07:00
|
|
|
local mkdir = t.mkdir
|
2024-04-20 08:44:13 -07:00
|
|
|
local rmdir = n.rmdir
|
2024-04-08 02:03:20 -07:00
|
|
|
local is_os = t.is_os
|
2015-04-02 08:49:22 -07:00
|
|
|
|
2023-01-16 12:43:13 -07:00
|
|
|
local testlog = 'Xtest-server-log'
|
|
|
|
|
2017-05-11 06:46:13 -07:00
|
|
|
local function clear_serverlist()
|
2024-01-12 10:59:57 -07:00
|
|
|
for _, server in pairs(fn.serverlist()) do
|
|
|
|
fn.serverstop(server)
|
2018-04-08 08:20:25 -07:00
|
|
|
end
|
2017-05-11 06:46:13 -07:00
|
|
|
end
|
|
|
|
|
2018-04-08 04:06:26 -07:00
|
|
|
describe('server', function()
|
2023-01-16 12:43:13 -07:00
|
|
|
after_each(function()
|
2024-04-05 20:18:43 -07:00
|
|
|
check_close()
|
2023-01-16 12:43:13 -07:00
|
|
|
os.remove(testlog)
|
|
|
|
end)
|
|
|
|
|
2022-06-30 04:16:46 -07:00
|
|
|
it('serverstart() stores sockets in $XDG_RUNTIME_DIR', function()
|
|
|
|
local dir = 'Xtest_xdg_run'
|
|
|
|
mkdir(dir)
|
2024-04-06 00:19:13 -07:00
|
|
|
finally(function()
|
|
|
|
rmdir(dir)
|
|
|
|
end)
|
2022-06-30 04:16:46 -07:00
|
|
|
clear({ env = { XDG_RUNTIME_DIR = dir } })
|
2024-01-12 10:59:57 -07:00
|
|
|
matches(dir, fn.stdpath('run'))
|
2022-11-21 17:13:30 -07:00
|
|
|
if not is_os('win') then
|
2024-01-12 10:59:57 -07:00
|
|
|
matches(dir, fn.serverstart())
|
2022-06-30 04:16:46 -07:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2022-05-03 06:08:35 -07:00
|
|
|
it('serverstart(), serverstop() does not set $NVIM', function()
|
2022-06-30 04:16:46 -07:00
|
|
|
clear()
|
2015-05-09 01:25:19 -07:00
|
|
|
local s = eval('serverstart()')
|
|
|
|
assert(s ~= nil and s:len() > 0, 'serverstart() returned empty')
|
2022-05-03 06:08:35 -07:00
|
|
|
eq('', eval('$NVIM'))
|
|
|
|
eq('', eval('$NVIM_LISTEN_ADDRESS'))
|
2018-04-08 04:06:26 -07:00
|
|
|
eq(1, eval("serverstop('" .. s .. "')"))
|
2015-05-09 01:25:19 -07:00
|
|
|
eq('', eval('$NVIM_LISTEN_ADDRESS'))
|
|
|
|
end)
|
|
|
|
|
2022-05-03 06:08:35 -07:00
|
|
|
it('sets v:servername at startup or if all servers were stopped', function()
|
2022-06-30 04:16:46 -07:00
|
|
|
clear()
|
2024-01-12 10:59:57 -07:00
|
|
|
local initial_server = api.nvim_get_vvar('servername')
|
2015-05-09 01:25:19 -07:00
|
|
|
assert(initial_server ~= nil and initial_server:len() > 0, 'v:servername was not initialized')
|
|
|
|
|
|
|
|
-- v:servername is readonly so we cannot unset it--but we can test that it
|
|
|
|
-- does not get set again thereafter.
|
2024-01-12 10:59:57 -07:00
|
|
|
local s = fn.serverstart()
|
2015-05-09 01:25:19 -07:00
|
|
|
assert(s ~= nil and s:len() > 0, 'serverstart() returned empty')
|
|
|
|
neq(initial_server, s)
|
|
|
|
|
|
|
|
-- serverstop() does _not_ modify v:servername...
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(1, fn.serverstop(s))
|
|
|
|
eq(initial_server, api.nvim_get_vvar('servername'))
|
2015-05-09 01:25:19 -07:00
|
|
|
|
|
|
|
-- ...unless we stop _all_ servers.
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(1, fn.serverstop(fn.serverlist()[1]))
|
|
|
|
eq('', api.nvim_get_vvar('servername'))
|
2015-05-09 01:25:19 -07:00
|
|
|
|
2022-05-03 06:08:35 -07:00
|
|
|
-- v:servername and $NVIM take the next available server.
|
2022-11-21 17:13:30 -07:00
|
|
|
local servername = (
|
|
|
|
is_os('win') and [[\\.\pipe\Xtest-functional-server-pipe]]
|
2022-06-01 11:28:14 -07:00
|
|
|
or './Xtest-functional-server-socket'
|
|
|
|
)
|
2024-01-12 10:59:57 -07:00
|
|
|
fn.serverstart(servername)
|
|
|
|
eq(servername, api.nvim_get_vvar('servername'))
|
2022-05-03 06:08:35 -07:00
|
|
|
-- Not set in the current process, only in children.
|
|
|
|
eq('', eval('$NVIM'))
|
2015-04-02 08:49:22 -07:00
|
|
|
end)
|
|
|
|
|
2018-04-08 04:06:26 -07:00
|
|
|
it('serverstop() returns false for invalid input', function()
|
2024-09-02 06:57:07 -07:00
|
|
|
clear {
|
|
|
|
args_rm = { '--listen' },
|
|
|
|
env = {
|
|
|
|
NVIM_LOG_FILE = testlog,
|
|
|
|
NVIM_LISTEN_ADDRESS = '',
|
|
|
|
},
|
|
|
|
}
|
2018-04-08 04:06:26 -07:00
|
|
|
eq(0, eval("serverstop('')"))
|
|
|
|
eq(0, eval("serverstop('bogus-socket-name')"))
|
2023-01-16 16:12:59 -07:00
|
|
|
assert_log('Not listening on bogus%-socket%-name', testlog, 10)
|
2015-05-09 01:25:19 -07:00
|
|
|
end)
|
|
|
|
|
2022-06-01 11:28:14 -07:00
|
|
|
it('parses endpoints', function()
|
2024-09-02 06:57:07 -07:00
|
|
|
clear {
|
|
|
|
args_rm = { '--listen' },
|
|
|
|
env = {
|
|
|
|
NVIM_LOG_FILE = testlog,
|
|
|
|
NVIM_LISTEN_ADDRESS = '',
|
|
|
|
},
|
|
|
|
}
|
2017-05-11 06:46:13 -07:00
|
|
|
clear_serverlist()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({}, fn.serverlist())
|
2017-05-11 06:46:13 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local s = fn.serverstart('127.0.0.1:0') -- assign random port
|
2017-11-02 02:45:38 -07:00
|
|
|
if #s > 0 then
|
2024-03-29 18:29:21 -07:00
|
|
|
matches('127.0.0.1:%d+', s)
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(s, fn.serverlist()[1])
|
2017-11-02 02:45:38 -07:00
|
|
|
clear_serverlist()
|
|
|
|
end
|
2017-05-11 06:46:13 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
s = fn.serverstart('127.0.0.1:') -- assign random port
|
2017-11-02 02:45:38 -07:00
|
|
|
if #s > 0 then
|
2024-03-29 18:29:21 -07:00
|
|
|
matches('127.0.0.1:%d+', s)
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(s, fn.serverlist()[1])
|
2017-11-02 02:45:38 -07:00
|
|
|
clear_serverlist()
|
|
|
|
end
|
2017-05-11 06:46:13 -07:00
|
|
|
|
2017-11-02 02:45:38 -07:00
|
|
|
local expected = {}
|
|
|
|
local v4 = '127.0.0.1:12345'
|
2024-01-12 10:59:57 -07:00
|
|
|
local status, _ = pcall(fn.serverstart, v4)
|
2018-05-09 12:44:18 -07:00
|
|
|
if status then
|
2017-11-02 02:45:38 -07:00
|
|
|
table.insert(expected, v4)
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall(fn.serverstart, v4) -- exists already; ignore
|
2023-01-16 16:12:59 -07:00
|
|
|
assert_log('Failed to start server: address already in use: 127%.0%.0%.1', testlog, 10)
|
2017-11-02 02:45:38 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local v6 = '::1:12345'
|
2024-01-12 10:59:57 -07:00
|
|
|
status, _ = pcall(fn.serverstart, v6)
|
2018-05-09 12:44:18 -07:00
|
|
|
if status then
|
2017-11-02 02:45:38 -07:00
|
|
|
table.insert(expected, v6)
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall(fn.serverstart, v6) -- exists already; ignore
|
2023-01-16 16:12:59 -07:00
|
|
|
assert_log('Failed to start server: address already in use: ::1', testlog, 10)
|
2017-11-02 02:45:38 -07:00
|
|
|
end
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(expected, fn.serverlist())
|
2017-05-11 06:46:13 -07:00
|
|
|
clear_serverlist()
|
|
|
|
|
2022-06-01 11:28:14 -07:00
|
|
|
-- Address without slashes is a "name" which is appended to a generated path. #8519
|
2024-01-12 10:59:57 -07:00
|
|
|
matches([[.*[/\\]xtest1%.2%.3%.4[^/\\]*]], fn.serverstart('xtest1.2.3.4'))
|
2022-06-01 11:28:14 -07:00
|
|
|
clear_serverlist()
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('Vim:Failed to start server: invalid argument', pcall_err(fn.serverstart, '127.0.0.1:65536')) -- invalid port
|
|
|
|
eq({}, fn.serverlist())
|
2017-05-11 06:46:13 -07:00
|
|
|
end)
|
2015-05-09 01:25:19 -07:00
|
|
|
|
2018-04-08 08:20:25 -07:00
|
|
|
it('serverlist() returns the list of servers', function()
|
2022-06-30 04:16:46 -07:00
|
|
|
clear()
|
2015-04-02 08:49:22 -07:00
|
|
|
-- There should already be at least one server.
|
2024-04-20 08:44:13 -07:00
|
|
|
local _n = eval('len(serverlist())')
|
2015-04-02 08:49:22 -07:00
|
|
|
|
2018-04-08 08:20:25 -07:00
|
|
|
-- Add some servers.
|
2022-11-21 17:13:30 -07:00
|
|
|
local servs = (
|
2017-01-11 21:08:19 -07:00
|
|
|
is_os('win') and { [[\\.\pipe\Xtest-pipe0934]], [[\\.\pipe\Xtest-pipe4324]] }
|
2022-06-01 11:28:14 -07:00
|
|
|
or { [[./Xtest-pipe0934]], [[./Xtest-pipe4324]] }
|
|
|
|
)
|
2015-04-02 08:49:22 -07:00
|
|
|
for _, s in ipairs(servs) do
|
2017-01-11 21:08:19 -07:00
|
|
|
eq(s, eval("serverstart('" .. s .. "')"))
|
2015-04-02 08:49:22 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local new_servs = eval('serverlist()')
|
|
|
|
|
|
|
|
-- Exactly #servs servers should be added.
|
2024-04-20 08:44:13 -07:00
|
|
|
eq(_n + #servs, #new_servs)
|
2015-04-02 08:49:22 -07:00
|
|
|
-- The new servers should be at the end of the list.
|
|
|
|
for i = 1, #servs do
|
2024-04-20 08:44:13 -07:00
|
|
|
eq(servs[i], new_servs[i + _n])
|
2018-04-08 04:06:26 -07:00
|
|
|
eq(1, eval("serverstop('" .. servs[i] .. "')"))
|
2015-04-02 08:49:22 -07:00
|
|
|
end
|
2017-01-11 21:08:19 -07:00
|
|
|
-- After serverstop() the servers should NOT be in the list.
|
2024-04-20 08:44:13 -07:00
|
|
|
eq(_n, eval('len(serverlist())'))
|
2015-04-02 08:49:22 -07:00
|
|
|
end)
|
|
|
|
end)
|
2018-04-08 08:20:25 -07:00
|
|
|
|
|
|
|
describe('startup --listen', function()
|
|
|
|
it('validates', function()
|
|
|
|
clear()
|
|
|
|
|
2024-09-02 06:57:07 -07:00
|
|
|
-- Tests args with and without "--headless".
|
|
|
|
local function _test(args, expected)
|
|
|
|
-- XXX: clear v:shell_error, sigh...
|
|
|
|
fn.system({ n.nvim_prog, '-es', '+qall!' })
|
|
|
|
assert(0 == eval('v:shell_error'))
|
|
|
|
local cmd = vim.list_extend({ unpack(n.nvim_argv) }, vim.list_extend({ '--headless' }, args))
|
|
|
|
local output = fn.system(cmd)
|
|
|
|
assert(0 ~= eval('v:shell_error'))
|
|
|
|
-- TODO(justinmk): output not properly captured on Windows?
|
|
|
|
if is_os('win') then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
matches(expected, output)
|
2024-09-03 02:18:17 -07:00
|
|
|
matches(expected, fn.system(vim.list_extend({ unpack(n.nvim_argv) }, args)))
|
2024-09-02 06:57:07 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
_test({ '--listen' }, 'nvim.*: Argument missing after: "%-%-listen"')
|
|
|
|
_test({ '--listen2' }, 'nvim.*: Garbage after option argument: "%-%-listen2"')
|
|
|
|
_test({ '--listen', n.eval('v:servername') }, 'nvim.*: Failed to %-%-listen: ".* already .*"')
|
|
|
|
_test({ '--listen', '/' }, 'nvim.*: Failed to %-%-listen: ".*"')
|
|
|
|
_test(
|
|
|
|
{ '--listen', 'https://example.com' },
|
|
|
|
('nvim.*: Failed to %%-%%-listen: "%s"'):format(
|
|
|
|
(is_os('mac') or is_os('win')) and 'unknown node or service'
|
|
|
|
or 'service not available for socket type'
|
|
|
|
)
|
|
|
|
)
|
2018-04-08 08:20:25 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('sets v:servername, overrides $NVIM_LISTEN_ADDRESS', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
local addr = (is_os('win') and [[\\.\pipe\Xtest-listen-pipe]] or './Xtest-listen-pipe')
|
2022-06-01 11:28:14 -07:00
|
|
|
clear({ env = { NVIM_LISTEN_ADDRESS = './Xtest-env-pipe' }, args = { '--listen', addr } })
|
2024-09-02 06:57:07 -07:00
|
|
|
eq('', eval('$NVIM_LISTEN_ADDRESS')) -- Cleared on startup.
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(addr, api.nvim_get_vvar('servername'))
|
2022-06-01 11:28:14 -07:00
|
|
|
|
|
|
|
-- Address without slashes is a "name" which is appended to a generated path. #8519
|
|
|
|
clear({ args = { '--listen', 'test-name' } })
|
2024-01-12 10:59:57 -07:00
|
|
|
matches([[.*[/\\]test%-name[^/\\]*]], api.nvim_get_vvar('servername'))
|
2018-04-08 08:20:25 -07:00
|
|
|
end)
|
|
|
|
end)
|