2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-09-05 05:56:00 -07:00
|
|
|
after_each(function()
|
|
|
|
check_close()
|
|
|
|
os.remove(testlog)
|
|
|
|
end)
|
2023-01-16 12:43:13 -07:00
|
|
|
|
2024-09-05 05:56:00 -07:00
|
|
|
before_each(function()
|
|
|
|
os.remove(testlog)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('server', function()
|
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)
|
|
|
|
|
2024-09-08 07:07:19 -07:00
|
|
|
it('broken $XDG_RUNTIME_DIR is not fatal #30282', function()
|
|
|
|
clear {
|
|
|
|
args_rm = { '--listen' },
|
|
|
|
env = { NVIM_LOG_FILE = testlog, XDG_RUNTIME_DIR = '/non-existent-dir/subdir//' },
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_os('win') then
|
|
|
|
-- Windows pipes have a special namespace and thus aren't decided by $XDG_RUNTIME_DIR.
|
|
|
|
matches('nvim', api.nvim_get_vvar('servername'))
|
|
|
|
else
|
|
|
|
eq('', api.nvim_get_vvar('servername'))
|
|
|
|
t.assert_log('Failed to start server%: no such file or directory', testlog, 100)
|
|
|
|
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')"))
|
2024-09-05 02:39:58 -07:00
|
|
|
t.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
|
2024-09-05 02:39:58 -07:00
|
|
|
t.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
|
2024-09-05 02:39:58 -07:00
|
|
|
t.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-09-08 12:48:32 -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()
|
2024-09-08 07:07:19 -07:00
|
|
|
-- Tests Nvim output when failing to start, with and without "--headless".
|
|
|
|
-- TODO(justinmk): clear() should have a way to get stdout if Nvim fails to start.
|
|
|
|
local function _test(args, env, expected)
|
|
|
|
local function run(cmd)
|
|
|
|
return n.exec_lua(function(cmd_, env_)
|
|
|
|
return vim
|
|
|
|
.system(cmd_, {
|
|
|
|
text = true,
|
|
|
|
env = vim.tbl_extend(
|
|
|
|
'force',
|
|
|
|
-- Avoid noise in the logs; we expect failures for these tests.
|
|
|
|
{ NVIM_LOG_FILE = testlog },
|
|
|
|
env_ or {}
|
|
|
|
),
|
|
|
|
})
|
|
|
|
:wait()
|
|
|
|
end, cmd, env) --[[@as vim.SystemCompleted]]
|
|
|
|
end
|
|
|
|
|
|
|
|
local cmd = vim.list_extend({ n.nvim_prog, '+qall!', '--headless' }, args)
|
|
|
|
local r = run(cmd)
|
|
|
|
eq(1, r.code)
|
|
|
|
matches(expected, (r.stderr .. r.stdout):gsub('\\n', ' '))
|
2018-04-08 08:20:25 -07:00
|
|
|
|
2024-09-08 07:07:19 -07:00
|
|
|
if is_os('win') then
|
|
|
|
return -- On Windows, output without --headless is garbage.
|
2024-09-02 06:57:07 -07:00
|
|
|
end
|
2024-09-08 07:07:19 -07:00
|
|
|
table.remove(cmd, 3) -- Remove '--headless'.
|
|
|
|
assert(not vim.tbl_contains(cmd, '--headless'))
|
|
|
|
r = run(cmd)
|
|
|
|
eq(1, r.code)
|
|
|
|
matches(expected, (r.stderr .. r.stdout):gsub('\\n', ' '))
|
|
|
|
end
|
|
|
|
|
|
|
|
it('validates', function()
|
|
|
|
clear { env = { NVIM_LOG_FILE = testlog } }
|
|
|
|
local in_use = n.eval('v:servername') ---@type string Address already used by another server.
|
2024-09-02 06:57:07 -07:00
|
|
|
|
2024-09-05 02:39:58 -07:00
|
|
|
t.assert_nolog('Failed to start server', testlog, 100)
|
|
|
|
t.assert_nolog('Host lookup failed', testlog, 100)
|
|
|
|
|
2024-09-08 07:07:19 -07:00
|
|
|
_test({ '--listen' }, nil, 'nvim.*: Argument missing after: "%-%-listen"')
|
|
|
|
_test({ '--listen2' }, nil, 'nvim.*: Garbage after option argument: "%-%-listen2"')
|
|
|
|
_test(
|
|
|
|
{ '--listen', in_use },
|
|
|
|
nil,
|
|
|
|
('nvim.*: Failed to %%-%%-listen: [^:]+ already [^:]+: "%s"'):format(vim.pesc(in_use))
|
|
|
|
)
|
|
|
|
_test({ '--listen', '/' }, nil, 'nvim.*: Failed to %-%-listen: [^:]+: "/"')
|
2024-09-02 06:57:07 -07:00
|
|
|
_test(
|
|
|
|
{ '--listen', 'https://example.com' },
|
2024-09-08 07:07:19 -07:00
|
|
|
nil,
|
|
|
|
('nvim.*: Failed to %%-%%-listen: %s: "https://example.com"'):format(
|
2024-09-05 02:39:58 -07:00
|
|
|
is_os('mac') and 'unknown node or service' or 'service not available for socket type'
|
2024-09-02 06:57:07 -07:00
|
|
|
)
|
|
|
|
)
|
2024-09-05 02:39:58 -07:00
|
|
|
|
|
|
|
t.assert_log('Failed to start server', testlog, 100)
|
|
|
|
t.assert_log('Host lookup failed', testlog, 100)
|
2024-09-08 07:07:19 -07:00
|
|
|
|
|
|
|
_test(
|
|
|
|
{},
|
|
|
|
{ NVIM_LISTEN_ADDRESS = in_use },
|
|
|
|
('nvim.*: Failed $NVIM_LISTEN_ADDRESS: [^:]+ already [^:]+: "%s"'):format(vim.pesc(in_use))
|
|
|
|
)
|
|
|
|
_test({}, { NVIM_LISTEN_ADDRESS = '/' }, 'nvim.*: Failed $NVIM_LISTEN_ADDRESS: [^:]+: "/"')
|
|
|
|
_test(
|
|
|
|
{},
|
|
|
|
{ NVIM_LISTEN_ADDRESS = 'https://example.com' },
|
|
|
|
('nvim.*: Failed $NVIM_LISTEN_ADDRESS: %s: "https://example.com"'):format(
|
|
|
|
is_os('mac') 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-09-08 12:48:32 -07:00
|
|
|
matches([[[/\\]test%-name[^/\\]*]], api.nvim_get_vvar('servername'))
|
2018-04-08 08:20:25 -07:00
|
|
|
end)
|
|
|
|
end)
|