Merge pull request #13227 from bfredl/earlyinspect

lua: make vim.inspect available early so it can be used for path debugging
This commit is contained in:
Björn Linse 2020-11-05 18:43:00 +01:00 committed by GitHub
commit d17e508796
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 119 additions and 103 deletions

View File

@ -55,6 +55,7 @@ set(GENERATED_UNICODE_TABLES ${GENERATED_DIR}/unicode_tables.generated.h)
set(VIM_MODULE_FILE ${GENERATED_DIR}/lua/vim_module.generated.h) set(VIM_MODULE_FILE ${GENERATED_DIR}/lua/vim_module.generated.h)
set(LUA_VIM_MODULE_SOURCE ${PROJECT_SOURCE_DIR}/src/nvim/lua/vim.lua) set(LUA_VIM_MODULE_SOURCE ${PROJECT_SOURCE_DIR}/src/nvim/lua/vim.lua)
set(LUA_SHARED_MODULE_SOURCE ${PROJECT_SOURCE_DIR}/runtime/lua/vim/shared.lua) set(LUA_SHARED_MODULE_SOURCE ${PROJECT_SOURCE_DIR}/runtime/lua/vim/shared.lua)
set(LUA_INSPECT_MODULE_SOURCE ${PROJECT_SOURCE_DIR}/runtime/lua/vim/inspect.lua)
set(CHAR_BLOB_GENERATOR ${GENERATOR_DIR}/gen_char_blob.lua) set(CHAR_BLOB_GENERATOR ${GENERATOR_DIR}/gen_char_blob.lua)
set(LINT_SUPPRESS_FILE ${PROJECT_BINARY_DIR}/errors.json) set(LINT_SUPPRESS_FILE ${PROJECT_BINARY_DIR}/errors.json)
set(LINT_SUPPRESS_URL_BASE "https://raw.githubusercontent.com/neovim/doc/gh-pages/reports/clint") set(LINT_SUPPRESS_URL_BASE "https://raw.githubusercontent.com/neovim/doc/gh-pages/reports/clint")
@ -323,10 +324,12 @@ add_custom_command(
COMMAND ${LUA_PRG} ${CHAR_BLOB_GENERATOR} ${VIM_MODULE_FILE} COMMAND ${LUA_PRG} ${CHAR_BLOB_GENERATOR} ${VIM_MODULE_FILE}
${LUA_VIM_MODULE_SOURCE} vim_module ${LUA_VIM_MODULE_SOURCE} vim_module
${LUA_SHARED_MODULE_SOURCE} shared_module ${LUA_SHARED_MODULE_SOURCE} shared_module
${LUA_INSPECT_MODULE_SOURCE} inspect_module
DEPENDS DEPENDS
${CHAR_BLOB_GENERATOR} ${CHAR_BLOB_GENERATOR}
${LUA_VIM_MODULE_SOURCE} ${LUA_VIM_MODULE_SOURCE}
${LUA_SHARED_MODULE_SOURCE} ${LUA_SHARED_MODULE_SOURCE}
${LUA_INSPECT_MODULE_SOURCE}
) )
list(APPEND NVIM_GENERATED_SOURCES list(APPEND NVIM_GENERATED_SOURCES

View File

@ -488,13 +488,29 @@ static int nlua_state_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
{ {
const char *code = (char *)&shared_module[0]; const char *code = (char *)&shared_module[0];
if (luaL_loadbuffer(lstate, code, strlen(code), "@shared.lua") if (luaL_loadbuffer(lstate, code, strlen(code), "@vim/shared.lua")
|| lua_pcall(lstate, 0, 0, 0)) { || lua_pcall(lstate, 0, 0, 0)) {
nlua_error(lstate, _("E5106: Error while creating shared module: %.*s")); nlua_error(lstate, _("E5106: Error while creating shared module: %.*s"));
return 1; return 1;
} }
} }
{
lua_getglobal(lstate, "package"); // [package]
lua_getfield(lstate, -1, "loaded"); // [package, loaded]
const char *code = (char *)&inspect_module[0];
if (luaL_loadbuffer(lstate, code, strlen(code), "@vim/inspect.lua")
|| lua_pcall(lstate, 0, 1, 0)) {
nlua_error(lstate, _("E5106: Error while creating inspect module: %.*s"));
return 1;
}
// [package, loaded, inspect]
lua_setfield(lstate, -2, "vim.inspect"); // [package, loaded]
lua_pop(lstate, 2); // []
}
{ {
const char *code = (char *)&vim_module[0]; const char *code = (char *)&vim_module[0];
if (luaL_loadbuffer(lstate, code, strlen(code), "@vim.lua") if (luaL_loadbuffer(lstate, code, strlen(code), "@vim.lua")

View File

@ -36,6 +36,9 @@
local vim = vim local vim = vim
assert(vim) assert(vim)
vim.inspect = package.loaded['vim.inspect']
assert(vim.inspect)
-- Internal-only until comments in #8107 are addressed. -- Internal-only until comments in #8107 are addressed.
-- Returns: -- Returns:
-- {errcode}, {output} -- {errcode}, {output}
@ -107,10 +110,6 @@ for s in (package.cpath..';'):gmatch('[^;]*;') do
end end
function vim._load_package(name) function vim._load_package(name)
-- tricky: when debugging this function we must let vim.inspect
-- module to be loaded first:
--local inspect = (name == "vim.inspect") and tostring or vim.inspect
local basename = name:gsub('%.', '/') local basename = name:gsub('%.', '/')
local paths = {"lua/"..basename..".lua", "lua/"..basename.."/init.lua"} local paths = {"lua/"..basename..".lua", "lua/"..basename.."/init.lua"}
for _,path in ipairs(paths) do for _,path in ipairs(paths) do
@ -260,10 +259,7 @@ end
-- These are for loading runtime modules lazily since they aren't available in -- These are for loading runtime modules lazily since they aren't available in
-- the nvim binary as specified in executor.c -- the nvim binary as specified in executor.c
local function __index(t, key) local function __index(t, key)
if key == 'inspect' then if key == 'treesitter' then
t.inspect = require('vim.inspect')
return t.inspect
elseif key == 'treesitter' then
t.treesitter = require('vim.treesitter') t.treesitter = require('vim.treesitter')
return t.treesitter return t.treesitter
elseif require('vim.uri')[key] ~= nil then elseif require('vim.uri')[key] ~= nil then

View File

@ -11,7 +11,7 @@ local eq = helpers.eq
local ok = helpers.ok local ok = helpers.ok
local eval = helpers.eval local eval = helpers.eval
local feed = helpers.feed local feed = helpers.feed
local pcall_err_withfile = helpers.pcall_err_withfile local pcall_err = helpers.pcall_err
local exec_lua = helpers.exec_lua local exec_lua = helpers.exec_lua
local matches = helpers.matches local matches = helpers.matches
local source = helpers.source local source = helpers.source
@ -119,6 +119,11 @@ describe('lua stdlib', function()
eq(1, funcs.luaeval('vim.stricmp("\\0C\\0", "\\0B\\0")')) eq(1, funcs.luaeval('vim.stricmp("\\0C\\0", "\\0B\\0")'))
end) end)
-- for brevity, match only the error header (not the traceback)
local function pcall_header(...)
return string.gsub(string.gsub(pcall_err(exec_lua, ...), '[\r\n].*', ''), '^Error executing lua: ', '')
end
it('vim.startswith', function() it('vim.startswith', function()
eq(true, funcs.luaeval('vim.startswith("123", "1")')) eq(true, funcs.luaeval('vim.startswith("123", "1")'))
eq(true, funcs.luaeval('vim.startswith("123", "")')) eq(true, funcs.luaeval('vim.startswith("123", "")'))
@ -129,8 +134,8 @@ describe('lua stdlib', function()
eq(false, funcs.luaeval('vim.startswith("123", "2")')) eq(false, funcs.luaeval('vim.startswith("123", "2")'))
eq(false, funcs.luaeval('vim.startswith("123", "1234")')) eq(false, funcs.luaeval('vim.startswith("123", "1234")'))
eq("string", type(pcall_err_withfile(funcs.luaeval, 'vim.startswith("123", nil)'))) eq("vim/shared.lua:0: prefix: expected string, got nil", pcall_header 'return vim.startswith("123", nil)')
eq("string", type(pcall_err_withfile(funcs.luaeval, 'vim.startswith(nil, "123")'))) eq("vim/shared.lua:0: s: expected string, got nil", pcall_header 'return vim.startswith(nil, "123")')
end) end)
it('vim.endswith', function() it('vim.endswith', function()
@ -143,8 +148,8 @@ describe('lua stdlib', function()
eq(false, funcs.luaeval('vim.endswith("123", "2")')) eq(false, funcs.luaeval('vim.endswith("123", "2")'))
eq(false, funcs.luaeval('vim.endswith("123", "1234")')) eq(false, funcs.luaeval('vim.endswith("123", "1234")'))
eq("string", type(pcall_err_withfile(funcs.luaeval, 'vim.endswith("123", nil)'))) eq("vim/shared.lua:0: suffix: expected string, got nil", pcall_header 'return vim.endswith("123", nil)')
eq("string", type(pcall_err_withfile(funcs.luaeval, 'vim.endswith(nil, "123")'))) eq("vim/shared.lua:0: s: expected string, got nil", pcall_header 'return vim.endswith(nil, "123")')
end) end)
it("vim.str_utfindex/str_byteindex", function() it("vim.str_utfindex/str_byteindex", function()
@ -183,10 +188,10 @@ describe('lua stdlib', function()
eq({"yy","xx"}, exec_lua("return test_table")) eq({"yy","xx"}, exec_lua("return test_table"))
-- Validates args. -- Validates args.
eq('.../helpers.lua:0: Error executing lua: vim.schedule: expected function', eq('Error executing lua: vim.schedule: expected function',
pcall_err_withfile(exec_lua, "vim.schedule('stringly')")) pcall_err(exec_lua, "vim.schedule('stringly')"))
eq('.../helpers.lua:0: Error executing lua: vim.schedule: expected function', eq('Error executing lua: vim.schedule: expected function',
pcall_err_withfile(exec_lua, "vim.schedule()")) pcall_err(exec_lua, "vim.schedule()"))
exec_lua([[ exec_lua([[
vim.schedule(function() vim.schedule(function()
@ -258,29 +263,29 @@ describe('lua stdlib', function()
} }
for _, t in ipairs(loops) do for _, t in ipairs(loops) do
matches(".*Infinite loop detected", pcall_err_withfile(split, t[1], t[2])) eq("Error executing lua: vim/shared.lua:0: Infinite loop detected", pcall_err(split, t[1], t[2]))
end end
-- Validates args. -- Validates args.
eq(true, pcall(split, 'string', 'string')) eq(true, pcall(split, 'string', 'string'))
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: shared.lua:0: s: expected string, got number Error executing lua: vim/shared.lua:0: s: expected string, got number
stack traceback: stack traceback:
shared.lua:0: in function 'gsplit' vim/shared.lua:0: in function 'gsplit'
shared.lua:0: in function <shared.lua:0>]]), vim/shared.lua:0: in function <vim/shared.lua:0>]]),
pcall_err_withfile(split, 1, 'string')) pcall_err(split, 1, 'string'))
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: shared.lua:0: sep: expected string, got number Error executing lua: vim/shared.lua:0: sep: expected string, got number
stack traceback: stack traceback:
shared.lua:0: in function 'gsplit' vim/shared.lua:0: in function 'gsplit'
shared.lua:0: in function <shared.lua:0>]]), vim/shared.lua:0: in function <vim/shared.lua:0>]]),
pcall_err_withfile(split, 'string', 1)) pcall_err(split, 'string', 1))
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: shared.lua:0: plain: expected boolean, got number Error executing lua: vim/shared.lua:0: plain: expected boolean, got number
stack traceback: stack traceback:
shared.lua:0: in function 'gsplit' vim/shared.lua:0: in function 'gsplit'
shared.lua:0: in function <shared.lua:0>]]), vim/shared.lua:0: in function <vim/shared.lua:0>]]),
pcall_err_withfile(split, 'string', 'string', 1)) pcall_err(split, 'string', 'string', 1))
end) end)
it('vim.trim', function() it('vim.trim', function()
@ -301,10 +306,10 @@ describe('lua stdlib', function()
-- Validates args. -- Validates args.
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: shared.lua:0: s: expected string, got number Error executing lua: vim/shared.lua:0: s: expected string, got number
stack traceback: stack traceback:
shared.lua:0: in function <shared.lua:0>]]), vim/shared.lua:0: in function <vim/shared.lua:0>]]),
pcall_err_withfile(trim, 2)) pcall_err(trim, 2))
end) end)
it('vim.inspect', function() it('vim.inspect', function()
@ -369,8 +374,8 @@ describe('lua stdlib', function()
return t1.f() ~= t2.f() return t1.f() ~= t2.f()
]])) ]]))
eq('.../helpers.lua:0: Error executing lua: shared.lua:0: Cannot deepcopy object of type thread', eq('Error executing lua: vim/shared.lua:0: Cannot deepcopy object of type thread',
pcall_err_withfile(exec_lua, [[ pcall_err(exec_lua, [[
local thread = coroutine.create(function () return 0 end) local thread = coroutine.create(function () return 0 end)
local t = {thr = thread} local t = {thr = thread}
vim.deepcopy(t) vim.deepcopy(t)
@ -383,10 +388,10 @@ describe('lua stdlib', function()
-- Validates args. -- Validates args.
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: shared.lua:0: s: expected string, got number Error executing lua: vim/shared.lua:0: s: expected string, got number
stack traceback: stack traceback:
shared.lua:0: in function <shared.lua:0>]]), vim/shared.lua:0: in function <vim/shared.lua:0>]]),
pcall_err_withfile(exec_lua, [[return vim.pesc(2)]])) pcall_err(exec_lua, [[return vim.pesc(2)]]))
end) end)
it('vim.tbl_keys', function() it('vim.tbl_keys', function()
@ -510,20 +515,20 @@ describe('lua stdlib', function()
return c.x.a == 1 and c.x.b == 2 and c.x.c == nil and count == 1 return c.x.a == 1 and c.x.b == 2 and c.x.c == nil and count == 1
]])) ]]))
eq('.../helpers.lua:0: Error executing lua: shared.lua:0: invalid "behavior": nil', eq('Error executing lua: vim/shared.lua:0: invalid "behavior": nil',
pcall_err_withfile(exec_lua, [[ pcall_err(exec_lua, [[
return vim.tbl_extend() return vim.tbl_extend()
]]) ]])
) )
eq('.../helpers.lua:0: Error executing lua: shared.lua:0: wrong number of arguments (given 1, expected at least 3)', eq('Error executing lua: vim/shared.lua:0: wrong number of arguments (given 1, expected at least 3)',
pcall_err_withfile(exec_lua, [[ pcall_err(exec_lua, [[
return vim.tbl_extend("keep") return vim.tbl_extend("keep")
]]) ]])
) )
eq('.../helpers.lua:0: Error executing lua: shared.lua:0: wrong number of arguments (given 2, expected at least 3)', eq('Error executing lua: vim/shared.lua:0: wrong number of arguments (given 2, expected at least 3)',
pcall_err_withfile(exec_lua, [[ pcall_err(exec_lua, [[
return vim.tbl_extend("keep", {}) return vim.tbl_extend("keep", {})
]]) ]])
) )
@ -598,20 +603,20 @@ describe('lua stdlib', function()
return vim.tbl_islist(c) and count == 0 return vim.tbl_islist(c) and count == 0
]])) ]]))
eq('.../helpers.lua:0: Error executing lua: shared.lua:0: invalid "behavior": nil', eq('Error executing lua: vim/shared.lua:0: invalid "behavior": nil',
pcall_err_withfile(exec_lua, [[ pcall_err(exec_lua, [[
return vim.tbl_deep_extend() return vim.tbl_deep_extend()
]]) ]])
) )
eq('.../helpers.lua:0: Error executing lua: shared.lua:0: wrong number of arguments (given 1, expected at least 3)', eq('Error executing lua: vim/shared.lua:0: wrong number of arguments (given 1, expected at least 3)',
pcall_err_withfile(exec_lua, [[ pcall_err(exec_lua, [[
return vim.tbl_deep_extend("keep") return vim.tbl_deep_extend("keep")
]]) ]])
) )
eq('.../helpers.lua:0: Error executing lua: shared.lua:0: wrong number of arguments (given 2, expected at least 3)', eq('Error executing lua: vim/shared.lua:0: wrong number of arguments (given 2, expected at least 3)',
pcall_err_withfile(exec_lua, [[ pcall_err(exec_lua, [[
return vim.tbl_deep_extend("keep", {}) return vim.tbl_deep_extend("keep", {})
]]) ]])
) )
@ -644,10 +649,10 @@ describe('lua stdlib', function()
it('vim.list_extend', function() it('vim.list_extend', function()
eq({1,2,3}, exec_lua [[ return vim.list_extend({1}, {2,3}) ]]) eq({1,2,3}, exec_lua [[ return vim.list_extend({1}, {2,3}) ]])
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: shared.lua:0: src: expected table, got nil Error executing lua: vim/shared.lua:0: src: expected table, got nil
stack traceback: stack traceback:
shared.lua:0: in function <shared.lua:0>]]), vim/shared.lua:0: in function <vim/shared.lua:0>]]),
pcall_err_withfile(exec_lua, [[ return vim.list_extend({1}, nil) ]])) pcall_err(exec_lua, [[ return vim.list_extend({1}, nil) ]]))
eq({1,2}, exec_lua [[ return vim.list_extend({1}, {2;a=1}) ]]) eq({1,2}, exec_lua [[ return vim.list_extend({1}, {2;a=1}) ]])
eq(true, exec_lua [[ local a = {1} return vim.list_extend(a, {2;a=1}) == a ]]) eq(true, exec_lua [[ local a = {1} return vim.list_extend(a, {2;a=1}) == a ]])
eq({2}, exec_lua [[ return vim.list_extend({}, {2;a=1}, 1) ]]) eq({2}, exec_lua [[ return vim.list_extend({}, {2;a=1}, 1) ]])
@ -670,8 +675,8 @@ describe('lua stdlib', function()
assert(vim.deep_equal(a, { A = 1; [1] = 'A'; })) assert(vim.deep_equal(a, { A = 1; [1] = 'A'; }))
vim.tbl_add_reverse_lookup(a) vim.tbl_add_reverse_lookup(a)
]] ]]
matches('.../helpers.lua:0: Error executing lua: shared.lua:0: The reverse lookup found an existing value for "[1A]" while processing key "[1A]"', matches('^Error executing lua: vim/shared%.lua:0: The reverse lookup found an existing value for "[1A]" while processing key "[1A]"$',
pcall_err_withfile(exec_lua, code)) pcall_err(exec_lua, code))
end) end)
it('vim.call, vim.fn', function() it('vim.call, vim.fn', function()
@ -843,76 +848,76 @@ describe('lua stdlib', function()
exec_lua("vim.validate{arg1={2, function(a) return (a % 2) == 0 end, 'even number' }}") exec_lua("vim.validate{arg1={2, function(a) return (a % 2) == 0 end, 'even number' }}")
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: [string "<nvim>"]:0: opt[1]: expected table, got number Error executing lua: [string "<nvim>"]:0: opt[1]: expected table, got number
stack traceback: stack traceback:
[string "<nvim>"]:0: in main chunk]]), [string "<nvim>"]:0: in main chunk]]),
pcall_err_withfile(exec_lua, "vim.validate{ 1, 'x' }")) pcall_err(exec_lua, "vim.validate{ 1, 'x' }"))
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: [string "<nvim>"]:0: invalid type name: x Error executing lua: [string "<nvim>"]:0: invalid type name: x
stack traceback: stack traceback:
[string "<nvim>"]:0: in main chunk]]), [string "<nvim>"]:0: in main chunk]]),
pcall_err_withfile(exec_lua, "vim.validate{ arg1={ 1, 'x' }}")) pcall_err(exec_lua, "vim.validate{ arg1={ 1, 'x' }}"))
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: [string "<nvim>"]:0: invalid type name: 1 Error executing lua: [string "<nvim>"]:0: invalid type name: 1
stack traceback: stack traceback:
[string "<nvim>"]:0: in main chunk]]), [string "<nvim>"]:0: in main chunk]]),
pcall_err_withfile(exec_lua, "vim.validate{ arg1={ 1, 1 }}")) pcall_err(exec_lua, "vim.validate{ arg1={ 1, 1 }}"))
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: [string "<nvim>"]:0: invalid type name: nil Error executing lua: [string "<nvim>"]:0: invalid type name: nil
stack traceback: stack traceback:
[string "<nvim>"]:0: in main chunk]]), [string "<nvim>"]:0: in main chunk]]),
pcall_err_withfile(exec_lua, "vim.validate{ arg1={ 1 }}")) pcall_err(exec_lua, "vim.validate{ arg1={ 1 }}"))
-- Validated parameters are required by default. -- Validated parameters are required by default.
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: [string "<nvim>"]:0: arg1: expected string, got nil Error executing lua: [string "<nvim>"]:0: arg1: expected string, got nil
stack traceback: stack traceback:
[string "<nvim>"]:0: in main chunk]]), [string "<nvim>"]:0: in main chunk]]),
pcall_err_withfile(exec_lua, "vim.validate{ arg1={ nil, 's' }}")) pcall_err(exec_lua, "vim.validate{ arg1={ nil, 's' }}"))
-- Explicitly required. -- Explicitly required.
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: [string "<nvim>"]:0: arg1: expected string, got nil Error executing lua: [string "<nvim>"]:0: arg1: expected string, got nil
stack traceback: stack traceback:
[string "<nvim>"]:0: in main chunk]]), [string "<nvim>"]:0: in main chunk]]),
pcall_err_withfile(exec_lua, "vim.validate{ arg1={ nil, 's', false }}")) pcall_err(exec_lua, "vim.validate{ arg1={ nil, 's', false }}"))
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: [string "<nvim>"]:0: arg1: expected table, got number Error executing lua: [string "<nvim>"]:0: arg1: expected table, got number
stack traceback: stack traceback:
[string "<nvim>"]:0: in main chunk]]), [string "<nvim>"]:0: in main chunk]]),
pcall_err_withfile(exec_lua, "vim.validate{arg1={1, 't'}}")) pcall_err(exec_lua, "vim.validate{arg1={1, 't'}}"))
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: [string "<nvim>"]:0: arg2: expected string, got number Error executing lua: [string "<nvim>"]:0: arg2: expected string, got number
stack traceback: stack traceback:
[string "<nvim>"]:0: in main chunk]]), [string "<nvim>"]:0: in main chunk]]),
pcall_err_withfile(exec_lua, "vim.validate{arg1={{}, 't'}, arg2={1, 's'}}")) pcall_err(exec_lua, "vim.validate{arg1={{}, 't'}, arg2={1, 's'}}"))
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: [string "<nvim>"]:0: arg2: expected string, got nil Error executing lua: [string "<nvim>"]:0: arg2: expected string, got nil
stack traceback: stack traceback:
[string "<nvim>"]:0: in main chunk]]), [string "<nvim>"]:0: in main chunk]]),
pcall_err_withfile(exec_lua, "vim.validate{arg1={{}, 't'}, arg2={nil, 's'}}")) pcall_err(exec_lua, "vim.validate{arg1={{}, 't'}, arg2={nil, 's'}}"))
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: [string "<nvim>"]:0: arg2: expected string, got nil Error executing lua: [string "<nvim>"]:0: arg2: expected string, got nil
stack traceback: stack traceback:
[string "<nvim>"]:0: in main chunk]]), [string "<nvim>"]:0: in main chunk]]),
pcall_err_withfile(exec_lua, "vim.validate{arg1={{}, 't'}, arg2={nil, 's'}}")) pcall_err(exec_lua, "vim.validate{arg1={{}, 't'}, arg2={nil, 's'}}"))
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: [string "<nvim>"]:0: arg1: expected even number, got 3 Error executing lua: [string "<nvim>"]:0: arg1: expected even number, got 3
stack traceback: stack traceback:
[string "<nvim>"]:0: in main chunk]]), [string "<nvim>"]:0: in main chunk]]),
pcall_err_withfile(exec_lua, "vim.validate{arg1={3, function(a) return a == 1 end, 'even number'}}")) pcall_err(exec_lua, "vim.validate{arg1={3, function(a) return a == 1 end, 'even number'}}"))
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: [string "<nvim>"]:0: arg1: expected ?, got 3 Error executing lua: [string "<nvim>"]:0: arg1: expected ?, got 3
stack traceback: stack traceback:
[string "<nvim>"]:0: in main chunk]]), [string "<nvim>"]:0: in main chunk]]),
pcall_err_withfile(exec_lua, "vim.validate{arg1={3, function(a) return a == 1 end}}")) pcall_err(exec_lua, "vim.validate{arg1={3, function(a) return a == 1 end}}"))
-- Pass an additional message back. -- Pass an additional message back.
eq(dedent([[ eq(dedent([[
.../helpers.lua:0: Error executing lua: [string "<nvim>"]:0: arg1: expected ?, got 3. Info: TEST_MSG Error executing lua: [string "<nvim>"]:0: arg1: expected ?, got 3. Info: TEST_MSG
stack traceback: stack traceback:
[string "<nvim>"]:0: in main chunk]]), [string "<nvim>"]:0: in main chunk]]),
pcall_err_withfile(exec_lua, "vim.validate{arg1={3, function(a) return a == 1, 'TEST_MSG' end}}")) pcall_err(exec_lua, "vim.validate{arg1={3, function(a) return a == 1, 'TEST_MSG' end}}"))
end) end)
it('vim.is_callable', function() it('vim.is_callable', function()
@ -1057,10 +1062,10 @@ describe('lua stdlib', function()
]] ]]
eq('', funcs.luaeval "vim.bo.filetype") eq('', funcs.luaeval "vim.bo.filetype")
eq(true, funcs.luaeval "vim.bo[BUF].modifiable") eq(true, funcs.luaeval "vim.bo[BUF].modifiable")
matches("^.../helpers.lua:0: Error executing lua: .*: Invalid option name: 'nosuchopt'$", matches("^Error executing lua: .*: Invalid option name: 'nosuchopt'$",
pcall_err_withfile(exec_lua, 'return vim.bo.nosuchopt')) pcall_err(exec_lua, 'return vim.bo.nosuchopt'))
matches("^.../helpers.lua:0: Error executing lua: .*: Expected lua string$", matches("^Error executing lua: .*: Expected lua string$",
pcall_err_withfile(exec_lua, 'return vim.bo[0][0].autoread')) pcall_err(exec_lua, 'return vim.bo[0][0].autoread'))
end) end)
it('vim.wo', function() it('vim.wo', function()
@ -1076,10 +1081,10 @@ describe('lua stdlib', function()
eq(0, funcs.luaeval "vim.wo.cole") eq(0, funcs.luaeval "vim.wo.cole")
eq(0, funcs.luaeval "vim.wo[0].cole") eq(0, funcs.luaeval "vim.wo[0].cole")
eq(0, funcs.luaeval "vim.wo[1001].cole") eq(0, funcs.luaeval "vim.wo[1001].cole")
matches("^.../helpers.lua:0: Error executing lua: .*: Invalid option name: 'notanopt'$", matches("^Error executing lua: .*: Invalid option name: 'notanopt'$",
pcall_err_withfile(exec_lua, 'return vim.wo.notanopt')) pcall_err(exec_lua, 'return vim.wo.notanopt'))
matches("^.../helpers.lua:0: Error executing lua: .*: Expected lua string$", matches("^Error executing lua: .*: Expected lua string$",
pcall_err_withfile(exec_lua, 'return vim.wo[0][0].list')) pcall_err(exec_lua, 'return vim.wo[0][0].list'))
eq(2, funcs.luaeval "vim.wo[1000].cole") eq(2, funcs.luaeval "vim.wo[1000].cole")
exec_lua [[ exec_lua [[
vim.wo[1000].cole = 0 vim.wo[1000].cole = 0
@ -1311,21 +1316,15 @@ describe('lua stdlib', function()
end) end)
it('should not crash when callback errors', function() it('should not crash when callback errors', function()
local pcall_result = exec_lua [[ eq({false, '[string "<nvim>"]:1: As Expected'}, exec_lua [[
return {pcall(function() vim.wait(1000, function() error("As Expected") end) end)} return {pcall(function() vim.wait(1000, function() error("As Expected") end) end)}
]] ]])
eq(pcall_result[1], false)
matches('As Expected', pcall_result[2])
end) end)
it('if callback is passed, it must be a function', function() it('if callback is passed, it must be a function', function()
local pcall_result = exec_lua [[ eq({false, 'vim.wait: if passed, condition must be a function'}, exec_lua [[
return {pcall(function() vim.wait(1000, 13) end)} return {pcall(function() vim.wait(1000, 13) end)}
]] ]])
eq(pcall_result[1], false)
matches('if passed, condition must be a function', pcall_result[2])
end) end)
it('should allow waiting with no callback, explicit', function() it('should allow waiting with no callback, explicit', function()

View File

@ -820,12 +820,12 @@ describe('LSP', function()
eq(dedent([[ eq(dedent([[
Error executing lua: .../lsp.lua:0: cmd: expected list, got nvim Error executing lua: .../lsp.lua:0: cmd: expected list, got nvim
stack traceback: stack traceback:
.../lsp.lua:0: in function .../lsp.lua:0>]]), .../lsp.lua:0: in function <.../lsp.lua:0>]]),
pcall_err(_cmd_parts, 'nvim')) pcall_err(_cmd_parts, 'nvim'))
eq(dedent([[ eq(dedent([[
Error executing lua: .../lsp.lua:0: cmd argument: expected string, got number Error executing lua: .../lsp.lua:0: cmd argument: expected string, got number
stack traceback: stack traceback:
.../lsp.lua:0: in function .../lsp.lua:0>]]), .../lsp.lua:0: in function <.../lsp.lua:0>]]),
pcall_err(_cmd_parts, {'nvim', 1})) pcall_err(_cmd_parts, {'nvim', 1}))
end) end)
end) end)

View File

@ -139,7 +139,9 @@ function module.pcall_err_withfile(fn, ...)
-- C:/long/path/foo.lua:186: Expected string, got number -- C:/long/path/foo.lua:186: Expected string, got number
-- to: -- to:
-- .../foo.lua:0: Expected string, got number -- .../foo.lua:0: Expected string, got number
local errmsg = tostring(rv):gsub('[^%s]-[/\\]([^%s:/\\]+):%d+', '.../%1:0') local errmsg = tostring(rv):gsub('([%s<])vim[/\\]([^%s:/\\]+):%d+', '%1\xffvim\xff%2:0')
:gsub('[^%s<]-[/\\]([^%s:/\\]+):%d+', '.../%1:0')
:gsub('\xffvim\xff', 'vim/')
-- Scrub numbers in paths/stacktraces: -- Scrub numbers in paths/stacktraces:
-- shared.lua:0: in function 'gsplit' -- shared.lua:0: in function 'gsplit'
-- shared.lua:0: in function <shared.lua:0>' -- shared.lua:0: in function <shared.lua:0>'