diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/functional/api/server_requests_spec.lua | 2 | ||||
-rw-r--r-- | test/functional/core/fileio_spec.lua | 72 | ||||
-rw-r--r-- | test/functional/core/startup_spec.lua | 3 | ||||
-rw-r--r-- | test/functional/options/defaults_spec.lua | 8 | ||||
-rw-r--r-- | test/functional/terminal/api_spec.lua | 4 | ||||
-rw-r--r-- | test/functional/vimscript/server_spec.lua | 17 | ||||
-rw-r--r-- | test/functional/vimscript/timer_spec.lua | 2 | ||||
-rw-r--r-- | test/helpers.lua | 42 | ||||
-rw-r--r-- | test/unit/os/env_spec.lua | 2 | ||||
-rw-r--r-- | test/unit/os/users_spec.lua | 12 | ||||
-rw-r--r-- | test/unit/tempfile_spec.lua | 12 |
11 files changed, 144 insertions, 32 deletions
diff --git a/test/functional/api/server_requests_spec.lua b/test/functional/api/server_requests_spec.lua index cdcf08c348..00a4dd041d 100644 --- a/test/functional/api/server_requests_spec.lua +++ b/test/functional/api/server_requests_spec.lua @@ -170,7 +170,7 @@ describe('server -> client', function() if method == "notification" then eq('done!', eval('rpcrequest('..cid..', "nested")')) elseif method == "nested_done" then - ok(false, 'this should never have been sent') + ok(false, 'never sent', 'sent') end end diff --git a/test/functional/core/fileio_spec.lua b/test/functional/core/fileio_spec.lua index bc025771d1..a4d22685e8 100644 --- a/test/functional/core/fileio_spec.lua +++ b/test/functional/core/fileio_spec.lua @@ -1,22 +1,28 @@ local helpers = require('test.functional.helpers')(after_each) +local assert_log = helpers.assert_log +local assert_nolog = helpers.assert_nolog local clear = helpers.clear local command = helpers.command local eq = helpers.eq +local ok = helpers.ok local feed = helpers.feed local funcs = helpers.funcs local nvim_prog = helpers.nvim_prog local request = helpers.request local retry = helpers.retry local rmdir = helpers.rmdir +local matches = helpers.matches local mkdir = helpers.mkdir local sleep = helpers.sleep local read_file = helpers.read_file +local tmpname = helpers.tmpname local trim = helpers.trim local currentdir = helpers.funcs.getcwd local iswin = helpers.iswin local assert_alive = helpers.assert_alive local expect_exit = helpers.expect_exit +local write_file = helpers.write_file describe('fileio', function() before_each(function() @@ -140,3 +146,69 @@ describe('fileio', function() end) end) +describe('tmpdir', function() + local tmproot_pat = [=[.*[/\\]nvim%.[^/\\]+]=] + local testlog = 'Xtest_tmpdir_log' + local faketmp + + before_each(function() + -- Fake /tmp dir so that we can mess it up. + faketmp = tmpname() + os.remove(faketmp) + mkdir(faketmp) + end) + + after_each(function() + expect_exit(command, ':qall!') + os.remove(testlog) + end) + + it('failure modes', function() + clear({ env={ NVIM_LOG_FILE=testlog, TMPDIR=faketmp, } }) + assert_nolog('tempdir is not a directory', testlog) + assert_nolog('tempdir has invalid permissions', testlog) + + -- Tempfiles typically look like: "…/nvim.<user>/xxx/0". + -- - "…/nvim.<user>/xxx/" is the per-process tmpdir, not shared with other Nvims. + -- - "…/nvim.<user>/" is the tmpdir root, shared by all Nvims (normally). + local tmproot = (funcs.tempname()):match(tmproot_pat) + ok(tmproot:len() > 4, 'tmproot like "nvim.foo"', tmproot) + + -- Test how Nvim handles invalid tmpdir root (by hostile users or accidents). + -- + -- "…/nvim.<user>/" is not a directory: + expect_exit(command, ':qall!') + rmdir(tmproot) + write_file(tmproot, '') -- Not a directory, vim_mktempdir() should skip it. + clear({ env={ NVIM_LOG_FILE=testlog, TMPDIR=faketmp, } }) + matches(tmproot_pat, funcs.stdpath('run')) -- Tickle vim_mktempdir(). + -- Assert that broken tmpdir root was handled. + retry(nil, 1000, function() + assert_log('tempdir root not a directory', testlog, 100) + end) + + -- "…/nvim.<user>/" has wrong permissions: + if iswin() then + return -- TODO(justinmk): need setfperm/getfperm on Windows. #8244 + end + os.remove(testlog) + os.remove(tmproot) + mkdir(tmproot) + funcs.setfperm(tmproot, 'rwxr--r--') -- Invalid permissions, vim_mktempdir() should skip it. + clear({ env={ NVIM_LOG_FILE=testlog, TMPDIR=faketmp, } }) + matches(tmproot_pat, funcs.stdpath('run')) -- Tickle vim_mktempdir(). + -- Assert that broken tmpdir root was handled. + retry(nil, 1000, function() + assert_log('tempdir root has invalid permissions', testlog, 100) + end) + end) + + it('too long', function() + local bigname = ('%s/%s'):format(faketmp, ('x'):rep(666)) + mkdir(bigname) + clear({ env={ NVIM_LOG_FILE=testlog, TMPDIR=bigname, } }) + matches(tmproot_pat, funcs.stdpath('run')) -- Tickle vim_mktempdir(). + local len = (funcs.tempname()):len() + ok(len > 4 and len < 256, '4 < len < 256', tostring(len)) + end) +end) diff --git a/test/functional/core/startup_spec.lua b/test/functional/core/startup_spec.lua index 5ba80a3646..fc9ea72ff2 100644 --- a/test/functional/core/startup_spec.lua +++ b/test/functional/core/startup_spec.lua @@ -399,7 +399,8 @@ describe('startup', function() eq({'ordinary', 'FANCY', 'mittel', 'FANCY after', 'ordinary after'}, exec_lua [[ return _G.test_loadorder ]]) local rtp = meths.get_option'rtp' - ok(startswith(rtp, 'test/functional/fixtures/nvim,test/functional/fixtures/pack/*/start/*,test/functional/fixtures/start/*,test/functional/fixtures,test/functional/fixtures/middle,'), 'rtp='..rtp) + ok(startswith(rtp, 'test/functional/fixtures/nvim,test/functional/fixtures/pack/*/start/*,test/functional/fixtures/start/*,test/functional/fixtures,test/functional/fixtures/middle,'), + 'startswith(…)', 'rtp='..rtp) end) it("handles the correct order with opt packages and after/", function() diff --git a/test/functional/options/defaults_spec.lua b/test/functional/options/defaults_spec.lua index 92873e9937..9244ca0974 100644 --- a/test/functional/options/defaults_spec.lua +++ b/test/functional/options/defaults_spec.lua @@ -9,6 +9,7 @@ local clear = helpers.clear local exc_exec = helpers.exc_exec local eval = helpers.eval local eq = helpers.eq +local ok = helpers.ok local funcs = helpers.funcs local insert = helpers.insert local iswin = helpers.iswin @@ -238,7 +239,7 @@ describe('startup defaults', function() end) end) -describe('XDG-based defaults', function() +describe('XDG defaults', function() -- Need separate describe() blocks to not run clear() twice. -- Do not put before_each() here for the same reasons. @@ -282,6 +283,7 @@ describe('XDG-based defaults', function() eq('.', meths.get_option('viewdir')) eq('.', meths.get_option('directory')) eq('.', meths.get_option('undodir')) + ok((funcs.tempname()):len() > 4) end) end) @@ -306,6 +308,7 @@ describe('XDG-based defaults', function() .. env_sep.. root_path .. ('/b'):rep(2048) .. (env_sep .. root_path .. '/c'):rep(512)), XDG_DATA_HOME=(root_path .. ('/X'):rep(4096)), + XDG_RUNTIME_DIR=(root_path .. ('/X'):rep(4096)), XDG_STATE_HOME=(root_path .. ('/X'):rep(4096)), XDG_DATA_DIRS=(root_path .. ('/A'):rep(2048) .. env_sep .. root_path .. ('/B'):rep(2048) @@ -376,6 +379,7 @@ describe('XDG-based defaults', function() XDG_CONFIG_HOME='$XDG_DATA_HOME', XDG_CONFIG_DIRS='$XDG_DATA_DIRS', XDG_DATA_HOME='$XDG_CONFIG_HOME', + XDG_RUNTIME_DIR='$XDG_RUNTIME_DIR', XDG_STATE_HOME='$XDG_CONFIG_HOME', XDG_DATA_DIRS='$XDG_CONFIG_DIRS', }}) @@ -438,6 +442,7 @@ describe('XDG-based defaults', function() meths.get_option('undodir'):gsub('\\', '/')) eq(('$XDG_CONFIG_HOME/' .. state_dir .. '/view//'), meths.get_option('viewdir'):gsub('\\', '/')) + eq(nil, (funcs.tempname()):match('XDG_RUNTIME_DIR')) end) end) @@ -519,6 +524,7 @@ describe('stdpath()', function() eq(statedir, funcs.fnamemodify(funcs.stdpath('state'), ':t')) eq('table', type(funcs.stdpath('config_dirs'))) eq('table', type(funcs.stdpath('data_dirs'))) + eq('string', type(funcs.stdpath('run'))) assert_alive() -- Check for crash. #8393 end) diff --git a/test/functional/terminal/api_spec.lua b/test/functional/terminal/api_spec.lua index b3a0b8a2c8..5305b8af9c 100644 --- a/test/functional/terminal/api_spec.lua +++ b/test/functional/terminal/api_spec.lua @@ -48,8 +48,8 @@ describe('api', function() {3:-- TERMINAL --} | ]]) - ok(socket_session1:request("nvim_ui_attach", 42, 6, {rgb=true})) - ok(socket_session2:request("nvim_ui_attach", 25, 30, {rgb=true})) + ok((socket_session1:request("nvim_ui_attach", 42, 6, {rgb=true}))) + ok((socket_session2:request("nvim_ui_attach", 25, 30, {rgb=true}))) socket_session1:notify("nvim_input", "\n[socket 1] this is more than 25 columns") socket_session2:notify("nvim_input", "\n[socket 2] input") diff --git a/test/functional/vimscript/server_spec.lua b/test/functional/vimscript/server_spec.lua index ea07be231d..6e95459630 100644 --- a/test/functional/vimscript/server_spec.lua +++ b/test/functional/vimscript/server_spec.lua @@ -5,6 +5,7 @@ local iswin = helpers.iswin local ok = helpers.ok local matches = helpers.matches local pcall_err = helpers.pcall_err +local mkdir = helpers.mkdir local function clear_serverlist() for _, server in pairs(funcs.serverlist()) do @@ -13,9 +14,19 @@ local function clear_serverlist() end describe('server', function() - before_each(clear) + it('serverstart() stores sockets in $XDG_RUNTIME_DIR', function() + local dir = 'Xtest_xdg_run' + mkdir(dir) + clear({ env={ XDG_RUNTIME_DIR=dir } }) + matches(dir, funcs.stdpath('run')) + if not iswin() then + matches(dir, funcs.serverstart()) + end + end) + it('serverstart(), serverstop() does not set $NVIM', function() + clear() local s = eval('serverstart()') assert(s ~= nil and s:len() > 0, "serverstart() returned empty") eq('', eval('$NVIM')) @@ -34,6 +45,7 @@ describe('server', function() end) it('sets v:servername at startup or if all servers were stopped', function() + clear() local initial_server = meths.get_vvar('servername') assert(initial_server ~= nil and initial_server:len() > 0, 'v:servername was not initialized') @@ -62,11 +74,13 @@ describe('server', function() end) it('serverstop() returns false for invalid input', function() + clear() eq(0, eval("serverstop('')")) eq(0, eval("serverstop('bogus-socket-name')")) end) it('parses endpoints', function() + clear() clear_serverlist() eq({}, funcs.serverlist()) @@ -111,6 +125,7 @@ describe('server', function() end) it('serverlist() returns the list of servers', function() + clear() -- There should already be at least one server. local n = eval('len(serverlist())') diff --git a/test/functional/vimscript/timer_spec.lua b/test/functional/vimscript/timer_spec.lua index 20f2afb20f..5463cfb234 100644 --- a/test/functional/vimscript/timer_spec.lua +++ b/test/functional/vimscript/timer_spec.lua @@ -96,7 +96,7 @@ describe('timers', function() nvim_async("command", "let g:val = 0 | let g:c = getchar()") retry(nil, nil, function() local val = eval("g:val") - ok(val >= 2, "expected >= 2, got: "..tostring(val)) + ok(val >= 2, '>= 2', tostring(val)) eq(0, eval("getchar(1)")) end) feed("c") diff --git a/test/helpers.lua b/test/helpers.lua index 9a77ca1956..7ec9beea92 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -57,8 +57,16 @@ end function module.neq(expected, actual, context) return assert.are_not.same(expected, actual, context) end -function module.ok(res, msg) - return assert.is_true(res, msg) + +--- 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 +function module.ok(cond, expected, actual) + assert((not expected and not actual) or (expected and actual), 'if "expected" is given, "actual" is also required') + local msg = expected and ('expected %s, got: %s'):format(expected, tostring(actual)) or nil + return assert(cond, msg) end local function epicfail(state, arguments, _) @@ -77,20 +85,33 @@ function module.matches(pat, actual) error(string.format('Pattern does not match.\nPattern:\n%s\nActual:\n%s', pat, actual)) end ---- Asserts that `pat` matches one or more lines in the tail of $NVIM_LOG_FILE. +--- Asserts that `pat` matches (or *not* if inverse=true) any line in the tail of `logfile`. --- ----@param pat string Lua pattern to search for 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 -function module.assert_log(pat, logfile, nrlines) +---@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 inverse (boolean) Assert that the pattern does NOT match. +function module.assert_log(pat, logfile, nrlines, inverse) logfile = logfile or os.getenv('NVIM_LOG_FILE') or '.nvimlog' + assert(logfile ~= nil, 'no logfile') nrlines = nrlines or 10 + inverse = inverse or false local lines = module.read_file_list(logfile, -nrlines) or {} + 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 - if line:match(pat) then return end + if line:match(pat) then + if inverse then error(msg) else return end + end end - error(string.format('Pattern %q not found in log (last %d lines): %s:\n%s', - pat, nrlines, logfile, ' '..table.concat(lines, '\n '))) + if not inverse then error(msg) end +end + +--- Asserts that `pat` does NOT matche any line in the tail of `logfile`. +--- +--- @see assert_log +function module.assert_nolog(pat, logfile, nrlines) + return module.assert_log(pat, logfile, nrlines, true) end -- Invokes `fn` and returns the error string (with truncated paths), or raises @@ -284,6 +305,7 @@ local function tmpdir_is_local(dir) return not not (dir and string.find(dir, 'Xtest')) end +--- Creates a new temporary file for use by tests. module.tmpname = (function() local seq = 0 local tmpdir = tmpdir_get() diff --git a/test/unit/os/env_spec.lua b/test/unit/os/env_spec.lua index c039b95d16..71177f4c65 100644 --- a/test/unit/os/env_spec.lua +++ b/test/unit/os/env_spec.lua @@ -266,7 +266,7 @@ describe('env.c', function() itp('does not crash #3725', function() local name_out = ffi.new('char[100]') - cimp.os_get_user_name(name_out, 100) + cimp.os_get_username(name_out, 100) local curuser = ffi.string(name_out) local src = to_cstr("~"..curuser.."/Vcs/django-rest-framework/rest_framework/renderers.py") diff --git a/test/unit/os/users_spec.lua b/test/unit/os/users_spec.lua index f92413c7de..679e76fae1 100644 --- a/test/unit/os/users_spec.lua +++ b/test/unit/os/users_spec.lua @@ -48,10 +48,10 @@ describe('users function', function() end) end) - describe('os_get_user_name', function() + describe('os_get_username', function() itp('should write the username into the buffer and return OK', function() local name_out = ffi.new('char[100]') - eq(OK, users.os_get_user_name(name_out, 100)) + eq(OK, users.os_get_username(name_out, 100)) eq(current_username, ffi.string(name_out)) end) end) @@ -73,18 +73,18 @@ describe('users function', function() end) end) - describe('os_get_user_directory', function() + describe('os_get_userdir', function() itp('should return NULL if called with NULL', function() - eq(NULL, users.os_get_user_directory(NULL)) + eq(NULL, users.os_get_userdir(NULL)) end) itp('should return $HOME for the current user', function() local home = os.getenv('HOME') - eq(home, ffi.string((users.os_get_user_directory(current_username)))) + eq(home, ffi.string((users.os_get_userdir(current_username)))) end) itp('should return NULL if the user is not found', function() - eq(NULL, users.os_get_user_directory('neovim_user_not_found_test')) + eq(NULL, users.os_get_userdir('neovim_user_not_found_test')) end) end) end) diff --git a/test/unit/tempfile_spec.lua b/test/unit/tempfile_spec.lua index c05abfd640..44bd19c1d2 100644 --- a/test/unit/tempfile_spec.lua +++ b/test/unit/tempfile_spec.lua @@ -24,7 +24,7 @@ describe('tempfile related functions', function() end describe('vim_gettempdir', function() - itp('returns path to Neovim own temp directory', function() + itp('returns path to Nvim own temp directory', function() local dir = vim_gettempdir() assert.True(dir ~= nil and dir:len() > 0) -- os_file_is_writable returns 2 for a directory which we have rights @@ -36,9 +36,7 @@ describe('tempfile related functions', function() end) itp('returns the same directory on each call', function() - local dir1 = vim_gettempdir() - local dir2 = vim_gettempdir() - eq(dir1, dir2) + eq(vim_gettempdir(), vim_gettempdir()) end) end) @@ -54,12 +52,10 @@ describe('tempfile related functions', function() end) itp('generate different names on each call', function() - local fst = vim_tempname() - local snd = vim_tempname() - neq(fst, snd) + neq(vim_tempname(), vim_tempname()) end) - itp('generate file name in Neovim own temp directory', function() + itp('generate file name in Nvim own temp directory', function() local dir = vim_gettempdir() local file = vim_tempname() eq(string.sub(file, 1, string.len(dir)), dir) |