Merge #8621 from janlazo/vim-8.0.0642

This commit is contained in:
Justin M. Keyes 2018-06-25 02:08:21 +02:00 committed by GitHub
commit e475476d10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 17 deletions

View File

@ -17675,7 +17675,7 @@ static void f_wordcount(typval_T *argvars, typval_T *rettv, FunPtr fptr)
/// "writefile()" function
static void f_writefile(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
rettv->vval.v_number = 0; // Assuming success.
rettv->vval.v_number = -1;
if (check_restricted() || check_secure()) {
return;
@ -17685,6 +17685,12 @@ static void f_writefile(typval_T *argvars, typval_T *rettv, FunPtr fptr)
EMSG2(_(e_listarg), "writefile()");
return;
}
const list_T *const list = argvars[0].vval.v_list;
TV_LIST_ITER_CONST(list, li, {
if (!tv_check_str_or_nr(TV_LIST_ITEM_TV(li))) {
return;
}
});
bool binary = false;
bool append = false;
@ -17716,7 +17722,6 @@ static void f_writefile(typval_T *argvars, typval_T *rettv, FunPtr fptr)
}
FileDescriptor fp;
int error;
rettv->vval.v_number = -1;
if (*fname == NUL) {
EMSG(_("E482: Can't open file with an empty name"));
} else if ((error = file_open(&fp, fname,
@ -17725,7 +17730,7 @@ static void f_writefile(typval_T *argvars, typval_T *rettv, FunPtr fptr)
emsgf(_("E482: Can't open file %s for writing: %s"),
fname, os_strerror(error));
} else {
if (write_list(&fp, argvars[0].vval.v_list, binary)) {
if (write_list(&fp, list, binary)) {
rettv->vval.v_number = 0;
}
if ((error = file_close(&fp, do_fsync)) != 0) {

View File

@ -1,5 +1,6 @@
" Tests for the writefile() function.
function! Test_WriteFile()
func Test_writefile()
let f = tempname()
call writefile(["over","written"], f, "b")
call writefile(["hello","world"], f, "b")
@ -13,4 +14,20 @@ function! Test_WriteFile()
call assert_equal("morning", l[3])
call assert_equal("vimmers", l[4])
call delete(f)
endfunction
endfunc
func Test_writefile_fails_gently()
call assert_fails('call writefile(["test"], "Xfile", [])', 'E730:')
call assert_false(filereadable("Xfile"))
call delete("Xfile")
call assert_fails('call writefile(["test", [], [], [], "tset"], "Xfile")', 'E745:')
call assert_false(filereadable("Xfile"))
call delete("Xfile")
call assert_fails('call writefile([], "Xfile", [])', 'E730:')
call assert_false(filereadable("Xfile"))
call delete("Xfile")
call assert_fails('call writefile([], [])', 'E730:')
endfunc

View File

@ -127,23 +127,20 @@ describe('writefile()', function()
eq('TEST', read_file(fname))
end)
it('stops writing to file after error in list', function()
it('does not write to file if error in list', function()
local args = '["tset"] + repeat([%s], 3), "' .. fname .. '"'
eq('\nE806: using Float as a String',
eq('\nE805: Expected a Number or a String, Float found',
redir_exec(('call writefile(%s)'):format(args:format('0.0'))))
eq('tset\n', read_file(fname))
eq(nil, read_file(fname))
write_file(fname, 'TEST')
eq('\nE730: using List as a String',
eq('\nE745: Expected a Number or a String, List found',
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',
eq('TEST', read_file(fname))
eq('\nE728: Expected a Number or a String, Dictionary found',
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',
eq('TEST', read_file(fname))
eq('\nE703: Expected a Number or a String, Funcref found',
redir_exec(('call writefile(%s)'):format(args:format('function("tr")'))))
eq('tset\n', read_file(fname))
write_file(fname, 'TEST')
eq('TEST', read_file(fname))
end)
end)