2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
|
|
|
|
2017-04-08 17:31:13 -07:00
|
|
|
local eq, eval, clear, write_file, source, insert =
|
2024-04-20 08:44:13 -07:00
|
|
|
t.eq, n.eval, n.clear, t.write_file, n.source, n.insert
|
2024-04-08 02:03:20 -07:00
|
|
|
local pcall_err = t.pcall_err
|
2024-04-20 08:44:13 -07:00
|
|
|
local command = n.command
|
|
|
|
local feed_command = n.feed_command
|
|
|
|
local fn = n.fn
|
|
|
|
local api = n.api
|
2024-04-08 02:03:20 -07:00
|
|
|
local skip = t.skip
|
|
|
|
local is_os = t.is_os
|
|
|
|
local is_ci = t.is_ci
|
2016-04-15 05:57:29 -07:00
|
|
|
|
2017-04-02 17:04:10 -07:00
|
|
|
local fname = 'Xtest-functional-ex_cmds-write'
|
|
|
|
local fname_bak = fname .. '~'
|
|
|
|
local fname_broken = fname_bak .. 'broken'
|
|
|
|
|
2016-04-15 05:57:29 -07:00
|
|
|
describe(':write', function()
|
2016-10-21 13:03:01 -07:00
|
|
|
local function cleanup()
|
2016-04-15 05:57:29 -07:00
|
|
|
os.remove('test_bkc_file.txt')
|
|
|
|
os.remove('test_bkc_link.txt')
|
2016-10-21 13:03:01 -07:00
|
|
|
os.remove('test_fifo')
|
2022-11-06 20:31:50 -07:00
|
|
|
os.remove('test/write/p_opt.txt')
|
|
|
|
os.remove('test/write')
|
|
|
|
os.remove('test')
|
2017-04-02 17:04:10 -07:00
|
|
|
os.remove(fname)
|
|
|
|
os.remove(fname_bak)
|
|
|
|
os.remove(fname_broken)
|
2016-10-21 13:03:01 -07:00
|
|
|
end
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
|
|
|
cleanup()
|
|
|
|
end)
|
|
|
|
after_each(function()
|
|
|
|
cleanup()
|
2016-06-06 18:54:35 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('&backupcopy=auto preserves symlinks', function()
|
2017-04-08 14:12:26 -07:00
|
|
|
command('set backupcopy=auto')
|
2016-04-15 05:57:29 -07:00
|
|
|
write_file('test_bkc_file.txt', 'content0')
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2024-01-02 18:09:18 -07:00
|
|
|
command('silent !mklink test_bkc_link.txt test_bkc_file.txt')
|
2017-10-05 08:29:54 -07:00
|
|
|
else
|
2024-01-02 18:09:18 -07:00
|
|
|
command('silent !ln -s test_bkc_file.txt test_bkc_link.txt')
|
2017-10-05 08:29:54 -07:00
|
|
|
end
|
2018-05-25 04:42:59 -07:00
|
|
|
if eval('v:shell_error') ~= 0 then
|
2019-10-17 19:46:30 -07:00
|
|
|
pending('Cannot create symlink')
|
2018-05-18 16:30:15 -07:00
|
|
|
end
|
2016-04-15 05:57:29 -07:00
|
|
|
source([[
|
|
|
|
edit test_bkc_link.txt
|
|
|
|
call setline(1, ['content1'])
|
|
|
|
write
|
|
|
|
]])
|
|
|
|
eq(eval("['content1']"), eval("readfile('test_bkc_file.txt')"))
|
|
|
|
eq(eval("['content1']"), eval("readfile('test_bkc_link.txt')"))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('&backupcopy=no replaces symlink with new file', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
skip(is_ci('cirrus'))
|
2017-04-08 14:12:26 -07:00
|
|
|
command('set backupcopy=no')
|
2016-04-15 05:57:29 -07:00
|
|
|
write_file('test_bkc_file.txt', 'content0')
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2024-01-02 18:09:18 -07:00
|
|
|
command('silent !mklink test_bkc_link.txt test_bkc_file.txt')
|
2017-10-05 08:29:54 -07:00
|
|
|
else
|
2024-01-02 18:09:18 -07:00
|
|
|
command('silent !ln -s test_bkc_file.txt test_bkc_link.txt')
|
2017-10-05 08:29:54 -07:00
|
|
|
end
|
2018-05-25 04:42:59 -07:00
|
|
|
if eval('v:shell_error') ~= 0 then
|
2019-10-17 19:46:30 -07:00
|
|
|
pending('Cannot create symlink')
|
2018-05-18 16:30:15 -07:00
|
|
|
end
|
2016-04-15 05:57:29 -07:00
|
|
|
source([[
|
|
|
|
edit test_bkc_link.txt
|
|
|
|
call setline(1, ['content1'])
|
|
|
|
write
|
|
|
|
]])
|
|
|
|
eq(eval("['content0']"), eval("readfile('test_bkc_file.txt')"))
|
|
|
|
eq(eval("['content1']"), eval("readfile('test_bkc_link.txt')"))
|
|
|
|
end)
|
2016-10-21 13:03:01 -07:00
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
it('appends FIFO file', function()
|
2018-05-18 14:30:21 -07:00
|
|
|
-- mkfifo creates read-only .lnk files on Windows
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') or eval("executable('mkfifo')") == 0 then
|
2019-10-17 19:46:30 -07:00
|
|
|
pending('missing "mkfifo" command')
|
2016-10-21 13:03:01 -07:00
|
|
|
end
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
local text = 'some fifo text from write_spec'
|
|
|
|
assert(os.execute('mkfifo test_fifo'))
|
2016-10-21 13:03:01 -07:00
|
|
|
insert(text)
|
|
|
|
|
|
|
|
-- Blocks until a consumer reads the FIFO.
|
2024-01-02 18:09:18 -07:00
|
|
|
feed_command('write >> test_fifo')
|
2016-10-21 13:03:01 -07:00
|
|
|
|
|
|
|
-- Read the FIFO, this will unblock the :write above.
|
2024-01-02 18:09:18 -07:00
|
|
|
local fifo = assert(io.open('test_fifo'))
|
|
|
|
eq(text .. '\n', fifo:read('*all'))
|
2016-10-21 13:03:01 -07:00
|
|
|
fifo:close()
|
|
|
|
end)
|
2017-04-02 17:04:10 -07:00
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
it('++p creates missing parent directories', function()
|
2022-11-06 20:31:50 -07:00
|
|
|
eq(0, eval("filereadable('p_opt.txt')"))
|
2024-01-02 18:09:18 -07:00
|
|
|
command('write ++p p_opt.txt')
|
2022-11-06 20:31:50 -07:00
|
|
|
eq(1, eval("filereadable('p_opt.txt')"))
|
2024-01-02 18:09:18 -07:00
|
|
|
os.remove('p_opt.txt')
|
2022-11-06 20:31:50 -07:00
|
|
|
|
|
|
|
eq(0, eval("filereadable('p_opt.txt')"))
|
2024-01-02 18:09:18 -07:00
|
|
|
command('write ++p ./p_opt.txt')
|
2022-11-06 20:31:50 -07:00
|
|
|
eq(1, eval("filereadable('p_opt.txt')"))
|
2024-01-02 18:09:18 -07:00
|
|
|
os.remove('p_opt.txt')
|
2022-11-06 20:31:50 -07:00
|
|
|
|
|
|
|
eq(0, eval("filereadable('test/write/p_opt.txt')"))
|
2024-01-02 18:09:18 -07:00
|
|
|
command('write ++p test/write/p_opt.txt')
|
2022-11-06 20:31:50 -07:00
|
|
|
eq(1, eval("filereadable('test/write/p_opt.txt')"))
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('Vim(write):E32: No file name', pcall_err(command, 'write ++p test_write/'))
|
2022-11-21 17:13:30 -07:00
|
|
|
if not is_os('win') then
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
2024-01-12 10:59:57 -07:00
|
|
|
('Vim(write):E17: "' .. fn.fnamemodify('.', ':p:h') .. '" is a directory'),
|
2024-01-02 18:09:18 -07:00
|
|
|
pcall_err(command, 'write ++p .')
|
|
|
|
)
|
|
|
|
eq(
|
2024-01-12 10:59:57 -07:00
|
|
|
('Vim(write):E17: "' .. fn.fnamemodify('.', ':p:h') .. '" is a directory'),
|
2024-01-02 18:09:18 -07:00
|
|
|
pcall_err(command, 'write ++p ./')
|
|
|
|
)
|
2022-11-06 20:31:50 -07:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2017-04-02 17:04:10 -07:00
|
|
|
it('errors out correctly', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
skip(is_ci('cirrus'))
|
2017-04-02 17:04:10 -07:00
|
|
|
command('let $HOME=""')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(fn.fnamemodify('.', ':p:h'), fn.fnamemodify('.', ':p:h:~'))
|
2017-04-02 17:04:10 -07:00
|
|
|
-- Message from check_overwrite
|
2022-11-21 17:13:30 -07:00
|
|
|
if not is_os('win') then
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
2024-01-12 10:59:57 -07:00
|
|
|
('Vim(write):E17: "' .. fn.fnamemodify('.', ':p:h') .. '" is a directory'),
|
2024-01-02 18:09:18 -07:00
|
|
|
pcall_err(command, 'write .')
|
|
|
|
)
|
2017-10-05 08:29:54 -07:00
|
|
|
end
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_option_value('writeany', true, {})
|
2017-04-02 17:04:10 -07:00
|
|
|
-- Message from buf_write
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('Vim(write):E502: "." is a directory', pcall_err(command, 'write .'))
|
2024-01-12 10:59:57 -07:00
|
|
|
fn.mkdir(fname_bak)
|
|
|
|
api.nvim_set_option_value('backupdir', '.', {})
|
|
|
|
api.nvim_set_option_value('backup', true, {})
|
2017-04-02 17:04:10 -07:00
|
|
|
write_file(fname, 'content0')
|
2021-09-19 02:29:37 -07:00
|
|
|
command('edit ' .. fname)
|
2024-01-12 10:59:57 -07:00
|
|
|
fn.setline(1, 'TTY')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq("Vim(write):E510: Can't make backup file (add ! to override)", pcall_err(command, 'write'))
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_option_value('backup', false, {})
|
|
|
|
fn.setfperm(fname, 'r--------')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
'Vim(write):E505: "Xtest-functional-ex_cmds-write" is read-only (add ! to override)',
|
|
|
|
pcall_err(command, 'write')
|
|
|
|
)
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2017-10-05 08:29:54 -07:00
|
|
|
eq(0, os.execute('del /q/f ' .. fname))
|
|
|
|
eq(0, os.execute('rd /q/s ' .. fname_bak))
|
|
|
|
else
|
|
|
|
eq(true, os.remove(fname))
|
|
|
|
eq(true, os.remove(fname_bak))
|
|
|
|
end
|
2017-04-02 17:04:10 -07:00
|
|
|
write_file(fname_bak, 'TTYX')
|
2022-11-21 17:13:30 -07:00
|
|
|
skip(is_os('win'), [[FIXME: exc_exec('write!') outputs 0 in Windows]])
|
2024-01-12 05:03:25 -07:00
|
|
|
vim.uv.fs_symlink(fname_bak .. ('/xxxxx'):rep(20), fname)
|
2024-01-02 18:09:18 -07:00
|
|
|
eq("Vim(write):E166: Can't open linked file for writing", pcall_err(command, 'write!'))
|
2017-04-02 17:04:10 -07:00
|
|
|
end)
|
2016-04-15 05:57:29 -07:00
|
|
|
end)
|