2023-12-13 06:04:24 -07:00
|
|
|
local luaassert = require('luassert')
|
2023-01-16 16:12:59 -07:00
|
|
|
local busted = require('busted')
|
2024-01-12 04:41:09 -07:00
|
|
|
local uv = vim.uv
|
2022-06-27 03:08:59 -07:00
|
|
|
local Paths = require('test.cmakeconfig.paths')
|
2016-04-23 16:53:11 -07:00
|
|
|
|
2023-12-13 06:04:24 -07:00
|
|
|
luaassert:set_parameter('TableFormatLevel', 100)
|
2019-09-03 04:29:49 -07:00
|
|
|
|
2017-03-16 14:04:03 -07:00
|
|
|
local quote_me = '[^.%w%+%-%@%_%/]' -- complement (needn't quote)
|
2024-01-12 04:41:09 -07:00
|
|
|
|
|
|
|
--- @param str string
|
|
|
|
--- @return string
|
2017-03-16 14:04:03 -07:00
|
|
|
local function shell_quote(str)
|
|
|
|
if string.find(str, quote_me) or str == '' then
|
|
|
|
return '"' .. str:gsub('[$%%"\\]', '\\%0') .. '"'
|
|
|
|
end
|
2024-01-12 04:41:09 -07:00
|
|
|
return str
|
2017-03-16 14:04:03 -07:00
|
|
|
end
|
|
|
|
|
2024-09-08 12:48:32 -07:00
|
|
|
--- Functions executing in the context of the test runner (not the current nvim test session).
|
2024-04-08 02:03:20 -07:00
|
|
|
--- @class test.testutil
|
2024-04-20 09:06:49 -07:00
|
|
|
local M = {
|
2024-01-15 12:41:22 -07:00
|
|
|
paths = Paths,
|
2019-07-21 16:13:11 -07:00
|
|
|
}
|
|
|
|
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @param p string
|
|
|
|
--- @return string
|
2023-04-13 15:09:56 -07:00
|
|
|
local function relpath(p)
|
|
|
|
p = vim.fs.normalize(p)
|
2024-01-15 13:16:30 -07:00
|
|
|
return (p:gsub('^' .. uv.cwd, ''))
|
2023-04-13 15:09:56 -07:00
|
|
|
end
|
|
|
|
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @param path string
|
|
|
|
--- @return boolean
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.isdir(path)
|
2023-04-04 12:59:06 -07:00
|
|
|
if not path then
|
|
|
|
return false
|
|
|
|
end
|
2024-01-12 04:41:09 -07:00
|
|
|
local stat = uv.fs_stat(path)
|
2023-04-04 12:59:06 -07:00
|
|
|
if not stat then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return stat.type == 'directory'
|
|
|
|
end
|
|
|
|
|
2024-09-08 12:29:20 -07:00
|
|
|
--- (Only on Windows) Replaces yucky "\\" slashes with delicious "/" slashes in a string, or all
|
|
|
|
--- string values in a table (recursively).
|
|
|
|
---
|
|
|
|
--- @param obj string|table
|
|
|
|
--- @return any
|
|
|
|
function M.fix_slashes(obj)
|
|
|
|
if not M.is_os('win') then
|
|
|
|
return obj
|
|
|
|
end
|
|
|
|
if type(obj) == 'string' then
|
|
|
|
local ret = obj:gsub('\\', '/')
|
|
|
|
return ret
|
|
|
|
elseif type(obj) == 'table' then
|
|
|
|
--- @cast obj table<any,any>
|
|
|
|
local ret = {} --- @type table<any,any>
|
|
|
|
for k, v in pairs(obj) do
|
|
|
|
ret[k] = M.fix_slashes(v)
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
assert(false, 'expected string or table of strings, got ' .. type(obj))
|
|
|
|
end
|
|
|
|
|
2024-01-15 13:21:14 -07:00
|
|
|
--- @param ... string|string[]
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @return string
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.argss_to_cmd(...)
|
2024-01-15 13:21:14 -07:00
|
|
|
local cmd = {} --- @type string[]
|
2017-03-16 14:04:03 -07:00
|
|
|
for i = 1, select('#', ...) do
|
|
|
|
local arg = select(i, ...)
|
|
|
|
if type(arg) == 'string' then
|
2024-01-15 13:21:14 -07:00
|
|
|
cmd[#cmd + 1] = shell_quote(arg)
|
2017-03-16 14:04:03 -07:00
|
|
|
else
|
2024-01-15 13:21:14 -07:00
|
|
|
--- @cast arg string[]
|
2017-03-17 00:57:19 -07:00
|
|
|
for _, subarg in ipairs(arg) do
|
2024-01-15 13:21:14 -07:00
|
|
|
cmd[#cmd + 1] = shell_quote(subarg)
|
2017-03-16 14:04:03 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-01-15 13:21:14 -07:00
|
|
|
return table.concat(cmd, ' ')
|
2017-03-16 14:04:03 -07:00
|
|
|
end
|
|
|
|
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.popen_r(...)
|
|
|
|
return io.popen(M.argss_to_cmd(...), 'r')
|
2017-03-16 14:04:03 -07:00
|
|
|
end
|
|
|
|
|
2024-01-15 12:49:08 -07:00
|
|
|
--- Calls fn() until it succeeds, up to `max` times or until `max_ms`
|
|
|
|
--- milliseconds have passed.
|
|
|
|
--- @param max integer?
|
|
|
|
--- @param max_ms integer?
|
|
|
|
--- @param fn function
|
|
|
|
--- @return any
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.retry(max, max_ms, fn)
|
2023-12-13 06:04:24 -07:00
|
|
|
luaassert(max == nil or max > 0)
|
|
|
|
luaassert(max_ms == nil or max_ms > 0)
|
2023-01-16 16:12:59 -07:00
|
|
|
local tries = 1
|
|
|
|
local timeout = (max_ms and max_ms or 10000)
|
2024-01-12 04:41:09 -07:00
|
|
|
local start_time = uv.now()
|
2023-01-16 16:12:59 -07:00
|
|
|
while true do
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @type boolean, any
|
2023-01-16 16:12:59 -07:00
|
|
|
local status, result = pcall(fn)
|
|
|
|
if status then
|
|
|
|
return result
|
|
|
|
end
|
2024-01-12 04:41:09 -07:00
|
|
|
uv.update_time() -- Update cached value of luv.now() (libuv: uv_now()).
|
|
|
|
if (max and tries >= max) or (uv.now() - start_time > timeout) then
|
2023-12-04 15:32:39 -07:00
|
|
|
busted.fail(string.format('retry() attempts: %d\n%s', tries, tostring(result)), 2)
|
2023-01-16 16:12:59 -07:00
|
|
|
end
|
|
|
|
tries = tries + 1
|
2024-01-12 04:41:09 -07:00
|
|
|
uv.sleep(20) -- Avoid hot loop...
|
2023-01-16 16:12:59 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-23 16:53:11 -07:00
|
|
|
local check_logs_useless_lines = {
|
2023-12-04 15:32:39 -07:00
|
|
|
['Warning: noted but unhandled ioctl'] = 1,
|
|
|
|
['could cause spurious value errors to appear'] = 2,
|
|
|
|
['See README_MISSING_SYSCALL_OR_IOCTL for guidance'] = 3,
|
2016-04-23 16:53:11 -07:00
|
|
|
}
|
|
|
|
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.eq(expected, actual, context)
|
2023-12-13 06:04:24 -07:00
|
|
|
return luaassert.are.same(expected, actual, context)
|
2016-04-23 16:53:11 -07:00
|
|
|
end
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.neq(expected, actual, context)
|
2023-12-13 06:04:24 -07:00
|
|
|
return luaassert.are_not.same(expected, actual, context)
|
2016-04-23 16:53:11 -07:00
|
|
|
end
|
2022-06-30 04:16:46 -07:00
|
|
|
|
|
|
|
--- Asserts that `cond` is true, or prints a message.
|
|
|
|
---
|
|
|
|
--- @param cond (boolean) expression to assert
|
|
|
|
--- @param expected (any) description of expected result
|
|
|
|
--- @param actual (any) description of actual result
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.ok(cond, expected, actual)
|
2023-12-13 06:04:24 -07:00
|
|
|
luaassert(
|
2023-12-04 15:32:39 -07:00
|
|
|
(not expected and not actual) or (expected and actual),
|
|
|
|
'if "expected" is given, "actual" is also required'
|
|
|
|
)
|
2022-06-30 04:16:46 -07:00
|
|
|
local msg = expected and ('expected %s, got: %s'):format(expected, tostring(actual)) or nil
|
2023-12-13 06:04:24 -07:00
|
|
|
return luaassert(cond, msg)
|
2018-07-28 18:49:11 -07:00
|
|
|
end
|
2020-09-10 09:52:56 -07:00
|
|
|
|
|
|
|
local function epicfail(state, arguments, _)
|
|
|
|
state.failure_message = arguments[1]
|
|
|
|
return false
|
|
|
|
end
|
2023-12-13 06:04:24 -07:00
|
|
|
luaassert:register('assertion', 'epicfail', epicfail)
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.fail(msg)
|
2023-12-13 06:04:24 -07:00
|
|
|
return luaassert.epicfail(msg)
|
2020-09-10 09:52:56 -07:00
|
|
|
end
|
|
|
|
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @param pat string
|
|
|
|
--- @param actual string
|
|
|
|
--- @return boolean
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.matches(pat, actual)
|
2018-04-10 16:43:15 -07:00
|
|
|
if nil ~= string.match(actual, pat) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
error(string.format('Pattern does not match.\nPattern:\n%s\nActual:\n%s', pat, actual))
|
|
|
|
end
|
2019-09-03 13:51:45 -07:00
|
|
|
|
2022-06-30 04:16:46 -07:00
|
|
|
--- Asserts that `pat` matches (or *not* if inverse=true) any line in the tail of `logfile`.
|
2020-02-16 20:02:09 -07:00
|
|
|
---
|
2023-01-16 16:12:59 -07:00
|
|
|
--- Retries for 1 second in case of filesystem delay.
|
|
|
|
---
|
2022-06-30 04:16:46 -07:00
|
|
|
---@param pat (string) Lua pattern to match lines in the log file
|
2023-12-05 13:52:06 -07:00
|
|
|
---@param logfile? (string) Full path to log file (default=$NVIM_LOG_FILE)
|
2024-09-05 02:39:58 -07:00
|
|
|
---@param nrlines? (number) Search up to this many log lines (default 10)
|
2023-12-05 13:52:06 -07:00
|
|
|
---@param inverse? (boolean) Assert that the pattern does NOT match.
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.assert_log(pat, logfile, nrlines, inverse)
|
2020-02-16 20:02:09 -07:00
|
|
|
logfile = logfile or os.getenv('NVIM_LOG_FILE') or '.nvimlog'
|
2023-12-13 06:04:24 -07:00
|
|
|
luaassert(logfile ~= nil, 'no logfile')
|
2022-05-23 21:44:15 -07:00
|
|
|
nrlines = nrlines or 10
|
2022-06-30 04:16:46 -07:00
|
|
|
inverse = inverse or false
|
2023-01-16 16:12:59 -07:00
|
|
|
|
2024-04-20 09:06:49 -07:00
|
|
|
M.retry(nil, 1000, function()
|
|
|
|
local lines = M.read_file_list(logfile, -nrlines) or {}
|
2023-12-04 15:32:39 -07:00
|
|
|
local msg = string.format(
|
|
|
|
'Pattern %q %sfound in log (last %d lines): %s:\n%s',
|
|
|
|
pat,
|
|
|
|
(inverse and '' or 'not '),
|
|
|
|
nrlines,
|
|
|
|
logfile,
|
|
|
|
' ' .. table.concat(lines, '\n ')
|
|
|
|
)
|
|
|
|
for _, line in ipairs(lines) do
|
2023-01-16 16:12:59 -07:00
|
|
|
if line:match(pat) then
|
2023-12-04 15:32:39 -07:00
|
|
|
if inverse then
|
|
|
|
error(msg)
|
|
|
|
else
|
|
|
|
return
|
|
|
|
end
|
2023-01-16 16:12:59 -07:00
|
|
|
end
|
2022-06-30 04:16:46 -07:00
|
|
|
end
|
2023-12-04 15:32:39 -07:00
|
|
|
if not inverse then
|
|
|
|
error(msg)
|
|
|
|
end
|
2023-01-16 16:12:59 -07:00
|
|
|
end)
|
2022-06-30 04:16:46 -07:00
|
|
|
end
|
|
|
|
|
2023-01-16 16:12:59 -07:00
|
|
|
--- Asserts that `pat` does NOT match any line in the tail of `logfile`.
|
2022-06-30 04:16:46 -07:00
|
|
|
---
|
|
|
|
--- @see assert_log
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @param pat (string) Lua pattern to match lines in the log file
|
|
|
|
--- @param logfile? (string) Full path to log file (default=$NVIM_LOG_FILE)
|
|
|
|
--- @param nrlines? (number) Search up to this many log lines
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.assert_nolog(pat, logfile, nrlines)
|
|
|
|
return M.assert_log(pat, logfile, nrlines, true)
|
2020-02-16 20:02:09 -07:00
|
|
|
end
|
|
|
|
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @param fn fun(...): any
|
|
|
|
--- @param ... any
|
|
|
|
--- @return boolean, any
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.pcall(fn, ...)
|
2023-12-13 06:04:24 -07:00
|
|
|
luaassert(type(fn) == 'function')
|
2019-09-03 13:51:45 -07:00
|
|
|
local status, rv = pcall(fn, ...)
|
2022-11-14 03:01:35 -07:00
|
|
|
if status then
|
|
|
|
return status, rv
|
2019-09-03 13:51:45 -07:00
|
|
|
end
|
2022-11-14 03:01:35 -07:00
|
|
|
|
2020-09-12 19:04:22 -07:00
|
|
|
-- From:
|
|
|
|
-- C:/long/path/foo.lua:186: Expected string, got number
|
|
|
|
-- to:
|
|
|
|
-- .../foo.lua:0: Expected string, got number
|
2023-12-04 15:32:39 -07:00
|
|
|
local errmsg = tostring(rv)
|
|
|
|
:gsub('([%s<])vim[/\\]([^%s:/\\]+):%d+', '%1\xffvim\xff%2:0')
|
|
|
|
:gsub('[^%s<]-[/\\]([^%s:/\\]+):%d+', '.../%1:0')
|
|
|
|
:gsub('\xffvim\xff', 'vim/')
|
2023-07-19 02:02:49 -07:00
|
|
|
|
2020-09-12 19:04:22 -07:00
|
|
|
-- Scrub numbers in paths/stacktraces:
|
|
|
|
-- shared.lua:0: in function 'gsplit'
|
|
|
|
-- shared.lua:0: in function <shared.lua:0>'
|
2023-07-19 02:02:49 -07:00
|
|
|
errmsg = errmsg:gsub('([^%s].lua):%d+', '%1:0')
|
|
|
|
-- [string "<nvim>"]:0:
|
|
|
|
-- [string ":lua"]:0:
|
|
|
|
-- [string ":luado"]:0:
|
|
|
|
errmsg = errmsg:gsub('(%[string "[^"]+"%]):%d+', '%1:0')
|
|
|
|
|
2020-09-12 19:04:22 -07:00
|
|
|
-- Scrub tab chars:
|
|
|
|
errmsg = errmsg:gsub('\t', ' ')
|
|
|
|
-- In Lua 5.1, we sometimes get a "(tail call): ?" on the last line.
|
|
|
|
-- We remove this so that the tests are not lua dependent.
|
|
|
|
errmsg = errmsg:gsub('%s*%(tail call%): %?', '')
|
|
|
|
|
2022-11-14 03:01:35 -07:00
|
|
|
return status, errmsg
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Invokes `fn` and returns the error string (with truncated paths), or raises
|
|
|
|
-- an error if `fn` succeeds.
|
|
|
|
--
|
|
|
|
-- Replaces line/column numbers with zero:
|
|
|
|
-- shared.lua:0: in function 'gsplit'
|
|
|
|
-- shared.lua:0: in function <shared.lua:0>'
|
|
|
|
--
|
|
|
|
-- Usage:
|
|
|
|
-- -- Match exact string.
|
|
|
|
-- eq('e', pcall_err(function(a, b) error('e') end, 'arg1', 'arg2'))
|
|
|
|
-- -- Match Lua pattern.
|
|
|
|
-- matches('e[or]+$', pcall_err(function(a, b) error('some error') end, 'arg1', 'arg2'))
|
|
|
|
--
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @param fn function
|
|
|
|
--- @return string
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.pcall_err_withfile(fn, ...)
|
2023-12-13 06:04:24 -07:00
|
|
|
luaassert(type(fn) == 'function')
|
2024-04-20 09:06:49 -07:00
|
|
|
local status, rv = M.pcall(fn, ...)
|
2022-11-14 03:01:35 -07:00
|
|
|
if status == true then
|
|
|
|
error('expected failure, but got success')
|
|
|
|
end
|
|
|
|
return rv
|
2019-09-03 13:51:45 -07:00
|
|
|
end
|
|
|
|
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @param fn function
|
|
|
|
--- @param ... any
|
|
|
|
--- @return string
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.pcall_err_withtrace(fn, ...)
|
|
|
|
local errmsg = M.pcall_err_withfile(fn, ...)
|
2020-09-12 19:04:22 -07:00
|
|
|
|
2024-01-15 12:49:08 -07:00
|
|
|
return (
|
|
|
|
errmsg
|
2024-04-20 08:44:13 -07:00
|
|
|
:gsub('^%.%.%./testnvim%.lua:0: ', '')
|
2024-01-15 12:49:08 -07:00
|
|
|
:gsub('^Error executing lua:- ', '')
|
|
|
|
:gsub('^%[string "<nvim>"%]:0: ', '')
|
|
|
|
)
|
2020-09-12 19:04:22 -07:00
|
|
|
end
|
|
|
|
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @param fn function
|
|
|
|
--- @param ... any
|
|
|
|
--- @return string
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.pcall_err(fn, ...)
|
|
|
|
return M.remove_trace(M.pcall_err_withtrace(fn, ...))
|
2021-11-06 07:26:10 -07:00
|
|
|
end
|
|
|
|
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @param s string
|
|
|
|
--- @return string
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.remove_trace(s)
|
2023-12-04 15:32:39 -07:00
|
|
|
return (s:gsub('\n%s*stack traceback:.*', ''))
|
2021-11-06 07:26:10 -07:00
|
|
|
end
|
|
|
|
|
2017-04-09 06:49:31 -07:00
|
|
|
-- initial_path: directory to recurse into
|
|
|
|
-- re: include pattern (string)
|
|
|
|
-- exc_re: exclude pattern(s) (string or table)
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.glob(initial_path, re, exc_re)
|
2017-04-09 06:49:31 -07:00
|
|
|
exc_re = type(exc_re) == 'table' and exc_re or { exc_re }
|
2024-01-15 12:49:08 -07:00
|
|
|
local paths_to_check = { initial_path } --- @type string[]
|
|
|
|
local ret = {} --- @type string[]
|
|
|
|
local checked_files = {} --- @type table<string,true>
|
2017-04-09 06:49:31 -07:00
|
|
|
local function is_excluded(path)
|
|
|
|
for _, pat in pairs(exc_re) do
|
2023-12-04 15:32:39 -07:00
|
|
|
if path:match(pat) then
|
|
|
|
return true
|
|
|
|
end
|
2017-04-09 06:49:31 -07:00
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2017-04-14 11:34:54 -07:00
|
|
|
if is_excluded(initial_path) then
|
|
|
|
return ret
|
|
|
|
end
|
2016-11-04 08:20:58 -07:00
|
|
|
while #paths_to_check > 0 do
|
|
|
|
local cur_path = paths_to_check[#paths_to_check]
|
|
|
|
paths_to_check[#paths_to_check] = nil
|
2023-04-04 12:59:06 -07:00
|
|
|
for e in vim.fs.dir(cur_path) do
|
2016-11-04 08:20:58 -07:00
|
|
|
local full_path = cur_path .. '/' .. e
|
|
|
|
local checked_path = full_path:sub(#initial_path + 1)
|
2017-04-09 06:49:31 -07:00
|
|
|
if (not is_excluded(checked_path)) and e:sub(1, 1) ~= '.' then
|
2024-01-12 04:41:09 -07:00
|
|
|
local stat = uv.fs_stat(full_path)
|
2023-04-04 12:59:06 -07:00
|
|
|
if stat then
|
|
|
|
local check_key = stat.dev .. ':' .. tostring(stat.ino)
|
2017-03-11 14:25:55 -07:00
|
|
|
if not checked_files[check_key] then
|
|
|
|
checked_files[check_key] = true
|
2023-04-04 12:59:06 -07:00
|
|
|
if stat.type == 'directory' then
|
2017-03-11 14:25:55 -07:00
|
|
|
paths_to_check[#paths_to_check + 1] = full_path
|
|
|
|
elseif not re or checked_path:match(re) then
|
|
|
|
ret[#ret + 1] = full_path
|
|
|
|
end
|
2016-11-04 08:20:58 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.check_logs()
|
2016-04-23 16:53:11 -07:00
|
|
|
local log_dir = os.getenv('LOG_DIR')
|
2019-08-29 23:26:55 -07:00
|
|
|
local runtime_errors = {}
|
2024-04-20 09:06:49 -07:00
|
|
|
if log_dir and M.isdir(log_dir) then
|
2023-04-04 12:59:06 -07:00
|
|
|
for tail in vim.fs.dir(log_dir) do
|
2016-04-23 16:53:11 -07:00
|
|
|
if tail:sub(1, 30) == 'valgrind-' or tail:find('san%.') then
|
|
|
|
local file = log_dir .. '/' .. tail
|
2023-12-13 06:04:24 -07:00
|
|
|
local fd = assert(io.open(file))
|
2016-04-23 16:53:11 -07:00
|
|
|
local start_msg = ('='):rep(20) .. ' File ' .. file .. ' ' .. ('='):rep(20)
|
2024-01-15 12:49:08 -07:00
|
|
|
local lines = {} --- @type string[]
|
2016-04-23 16:53:11 -07:00
|
|
|
local warning_line = 0
|
|
|
|
for line in fd:lines() do
|
|
|
|
local cur_warning_line = check_logs_useless_lines[line]
|
|
|
|
if cur_warning_line == warning_line + 1 then
|
|
|
|
warning_line = cur_warning_line
|
|
|
|
else
|
|
|
|
lines[#lines + 1] = line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
fd:close()
|
|
|
|
if #lines > 0 then
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @type boolean?, file*?
|
2020-09-06 09:09:36 -07:00
|
|
|
local status, f
|
2016-04-23 16:53:11 -07:00
|
|
|
local out = io.stdout
|
2020-09-06 09:09:36 -07:00
|
|
|
if os.getenv('SYMBOLIZER') then
|
2024-04-20 09:06:49 -07:00
|
|
|
status, f = pcall(M.popen_r, os.getenv('SYMBOLIZER'), '-l', file)
|
2020-09-06 09:09:36 -07:00
|
|
|
end
|
2016-04-23 16:53:11 -07:00
|
|
|
out:write(start_msg .. '\n')
|
2020-09-06 09:09:36 -07:00
|
|
|
if status then
|
2024-01-15 12:49:08 -07:00
|
|
|
assert(f)
|
2020-09-06 09:09:36 -07:00
|
|
|
for line in f:lines() do
|
2023-12-04 15:32:39 -07:00
|
|
|
out:write('= ' .. line .. '\n')
|
2020-09-06 09:09:36 -07:00
|
|
|
end
|
|
|
|
f:close()
|
|
|
|
else
|
|
|
|
out:write('= ' .. table.concat(lines, '\n= ') .. '\n')
|
|
|
|
end
|
2016-04-23 16:53:11 -07:00
|
|
|
out:write(select(1, start_msg:gsub('.', '=')) .. '\n')
|
2019-08-29 23:26:55 -07:00
|
|
|
table.insert(runtime_errors, file)
|
2016-04-23 16:53:11 -07:00
|
|
|
end
|
2020-09-06 09:09:36 -07:00
|
|
|
os.remove(file)
|
2016-04-23 16:53:11 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-12-13 06:04:24 -07:00
|
|
|
luaassert(
|
2023-12-04 15:32:39 -07:00
|
|
|
0 == #runtime_errors,
|
|
|
|
string.format('Found runtime errors in logfile(s): %s', table.concat(runtime_errors, ', '))
|
|
|
|
)
|
2016-04-23 16:53:11 -07:00
|
|
|
end
|
|
|
|
|
2024-11-06 04:05:27 -07:00
|
|
|
local sysname = uv.os_uname().sysname:lower()
|
2017-01-02 22:46:44 -07:00
|
|
|
|
2024-02-07 04:24:44 -07:00
|
|
|
--- @param s 'win'|'mac'|'freebsd'|'openbsd'|'bsd'
|
|
|
|
--- @return boolean
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.is_os(s)
|
2023-12-04 15:32:39 -07:00
|
|
|
if not (s == 'win' or s == 'mac' or s == 'freebsd' or s == 'openbsd' or s == 'bsd') then
|
|
|
|
error('unknown platform: ' .. tostring(s))
|
|
|
|
end
|
|
|
|
return not not (
|
2024-11-06 04:05:27 -07:00
|
|
|
(s == 'win' and (sysname:find('windows') or sysname:find('mingw')))
|
|
|
|
or (s == 'mac' and sysname == 'darwin')
|
|
|
|
or (s == 'freebsd' and sysname == 'freebsd')
|
|
|
|
or (s == 'openbsd' and sysname == 'openbsd')
|
|
|
|
or (s == 'bsd' and sysname:find('bsd'))
|
2023-12-04 15:32:39 -07:00
|
|
|
)
|
2019-09-04 06:58:04 -07:00
|
|
|
end
|
|
|
|
|
2024-12-13 07:22:59 -07:00
|
|
|
local architecture = uv.os_uname().machine
|
|
|
|
|
|
|
|
--- @param s 'x86_64'|'arm64'
|
|
|
|
--- @return boolean
|
|
|
|
function M.is_arch(s)
|
|
|
|
if not (s == 'x86_64' or s == 'arm64') then
|
|
|
|
error('unknown architecture: ' .. tostring(s))
|
|
|
|
end
|
|
|
|
return s == architecture
|
|
|
|
end
|
|
|
|
|
2024-02-07 04:24:44 -07:00
|
|
|
local tmpname_id = 0
|
2024-11-06 04:05:27 -07:00
|
|
|
local tmpdir = os.getenv('TMPDIR') or os.getenv('TEMP')
|
|
|
|
local tmpdir_is_local = not not (tmpdir and tmpdir:find('Xtest'))
|
2024-02-07 04:24:44 -07:00
|
|
|
|
2024-11-06 04:05:27 -07:00
|
|
|
local function get_tmpname()
|
|
|
|
if tmpdir_is_local then
|
2024-02-07 04:24:44 -07:00
|
|
|
-- Cannot control os.tmpname() dir, so hack our own tmpname() impl.
|
|
|
|
tmpname_id = tmpname_id + 1
|
|
|
|
-- "…/Xtest_tmpdir/T42.7"
|
2024-11-06 04:05:27 -07:00
|
|
|
return ('%s/%s.%d'):format(tmpdir, (_G._nvim_test_id or 'nvim-test'), tmpname_id)
|
2023-12-04 15:32:39 -07:00
|
|
|
end
|
2024-02-07 04:24:44 -07:00
|
|
|
|
|
|
|
local fname = os.tmpname()
|
2024-09-03 02:18:17 -07:00
|
|
|
|
2024-04-20 09:06:49 -07:00
|
|
|
if M.is_os('win') and fname:sub(1, 2) == '\\s' then
|
2024-02-07 04:24:44 -07:00
|
|
|
-- In Windows tmpname() returns a filename starting with
|
|
|
|
-- special sequence \s, prepend $TEMP path
|
|
|
|
return tmpdir .. fname
|
2024-04-20 09:06:49 -07:00
|
|
|
elseif M.is_os('mac') and fname:match('^/tmp') then
|
2024-02-07 04:24:44 -07:00
|
|
|
-- In OS X /tmp links to /private/tmp
|
|
|
|
return '/private' .. fname
|
|
|
|
end
|
2024-11-06 04:05:27 -07:00
|
|
|
return fname
|
|
|
|
end
|
2024-02-07 04:24:44 -07:00
|
|
|
|
2024-11-06 04:05:27 -07:00
|
|
|
--- Generates a unique filepath for use by tests, in a test-specific "…/Xtest_tmpdir/T42.7"
|
|
|
|
--- directory (which is cleaned up by the test runner).
|
|
|
|
---
|
|
|
|
--- @param create? boolean (default true) Create the file.
|
|
|
|
--- @return string
|
|
|
|
function M.tmpname(create)
|
|
|
|
local fname = get_tmpname()
|
|
|
|
os.remove(fname)
|
|
|
|
if create ~= false then
|
|
|
|
assert(io.open(fname, 'w')):close()
|
|
|
|
end
|
2024-02-07 04:24:44 -07:00
|
|
|
return fname
|
|
|
|
end
|
2017-01-02 22:46:44 -07:00
|
|
|
|
2019-04-06 18:54:22 -07:00
|
|
|
local function deps_prefix()
|
|
|
|
local env = os.getenv('DEPS_PREFIX')
|
|
|
|
return (env and env ~= '') and env or '.deps/usr'
|
|
|
|
end
|
|
|
|
|
2016-11-04 08:20:58 -07:00
|
|
|
local tests_skipped = 0
|
|
|
|
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.check_cores(app, force) -- luacheck: ignore
|
2023-03-05 06:50:55 -07:00
|
|
|
-- Temporary workaround: skip core check as it interferes with CI.
|
|
|
|
if true then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
app = app or 'build/bin/nvim' -- luacheck: ignore
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @type string, string?, string[]
|
2016-11-04 08:20:58 -07:00
|
|
|
local initial_path, re, exc_re
|
2023-12-04 15:32:39 -07:00
|
|
|
local gdb_db_cmd =
|
|
|
|
'gdb -n -batch -ex "thread apply all bt full" "$_NVIM_TEST_APP" -c "$_NVIM_TEST_CORE"'
|
2016-11-04 08:20:58 -07:00
|
|
|
local lldb_db_cmd = 'lldb -Q -o "bt all" -f "$_NVIM_TEST_APP" -c "$_NVIM_TEST_CORE"'
|
|
|
|
local random_skip = false
|
2017-04-14 11:34:54 -07:00
|
|
|
-- Workspace-local $TMPDIR, scrubbed and pattern-escaped.
|
|
|
|
-- "./Xtest-tmpdir/" => "Xtest%-tmpdir"
|
2024-11-06 04:05:27 -07:00
|
|
|
local local_tmpdir = nil
|
|
|
|
if tmpdir_is_local and tmpdir then
|
|
|
|
local_tmpdir = vim.pesc(relpath(tmpdir):gsub('^[ ./]+', ''):gsub('%/+$', ''))
|
|
|
|
end
|
|
|
|
|
2024-01-15 12:49:08 -07:00
|
|
|
local db_cmd --- @type string
|
2024-01-15 12:49:24 -07:00
|
|
|
local test_glob_dir = os.getenv('NVIM_TEST_CORE_GLOB_DIRECTORY')
|
|
|
|
if test_glob_dir and test_glob_dir ~= '' then
|
|
|
|
initial_path = test_glob_dir
|
2016-11-04 08:20:58 -07:00
|
|
|
re = os.getenv('NVIM_TEST_CORE_GLOB_RE')
|
2017-04-09 06:49:31 -07:00
|
|
|
exc_re = { os.getenv('NVIM_TEST_CORE_EXC_RE'), local_tmpdir }
|
2016-11-04 08:20:58 -07:00
|
|
|
db_cmd = os.getenv('NVIM_TEST_CORE_DB_CMD') or gdb_db_cmd
|
2024-01-15 12:49:08 -07:00
|
|
|
random_skip = os.getenv('NVIM_TEST_CORE_RANDOM_SKIP') ~= ''
|
2024-04-20 09:06:49 -07:00
|
|
|
elseif M.is_os('mac') then
|
2016-11-04 08:20:58 -07:00
|
|
|
initial_path = '/cores'
|
|
|
|
re = nil
|
2017-04-09 06:49:31 -07:00
|
|
|
exc_re = { local_tmpdir }
|
2016-11-04 08:20:58 -07:00
|
|
|
db_cmd = lldb_db_cmd
|
|
|
|
else
|
|
|
|
initial_path = '.'
|
2024-04-20 09:06:49 -07:00
|
|
|
if M.is_os('freebsd') then
|
2021-04-02 21:15:51 -07:00
|
|
|
re = '/nvim.core$'
|
|
|
|
else
|
|
|
|
re = '/core[^/]*$'
|
|
|
|
end
|
2023-12-04 15:32:39 -07:00
|
|
|
exc_re = { '^/%.deps$', '^/%' .. deps_prefix() .. '$', local_tmpdir, '^/%node_modules$' }
|
2016-11-04 08:20:58 -07:00
|
|
|
db_cmd = gdb_db_cmd
|
|
|
|
random_skip = true
|
|
|
|
end
|
|
|
|
-- Finding cores takes too much time on linux
|
2017-04-05 22:23:33 -07:00
|
|
|
if not force and random_skip and math.random() < 0.9 then
|
2016-11-04 08:20:58 -07:00
|
|
|
tests_skipped = tests_skipped + 1
|
|
|
|
return
|
|
|
|
end
|
2024-04-20 09:06:49 -07:00
|
|
|
local cores = M.glob(initial_path, re, exc_re)
|
2016-11-04 08:20:58 -07:00
|
|
|
local found_cores = 0
|
|
|
|
local out = io.stdout
|
|
|
|
for _, core in ipairs(cores) do
|
2023-12-04 15:32:39 -07:00
|
|
|
local len = 80 - #core - #'Core file ' - 2
|
2016-11-04 08:20:58 -07:00
|
|
|
local esigns = ('='):rep(len / 2)
|
|
|
|
out:write(('\n%s Core file %s %s\n'):format(esigns, core, esigns))
|
|
|
|
out:flush()
|
2017-09-05 08:38:14 -07:00
|
|
|
os.execute(db_cmd:gsub('%$_NVIM_TEST_APP', app):gsub('%$_NVIM_TEST_CORE', core) .. ' 2>&1')
|
|
|
|
out:write('\n')
|
2016-11-04 08:20:58 -07:00
|
|
|
found_cores = found_cores + 1
|
|
|
|
os.remove(core)
|
|
|
|
end
|
|
|
|
if found_cores ~= 0 then
|
|
|
|
out:write(('\nTests covered by this check: %u\n'):format(tests_skipped + 1))
|
|
|
|
end
|
|
|
|
tests_skipped = 0
|
2017-02-22 11:14:06 -07:00
|
|
|
if found_cores > 0 then
|
2023-12-04 15:32:39 -07:00
|
|
|
error('crash detected (see above)')
|
2017-02-22 11:14:06 -07:00
|
|
|
end
|
2016-11-04 08:20:58 -07:00
|
|
|
end
|
|
|
|
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @return string?
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.repeated_read_cmd(...)
|
2017-03-17 00:57:19 -07:00
|
|
|
for _ = 1, 10 do
|
2024-04-20 09:06:49 -07:00
|
|
|
local stream = M.popen_r(...)
|
2017-03-17 00:57:19 -07:00
|
|
|
local ret = stream:read('*a')
|
|
|
|
stream:close()
|
|
|
|
if ret then
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
end
|
2024-04-20 09:06:49 -07:00
|
|
|
print('ERROR: Failed to execute ' .. M.argss_to_cmd(...) .. ': nil return after 10 attempts')
|
2017-03-17 00:57:19 -07:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @generic T
|
|
|
|
--- @param orig T
|
|
|
|
--- @return T
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.shallowcopy(orig)
|
2017-11-12 15:10:39 -07:00
|
|
|
if type(orig) ~= 'table' then
|
|
|
|
return orig
|
|
|
|
end
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @cast orig table<any,any>
|
|
|
|
local copy = {} --- @type table<any,any>
|
2017-01-20 14:00:47 -07:00
|
|
|
for orig_key, orig_value in pairs(orig) do
|
|
|
|
copy[orig_key] = orig_value
|
|
|
|
end
|
|
|
|
return copy
|
|
|
|
end
|
|
|
|
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @param d1 table<any,any>
|
|
|
|
--- @param d2 table<any,any>
|
|
|
|
--- @return table<any,any>
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.mergedicts_copy(d1, d2)
|
|
|
|
local ret = M.shallowcopy(d1)
|
2017-11-05 11:06:12 -07:00
|
|
|
for k, v in pairs(d2) do
|
2024-01-15 14:14:32 -07:00
|
|
|
if d2[k] == vim.NIL then
|
2017-11-05 11:06:12 -07:00
|
|
|
ret[k] = nil
|
2017-11-06 10:28:37 -07:00
|
|
|
elseif type(d1[k]) == 'table' and type(v) == 'table' then
|
2024-04-20 09:06:49 -07:00
|
|
|
ret[k] = M.mergedicts_copy(d1[k], v)
|
2017-11-05 11:06:12 -07:00
|
|
|
else
|
2017-11-06 10:28:37 -07:00
|
|
|
ret[k] = v
|
2017-11-05 11:06:12 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2024-01-15 12:49:08 -07:00
|
|
|
--- dictdiff: find a diff so that mergedicts_copy(d1, diff) is equal to d2
|
|
|
|
---
|
|
|
|
--- Note: does not do copies of d2 values used.
|
|
|
|
--- @param d1 table<any,any>
|
|
|
|
--- @param d2 table<any,any>
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.dictdiff(d1, d2)
|
2024-01-15 12:49:08 -07:00
|
|
|
local ret = {} --- @type table<any,any>
|
2017-11-12 08:52:49 -07:00
|
|
|
local hasdiff = false
|
|
|
|
for k, v in pairs(d1) do
|
|
|
|
if d2[k] == nil then
|
|
|
|
hasdiff = true
|
2024-01-15 14:14:32 -07:00
|
|
|
ret[k] = vim.NIL
|
2017-11-12 08:52:49 -07:00
|
|
|
elseif type(v) == type(d2[k]) then
|
2017-11-12 15:10:39 -07:00
|
|
|
if type(v) == 'table' then
|
2024-04-20 09:06:49 -07:00
|
|
|
local subdiff = M.dictdiff(v, d2[k])
|
2017-11-12 08:52:49 -07:00
|
|
|
if subdiff ~= nil then
|
|
|
|
hasdiff = true
|
|
|
|
ret[k] = subdiff
|
|
|
|
end
|
|
|
|
elseif v ~= d2[k] then
|
|
|
|
ret[k] = d2[k]
|
2017-11-12 15:10:39 -07:00
|
|
|
hasdiff = true
|
2017-11-12 08:52:49 -07:00
|
|
|
end
|
|
|
|
else
|
|
|
|
ret[k] = d2[k]
|
2017-11-12 15:10:39 -07:00
|
|
|
hasdiff = true
|
2017-11-12 08:52:49 -07:00
|
|
|
end
|
|
|
|
end
|
2024-04-20 09:06:49 -07:00
|
|
|
local shallowcopy = M.shallowcopy
|
2017-11-12 08:52:49 -07:00
|
|
|
for k, v in pairs(d2) do
|
|
|
|
if d1[k] == nil then
|
2017-11-12 15:10:39 -07:00
|
|
|
ret[k] = shallowcopy(v)
|
2017-11-12 08:52:49 -07:00
|
|
|
hasdiff = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if hasdiff then
|
|
|
|
return ret
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-29 15:26:21 -07:00
|
|
|
-- Concat list-like tables.
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.concat_tables(...)
|
2024-01-15 12:49:08 -07:00
|
|
|
local ret = {} --- @type table<any,any>
|
2017-03-04 15:09:55 -07:00
|
|
|
for i = 1, select('#', ...) do
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @type table<any,any>
|
2017-03-04 15:09:55 -07:00
|
|
|
local tbl = select(i, ...)
|
|
|
|
if tbl then
|
|
|
|
for _, v in ipairs(tbl) do
|
|
|
|
ret[#ret + 1] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @param str string
|
2023-08-10 06:21:56 -07:00
|
|
|
--- @param leave_indent? integer
|
2023-04-03 04:01:23 -07:00
|
|
|
--- @return string
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.dedent(str, leave_indent)
|
2017-04-01 02:52:28 -07:00
|
|
|
-- find minimum common indent across lines
|
2023-08-10 06:21:56 -07:00
|
|
|
local indent --- @type string?
|
2017-04-01 02:52:28 -07:00
|
|
|
for line in str:gmatch('[^\n]+') do
|
|
|
|
local line_indent = line:match('^%s+') or ''
|
|
|
|
if indent == nil or #line_indent < #indent then
|
|
|
|
indent = line_indent
|
|
|
|
end
|
|
|
|
end
|
2023-08-10 06:21:56 -07:00
|
|
|
|
|
|
|
if not indent or #indent == 0 then
|
2017-04-01 02:52:28 -07:00
|
|
|
-- no minimum common indent
|
|
|
|
return str
|
|
|
|
end
|
2023-08-10 06:21:56 -07:00
|
|
|
|
2017-06-20 08:17:19 -07:00
|
|
|
local left_indent = (' '):rep(leave_indent or 0)
|
2017-04-01 02:52:28 -07:00
|
|
|
-- create a pattern for the indent
|
|
|
|
indent = indent:gsub('%s', '[ \t]')
|
|
|
|
-- strip it from the first line
|
2023-12-04 15:32:39 -07:00
|
|
|
str = str:gsub('^' .. indent, left_indent)
|
2017-04-01 02:52:28 -07:00
|
|
|
-- strip it from the remaining lines
|
2023-12-04 15:32:39 -07:00
|
|
|
str = str:gsub('[\n]' .. indent, '\n' .. left_indent)
|
2017-04-01 02:52:28 -07:00
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.intchar2lua(ch)
|
2017-11-04 16:41:44 -07:00
|
|
|
ch = tonumber(ch)
|
|
|
|
return (20 <= ch and ch < 127) and ('%c'):format(ch) or ch
|
|
|
|
end
|
|
|
|
|
2024-01-15 12:49:24 -07:00
|
|
|
--- @param str string
|
|
|
|
--- @return string
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.hexdump(str)
|
2018-04-27 01:07:26 -07:00
|
|
|
local len = string.len(str)
|
2023-12-04 15:32:39 -07:00
|
|
|
local dump = ''
|
|
|
|
local hex = ''
|
|
|
|
local asc = ''
|
2018-04-27 01:07:26 -07:00
|
|
|
|
|
|
|
for i = 1, len do
|
|
|
|
if 1 == i % 8 then
|
2023-12-04 15:32:39 -07:00
|
|
|
dump = dump .. hex .. asc .. '\n'
|
|
|
|
hex = string.format('%04x: ', i - 1)
|
|
|
|
asc = ''
|
2018-04-27 01:07:26 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local ord = string.byte(str, i)
|
2023-12-04 15:32:39 -07:00
|
|
|
hex = hex .. string.format('%02x ', ord)
|
2018-04-27 01:07:26 -07:00
|
|
|
if ord >= 32 and ord <= 126 then
|
|
|
|
asc = asc .. string.char(ord)
|
|
|
|
else
|
2023-12-04 15:32:39 -07:00
|
|
|
asc = asc .. '.'
|
2018-04-27 01:07:26 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
return dump .. hex .. string.rep(' ', 8 - len % 8) .. asc
|
2018-04-27 01:07:26 -07:00
|
|
|
end
|
|
|
|
|
2024-01-15 12:49:08 -07:00
|
|
|
--- Reads text lines from `filename` into a table.
|
|
|
|
--- @param filename string path to file
|
|
|
|
--- @param start? integer start line (1-indexed), negative means "lines before end" (tail)
|
|
|
|
--- @return string[]?
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.read_file_list(filename, start)
|
2018-09-23 03:04:05 -07:00
|
|
|
local lnum = (start ~= nil and type(start) == 'number') and start or 1
|
|
|
|
local tail = (lnum < 0)
|
|
|
|
local maxlines = tail and math.abs(lnum) or nil
|
|
|
|
local file = io.open(filename, 'r')
|
|
|
|
if not file then
|
|
|
|
return nil
|
|
|
|
end
|
2021-12-07 15:07:16 -07:00
|
|
|
|
|
|
|
-- There is no need to read more than the last 2MB of the log file, so seek
|
|
|
|
-- to that.
|
2023-12-04 15:32:39 -07:00
|
|
|
local file_size = file:seek('end')
|
2021-12-07 15:07:16 -07:00
|
|
|
local offset = file_size - 2000000
|
|
|
|
if offset < 0 then
|
|
|
|
offset = 0
|
|
|
|
end
|
2023-12-04 15:32:39 -07:00
|
|
|
file:seek('set', offset)
|
2021-12-07 15:07:16 -07:00
|
|
|
|
2018-09-23 03:04:05 -07:00
|
|
|
local lines = {}
|
|
|
|
local i = 1
|
2023-12-04 15:32:39 -07:00
|
|
|
local line = file:read('*l')
|
2021-12-07 15:07:16 -07:00
|
|
|
while line ~= nil do
|
2018-09-23 03:04:05 -07:00
|
|
|
if i >= start then
|
|
|
|
table.insert(lines, line)
|
|
|
|
if #lines > maxlines then
|
|
|
|
table.remove(lines, 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
i = i + 1
|
2023-12-04 15:32:39 -07:00
|
|
|
line = file:read('*l')
|
2018-09-23 03:04:05 -07:00
|
|
|
end
|
|
|
|
file:close()
|
|
|
|
return lines
|
|
|
|
end
|
|
|
|
|
2024-01-15 12:49:08 -07:00
|
|
|
--- Reads the entire contents of `filename` into a string.
|
|
|
|
--- @param filename string
|
|
|
|
--- @return string?
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.read_file(filename)
|
2018-09-23 03:04:05 -07:00
|
|
|
local file = io.open(filename, 'r')
|
2018-04-27 01:07:26 -07:00
|
|
|
if not file then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
local ret = file:read('*a')
|
|
|
|
file:close()
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Dedent the given text and write it to the file name.
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.write_file(name, text, no_dedent, append)
|
2023-12-13 06:04:24 -07:00
|
|
|
local file = assert(io.open(name, (append and 'a' or 'w')))
|
2018-04-27 01:07:26 -07:00
|
|
|
if type(text) == 'table' then
|
|
|
|
-- Byte blob
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @type string[]
|
2018-04-27 01:07:26 -07:00
|
|
|
local bytes = text
|
|
|
|
text = ''
|
|
|
|
for _, char in ipairs(bytes) do
|
|
|
|
text = ('%s%c'):format(text, char)
|
|
|
|
end
|
|
|
|
elseif not no_dedent then
|
2024-04-20 09:06:49 -07:00
|
|
|
text = M.dedent(text)
|
2018-04-27 01:07:26 -07:00
|
|
|
end
|
|
|
|
file:write(text)
|
|
|
|
file:flush()
|
|
|
|
file:close()
|
|
|
|
end
|
|
|
|
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @param name? 'cirrus'|'github'
|
|
|
|
--- @return boolean
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.is_ci(name)
|
2019-08-04 18:46:37 -07:00
|
|
|
local any = (name == nil)
|
2023-12-13 06:04:24 -07:00
|
|
|
luaassert(any or name == 'github' or name == 'cirrus')
|
2020-11-22 12:41:55 -07:00
|
|
|
local gh = ((any or name == 'github') and nil ~= os.getenv('GITHUB_ACTIONS'))
|
2022-10-17 08:16:31 -07:00
|
|
|
local cirrus = ((any or name == 'cirrus') and nil ~= os.getenv('CIRRUS_CI'))
|
|
|
|
return gh or cirrus
|
2018-08-28 13:13:34 -07:00
|
|
|
end
|
|
|
|
|
2020-02-16 20:02:09 -07:00
|
|
|
-- Gets the (tail) contents of `logfile`.
|
2019-07-24 09:12:55 -07:00
|
|
|
-- Also moves the file to "${NVIM_LOG_FILE}.displayed" on CI environments.
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.read_nvim_log(logfile, ci_rename)
|
2020-02-08 19:08:02 -07:00
|
|
|
logfile = logfile or os.getenv('NVIM_LOG_FILE') or '.nvimlog'
|
2024-04-20 09:06:49 -07:00
|
|
|
local is_ci = M.is_ci()
|
2022-02-03 14:30:51 -07:00
|
|
|
local keep = is_ci and 100 or 10
|
2024-04-20 09:06:49 -07:00
|
|
|
local lines = M.read_file_list(logfile, -keep) or {}
|
2023-12-04 15:32:39 -07:00
|
|
|
local log = (
|
|
|
|
('-'):rep(78)
|
|
|
|
.. '\n'
|
|
|
|
.. string.format('$NVIM_LOG_FILE: %s\n', logfile)
|
|
|
|
.. (#lines > 0 and '(last ' .. tostring(keep) .. ' lines)\n' or '(empty)\n')
|
|
|
|
)
|
|
|
|
for _, line in ipairs(lines) do
|
|
|
|
log = log .. line .. '\n'
|
|
|
|
end
|
|
|
|
log = log .. ('-'):rep(78) .. '\n'
|
2020-02-16 20:02:09 -07:00
|
|
|
if is_ci and ci_rename then
|
2019-07-24 09:12:55 -07:00
|
|
|
os.rename(logfile, logfile .. '.displayed')
|
2018-08-28 13:13:34 -07:00
|
|
|
end
|
|
|
|
return log
|
|
|
|
end
|
|
|
|
|
2024-01-12 04:41:09 -07:00
|
|
|
--- @param path string
|
2024-01-15 14:57:11 -07:00
|
|
|
--- @return boolean?
|
2024-04-20 09:06:49 -07:00
|
|
|
function M.mkdir(path)
|
2023-04-04 12:59:06 -07:00
|
|
|
-- 493 is 0755 in decimal
|
2024-01-15 14:57:11 -07:00
|
|
|
return (uv.fs_mkdir(path, 493))
|
2023-04-04 12:59:06 -07:00
|
|
|
end
|
|
|
|
|
2024-04-20 08:44:13 -07:00
|
|
|
--- @param expected any[]
|
|
|
|
--- @param received any[]
|
|
|
|
--- @param kind string
|
|
|
|
--- @return any
|
|
|
|
function M.expect_events(expected, received, kind)
|
|
|
|
if not pcall(M.eq, expected, received) then
|
|
|
|
local msg = 'unexpected ' .. kind .. ' received.\n\n'
|
|
|
|
|
|
|
|
msg = msg .. 'received events:\n'
|
|
|
|
for _, e in ipairs(received) do
|
|
|
|
msg = msg .. ' ' .. vim.inspect(e) .. ';\n'
|
|
|
|
end
|
|
|
|
msg = msg .. '\nexpected events:\n'
|
|
|
|
for _, e in ipairs(expected) do
|
|
|
|
msg = msg .. ' ' .. vim.inspect(e) .. ';\n'
|
|
|
|
end
|
|
|
|
M.fail(msg)
|
|
|
|
end
|
|
|
|
return received
|
|
|
|
end
|
|
|
|
|
|
|
|
--- @param cond boolean
|
|
|
|
--- @param reason? string
|
|
|
|
--- @return boolean
|
|
|
|
function M.skip(cond, reason)
|
|
|
|
if cond then
|
|
|
|
--- @type fun(reason: string)
|
|
|
|
local pending = getfenv(2).pending
|
|
|
|
pending(reason or 'FIXME')
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Calls pending() and returns `true` if the system is too slow to
|
|
|
|
-- run fragile or expensive tests. Else returns `false`.
|
|
|
|
function M.skip_fragile(pending_fn, cond)
|
|
|
|
if pending_fn == nil or type(pending_fn) ~= type(function() end) then
|
|
|
|
error('invalid pending_fn')
|
|
|
|
end
|
|
|
|
if cond then
|
|
|
|
pending_fn('skipped (test is fragile on this system)', function() end)
|
|
|
|
return true
|
|
|
|
elseif os.getenv('TEST_SKIP_FRAGILE') then
|
|
|
|
pending_fn('skipped (TEST_SKIP_FRAGILE)', function() end)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2024-04-20 09:06:49 -07:00
|
|
|
return M
|