2016-07-31 10:13:19 -07:00
|
|
|
local helpers = require('test.functional.helpers')(after_each)
|
2017-02-14 10:46:12 -07:00
|
|
|
local lfs = require('lfs')
|
2016-07-31 10:13:19 -07:00
|
|
|
|
|
|
|
local clear = helpers.clear
|
|
|
|
local eq = helpers.eq
|
|
|
|
local funcs = helpers.funcs
|
2017-02-14 10:46:12 -07:00
|
|
|
local meths = helpers.meths
|
2016-07-31 10:13:19 -07:00
|
|
|
local exc_exec = helpers.exc_exec
|
|
|
|
local read_file = helpers.read_file
|
2017-02-14 10:46:12 -07:00
|
|
|
local write_file = helpers.write_file
|
|
|
|
local redir_exec = helpers.redir_exec
|
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()
|
|
|
|
lfs.mkdir(dname)
|
|
|
|
lfs.mkdir(ddname)
|
|
|
|
clear()
|
|
|
|
end)
|
|
|
|
|
|
|
|
after_each(function()
|
|
|
|
os.remove(fname)
|
|
|
|
os.remove(dfname)
|
|
|
|
lfs.rmdir(ddname)
|
|
|
|
lfs.rmdir(dname)
|
|
|
|
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))
|
|
|
|
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)
|
|
|
|
|
2017-04-02 16:11:27 -07:00
|
|
|
it('writes with s and S', function()
|
|
|
|
eq(0, funcs.writefile({'\na\nb\n'}, fname, 'bs'))
|
|
|
|
eq('\0a\0b\0', read_file(fname))
|
|
|
|
eq(0, funcs.writefile({'a\n\n\nb'}, fname, 'bS'))
|
|
|
|
eq('a\0\0\0b', read_file(fname))
|
|
|
|
end)
|
|
|
|
|
2016-07-31 10:13:19 -07:00
|
|
|
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)
|
|
|
|
|
2017-02-14 10:46:12 -07:00
|
|
|
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)
|
|
|
|
|
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')
|
|
|
|
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)))
|
2016-07-31 10:13:19 -07:00
|
|
|
for _, arg in ipairs({'0', '0.0', 'function("tr")', '{}', '"test"'}) do
|
2017-02-14 10:46:12 -07:00
|
|
|
eq('\nE686: Argument of writefile() must be a List',
|
|
|
|
redir_exec(('call writefile(%s, "%s", "b")'):format(arg, fname)))
|
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
|
|
|
|
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")'))))
|
2016-07-31 10:13:19 -07:00
|
|
|
end
|
2017-04-02 16:11:27 -07:00
|
|
|
eq('\nE5060: Unknown flag: «»',
|
|
|
|
redir_exec(('call writefile([], "%s", "bs«»")'):format(fname)))
|
2017-02-14 10:46:12 -07:00
|
|
|
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')
|
2016-07-31 10:13:19 -07:00
|
|
|
end)
|
|
|
|
end)
|