writefile_spec.lua 5.8 KB

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