writefile_spec.lua 6.5 KB

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