test: tmpname() can skip file creation

This commit is contained in:
Justin M. Keyes 2024-09-02 19:14:09 +02:00
parent ef8067a19d
commit 137f98cf64
4 changed files with 25 additions and 49 deletions

View File

@ -6,17 +6,11 @@ require('os')
local eval = n.eval local eval = n.eval
local command = n.command local command = n.command
local eq, neq = t.eq, t.neq local eq, neq = t.eq, t.neq
local tempfile = t.tmpname() local tempfile = t.tmpname(false)
local source = n.source local source = n.source
local matches = t.matches local matches = t.matches
local read_file = t.read_file local read_file = t.read_file
-- tmpname() also creates the file on POSIX systems. Remove it again.
-- We just need the name, ignoring any race conditions.
if uv.fs_stat(tempfile).uid then
os.remove(tempfile)
end
local function assert_file_exists(filepath) local function assert_file_exists(filepath)
neq(nil, uv.fs_stat(filepath).uid) neq(nil, uv.fs_stat(filepath).uid)
end end
@ -31,6 +25,7 @@ describe(':profile', function()
after_each(function() after_each(function()
n.expect_exit(command, 'qall!') n.expect_exit(command, 'qall!')
if uv.fs_stat(tempfile).uid ~= nil then if uv.fs_stat(tempfile).uid ~= nil then
-- Delete the tempfile. We just need the name, ignoring any race conditions.
os.remove(tempfile) os.remove(tempfile)
end end
end) end)

View File

@ -74,8 +74,7 @@ describe('vim.loader', function()
vim.loader.enable() vim.loader.enable()
]] ]]
local tmp = t.tmpname() local tmp = t.tmpname(false)
assert(os.remove(tmp))
assert(t.mkdir(tmp)) assert(t.mkdir(tmp))
assert(t.mkdir(tmp .. '/%')) assert(t.mkdir(tmp .. '/%'))
local tmp1 = tmp .. '/%/x' local tmp1 = tmp .. '/%/x'

View File

