aboutsummaryrefslogtreecommitdiff
path: root/test/testutil.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/testutil.lua')
-rw-r--r--test/testutil.lua54
1 files changed, 43 insertions, 11 deletions
diff --git a/test/testutil.lua b/test/testutil.lua
index 439f13cf49..a920f658a1 100644
--- a/test/testutil.lua
+++ b/test/testutil.lua
@@ -16,7 +16,7 @@ local function shell_quote(str)
return str
end
---- This module uses functions from the context of the test runner.
+--- Functions executing in the context of the test runner (not the current nvim test session).
--- @class test.testutil
local M = {
paths = Paths,
@@ -42,6 +42,29 @@ function M.isdir(path)
return stat.type == 'directory'
end
+--- (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
+
--- @param ... string|string[]
--- @return string
function M.argss_to_cmd(...)
@@ -143,7 +166,7 @@ end
---
---@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
+---@param nrlines? (number) Search up to this many log lines (default 10)
---@param inverse? (boolean) Assert that the pattern does NOT match.
function M.assert_log(pat, logfile, nrlines, inverse)
logfile = logfile or os.getenv('NVIM_LOG_FILE') or '.nvimlog'
@@ -369,7 +392,7 @@ function M.check_logs()
)
end
-function M.sysname()
+local function sysname()
return uv.os_uname().sysname:lower()
end
@@ -380,11 +403,11 @@ function M.is_os(s)
error('unknown platform: ' .. tostring(s))
end
return not not (
- (s == 'win' and (M.sysname():find('windows') or M.sysname():find('mingw')))
- or (s == 'mac' and M.sysname() == 'darwin')
- or (s == 'freebsd' and M.sysname() == 'freebsd')
- or (s == 'openbsd' and M.sysname() == 'openbsd')
- or (s == 'bsd' and M.sysname():find('bsd'))
+ (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'))
)
end
@@ -402,18 +425,27 @@ end
local tmpname_id = 0
local tmpdir = tmpdir_get()
---- Creates a new temporary file for use by tests.
-function M.tmpname()
+--- 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), and writes the file unless `create=false`.
+---
+---@param create? boolean (default true) Write the file.
+function M.tmpname(create)
if tmpdir_is_local(tmpdir) then
-- Cannot control os.tmpname() dir, so hack our own tmpname() impl.
tmpname_id = tmpname_id + 1
-- "…/Xtest_tmpdir/T42.7"
local fname = ('%s/%s.%d'):format(tmpdir, (_G._nvim_test_id or 'nvim-test'), tmpname_id)
- io.open(fname, 'w'):close()
+ if create ~= false then
+ io.open(fname, 'w'):close()
+ end
return fname
end
local fname = os.tmpname()
+ if create == false then
+ os.remove(fname)
+ end
+
if M.is_os('win') and fname:sub(1, 2) == '\\s' then
-- In Windows tmpname() returns a filename starting with
-- special sequence \s, prepend $TEMP path