test/LSP: assert contents of log file

This commit is contained in:
Justin M. Keyes
2020-02-16 19:02:09 -08:00
parent a446fbc8fa
commit 6e13b9d261
6 changed files with 38 additions and 8 deletions

View File

@@ -151,7 +151,7 @@ int server_start(const char *endpoint)
result = socket_watcher_start(watcher, MAX_CONNECTIONS, connection_cb); result = socket_watcher_start(watcher, MAX_CONNECTIONS, connection_cb);
if (result < 0) { if (result < 0) {
WLOG("Failed to start server: %s", uv_strerror(result)); WLOG("Failed to start server: %s: %s", uv_strerror(result), watcher->addr);
socket_watcher_close(watcher, free_server); socket_watcher_close(watcher, free_server);
return result; return result;
} }

View File

@@ -7,7 +7,7 @@ return function(options)
local handler = require 'busted.outputHandlers.TAP'(options) local handler = require 'busted.outputHandlers.TAP'(options)
local suiteEnd = function() local suiteEnd = function()
io.write(global_helpers.read_nvim_log()) io.write(global_helpers.read_nvim_log(nil, true))
return nil, true return nil, true
end end
busted.subscribe({ 'suite', 'end' }, suiteEnd) busted.subscribe({ 'suite', 'end' }, suiteEnd)

View File

@@ -196,7 +196,7 @@ return function(options)
local tests = (testCount == 1 and 'test' or 'tests') local tests = (testCount == 1 and 'test' or 'tests')
local files = (fileCount == 1 and 'file' or 'files') local files = (fileCount == 1 and 'file' or 'files')
io.write(globalTeardown) io.write(globalTeardown)
io.write(global_helpers.read_nvim_log()) io.write(global_helpers.read_nvim_log(nil, true))
io.write(suiteEndString:format(testCount, tests, fileCount, files, elapsedTime_ms)) io.write(suiteEndString:format(testCount, tests, fileCount, files, elapsedTime_ms))
io.write(getSummaryString()) io.write(getSummaryString())
io.flush() io.flush()

View File

@@ -1,6 +1,16 @@
local protocol = require 'vim.lsp.protocol' local protocol = require 'vim.lsp.protocol'
-- Logs to $NVIM_LOG_FILE.
--
-- TODO(justinmk): remove after https://github.com/neovim/neovim/pull/7062
local function log(loglevel, area, msg)
vim.fn.writefile(
{string.format('%s %s: %s', loglevel, area, msg)},
vim.env.NVIM_LOG_FILE,
'a')
end
local function message_parts(sep, ...) local function message_parts(sep, ...)
local parts = {} local parts = {}
for i = 1, select("#", ...) do for i = 1, select("#", ...) do
@@ -422,7 +432,7 @@ local kill_timer = vim.loop.new_timer()
kill_timer:start(_G.TIMEOUT or 1e3, 0, function() kill_timer:start(_G.TIMEOUT or 1e3, 0, function()
kill_timer:stop() kill_timer:stop()
kill_timer:close() kill_timer:close()
-- TODO: log('TIMEOUT') log('ERROR', 'LSP', 'TIMEOUT')
io.stderr:write("TIMEOUT") io.stderr:write("TIMEOUT")
os.exit(100) os.exit(100)
end) end)
@@ -433,7 +443,7 @@ local status, err = pcall(assert(tests[test_name], "Test not found"))
kill_timer:stop() kill_timer:stop()
kill_timer:close() kill_timer:close()
if not status then if not status then
-- TODO: log(err) log('ERROR', 'LSP', tostring(err))
io.stderr:write(err) io.stderr:write(err)
os.exit(101) os.exit(101)
end end

View File

