2022-11-04 18:24:34 -07:00
|
|
|
|
local lfs = require('lfs')
|
2018-04-22 19:43:00 -07:00
|
|
|
|
local helpers = require('test.functional.helpers')(after_each)
|
|
|
|
|
|
2022-06-30 04:16:46 -07:00
|
|
|
|
local assert_log = helpers.assert_log
|
|
|
|
|
local assert_nolog = helpers.assert_nolog
|
2018-04-22 19:43:00 -07:00
|
|
|
|
local clear = helpers.clear
|
|
|
|
|
local command = helpers.command
|
|
|
|
|
local eq = helpers.eq
|
2022-11-04 18:24:34 -07:00
|
|
|
|
local neq = helpers.neq
|
2022-06-30 04:16:46 -07:00
|
|
|
|
local ok = helpers.ok
|
2018-04-22 19:43:00 -07:00
|
|
|
|
local feed = helpers.feed
|
|
|
|
|
local funcs = helpers.funcs
|
|
|
|
|
local nvim_prog = helpers.nvim_prog
|
2018-04-23 15:27:09 -07:00
|
|
|
|
local request = helpers.request
|
|
|
|
|
local retry = helpers.retry
|
2018-04-22 19:43:00 -07:00
|
|
|
|
local rmdir = helpers.rmdir
|
2022-06-30 04:16:46 -07:00
|
|
|
|
local matches = helpers.matches
|
2019-11-18 00:38:27 -07:00
|
|
|
|
local mkdir = helpers.mkdir
|
2018-04-22 19:43:00 -07:00
|
|
|
|
local sleep = helpers.sleep
|
2019-03-23 16:21:26 -07:00
|
|
|
|
local read_file = helpers.read_file
|
2022-06-30 04:16:46 -07:00
|
|
|
|
local tmpname = helpers.tmpname
|
2019-03-30 10:14:20 -07:00
|
|
|
|
local trim = helpers.trim
|
2019-11-18 00:38:27 -07:00
|
|
|
|
local currentdir = helpers.funcs.getcwd
|
2021-11-23 21:07:47 -07:00
|
|
|
|
local assert_alive = helpers.assert_alive
|
2023-01-14 18:34:21 -07:00
|
|
|
|
local check_close = helpers.check_close
|
2022-06-08 14:22:50 -07:00
|
|
|
|
local expect_exit = helpers.expect_exit
|
2022-06-30 04:16:46 -07:00
|
|
|
|
local write_file = helpers.write_file
|
2022-09-24 18:57:10 -07:00
|
|
|
|
local Screen = require('test.functional.ui.screen')
|
|
|
|
|
local feed_command = helpers.feed_command
|
2022-11-13 06:52:19 -07:00
|
|
|
|
local skip = helpers.skip
|
2022-11-21 17:13:30 -07:00
|
|
|
|
local is_os = helpers.is_os
|
|
|
|
|
local is_ci = helpers.is_ci
|
2018-04-22 19:43:00 -07:00
|
|
|
|
|
|
|
|
|
describe('fileio', function()
|
|
|
|
|
before_each(function()
|
|
|
|
|
end)
|
|
|
|
|
after_each(function()
|
2023-01-14 18:34:21 -07:00
|
|
|
|
check_close()
|
2018-04-22 19:43:00 -07:00
|
|
|
|
os.remove('Xtest_startup_shada')
|
|
|
|
|
os.remove('Xtest_startup_file1')
|
2019-03-23 16:21:26 -07:00
|
|
|
|
os.remove('Xtest_startup_file1~')
|
2018-04-22 19:43:00 -07:00
|
|
|
|
os.remove('Xtest_startup_file2')
|
2019-07-23 11:56:27 -07:00
|
|
|
|
os.remove('Xtest_тест.md')
|
2021-11-23 21:07:47 -07:00
|
|
|
|
os.remove('Xtest-u8-int-max')
|
2022-09-24 18:57:10 -07:00
|
|
|
|
os.remove('Xtest-overwrite-forced')
|
2018-04-22 19:43:00 -07:00
|
|
|
|
rmdir('Xtest_startup_swapdir')
|
2019-11-18 00:38:27 -07:00
|
|
|
|
rmdir('Xtest_backupdir')
|
2018-04-22 19:43:00 -07:00
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
it('fsync() codepaths #8304', function()
|
|
|
|
|
clear({ args={ '-i', 'Xtest_startup_shada',
|
|
|
|
|
'--cmd', 'set directory=Xtest_startup_swapdir' } })
|
|
|
|
|
|
|
|
|
|
-- These cases ALWAYS force fsync (regardless of 'fsync' option):
|
|
|
|
|
|
|
|
|
|
-- 1. Idle (CursorHold) with modified buffers (+ 'swapfile').
|
|
|
|
|
command('write Xtest_startup_file1')
|
|
|
|
|
feed('ifoo<esc>h')
|
2018-04-23 15:27:09 -07:00
|
|
|
|
command('write')
|
|
|
|
|
eq(0, request('nvim__stats').fsync) -- 'nofsync' is the default.
|
|
|
|
|
command('set swapfile')
|
|
|
|
|
command('set updatetime=1')
|
|
|
|
|
feed('izub<esc>h') -- File is 'modified'.
|
|
|
|
|
sleep(3) -- Allow 'updatetime' to expire.
|
|
|
|
|
retry(3, nil, function()
|
|
|
|
|
eq(1, request('nvim__stats').fsync)
|
|
|
|
|
end)
|
|
|
|
|
command('set updatetime=9999')
|
2018-04-22 19:43:00 -07:00
|
|
|
|
|
|
|
|
|
-- 2. Exit caused by deadly signal (+ 'swapfile').
|
|
|
|
|
local j = funcs.jobstart({ nvim_prog, '-u', 'NONE', '-i',
|
|
|
|
|
'Xtest_startup_shada', '--headless',
|
|
|
|
|
'-c', 'set swapfile',
|
|
|
|
|
'-c', 'write Xtest_startup_file2',
|
|
|
|
|
'-c', 'put =localtime()', })
|
|
|
|
|
sleep(10) -- Let Nvim start.
|
|
|
|
|
funcs.jobstop(j) -- Send deadly signal.
|
|
|
|
|
|
|
|
|
|
-- 3. SIGPWR signal.
|
|
|
|
|
-- ??
|
|
|
|
|
|
|
|
|
|
-- 4. Explicit :preserve command.
|
|
|
|
|
command('preserve')
|
2018-04-23 15:27:09 -07:00
|
|
|
|
eq(2, request('nvim__stats').fsync)
|
2018-04-22 19:43:00 -07:00
|
|
|
|
|
|
|
|
|
-- 5. Enable 'fsync' option, write file.
|
|
|
|
|
command('set fsync')
|
|
|
|
|
feed('ibaz<esc>h')
|
|
|
|
|
command('write')
|
2018-04-23 15:27:09 -07:00
|
|
|
|
eq(4, request('nvim__stats').fsync)
|
2018-04-22 19:43:00 -07:00
|
|
|
|
end)
|
2019-03-23 16:21:26 -07:00
|
|
|
|
|
2019-05-20 13:33:19 -07:00
|
|
|
|
it('backup #9709', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
|
skip(is_ci('cirrus'))
|
2019-03-23 16:21:26 -07:00
|
|
|
|
clear({ args={ '-i', 'Xtest_startup_shada',
|
|
|
|
|
'--cmd', 'set directory=Xtest_startup_swapdir' } })
|
|
|
|
|
|
|
|
|
|
command('write Xtest_startup_file1')
|
|
|
|
|
feed('ifoo<esc>')
|
|
|
|
|
command('set backup')
|
|
|
|
|
command('set backupcopy=yes')
|
|
|
|
|
command('write')
|
|
|
|
|
feed('Abar<esc>')
|
|
|
|
|
command('write')
|
|
|
|
|
|
2019-03-30 10:14:20 -07:00
|
|
|
|
local foobar_contents = trim(read_file('Xtest_startup_file1'))
|
|
|
|
|
local bar_contents = trim(read_file('Xtest_startup_file1~'))
|
2019-03-23 16:21:26 -07:00
|
|
|
|
|
2019-03-30 10:14:20 -07:00
|
|
|
|
eq('foobar', foobar_contents);
|
|
|
|
|
eq('foo', bar_contents);
|
2019-07-23 11:56:27 -07:00
|
|
|
|
end)
|
2019-03-23 16:21:26 -07:00
|
|
|
|
|
2019-11-18 00:38:27 -07:00
|
|
|
|
it('backup with full path #11214', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
|
skip(is_ci('cirrus'))
|
2019-11-18 00:38:27 -07:00
|
|
|
|
clear()
|
|
|
|
|
mkdir('Xtest_backupdir')
|
|
|
|
|
command('set backup')
|
|
|
|
|
command('set backupdir=Xtest_backupdir//')
|
|
|
|
|
command('write Xtest_startup_file1')
|
|
|
|
|
feed('ifoo<esc>')
|
|
|
|
|
command('write')
|
|
|
|
|
feed('Abar<esc>')
|
|
|
|
|
command('write')
|
|
|
|
|
|
|
|
|
|
-- Backup filename = fullpath, separators replaced with "%".
|
|
|
|
|
local backup_file_name = string.gsub(currentdir()..'/Xtest_startup_file1',
|
2022-11-21 17:13:30 -07:00
|
|
|
|
is_os('win') and '[:/\\]' or '/', '%%') .. '~'
|
2019-11-18 00:38:27 -07:00
|
|
|
|
local foo_contents = trim(read_file('Xtest_backupdir/'..backup_file_name))
|
|
|
|
|
local foobar_contents = trim(read_file('Xtest_startup_file1'))
|
|
|
|
|
|
|
|
|
|
eq('foobar', foobar_contents);
|
|
|
|
|
eq('foo', foo_contents);
|
|
|
|
|
end)
|
|
|
|
|
|
2022-11-04 18:24:34 -07:00
|
|
|
|
it('backup symlinked files #11349', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
|
skip(is_ci('cirrus'))
|
2022-11-04 18:24:34 -07:00
|
|
|
|
clear()
|
|
|
|
|
|
|
|
|
|
local initial_content = 'foo'
|
|
|
|
|
local link_file_name = 'Xtest_startup_file2'
|
|
|
|
|
local backup_file_name = link_file_name .. '~'
|
|
|
|
|
|
|
|
|
|
write_file('Xtest_startup_file1', initial_content, false)
|
|
|
|
|
lfs.link('Xtest_startup_file1', link_file_name, true)
|
|
|
|
|
command('set backup')
|
|
|
|
|
command('set backupcopy=yes')
|
|
|
|
|
command('edit ' .. link_file_name)
|
|
|
|
|
feed('Abar<esc>')
|
|
|
|
|
command('write')
|
|
|
|
|
|
|
|
|
|
local backup_raw = read_file(backup_file_name)
|
|
|
|
|
neq(nil, backup_raw, "Expected backup file " .. backup_file_name .. "to exist but did not")
|
|
|
|
|
eq(initial_content, trim(backup_raw), 'Expected backup to contain original contents')
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
|
2022-11-28 18:45:48 -07:00
|
|
|
|
it('backup symlinked files in first available backupdir #11349', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
|
skip(is_ci('cirrus'))
|
2022-11-04 18:24:34 -07:00
|
|
|
|
clear()
|
|
|
|
|
|
|
|
|
|
local initial_content = 'foo'
|
|
|
|
|
local backup_dir = 'Xtest_backupdir'
|
|
|
|
|
local sep = helpers.get_pathsep()
|
|
|
|
|
local link_file_name = 'Xtest_startup_file2'
|
|
|
|
|
local backup_file_name = backup_dir .. sep .. link_file_name .. '~'
|
|
|
|
|
|
|
|
|
|
write_file('Xtest_startup_file1', initial_content, false)
|
|
|
|
|
lfs.link('Xtest_startup_file1', link_file_name, true)
|
|
|
|
|
mkdir(backup_dir)
|
|
|
|
|
command('set backup')
|
|
|
|
|
command('set backupcopy=yes')
|
|
|
|
|
command('set backupdir=.__this_does_not_exist__,' .. backup_dir)
|
|
|
|
|
command('edit ' .. link_file_name)
|
|
|
|
|
feed('Abar<esc>')
|
|
|
|
|
command('write')
|
|
|
|
|
|
|
|
|
|
local backup_raw = read_file(backup_file_name)
|
|
|
|
|
neq(nil, backup_raw, "Expected backup file " .. backup_file_name .. " to exist but did not")
|
|
|
|
|
eq(initial_content, trim(backup_raw), 'Expected backup to contain original contents')
|
|
|
|
|
end)
|
|
|
|
|
|
2019-07-23 11:56:27 -07:00
|
|
|
|
it('readfile() on multibyte filename #10586', function()
|
|
|
|
|
clear()
|
|
|
|
|
local text = {
|
|
|
|
|
'line1',
|
|
|
|
|
' ...line2... ',
|
|
|
|
|
'',
|
|
|
|
|
'line3!',
|
|
|
|
|
'тест yay тест.',
|
|
|
|
|
'',
|
|
|
|
|
}
|
|
|
|
|
local fname = 'Xtest_тест.md'
|
|
|
|
|
funcs.writefile(text, fname, 's')
|
|
|
|
|
table.insert(text, '')
|
|
|
|
|
eq(text, funcs.readfile(fname, 'b'))
|
2019-03-23 16:21:26 -07:00
|
|
|
|
end)
|
2021-11-23 21:07:47 -07:00
|
|
|
|
it('read invalid u8 over INT_MAX doesn\'t segfault', function()
|
|
|
|
|
clear()
|
|
|
|
|
command('call writefile(0zFFFFFFFF, "Xtest-u8-int-max")')
|
|
|
|
|
-- This should not segfault
|
|
|
|
|
command('edit ++enc=utf32 Xtest-u8-int-max')
|
|
|
|
|
assert_alive()
|
|
|
|
|
end)
|
2022-09-24 18:57:10 -07:00
|
|
|
|
|
|
|
|
|
it(':w! does not show "file has been changed" warning', function()
|
|
|
|
|
clear()
|
|
|
|
|
write_file("Xtest-overwrite-forced", 'foobar')
|
|
|
|
|
command('set nofixendofline')
|
|
|
|
|
local screen = Screen.new(40,4)
|
|
|
|
|
screen:set_default_attr_ids({
|
|
|
|
|
[1] = {bold = true, foreground = Screen.colors.Blue1},
|
|
|
|
|
[2] = {foreground = Screen.colors.Grey100, background = Screen.colors.Red},
|
|
|
|
|
[3] = {bold = true, foreground = Screen.colors.SeaGreen4}
|
|
|
|
|
})
|
|
|
|
|
screen:attach()
|
2022-09-27 07:19:15 -07:00
|
|
|
|
command("set shortmess-=F")
|
2022-09-24 18:57:10 -07:00
|
|
|
|
|
|
|
|
|
command("e Xtest-overwrite-forced")
|
|
|
|
|
screen:expect([[
|
|
|
|
|
^foobar |
|
|
|
|
|
{1:~ }|
|
|
|
|
|
{1:~ }|
|
|
|
|
|
"Xtest-overwrite-forced" [noeol] 1L, 6B |
|
|
|
|
|
]])
|
|
|
|
|
|
|
|
|
|
-- Get current unix time.
|
|
|
|
|
local cur_unix_time = os.time(os.date("!*t"))
|
|
|
|
|
local future_time = cur_unix_time + 999999
|
|
|
|
|
-- Set the file's access/update time to be
|
|
|
|
|
-- greater than the time at which it was created.
|
|
|
|
|
local uv = require("luv")
|
|
|
|
|
uv.fs_utime('Xtest-overwrite-forced', future_time, future_time)
|
|
|
|
|
-- use async feed_command because nvim basically hangs on the prompt
|
|
|
|
|
feed_command("w")
|
|
|
|
|
screen:expect([[
|
|
|
|
|
{2:WARNING: The file has been changed since}|
|
|
|
|
|
{2: reading it!!!} |
|
|
|
|
|
{3:Do you really want to write to it (y/n)^?}|
|
|
|
|
|
|
|
|
|
|
|
]])
|
|
|
|
|
|
|
|
|
|
feed("n")
|
|
|
|
|
feed("<cr>")
|
|
|
|
|
screen:expect([[
|
|
|
|
|
^foobar |
|
|
|
|
|
{1:~ }|
|
|
|
|
|
{1:~ }|
|
|
|
|
|
|
|
|
|
|
|
]])
|
|
|
|
|
-- Use a screen test because the warning does not set v:errmsg.
|
|
|
|
|
command("w!")
|
|
|
|
|
screen:expect([[
|
|
|
|
|
^foobar |
|
|
|
|
|
{1:~ }|
|
|
|
|
|
{1:~ }|
|
|
|
|
|
<erwrite-forced" [noeol] 1L, 6B written |
|
|
|
|
|
]])
|
|
|
|
|
end)
|
2018-04-22 19:43:00 -07:00
|
|
|
|
end)
|
|
|
|
|
|
2022-06-30 04:16:46 -07:00
|
|
|
|
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()
|
2023-01-14 18:34:21 -07:00
|
|
|
|
check_close()
|
2022-06-30 04:16:46 -07:00
|
|
|
|
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.
|
2023-01-16 16:12:59 -07:00
|
|
|
|
assert_log('tempdir root not a directory', testlog, 100)
|
2022-06-30 04:16:46 -07:00
|
|
|
|
|
|
|
|
|
-- "…/nvim.<user>/" has wrong permissions:
|
2022-11-21 17:13:30 -07:00
|
|
|
|
skip(is_os('win'), 'TODO(justinmk): need setfperm/getfperm on Windows. #8244')
|
2022-06-30 04:16:46 -07:00
|
|
|
|
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.
|
2023-01-16 16:12:59 -07:00
|
|
|
|
assert_log('tempdir root has invalid permissions', testlog, 100)
|
2022-06-30 04:16:46 -07:00
|
|
|
|
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)
|