2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
2016-07-31 10:13:19 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local mkdir = t.mkdir
|
2024-04-20 08:44:13 -07:00
|
|
|
local clear = n.clear
|
2024-04-08 02:03:20 -07:00
|
|
|
local eq = t.eq
|
2024-04-20 08:44:13 -07:00
|
|
|
local fn = n.fn
|
|
|
|
local api = n.api
|
|
|
|
local exc_exec = n.exc_exec
|
2024-04-08 02:03:20 -07:00
|
|
|
local read_file = t.read_file
|
|
|
|
local write_file = t.write_file
|
|
|
|
local pcall_err = t.pcall_err
|
2024-04-20 08:44:13 -07:00
|
|
|
local command = n.command
|
2016-07-31 10:13:19 -07:00
|
|
|
|
|
|
|
local fname = 'Xtest-functional-eval-writefile'
|
2017-02-14 10:46:12 -07:00
|
|
|
local dname = fname .. '.d'
|
|
|
|
local dfname_tail = '1'
|
|
|
|
local dfname = dname .. '/' .. dfname_tail
|
|
|
|
local ddname_tail = '2'
|
|
|
|
local ddname = dname .. '/' .. ddname_tail
|
2016-07-31 10:13:19 -07:00
|
|
|
|
2017-02-14 10:46:12 -07:00
|
|
|
before_each(function()
|
2023-04-04 12:59:06 -07:00
|
|
|
mkdir(dname)
|
|
|
|
mkdir(ddname)
|
2017-02-14 10:46:12 -07:00
|
|
|
clear()
|
|
|
|
end)
|
|
|
|
|
|
|
|
after_each(function()
|
|
|
|
os.remove(fname)
|
|
|
|
os.remove(dfname)
|
2024-01-12 05:03:25 -07:00
|
|
|
vim.uv.fs_rmdir(ddname)
|
|
|
|
vim.uv.fs_rmdir(dname)
|
2017-02-14 10:46:12 -07:00
|
|
|
end)
|
2016-07-31 10:13:19 -07:00
|
|
|
|
|
|
|
describe('writefile()', function()
|
|
|
|
it('writes empty list to a file', function()
|
|
|
|
eq(nil, read_file(fname))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({}, fname))
|
2016-07-31 10:13:19 -07:00
|
|
|
eq('', read_file(fname))
|
|
|
|
os.remove(fname)
|
|
|
|
eq(nil, read_file(fname))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({}, fname, 'b'))
|
2016-07-31 10:13:19 -07:00
|
|
|
eq('', read_file(fname))
|
|
|
|
os.remove(fname)
|
|
|
|
eq(nil, read_file(fname))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({}, fname, 'ab'))
|
2016-07-31 10:13:19 -07:00
|
|
|
eq('', read_file(fname))
|
|
|
|
os.remove(fname)
|
|
|
|
eq(nil, read_file(fname))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({}, fname, 'a'))
|
2016-07-31 10:13:19 -07:00
|
|
|
eq('', read_file(fname))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('writes list with an empty string to a file', function()
|
|
|
|
eq(0, exc_exec(('call writefile([$XXX_NONEXISTENT_VAR_XXX], "%s", "b")'):format(fname)))
|
|
|
|
eq('', read_file(fname))
|
|
|
|
eq(0, exc_exec(('call writefile([$XXX_NONEXISTENT_VAR_XXX], "%s")'):format(fname)))
|
|
|
|
eq('\n', read_file(fname))
|
|
|
|
end)
|
|
|
|
|
2021-04-08 19:33:21 -07:00
|
|
|
it('writes list with a null string to a file', function()
|
|
|
|
eq(0, exc_exec(('call writefile([v:_null_string], "%s", "b")'):format(fname)))
|
|
|
|
eq('', read_file(fname))
|
|
|
|
eq(0, exc_exec(('call writefile([v:_null_string], "%s")'):format(fname)))
|
|
|
|
eq('\n', read_file(fname))
|
|
|
|
end)
|
|
|
|
|
2016-07-31 10:13:19 -07:00
|
|
|
it('appends to a file', function()
|
|
|
|
eq(nil, read_file(fname))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({ 'abc', 'def', 'ghi' }, fname))
|
2016-07-31 10:13:19 -07:00
|
|
|
eq('abc\ndef\nghi\n', read_file(fname))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({ 'jkl' }, fname, 'a'))
|
2016-07-31 10:13:19 -07:00
|
|
|
eq('abc\ndef\nghi\njkl\n', read_file(fname))
|
|
|
|
os.remove(fname)
|
|
|
|
eq(nil, read_file(fname))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({ 'abc', 'def', 'ghi' }, fname, 'b'))
|
2016-07-31 10:13:19 -07:00
|
|
|
eq('abc\ndef\nghi', read_file(fname))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({ 'jkl' }, fname, 'ab'))
|
2016-07-31 10:13:19 -07:00
|
|
|
eq('abc\ndef\nghijkl', read_file(fname))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('correctly treats NLs', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({ '\na\nb\n' }, fname, 'b'))
|
2016-07-31 10:13:19 -07:00
|
|
|
eq('\0a\0b\0', read_file(fname))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({ 'a\n\n\nb' }, fname, 'b'))
|
2016-07-31 10:13:19 -07:00
|
|
|
eq('a\0\0\0b', read_file(fname))
|
|
|
|
end)
|
|
|
|
|
2017-04-02 16:11:27 -07:00
|
|
|
it('writes with s and S', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({ '\na\nb\n' }, fname, 'bs'))
|
2017-04-02 16:11:27 -07:00
|
|
|
eq('\0a\0b\0', read_file(fname))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({ 'a\n\n\nb' }, fname, 'bS'))
|
2017-04-02 16:11:27 -07:00
|
|
|
eq('a\0\0\0b', read_file(fname))
|
|
|
|
end)
|
|
|
|
|
2016-07-31 10:13:19 -07:00
|
|
|
it('correctly overwrites file', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({ '\na\nb\n' }, fname, 'b'))
|
2016-07-31 10:13:19 -07:00
|
|
|
eq('\0a\0b\0', read_file(fname))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({ 'a\n' }, fname, 'b'))
|
2016-07-31 10:13:19 -07:00
|
|
|
eq('a\0', read_file(fname))
|
|
|
|
end)
|
|
|
|
|
2017-02-14 10:46:12 -07:00
|
|
|
it('shows correct file name when supplied numbers', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_current_dir(dname)
|
2021-09-19 02:29:37 -07:00
|
|
|
eq(
|
|
|
|
"Vim(call):E482: Can't open file 2 for writing: illegal operation on a directory",
|
|
|
|
pcall_err(command, ('call writefile([42], %s)'):format(ddname_tail))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2017-02-14 10:46:12 -07:00
|
|
|
end)
|
|
|
|
|
2022-11-06 20:31:50 -07:00
|
|
|
it('writefile(..., "p") creates missing parent directories', function()
|
|
|
|
os.remove(dname)
|
|
|
|
eq(nil, read_file(dfname))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({ 'abc', 'def', 'ghi' }, dfname, 'p'))
|
2022-11-06 20:31:50 -07:00
|
|
|
eq('abc\ndef\nghi\n', read_file(dfname))
|
|
|
|
os.remove(dfname)
|
|
|
|
os.remove(dname)
|
|
|
|
eq(nil, read_file(dfname))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, fn.writefile({ '\na\nb\n' }, dfname, 'pb'))
|
2022-11-06 20:31:50 -07:00
|
|
|
eq('\0a\0b\0', read_file(dfname))
|
|
|
|
os.remove(dfname)
|
|
|
|
os.remove(dname)
|
|
|
|
eq(
|
|
|
|
'Vim(call):E32: No file name',
|
|
|
|
pcall_err(command, ('call writefile([], "%s", "p")'):format(dfname .. '.d/'))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2022-11-06 20:31:50 -07:00
|
|
|
eq(
|
|
|
|
"Vim(call):E482: Can't open file ./ for writing: illegal operation on a directory",
|
|
|
|
pcall_err(command, 'call writefile([], "./", "p")')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2022-11-06 20:31:50 -07:00
|
|
|
eq(
|
|
|
|
"Vim(call):E482: Can't open file . for writing: illegal operation on a directory",
|
|
|
|
pcall_err(command, 'call writefile([], ".", "p")')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2022-11-06 20:31:50 -07:00
|
|
|
end)
|
|
|
|
|
2016-07-31 10:13:19 -07:00
|
|
|
it('errors out with invalid arguments', function()
|
2017-02-14 10:46:12 -07:00
|
|
|
write_file(fname, 'TEST')
|
2021-09-19 02:29:37 -07:00
|
|
|
eq(
|
|
|
|
'Vim(call):E119: Not enough arguments for function: writefile',
|
|
|
|
pcall_err(command, 'call writefile()')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-09-19 02:29:37 -07:00
|
|
|
eq(
|
|
|
|
'Vim(call):E119: Not enough arguments for function: writefile',
|
|
|
|
pcall_err(command, 'call writefile([])')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-09-19 02:29:37 -07:00
|
|
|
eq(
|
|
|
|
'Vim(call):E118: Too many arguments for function: writefile',
|
|
|
|
pcall_err(command, ('call writefile([], "%s", "b", 1)'):format(fname))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2016-07-31 10:13:19 -07:00
|
|
|
for _, arg in ipairs({ '0', '0.0', 'function("tr")', '{}', '"test"' }) do
|
2021-09-19 02:29:37 -07:00
|
|
|
eq(
|
|
|
|
'Vim(call):E475: Invalid argument: writefile() first argument must be a List or a Blob',
|
|
|
|
pcall_err(command, ('call writefile(%s, "%s", "b")'):format(arg, fname))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2016-07-31 10:13:19 -07:00
|
|
|
end
|
2017-02-14 10:46:12 -07:00
|
|
|
for _, args in ipairs({ '[], %s, "b"', '[], "' .. fname .. '", %s' }) do
|
2023-05-04 16:02:43 -07:00
|
|
|
eq(
|
|
|
|
'Vim(call):E730: Using a List as a String',
|
2021-09-19 02:29:37 -07:00
|
|
|
pcall_err(command, ('call writefile(%s)'):format(args:format('[]')))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-05-04 16:02:43 -07:00
|
|
|
eq(
|
|
|
|
'Vim(call):E731: Using a Dictionary as a String',
|
2021-09-19 02:29:37 -07:00
|
|
|
pcall_err(command, ('call writefile(%s)'):format(args:format('{}')))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-05-04 16:02:43 -07:00
|
|
|
eq(
|
|
|
|
'Vim(call):E729: Using a Funcref as a String',
|
2021-09-19 02:29:37 -07:00
|
|
|
pcall_err(command, ('call writefile(%s)'):format(args:format('function("tr")')))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2016-07-31 10:13:19 -07:00
|
|
|
end
|
2021-09-19 02:29:37 -07:00
|
|
|
eq(
|
|
|
|
'Vim(call):E5060: Unknown flag: «»',
|
|
|
|
pcall_err(command, ('call writefile([], "%s", "bs«»")'):format(fname))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2017-02-14 10:46:12 -07:00
|
|
|
eq('TEST', read_file(fname))
|
|
|
|
end)
|
|
|
|
|
2018-06-22 06:44:01 -07:00
|
|
|
it('does not write to file if error in list', function()
|
2017-02-14 10:46:12 -07:00
|
|
|
local args = '["tset"] + repeat([%s], 3), "' .. fname .. '"'
|
2021-09-19 02:29:37 -07:00
|
|
|
eq(
|
|
|
|
'Vim(call):E805: Expected a Number or a String, Float found',
|
|
|
|
pcall_err(command, ('call writefile(%s)'):format(args:format('0.0')))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2018-06-22 06:44:01 -07:00
|
|
|
eq(nil, read_file(fname))
|
2017-02-14 10:46:12 -07:00
|
|
|
write_file(fname, 'TEST')
|
2021-09-19 02:29:37 -07:00
|
|
|
eq(
|
|
|
|
'Vim(call):E745: Expected a Number or a String, List found',
|
|
|
|
pcall_err(command, ('call writefile(%s)'):format(args:format('[]')))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2018-06-22 06:44:01 -07:00
|
|
|
eq('TEST', read_file(fname))
|
2021-09-19 02:29:37 -07:00
|
|
|
eq(
|
|
|
|
'Vim(call):E728: Expected a Number or a String, Dictionary found',
|
|
|
|
pcall_err(command, ('call writefile(%s)'):format(args:format('{}')))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2018-06-22 06:44:01 -07:00
|
|
|
eq('TEST', read_file(fname))
|
2021-09-19 02:29:37 -07:00
|
|
|
eq(
|
|
|
|
'Vim(call):E703: Expected a Number or a String, Funcref found',
|
|
|
|
pcall_err(command, ('call writefile(%s)'):format(args:format('function("tr")')))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2018-06-22 06:44:01 -07:00
|
|
|
eq('TEST', read_file(fname))
|
2016-07-31 10:13:19 -07:00
|
|
|
end)
|
|
|
|
end)
|