From fe0eecf9cc9a9290cc67168779edc101e59d77eb Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 31 Jul 2016 20:13:19 +0300 Subject: eval: writefile: Give more adequate IO errors and do not call putc() --- test/functional/eval/writefile_spec.lua | 93 +++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 test/functional/eval/writefile_spec.lua (limited to 'test/functional/eval/writefile_spec.lua') diff --git a/test/functional/eval/writefile_spec.lua b/test/functional/eval/writefile_spec.lua new file mode 100644 index 0000000000..a8b773f756 --- /dev/null +++ b/test/functional/eval/writefile_spec.lua @@ -0,0 +1,93 @@ +local helpers = require('test.functional.helpers')(after_each) + +local clear = helpers.clear +local eq = helpers.eq +local funcs = helpers.funcs +local exc_exec = helpers.exc_exec +local read_file = helpers.read_file + +local fname = 'Xtest-functional-eval-writefile' + +before_each(clear) +after_each(function() os.remove(fname) end) + +describe('writefile()', function() + it('writes empty list to a file', function() + eq(nil, read_file(fname)) + eq(0, funcs.writefile({}, fname)) + eq('', read_file(fname)) + os.remove(fname) + eq(nil, read_file(fname)) + eq(0, funcs.writefile({}, fname, 'b')) + eq('', read_file(fname)) + os.remove(fname) + eq(nil, read_file(fname)) + eq(0, funcs.writefile({}, fname, 'ab')) + eq('', read_file(fname)) + os.remove(fname) + eq(nil, read_file(fname)) + eq(0, funcs.writefile({}, fname, 'a')) + 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) + + it('appends to a file', function() + eq(nil, read_file(fname)) + eq(0, funcs.writefile({'abc', 'def', 'ghi'}, fname)) + eq('abc\ndef\nghi\n', read_file(fname)) + eq(0, funcs.writefile({'jkl'}, fname, 'a')) + eq('abc\ndef\nghi\njkl\n', read_file(fname)) + os.remove(fname) + eq(nil, read_file(fname)) + eq(0, funcs.writefile({'abc', 'def', 'ghi'}, fname, 'b')) + eq('abc\ndef\nghi', read_file(fname)) + eq(0, funcs.writefile({'jkl'}, fname, 'ab')) + eq('abc\ndef\nghijkl', read_file(fname)) + end) + + it('correctly treats NLs', function() + eq(0, funcs.writefile({'\na\nb\n'}, fname, 'b')) + eq('\0a\0b\0', read_file(fname)) + eq(0, funcs.writefile({'a\n\n\nb'}, fname, 'b')) + eq('a\0\0\0b', read_file(fname)) + end) + + it('correctly overwrites file', function() + eq(0, funcs.writefile({'\na\nb\n'}, fname, 'b')) + eq('\0a\0b\0', read_file(fname)) + eq(0, funcs.writefile({'a\n'}, fname, 'b')) + eq('a\0', read_file(fname)) + end) + + it('errors out with invalid arguments', function() + eq('Vim(call):E119: Not enough arguments for function: writefile', + exc_exec('call writefile()')) + eq('Vim(call):E119: Not enough arguments for function: writefile', + exc_exec('call writefile([])')) + eq('Vim(call):E118: Too many arguments for function: writefile', + exc_exec(('call writefile([], "%s", "b", 1)'):format(fname))) + for _, arg in ipairs({'0', '0.0', 'function("tr")', '{}', '"test"'}) do + eq('Vim(call):E686: Argument of writefile() must be a List', + exc_exec(('call writefile(%s, "%s", "b")'):format(arg, fname))) + end + for _, args in ipairs({'%s, "b"', '"' .. fname .. '", %s'}) do + eq('Vim(call):E806: using Float as a String', + exc_exec(('call writefile([], %s)'):format(args:format('0.0')))) + eq('Vim(call):E730: using List as a String', + exc_exec(('call writefile([], %s)'):format(args:format('[]')))) + eq('Vim(call):E731: using Dictionary as a String', + exc_exec(('call writefile([], %s)'):format(args:format('{}')))) + eq('Vim(call):E729: using Funcref as a String', + exc_exec(('call writefile([], %s)'):format(args:format('function("tr")')))) + end + end) +end) -- cgit From f489827b5f9b8b1ecd684768a92871389217aa91 Mon Sep 17 00:00:00 2001 From: ZyX Date: Tue, 14 Feb 2017 20:46:12 +0300 Subject: eval: Fix error messages from writefile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- test/functional/eval/writefile_spec.lua | 85 +++++++++++++++++++++++++-------- 1 file changed, 66 insertions(+), 19 deletions(-) (limited to 'test/functional/eval/writefile_spec.lua') diff --git a/test/functional/eval/writefile_spec.lua b/test/functional/eval/writefile_spec.lua index a8b773f756..3052c616e0 100644 --- a/test/functional/eval/writefile_spec.lua +++ b/test/functional/eval/writefile_spec.lua @@ -1,15 +1,34 @@ local helpers = require('test.functional.helpers')(after_each) +local lfs = require('lfs') local clear = helpers.clear local eq = helpers.eq local funcs = helpers.funcs +local meths = helpers.meths local exc_exec = helpers.exc_exec 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 dname = fname .. '.d' +local dfname_tail = '1' +local dfname = dname .. '/' .. dfname_tail +local ddname_tail = '2' +local ddname = dname .. '/' .. ddname_tail -before_each(clear) -after_each(function() os.remove(fname) end) +before_each(function() + 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() it('writes empty list to a file', function() @@ -68,26 +87,54 @@ describe('writefile()', function() eq('a\0', read_file(fname)) 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() - eq('Vim(call):E119: Not enough arguments for function: writefile', - exc_exec('call writefile()')) - eq('Vim(call):E119: Not enough arguments for function: writefile', - exc_exec('call writefile([])')) - eq('Vim(call):E118: Too many arguments for function: writefile', - exc_exec(('call writefile([], "%s", "b", 1)'):format(fname))) + write_file(fname, 'TEST') + eq('\nE119: Not enough arguments for function: writefile', + redir_exec('call writefile()')) + eq('\nE119: Not enough arguments for function: writefile', + redir_exec('call writefile([])')) + 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 - eq('Vim(call):E686: Argument of writefile() must be a List', - exc_exec(('call writefile(%s, "%s", "b")'):format(arg, fname))) + eq('\nE686: Argument of writefile() must be a List', + redir_exec(('call writefile(%s, "%s", "b")'):format(arg, fname))) end - for _, args in ipairs({'%s, "b"', '"' .. fname .. '", %s'}) do - eq('Vim(call):E806: using Float as a String', - exc_exec(('call writefile([], %s)'):format(args:format('0.0')))) - eq('Vim(call):E730: using List as a String', - exc_exec(('call writefile([], %s)'):format(args:format('[]')))) - eq('Vim(call):E731: using Dictionary as a String', - exc_exec(('call writefile([], %s)'):format(args:format('{}')))) - eq('Vim(call):E729: using Funcref as a String', - exc_exec(('call writefile([], %s)'):format(args:format('function("tr")')))) + for _, args in ipairs({'[], %s, "b"', '[], "' .. fname .. '", %s'}) do + eq('\nE806: using Float as a String', + redir_exec(('call writefile(%s)'):format(args:format('0.0')))) + eq('\nE730: using List as a String', + redir_exec(('call writefile(%s)'):format(args:format('[]')))) + eq('\nE731: using Dictionary as a String', + redir_exec(('call writefile(%s)'):format(args:format('{}')))) + eq('\nE729: using Funcref as a String', + redir_exec(('call writefile(%s)'):format(args:format('function("tr")')))) 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) -- cgit