aboutsummaryrefslogtreecommitdiff
path: root/test/helpers.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/helpers.lua')
-rw-r--r--test/helpers.lua76
1 files changed, 69 insertions, 7 deletions
diff --git a/test/helpers.lua b/test/helpers.lua
index a774a67df3..6ef7a456f4 100644
--- a/test/helpers.lua
+++ b/test/helpers.lua
@@ -1,6 +1,7 @@
local assert = require('luassert')
local luv = require('luv')
local lfs = require('lfs')
+local relpath = require('pl.path').relpath
local quote_me = '[^.%w%+%-%@%_%/]' -- complement (needn't quote)
local function shell_quote(str)
@@ -45,8 +46,8 @@ local check_logs_useless_lines = {
['See README_MISSING_SYSCALL_OR_IOCTL for guidance']=3,
}
-local function eq(expected, actual)
- return assert.are.same(expected, actual)
+local function eq(expected, actual, ctx)
+ return assert.are.same(expected, actual, ctx)
end
local function neq(expected, actual)
return assert.are_not.same(expected, actual)
@@ -54,6 +55,9 @@ end
local function ok(res)
return assert.is_true(res)
end
+local function near(actual, expected, tolerance)
+ return assert.is.near(actual, expected, tolerance)
+end
local function matches(pat, actual)
if nil ~= string.match(actual, pat) then
return true
@@ -133,7 +137,6 @@ local function check_logs()
fd:close()
os.remove(file)
if #lines > 0 then
- -- local out = os.getenv('TRAVIS_CI_BUILD') and io.stdout or io.stderr
local out = io.stdout
out:write(start_msg .. '\n')
out:write('= ' .. table.concat(lines, '\n= ') .. '\n')
@@ -242,7 +245,7 @@ local function check_cores(app, force)
-- Workspace-local $TMPDIR, scrubbed and pattern-escaped.
-- "./Xtest-tmpdir/" => "Xtest%-tmpdir"
local local_tmpdir = (tmpdir_is_local(tmpdir_get())
- and tmpdir_get():gsub('^[ ./]+',''):gsub('%/+$',''):gsub('([^%w])', '%%%1')
+ and relpath(tmpdir_get()):gsub('^[ ./]+',''):gsub('%/+$',''):gsub('([^%w])', '%%%1')
or nil)
local db_cmd
if hasenv('NVIM_TEST_CORE_GLOB_DIRECTORY') then
@@ -643,8 +646,38 @@ local function hexdump(str)
return dump .. hex .. string.rep(" ", 8 - len % 8) .. asc
end
-local function read_file(name)
- local file = io.open(name, 'r')
+-- Reads text lines from `filename` into a table.
+--
+-- filename: path to file
+-- start: start line (1-indexed), negative means "lines before end" (tail)
+local function read_file_list(filename, start)
+ 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
+ local lines = {}
+ local i = 1
+ for line in file:lines() do
+ if i >= start then
+ table.insert(lines, line)
+ if #lines > maxlines then
+ table.remove(lines, 1)
+ end
+ end
+ i = i + 1
+ end
+ file:close()
+ return lines
+end
+
+-- Reads the entire contents of `filename` into a string.
+--
+-- filename: path to file
+local function read_file(filename)
+ local file = io.open(filename, 'r')
if not file then
return nil
end
@@ -671,6 +704,32 @@ local function write_file(name, text, no_dedent, append)
file:close()
end
+local function isCI()
+ local is_travis = nil ~= os.getenv('TRAVIS')
+ local is_appveyor = nil ~= os.getenv('APPVEYOR')
+ local is_quickbuild = nil ~= os.getenv('PR_NUMBER')
+ return is_travis or is_appveyor or is_quickbuild
+end
+
+-- Gets the contents of $NVIM_LOG_FILE for printing to the build log.
+-- Also removes the file, if the current environment looks like CI.
+local function read_nvim_log()
+ local logfile = os.getenv('NVIM_LOG_FILE') or '.nvimlog'
+ local keep = isCI() and 999 or 10
+ local lines = read_file_list(logfile, -keep) or {}
+ 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'
+ if isCI() then
+ os.remove(logfile)
+ end
+ return log
+end
+
local module = {
REMOVE_THIS = REMOVE_THIS,
argss_to_cmd = argss_to_cmd,
@@ -694,14 +753,17 @@ local module = {
map = map,
matches = matches,
mergedicts_copy = mergedicts_copy,
+ near = near,
neq = neq,
ok = ok,
popen_r = popen_r,
popen_w = popen_w,
read_file = read_file,
+ read_file_list = read_file_list,
+ read_nvim_log = read_nvim_log,
repeated_read_cmd = repeated_read_cmd,
- sleep = sleep,
shallowcopy = shallowcopy,
+ sleep = sleep,
table_flatten = table_flatten,
tmpname = tmpname,
uname = uname,