eval: Fix error messages from writefile

1. When calling writefile(list, fname, []) do not show error message twice.
2. Do not allow file name to be overwritten for writefile([1], 2).
3. Do not show “Can’t open file with an empty name” error after error like 
   “using Float as a String” when type of the second argument is not correct.
4. Do not give multiple error messages and still continue for code like 
   `writefile(["test", [], [], [], "tset"])`.

Note that to fix 4. ideally I need tv_check_str_or_nr which is currently present 
in two PRs: #6114 and #5119. I would want to avoid copying this function into 
a yet another PR.

Ref vim/vim#1476.
This commit is contained in:
ZyX 2017-02-14 20:46:12 +03:00
parent 066e6b8e9b
commit f489827b5f
2 changed files with 83 additions and 23 deletions

View File

@ -18033,7 +18033,11 @@ static bool write_list(FileDescriptor *const fp, const list_T *const list,
{ {
int error = 0; int error = 0;
for (const listitem_T *li = list->lv_first; li != NULL; li = li->li_next) { for (const listitem_T *li = list->lv_first; li != NULL; li = li->li_next) {
const char *const s = (const char *)get_tv_string((typval_T *)&li->li_tv); const char *const s = (const char *)get_tv_string_chk(
(typval_T *)&li->li_tv);
if (s == NULL) {
return false;
}
const char *hunk_start = s; const char *hunk_start = s;
for (const char *p = hunk_start;; p++) { for (const char *p = hunk_start;; p++) {
if (*p == NUL || *p == NL) { if (*p == NUL || *p == NL) {
@ -18168,15 +18172,24 @@ static void f_writefile(typval_T *argvars, typval_T *rettv, FunPtr fptr)
bool binary = false; bool binary = false;
bool append = false; bool append = false;
if (argvars[2].v_type != VAR_UNKNOWN) { if (argvars[2].v_type != VAR_UNKNOWN) {
if (vim_strchr(get_tv_string(&argvars[2]), 'b')) { const char *const flags = (const char *)get_tv_string_chk(&argvars[2]);
if (flags == NULL) {
return;
}
if (strchr(flags, 'b')) {
binary = true; binary = true;
} }
if (vim_strchr(get_tv_string(&argvars[2]), 'a')) { if (strchr(flags, 'a')) {
append = true; append = true;
} }
} }
const char *const fname = (const char *)get_tv_string(&argvars[1]); const char buf[NUMBUFLEN];
const char *const fname = (const char *)get_tv_string_buf_chk(&argvars[1],
(char_u *)buf);
if (fname == NULL) {
return;
}
FileDescriptor *fp; FileDescriptor *fp;
int error; int error;
rettv->vval.v_number = -1; rettv->vval.v_number = -1;

View File

@ -1,15 +1,34 @@
local helpers = require('test.functional.helpers')(after_each) local helpers = require('test.functional.helpers')(after_each)
local lfs = require('lfs')
local clear = helpers.clear local clear = helpers.clear
local eq = helpers.eq local eq = helpers.eq
local funcs = helpers.funcs local funcs = helpers.funcs
local meths = helpers.meths
local exc_exec = helpers.exc_exec local exc_exec = helpers.exc_exec
local read_file = helpers.read_file local read_file = helpers.read_file
local write_file = helpers.write_file
local redir_exec = helpers.redir_exec
local fname = 'Xtest-functional-eval-writefile' local fname = 'Xtest-functional-eval-writefile'
local dname = fname .. '.d'
local dfname_tail = '1'
local dfname = dname .. '/' .. dfname_tail
local ddname_tail = '2'
local ddname = dname .. '/' .. ddname_tail
before_each(clear) before_each(function()
after_each(function() os.remove(fname) end) lfs.mkdir(dname)
lfs.mkdir(ddname)
clear()
end)
after_each(function()
os.remove(fname)
os.remove(dfname)
lfs.rmdir(ddname)
lfs.rmdir(dname)
end)
describe('writefile()', function() describe('writefile()', function()
it('writes empty list to a file', function() it('writes empty list to a file', function()
@ -68,26 +87,54 @@ describe('writefile()', function()
eq('a\0', read_file(fname)) eq('a\0', read_file(fname))
end) end)
it('shows correct file name when supplied numbers', function()
meths.set_current_dir(dname)
eq('\nE482: Can\'t open file 2 for writing: illegal operation on a directory',
redir_exec(('call writefile([42], %s)'):format(ddname_tail)))
end)
it('errors out with invalid arguments', function() it('errors out with invalid arguments', function()
eq('Vim(call):E119: Not enough arguments for function: writefile', write_file(fname, 'TEST')
exc_exec('call writefile()')) eq('\nE119: Not enough arguments for function: writefile',
eq('Vim(call):E119: Not enough arguments for function: writefile', redir_exec('call writefile()'))
exc_exec('call writefile([])')) eq('\nE119: Not enough arguments for function: writefile',
eq('Vim(call):E118: Too many arguments for function: writefile', redir_exec('call writefile([])'))
exc_exec(('call writefile([], "%s", "b", 1)'):format(fname))) eq('\nE118: Too many arguments for function: writefile',
redir_exec(('call writefile([], "%s", "b", 1)'):format(fname)))
for _, arg in ipairs({'0', '0.0', 'function("tr")', '{}', '"test"'}) do for _, arg in ipairs({'0', '0.0', 'function("tr")', '{}', '"test"'}) do
eq('Vim(call):E686: Argument of writefile() must be a List', eq('\nE686: Argument of writefile() must be a List',
exc_exec(('call writefile(%s, "%s", "b")'):format(arg, fname))) redir_exec(('call writefile(%s, "%s", "b")'):format(arg, fname)))
end end
for _, args in ipairs({'%s, "b"', '"' .. fname .. '", %s'}) do for _, args in ipairs({'[], %s, "b"', '[], "' .. fname .. '", %s'}) do
eq('Vim(call):E806: using Float as a String', eq('\nE806: using Float as a String',
exc_exec(('call writefile([], %s)'):format(args:format('0.0')))) redir_exec(('call writefile(%s)'):format(args:format('0.0'))))
eq('Vim(call):E730: using List as a String', eq('\nE730: using List as a String',
exc_exec(('call writefile([], %s)'):format(args:format('[]')))) redir_exec(('call writefile(%s)'):format(args:format('[]'))))
eq('Vim(call):E731: using Dictionary as a String', eq('\nE731: using Dictionary as a String',
exc_exec(('call writefile([], %s)'):format(args:format('{}')))) redir_exec(('call writefile(%s)'):format(args:format('{}'))))
eq('Vim(call):E729: using Funcref as a String', eq('\nE729: using Funcref as a String',
exc_exec(('call writefile([], %s)'):format(args:format('function("tr")')))) redir_exec(('call writefile(%s)'):format(args:format('function("tr")'))))
end end
eq('TEST', read_file(fname))
end)
it('stops writing to file after error in list', function()
local args = '["tset"] + repeat([%s], 3), "' .. fname .. '"'
eq('\nE806: using Float as a String',
redir_exec(('call writefile(%s)'):format(args:format('0.0'))))
eq('tset\n', read_file(fname))
write_file(fname, 'TEST')
eq('\nE730: using List as a String',
redir_exec(('call writefile(%s)'):format(args:format('[]'))))
eq('tset\n', read_file(fname))
write_file(fname, 'TEST')
eq('\nE731: using Dictionary as a String',
redir_exec(('call writefile(%s)'):format(args:format('{}'))))
eq('tset\n', read_file(fname))
write_file(fname, 'TEST')
eq('\nE729: using Funcref as a String',
redir_exec(('call writefile(%s)'):format(args:format('function("tr")'))))
eq('tset\n', read_file(fname))
write_file(fname, 'TEST')
end) end)
end) end)