build: enable lintlua for test/unit/ dir #26396

Problem:
Not all Lua code is checked by stylua. Automating code-style is an
important mechanism for reducing time spent on accidental
(non-essential) complexity.

Solution:
- Enable lintlua for `test/unit/` directory.
- TODO: only `test/functional/` remains unchecked.

previous: 45fe4d11ad
previous: 517f0cc634
This commit is contained in:
Justin M. Keyes
2023-12-04 14:32:39 -08:00
committed by GitHub
parent 45fe4d11ad
commit c3836e40a2
51 changed files with 4067 additions and 2764 deletions

View File

@@ -49,7 +49,7 @@ local function child_call(func, ret)
return function(...)
local child_calls = child_calls_mod or child_calls_init
if child_pid ~= 0 then
child_calls[#child_calls + 1] = {func=func, args={...}}
child_calls[#child_calls + 1] = { func = func, args = { ... } }
return ret
else
return func(...)
@@ -62,7 +62,7 @@ end
--- @param func function
local function child_call_once(func, ...)
if child_pid ~= 0 then
child_calls_mod_once[#child_calls_mod_once + 1] = { func = func, args = {...} }
child_calls_mod_once[#child_calls_mod_once + 1] = { func = func, args = { ... } }
else
func(...)
end
@@ -75,7 +75,7 @@ local child_cleanups_mod_once = nil --- @type ChildCall[]?
local function child_cleanup_once(func, ...)
local child_cleanups = child_cleanups_mod_once
if child_pid ~= 0 then
child_cleanups[#child_cleanups + 1] = {func=func, args={...}}
child_cleanups[#child_cleanups + 1] = { func = func, args = { ... } }
else
func(...)
end
@@ -133,25 +133,28 @@ local pragma_pack_id = 1
local function filter_complex_blocks(body)
local result = {} --- @type string[]
for line in body:gmatch("[^\r\n]+") do
if not (string.find(line, "(^)", 1, true) ~= nil
or string.find(line, "_ISwupper", 1, true)
or string.find(line, "_Float")
or string.find(line, "__s128")
or string.find(line, "__u128")
or string.find(line, "msgpack_zone_push_finalizer")
or string.find(line, "msgpack_unpacker_reserve_buffer")
or string.find(line, "value_init_")
or string.find(line, "UUID_NULL") -- static const uuid_t UUID_NULL = {...}
or string.find(line, "inline _Bool")) then
for line in body:gmatch('[^\r\n]+') do
if
not (
string.find(line, '(^)', 1, true) ~= nil
or string.find(line, '_ISwupper', 1, true)
or string.find(line, '_Float')
or string.find(line, '__s128')
or string.find(line, '__u128')
or string.find(line, 'msgpack_zone_push_finalizer')
or string.find(line, 'msgpack_unpacker_reserve_buffer')
or string.find(line, 'value_init_')
or string.find(line, 'UUID_NULL') -- static const uuid_t UUID_NULL = {...}
or string.find(line, 'inline _Bool')
)
then
result[#result + 1] = line
end
end
return table.concat(result, "\n")
return table.concat(result, '\n')
end
local cdef = ffi.cdef
local cimportstr
@@ -184,9 +187,8 @@ local function cimport(...)
previous_defines = previous_defines_init
cdefs = cdefs_init
end
for _, path in ipairs({...}) do
if not (path:sub(1, 1) == '/' or path:sub(1, 1) == '.'
or path:sub(2, 2) == ':') then
for _, path in ipairs({ ... }) do
if not (path:sub(1, 1) == '/' or path:sub(1, 1) == '.' or path:sub(2, 2) == ':') then
path = './' .. path
end
if not preprocess_cache[path] then
@@ -205,15 +207,15 @@ local function cimport(...)
body = filter_complex_blocks(body)
-- add the formatted lines to a set
local new_cdefs = Set:new()
for line in body:gmatch("[^\r\n]+") do
for line in body:gmatch('[^\r\n]+') do
line = trim(line)
-- give each #pragma pack an unique id, so that they don't get removed
-- if they are inserted into the set
-- (they are needed in the right order with the struct definitions,
-- otherwise luajit has wrong memory layouts for the sturcts)
if line:match("#pragma%s+pack") then
if line:match('#pragma%s+pack') then
--- @type string
line = line .. " // " .. pragma_pack_id
line = line .. ' // ' .. pragma_pack_id
pragma_pack_id = pragma_pack_id + 1
end
new_cdefs:add(line)
@@ -277,13 +279,13 @@ end
local function alloc_log_new()
local log = {
log={}, --- @type ChildCallLog[]
lib=cimport('./src/nvim/memory.h'), --- @type table<string,function>
original_functions={}, --- @type table<string,function>
null={['\0:is_null']=true},
log = {}, --- @type ChildCallLog[]
lib = cimport('./src/nvim/memory.h'), --- @type table<string,function>
original_functions = {}, --- @type table<string,function>
null = { ['\0:is_null'] = true },
}
local allocator_functions = {'malloc', 'free', 'calloc', 'realloc'}
local allocator_functions = { 'malloc', 'free', 'calloc', 'realloc' }
function log:save_original_functions()
for _, funcname in ipairs(allocator_functions) do
@@ -301,7 +303,7 @@ local function alloc_log_new()
local kk = k
self.lib['mem_' .. k] = function(...)
--- @type ChildCallLog
local log_entry = { func = kk, args = {...} }
local log_entry = { func = kk, args = { ... } }
self.log[#self.log + 1] = log_entry
if kk == 'free' then
self.original_functions[kk](...)
@@ -314,7 +316,9 @@ local function alloc_log_new()
log_entry.args[i] = self.null
end
end
if self.hook then self:hook(log_entry) end
if self.hook then
self:hook(log_entry)
end
if log_entry.ret then
return log_entry.ret
end
@@ -355,7 +359,7 @@ local function alloc_log_new()
end
end
table.sort(toremove)
for i = #toremove,1,-1 do
for i = #toremove, 1, -1 do
table.remove(self.log, toremove[i])
end
end
@@ -365,11 +369,9 @@ local function alloc_log_new()
log:set_mocks()
end
function log:before_each()
end
function log:before_each() end
function log:after_each()
end
function log:after_each() end
log:setup()
@@ -397,13 +399,12 @@ function sc.fork()
end
function sc.pipe()
local ret = ffi.new('int[2]', {-1, -1})
local ret = ffi.new('int[2]', { -1, -1 })
ffi.errno(0)
local res = ffi.C.pipe(ret)
if (res ~= 0) then
if res ~= 0 then
local err = ffi.errno(0)
assert(res == 0, ("pipe() error: %u: %s"):format(
err, ffi.string(ffi.C.strerror(err))))
assert(res == 0, ('pipe() error: %u: %s'):format(err, ffi.string(ffi.C.strerror(err))))
end
assert(ret[0] ~= -1 and ret[1] ~= -1)
return ret[0], ret[1]
@@ -411,19 +412,16 @@ end
--- @return string
function sc.read(rd, len)
local ret = ffi.new('char[?]', len, {0})
local ret = ffi.new('char[?]', len, { 0 })
local total_bytes_read = 0
ffi.errno(0)
while total_bytes_read < len do
local bytes_read = tonumber(ffi.C.read(
rd,
ffi.cast('void*', ret + total_bytes_read),
len - total_bytes_read))
local bytes_read =
tonumber(ffi.C.read(rd, ffi.cast('void*', ret + total_bytes_read), len - total_bytes_read))
if bytes_read == -1 then
local err = ffi.errno(0)
if err ~= ffi.C.kPOSIXErrnoEINTR then
assert(false, ("read() error: %u: %s"):format(
err, ffi.string(ffi.C.strerror(err))))
assert(false, ('read() error: %u: %s'):format(err, ffi.string(ffi.C.strerror(err))))
end
elseif bytes_read == 0 then
break
@@ -439,15 +437,16 @@ function sc.write(wr, s)
local total_bytes_written = 0
ffi.errno(0)
while total_bytes_written < #s do
local bytes_written = tonumber(ffi.C.write(
wr,
ffi.cast('void*', wbuf + total_bytes_written),
#s - total_bytes_written))
local bytes_written = tonumber(
ffi.C.write(wr, ffi.cast('void*', wbuf + total_bytes_written), #s - total_bytes_written)
)
if bytes_written == -1 then
local err = ffi.errno(0)
if err ~= ffi.C.kPOSIXErrnoEINTR then
assert(false, ("write() error: %u: %s ('%s')"):format(
err, ffi.string(ffi.C.strerror(err)), s))
assert(
false,
("write() error: %u: %s ('%s')"):format(err, ffi.string(ffi.C.strerror(err)), s)
)
end
elseif bytes_written == 0 then
break
@@ -464,7 +463,7 @@ sc.close = ffi.C.close
--- @return integer
function sc.wait(pid)
ffi.errno(0)
local stat_loc = ffi.new('int[1]', {0})
local stat_loc = ffi.new('int[1]', { 0 })
while true do
local r = ffi.C.waitpid(pid, stat_loc, ffi.C.kPOSIXWaitWUNTRACED)
if r == -1 then
@@ -472,8 +471,7 @@ function sc.wait(pid)
if err == ffi.C.kPOSIXErrnoECHILD then
break
elseif err ~= ffi.C.kPOSIXErrnoEINTR then
assert(false, ("waitpid() error: %u: %s"):format(
err, ffi.string(ffi.C.strerror(err))))
assert(false, ('waitpid() error: %u: %s'):format(err, ffi.string(ffi.C.strerror(err))))
end
else
assert(r == pid)
@@ -489,7 +487,7 @@ sc.exit = ffi.C._exit
local function format_list(lst)
local ret = {} --- @type string[]
for _, v in ipairs(lst) do
ret[#ret+1] = assert:format({v, n=1})[1]
ret[#ret + 1] = assert:format({ v, n = 1 })[1]
end
return table.concat(ret, ', ')
end
@@ -498,9 +496,8 @@ if os.getenv('NVIM_TEST_PRINT_SYSCALLS') == '1' then
for k_, v_ in pairs(sc) do
(function(k, v)
sc[k] = function(...)
local rets = {v(...)}
io.stderr:write(('%s(%s) = %s\n'):format(k, format_list({...}),
format_list(rets)))
local rets = { v(...) }
io.stderr:write(('%s(%s) = %s\n'):format(k, format_list({ ... }), format_list(rets)))
return unpack(rets)
end
end)(k_, v_)
@@ -512,9 +509,13 @@ local function just_fail(_)
end
say:set('assertion.just_fail.positive', '%s')
say:set('assertion.just_fail.negative', '%s')
assert:register('assertion', 'just_fail', just_fail,
'assertion.just_fail.positive',
'assertion.just_fail.negative')
assert:register(
'assertion',
'just_fail',
just_fail,
'assertion.just_fail.positive',
'assertion.just_fail.negative'
)
local hook_fnamelen = 30
local hook_sfnamelen = 30
@@ -561,7 +562,7 @@ local function child_sethook(wr)
local info = nil --- @type debuginfo?
if use_prev then
info = prev_info
elseif reason ~= 'tail return' then -- tail return
elseif reason ~= 'tail return' then -- tail return
info = debug.getinfo(2, 'nSl')
end
@@ -609,17 +610,20 @@ local function child_sethook(wr)
-- assert(-1 <= lnum and lnum <= 99999)
local lnum_s = lnum == -1 and 'nknwn' or ('%u'):format(lnum)
--- @type string
local msg = ( -- lua does not support %*
local msg = ( -- lua does not support %*
''
.. msgchar
.. whatchar
.. namewhatchar
.. ' '
.. source .. (' '):rep(hook_sfnamelen - #source)
.. source
.. (' '):rep(hook_sfnamelen - #source)
.. ':'
.. funcname .. (' '):rep(hook_fnamelen - #funcname)
.. funcname
.. (' '):rep(hook_fnamelen - #funcname)
.. ':'
.. ('0'):rep(hook_numlen - #lnum_s) .. lnum_s
.. ('0'):rep(hook_numlen - #lnum_s)
.. lnum_s
.. '\n'
)
-- eq(hook_msglen, #msg)
@@ -742,16 +746,16 @@ local function itp_parent(rd, pid, allow_failure, location)
sc.close(rd)
if not ok then
if allow_failure then
io.stderr:write('Errorred out ('..status..'):\n' .. tostring(emsg) .. '\n')
io.stderr:write('Errorred out (' .. status .. '):\n' .. tostring(emsg) .. '\n')
os.execute([[
sh -c "source ci/common/test.sh
check_core_dumps --delete \"]] .. Paths.test_luajit_prg .. [[\""]])
else
error(tostring(emsg)..'\nexit code: '..status)
error(tostring(emsg) .. '\nexit code: ' .. status)
end
elseif status ~= 0 then
if not allow_failure then
error("child process errored out with status "..status.."!\n\n"..location)
error('child process errored out with status ' .. status .. '!\n\n' .. location)
end
end
end
@@ -760,7 +764,9 @@ local function gen_itp(it)
child_calls_mod = {}
child_calls_mod_once = {}
child_cleanups_mod_once = {}
preprocess_cache_mod = map(function(v) return v end, preprocess_cache_init)
preprocess_cache_mod = map(function(v)
return v
end, preprocess_cache_init)
previous_defines_mod = previous_defines_init
cdefs_mod = cdefs_init:copy()
local function itp(name, func, allow_failure)
@@ -794,8 +800,7 @@ local function cppimport(path)
return cimport(Paths.test_source_path .. '/test/includes/pre/' .. path)
end
cimport('./src/nvim/types.h', './src/nvim/main.h', './src/nvim/os/time.h',
'./src/nvim/os/fs.h')
cimport('./src/nvim/types.h', './src/nvim/main.h', './src/nvim/os/time.h', './src/nvim/os/fs.h')
local function conv_enum(etab, eval)
local n = tonumber(eval)
@@ -844,7 +849,7 @@ local function ptr2addr(ptr)
return tonumber(ffi.cast('intptr_t', ffi.cast('void *', ptr)))
end
local s = ffi.new('char[64]', {0})
local s = ffi.new('char[64]', { 0 })
local function ptr2key(ptr)
ffi.C.snprintf(s, ffi.sizeof(s), '%p', ffi.cast('void *', ptr))
@@ -853,7 +858,9 @@ end
local function is_asan()
cimport('./src/nvim/version.h')
local status, res = pcall(function() return lib.version_cflags end)
local status, res = pcall(function()
return lib.version_cflags
end)
if status then
return ffi.string(res):match('-fsanitize=[a-z,]*address')
else