writefile_spec.lua (6707B)
1 local t = require('test.testutil') 2 local n = require('test.functional.testnvim')() 3 4 local mkdir = t.mkdir 5 local clear = n.clear 6 local eq = t.eq 7 local fn = n.fn 8 local api = n.api 9 local exc_exec = n.exc_exec 10 local read_file = t.read_file 11 local write_file = t.write_file 12 local pcall_err = t.pcall_err 13 local command = n.command 14 15 local fname = 'Xtest-functional-eval-writefile' 16 local dname = fname .. '.d' 17 local dfname_tail = '1' 18 local dfname = dname .. '/' .. dfname_tail 19 local ddname_tail = '2' 20 local ddname = dname .. '/' .. ddname_tail 21 22 before_each(function() 23 mkdir(dname) 24 mkdir(ddname) 25 clear() 26 end) 27 28 after_each(function() 29 os.remove(fname) 30 os.remove(dfname) 31 vim.uv.fs_rmdir(ddname) 32 vim.uv.fs_rmdir(dname) 33 end) 34 35 describe('writefile()', function() 36 it('writes empty list to a file', function() 37 eq(nil, read_file(fname)) 38 eq(0, fn.writefile({}, fname)) 39 eq('', read_file(fname)) 40 os.remove(fname) 41 eq(nil, read_file(fname)) 42 eq(0, fn.writefile({}, fname, 'b')) 43 eq('', read_file(fname)) 44 os.remove(fname) 45 eq(nil, read_file(fname)) 46 eq(0, fn.writefile({}, fname, 'ab')) 47 eq('', read_file(fname)) 48 os.remove(fname) 49 eq(nil, read_file(fname)) 50 eq(0, fn.writefile({}, fname, 'a')) 51 eq('', read_file(fname)) 52 end) 53 54 it('writes list with an empty string to a file', function() 55 eq(0, exc_exec(('call writefile([$XXX_NONEXISTENT_VAR_XXX], "%s", "b")'):format(fname))) 56 eq('', read_file(fname)) 57 eq(0, exc_exec(('call writefile([$XXX_NONEXISTENT_VAR_XXX], "%s")'):format(fname))) 58 eq('\n', read_file(fname)) 59 end) 60 61 it('writes list with a null string to a file', function() 62 eq(0, exc_exec(('call writefile([v:_null_string], "%s", "b")'):format(fname))) 63 eq('', read_file(fname)) 64 eq(0, exc_exec(('call writefile([v:_null_string], "%s")'):format(fname))) 65 eq('\n', read_file(fname)) 66 end) 67 68 it('appends to a file', function() 69 eq(nil, read_file(fname)) 70 eq(0, fn.writefile({ 'abc', 'def', 'ghi' }, fname)) 71 eq('abc\ndef\nghi\n', read_file(fname)) 72 eq(0, fn.writefile({ 'jkl' }, fname, 'a')) 73 eq('abc\ndef\nghi\njkl\n', read_file(fname)) 74 os.remove(fname) 75 eq(nil, read_file(fname)) 76 eq(0, fn.writefile({ 'abc', 'def', 'ghi' }, fname, 'b')) 77 eq('abc\ndef\nghi', read_file(fname)) 78 eq(0, fn.writefile({ 'jkl' }, fname, 'ab')) 79 eq('abc\ndef\nghijkl', read_file(fname)) 80 end) 81 82 it('correctly treats NLs', function() 83 eq(0, fn.writefile({ '\na\nb\n' }, fname, 'b')) 84 eq('\0a\0b\0', read_file(fname)) 85 eq(0, fn.writefile({ 'a\n\n\nb' }, fname, 'b')) 86 eq('a\0\0\0b', read_file(fname)) 87 end) 88 89 it('writes with s and S', function() 90 eq(0, fn.writefile({ '\na\nb\n' }, fname, 'bs')) 91 eq('\0a\0b\0', read_file(fname)) 92 eq(0, fn.writefile({ 'a\n\n\nb' }, fname, 'bS')) 93 eq('a\0\0\0b', read_file(fname)) 94 end) 95 96 it('correctly overwrites file', function() 97 eq(0, fn.writefile({ '\na\nb\n' }, fname, 'b')) 98 eq('\0a\0b\0', read_file(fname)) 99 eq(0, fn.writefile({ 'a\n' }, fname, 'b')) 100 eq('a\0', read_file(fname)) 101 end) 102 103 it('shows correct file name when supplied numbers', function() 104 api.nvim_set_current_dir(dname) 105 eq( 106 "Vim(call):E482: Can't open file 2 for writing: illegal operation on a directory", 107 pcall_err(command, ('call writefile([42], %s)'):format(ddname_tail)) 108 ) 109 end) 110 111 it('writefile(..., "p") creates missing parent directories', function() 112 os.remove(dname) 113 eq(nil, read_file(dfname)) 114 eq(0, fn.writefile({ 'abc', 'def', 'ghi' }, dfname, 'p')) 115 eq('abc\ndef\nghi\n', read_file(dfname)) 116 os.remove(dfname) 117 os.remove(dname) 118 eq(nil, read_file(dfname)) 119 eq(0, fn.writefile({ '\na\nb\n' }, dfname, 'pb')) 120 eq('\0a\0b\0', read_file(dfname)) 121 os.remove(dfname) 122 os.remove(dname) 123 eq( 124 'Vim(call):E32: No file name', 125 pcall_err(command, ('call writefile([], "%s", "p")'):format(dfname .. '.d/')) 126 ) 127 eq( 128 "Vim(call):E482: Can't open file ./ for writing: illegal operation on a directory", 129 pcall_err(command, 'call writefile([], "./", "p")') 130 ) 131 eq( 132 "Vim(call):E482: Can't open file . for writing: illegal operation on a directory", 133 pcall_err(command, 'call writefile([], ".", "p")') 134 ) 135 end) 136 137 it('errors out with invalid arguments', function() 138 write_file(fname, 'TEST') 139 eq( 140 'Vim(call):E119: Not enough arguments for function: writefile', 141 pcall_err(command, 'call writefile()') 142 ) 143 eq( 144 'Vim(call):E119: Not enough arguments for function: writefile', 145 pcall_err(command, 'call writefile([])') 146 ) 147 eq( 148 'Vim(call):E118: Too many arguments for function: writefile', 149 pcall_err(command, ('call writefile([], "%s", "b", 1)'):format(fname)) 150 ) 151 for _, arg in ipairs({ '0', '0.0', 'function("tr")', '{}', '"test"' }) do 152 eq( 153 'Vim(call):E475: Invalid argument: writefile() first argument must be a List or a Blob', 154 pcall_err(command, ('call writefile(%s, "%s", "b")'):format(arg, fname)) 155 ) 156 end 157 for _, args in ipairs({ '[], %s, "b"', '[], "' .. fname .. '", %s' }) do 158 eq( 159 'Vim(call):E730: Using a List as a String', 160 pcall_err(command, ('call writefile(%s)'):format(args:format('[]'))) 161 ) 162 eq( 163 'Vim(call):E731: Using a Dictionary as a String', 164 pcall_err(command, ('call writefile(%s)'):format(args:format('{}'))) 165 ) 166 eq( 167 'Vim(call):E729: Using a Funcref as a String', 168 pcall_err(command, ('call writefile(%s)'):format(args:format('function("tr")'))) 169 ) 170 end 171 eq( 172 'Vim(call):E5060: Unknown flag: «»', 173 pcall_err(command, ('call writefile([], "%s", "bs«»")'):format(fname)) 174 ) 175 eq('TEST', read_file(fname)) 176 end) 177 178 it('does not write to file if error in list', function() 179 local args = '["tset"] + repeat([%s], 3), "' .. fname .. '"' 180 eq( 181 'Vim(call):E805: Expected a Number or a String, Float found', 182 pcall_err(command, ('call writefile(%s)'):format(args:format('0.0'))) 183 ) 184 eq(nil, read_file(fname)) 185 write_file(fname, 'TEST') 186 eq( 187 'Vim(call):E745: Expected a Number or a String, List found', 188 pcall_err(command, ('call writefile(%s)'):format(args:format('[]'))) 189 ) 190 eq('TEST', read_file(fname)) 191 eq( 192 'Vim(call):E728: Expected a Number or a String, Dictionary found', 193 pcall_err(command, ('call writefile(%s)'):format(args:format('{}'))) 194 ) 195 eq('TEST', read_file(fname)) 196 eq( 197 'Vim(call):E703: Expected a Number or a String, Funcref found', 198 pcall_err(command, ('call writefile(%s)'):format(args:format('function("tr")'))) 199 ) 200 eq('TEST', read_file(fname)) 201 end) 202 end)