@@ -1,11 +1,13 @@
local helpers = require('test.functional.helpers')(after_each) local helpers = require('test.functional.helpers')(after_each)
local assert_log = helpers.assert_log
local clear = helpers.clear local clear = helpers.clear
local buf_lines = helpers.buf_lines local buf_lines = helpers.buf_lines
local dedent = helpers.dedent local dedent = helpers.dedent
local exec_lua = helpers.exec_lua local exec_lua = helpers.exec_lua
local eq = helpers.eq local eq = helpers.eq
local eq_dumplog = helpers.eq_dumplog local eq_dumplog = helpers.eq_dumplog
local pesc = helpers.pesc
local insert = helpers.insert local insert = helpers.insert
local retry = helpers.retry local retry = helpers.retry
local NIL = helpers.NIL local NIL = helpers.NIL
@@ -229,6 +231,8 @@ describe('LSP', function()
on_exit = function(code, signal) on_exit = function(code, signal)
eq_dumplog(fake_lsp_logfile, 101, code, "exit code") -- See fake-lsp-server.lua eq_dumplog(fake_lsp_logfile, 101, code, "exit code") -- See fake-lsp-server.lua
eq_dumplog(fake_lsp_logfile, 0, signal, "exit signal") eq_dumplog(fake_lsp_logfile, 0, signal, "exit signal")
assert_log(pesc([[assert_eq failed: left == "\"shutdown\"", right == "\"test\""]]),
fake_lsp_logfile)
end; end;
on_callback = function(...) on_callback = function(...)
eq(table.remove(expected_callbacks), {...}, "expected callback") eq(table.remove(expected_callbacks), {...}, "expected callback")

View File

@@ -82,6 +82,22 @@ function module.matches(pat, actual)
error(string.format('Pattern does not match.\nPattern:\n%s\nActual:\n%s', pat, actual)) error(string.format('Pattern does not match.\nPattern:\n%s\nActual:\n%s', pat, actual))
end end
--- Asserts that `pat` matches one or more lines in the tail of $NVIM_LOG_FILE.
---
--@param pat (string) Lua pattern to search for in the log file.
--@param logfile (string, default=$NVIM_LOG_FILE) full path to log file.
function module.assert_log(pat, logfile)
logfile = logfile or os.getenv('NVIM_LOG_FILE') or '.nvimlog'
local nrlines = 10
local lines = module.read_file_list(logfile, -nrlines) or {}
for _,line in ipairs(lines) do
if line:match(pat) then return end
end
local logtail = module.read_nvim_log(logfile)
error(string.format('Pattern %q not found in log (last %d lines): %s:\n%s',
pat, nrlines, logfile, logtail))
end
-- Invokes `fn` and returns the error string (may truncate full paths), or -- Invokes `fn` and returns the error string (may truncate full paths), or
-- raises an error if `fn` succeeds. -- raises an error if `fn` succeeds.
-- --
@@ -745,9 +761,9 @@ function module.isCI(name)
end end
-- Gets the contents of `logfile` for printing to the build log. -- Gets the (tail) contents of `logfile`.
-- Also moves the file to "${NVIM_LOG_FILE}.displayed" on CI environments. -- Also moves the file to "${NVIM_LOG_FILE}.displayed" on CI environments.
function module.read_nvim_log(logfile) function module.read_nvim_log(logfile, ci_rename)
logfile = logfile or os.getenv('NVIM_LOG_FILE') or '.nvimlog' logfile = logfile or os.getenv('NVIM_LOG_FILE') or '.nvimlog'
local is_ci = module.isCI() local is_ci = module.isCI()
local keep = is_ci and 999 or 10 local keep = is_ci and 999 or 10
@@ -759,7 +775,7 @@ function module.read_nvim_log(logfile)
log = log..line..'\n' log = log..line..'\n'
end end
log = log..('-'):rep(78)..'\n' log = log..('-'):rep(78)..'\n'
if is_ci then if is_ci and ci_rename then
os.rename(logfile, logfile .. '.displayed') os.rename(logfile, logfile .. '.displayed')
end end
return log return log