@ -800,8 +800,7 @@ describe('LSP', function()
eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler') eq(table.remove(expected_handlers), { err, result, ctx }, 'expected handler')
if ctx.method == 'start' then if ctx.method == 'start' then
local tmpfile_old = tmpname() local tmpfile_old = tmpname()
local tmpfile_new = tmpname() local tmpfile_new = tmpname(false)
os.remove(tmpfile_new)
exec_lua(function(oldname, newname) exec_lua(function(oldname, newname)
_G.BUFFER = vim.api.nvim_get_current_buf() _G.BUFFER = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_name(_G.BUFFER, oldname) vim.api.nvim_buf_set_name(_G.BUFFER, oldname)
@ -2370,8 +2369,7 @@ describe('LSP', function()
end) end)
it('Supports file creation with CreateFile payload', function() it('Supports file creation with CreateFile payload', function()
local tmpfile = tmpname() local tmpfile = tmpname(false)
os.remove(tmpfile) -- Should not exist, only interested in a tmpname
local uri = exec_lua('return vim.uri_from_fname(...)', tmpfile) local uri = exec_lua('return vim.uri_from_fname(...)', tmpfile)
local edit = { local edit = {
documentChanges = { documentChanges = {
@ -2388,9 +2386,7 @@ describe('LSP', function()
it( it(
'Supports file creation in folder that needs to be created with CreateFile payload', 'Supports file creation in folder that needs to be created with CreateFile payload',
function() function()
local tmpfile = tmpname() local tmpfile = tmpname(false) .. '/dummy/x/'
os.remove(tmpfile) -- Should not exist, only interested in a tmpname
tmpfile = tmpfile .. '/dummy/x/'
local uri = exec_lua('return vim.uri_from_fname(...)', tmpfile) local uri = exec_lua('return vim.uri_from_fname(...)', tmpfile)
local edit = { local edit = {
documentChanges = { documentChanges = {
@ -2468,8 +2464,7 @@ describe('LSP', function()
end) end)
it('DeleteFile fails if file does not exist and ignoreIfNotExists is false', function() it('DeleteFile fails if file does not exist and ignoreIfNotExists is false', function()
local tmpfile = tmpname() local tmpfile = tmpname(false)
os.remove(tmpfile)
local uri = exec_lua('return vim.uri_from_fname(...)', tmpfile) local uri = exec_lua('return vim.uri_from_fname(...)', tmpfile)
local edit = { local edit = {
documentChanges = { documentChanges = {
@ -2493,8 +2488,7 @@ describe('LSP', function()
it('Can rename an existing file', function() it('Can rename an existing file', function()
local old = tmpname() local old = tmpname()
write_file(old, 'Test content') write_file(old, 'Test content')
local new = tmpname() local new = tmpname(false)
os.remove(new) -- only reserve the name, file must not exist for the test scenario
local lines = exec_lua(function(old0, new0) local lines = exec_lua(function(old0, new0)
local old_bufnr = vim.fn.bufadd(old0) local old_bufnr = vim.fn.bufadd(old0)
vim.fn.bufload(old_bufnr) vim.fn.bufload(old_bufnr)
@ -2514,10 +2508,8 @@ describe('LSP', function()
it('Can rename a directory', function() it('Can rename a directory', function()
-- only reserve the name, file must not exist for the test scenario -- only reserve the name, file must not exist for the test scenario
local old_dir = tmpname() local old_dir = tmpname(false)
local new_dir = tmpname() local new_dir = tmpname(false)
os.remove(old_dir)
os.remove(new_dir)
n.mkdir_p(old_dir) n.mkdir_p(old_dir)
@ -2542,10 +2534,8 @@ describe('LSP', function()
end) end)
it('Does not touch buffers that do not match path prefix', function() it('Does not touch buffers that do not match path prefix', function()
local old = tmpname() local old = tmpname(false)
local new = tmpname() local new = tmpname(false)
os.remove(old)
os.remove(new)
n.mkdir_p(old) n.mkdir_p(old)
eq( eq(
@ -2604,8 +2594,7 @@ describe('LSP', function()
it('Maintains undo information for loaded buffer', function() it('Maintains undo information for loaded buffer', function()
local old = tmpname() local old = tmpname()
write_file(old, 'line') write_file(old, 'line')
local new = tmpname() local new = tmpname(false)
os.remove(new)
local undo_kept = exec_lua(function(old0, new0) local undo_kept = exec_lua(function(old0, new0)
vim.opt.undofile = true vim.opt.undofile = true
@ -2629,8 +2618,7 @@ describe('LSP', function()
it('Maintains undo information for unloaded buffer', function() it('Maintains undo information for unloaded buffer', function()
local old = tmpname() local old = tmpname()
write_file(old, 'line') write_file(old, 'line')
local new = tmpname() local new = tmpname(false)
os.remove(new)
local undo_kept = exec_lua(function(old0, new0) local undo_kept = exec_lua(function(old0, new0)
vim.opt.undofile = true vim.opt.undofile = true
@ -2651,8 +2639,7 @@ describe('LSP', function()
it('Does not rename file when it conflicts with a buffer without file', function() it('Does not rename file when it conflicts with a buffer without file', function()
local old = tmpname() local old = tmpname()
write_file(old, 'Old File') write_file(old, 'Old File')
local new = tmpname() local new = tmpname(false)
os.remove(new)
local lines = exec_lua(function(old0, new0) local lines = exec_lua(function(old0, new0)
local old_buf = vim.fn.bufadd(old0) local old_buf = vim.fn.bufadd(old0)
@ -5023,13 +5010,7 @@ describe('LSP', function()
end) end)
it('can connect to lsp server via pipe or domain_socket', function() it('can connect to lsp server via pipe or domain_socket', function()
local tmpfile --- @type string local tmpfile = is_os('win') and '\\\\.\\\\pipe\\pipe.test' or tmpname(false)
if is_os('win') then
tmpfile = '\\\\.\\\\pipe\\pipe.test'
else
tmpfile = tmpname()
os.remove(tmpfile)
end
local result = exec_lua(function(SOCK) local result = exec_lua(function(SOCK)
local uv = vim.uv local uv = vim.uv
local server = assert(uv.new_pipe(false)) local server = assert(uv.new_pipe(false))
@ -5135,9 +5116,7 @@ describe('LSP', function()
describe('#dynamic vim.lsp._dynamic', function() describe('#dynamic vim.lsp._dynamic', function()
it('supports dynamic registration', function() it('supports dynamic registration', function()
---@type string local root_dir = tmpname(false)
local root_dir = tmpname()
os.remove(root_dir)
mkdir(root_dir) mkdir(root_dir)
local tmpfile = root_dir .. '/dynamic.foo' local tmpfile = root_dir .. '/dynamic.foo'
local file = io.open(tmpfile, 'w') local file = io.open(tmpfile, 'w')
@ -5264,8 +5243,7 @@ describe('LSP', function()
) )
end end
local root_dir = tmpname() local root_dir = tmpname(false)
os.remove(root_dir)
mkdir(root_dir) mkdir(root_dir)
exec_lua(create_server_definition) exec_lua(create_server_definition)

View File

@ -402,14 +402,18 @@ end
local tmpname_id = 0 local tmpname_id = 0
local tmpdir = tmpdir_get() local tmpdir = tmpdir_get()
--- Creates a new temporary file for use by tests. --- Generates a unique file path for use by tests, and writes the file unless `create=false`.
function M.tmpname() ---
---@param create? boolean (default true) Write the file.
function M.tmpname(create)
if tmpdir_is_local(tmpdir) then if tmpdir_is_local(tmpdir) then
-- Cannot control os.tmpname() dir, so hack our own tmpname() impl. -- Cannot control os.tmpname() dir, so hack our own tmpname() impl.
tmpname_id = tmpname_id + 1 tmpname_id = tmpname_id + 1
-- "…/Xtest_tmpdir/T42.7" -- "…/Xtest_tmpdir/T42.7"
local fname = ('%s/%s.%d'):format(tmpdir, (_G._nvim_test_id or 'nvim-test'), tmpname_id) 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 return fname
end end