011_autocommands_spec.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. -- Tests for autocommands
  2. -- - FileWritePre writing a compressed file
  3. -- - FileReadPost reading a compressed file
  4. -- - BufNewFile reading a file template
  5. -- - BufReadPre decompressing the file to be read
  6. -- - FilterReadPre substituting characters in the temp file
  7. -- - FilterReadPost substituting characters after filtering
  8. -- - FileReadPre set options for decompression
  9. -- - FileReadPost decompress the file
  10. -- Note: This test is skipped if "gzip" is not available.
  11. -- $GZIP is made empty, "-v" would cause trouble.
  12. -- Use a FileChangedShell autocommand to avoid a prompt for "Xtestfile.gz"
  13. -- being modified outside of Vim (noticed on Solaris).
  14. local t = require('test.testutil')
  15. local n = require('test.functional.testnvim')()
  16. local clear, feed_command, expect, eq, neq, dedent, write_file, feed =
  17. n.clear, n.feed_command, n.expect, t.eq, t.neq, t.dedent, t.write_file, n.feed
  18. local command = n.command
  19. local read_file = t.read_file
  20. local is_os = t.is_os
  21. local function has_gzip()
  22. local null = is_os('win') and 'nul' or '/dev/null'
  23. return os.execute('gzip --help >' .. null .. ' 2>&1') == 0
  24. end
  25. local function prepare_gz_file(name, text)
  26. write_file(name, text .. '\n')
  27. -- Compress the file with gzip.
  28. command([[call system(['gzip', '--force', ']] .. name .. [['])]])
  29. -- This should create the .gz file and delete the original.
  30. neq(nil, vim.uv.fs_stat(name .. '.gz'))
  31. eq(nil, vim.uv.fs_stat(name))
  32. end
  33. describe('file reading, writing and bufnew and filter autocommands', function()
  34. local text1 = dedent([[
  35. start of testfile
  36. line 2 Abcdefghijklmnopqrstuvwxyz
  37. line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  38. line 4 Abcdefghijklmnopqrstuvwxyz
  39. line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  40. line 6 Abcdefghijklmnopqrstuvwxyz
  41. line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  42. line 8 Abcdefghijklmnopqrstuvwxyz
  43. line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  44. line 10 Abcdefghijklmnopqrstuvwxyz
  45. end of testfile]])
  46. setup(function()
  47. write_file(
  48. 'Xtest.c',
  49. [[
  50. /*
  51. * Here is a new .c file
  52. */
  53. ]]
  54. )
  55. end)
  56. before_each(function()
  57. clear({ env = { GZIP = nil } })
  58. end)
  59. teardown(function()
  60. os.remove('Xtestfile.gz')
  61. os.remove('Xtest.c')
  62. os.remove('test.out')
  63. end)
  64. if not has_gzip() then
  65. pending('skipped (missing `gzip` utility)', function() end)
  66. else
  67. it('FileReadPost (using gzip)', function()
  68. prepare_gz_file('Xtestfile', text1)
  69. --execute('au FileChangedShell * echo "caught FileChangedShell"')
  70. feed_command('set bin')
  71. feed_command("au FileReadPost *.gz '[,']!gzip -d")
  72. -- Read and decompress the testfile.
  73. feed_command('$r Xtestfile.gz')
  74. expect('\n' .. text1)
  75. end)
  76. it('BufReadPre, BufReadPost (using gzip)', function()
  77. prepare_gz_file('Xtestfile', text1)
  78. local gzip_data = read_file('Xtestfile.gz')
  79. -- Setup autocommands to decompress before reading and re-compress afterwards.
  80. feed_command("au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand('<afile>'))")
  81. feed_command("au BufReadPre *.gz call rename(expand('<afile>:r'), expand('<afile>'))")
  82. feed_command("au BufReadPost *.gz call rename(expand('<afile>'), expand('<afile>:r'))")
  83. feed_command("au BufReadPost *.gz exe '!gzip ' . shellescape(expand('<afile>:r'))")
  84. -- Edit compressed file.
  85. feed_command('e! Xtestfile.gz')
  86. -- Discard all prompts and messages.
  87. feed('<C-L>')
  88. -- Expect the decompressed file in the buffer.
  89. expect(text1)
  90. -- Expect the original file to be unchanged.
  91. eq(gzip_data, read_file('Xtestfile.gz'))
  92. end)
  93. -- luacheck: ignore 621 (Indentation)
  94. -- luacheck: ignore 611 (Line contains only whitespaces)
  95. it('FileReadPre, FileReadPost', function()
  96. prepare_gz_file('Xtestfile', text1)
  97. feed_command(
  98. 'au! FileReadPre *.gz exe "silent !gzip -d " . shellescape(expand("<afile>"))'
  99. )
  100. feed_command('au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))')
  101. feed_command("au! FileReadPost *.gz '[,']s/l/L/")
  102. -- Read compressed file.
  103. feed_command('$r Xtestfile.gz')
  104. -- Discard all prompts and messages.
  105. feed('<C-L>')
  106. expect([[
  107. start of testfiLe
  108. Line 2 Abcdefghijklmnopqrstuvwxyz
  109. Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  110. Line 4 Abcdefghijklmnopqrstuvwxyz
  111. Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  112. Line 6 Abcdefghijklmnopqrstuvwxyz
  113. Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  114. Line 8 Abcdefghijklmnopqrstuvwxyz
  115. Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  116. Line 10 Abcdefghijklmnopqrstuvwxyz
  117. end of testfiLe]])
  118. end)
  119. end
  120. it('FileAppendPre, FileAppendPost', function()
  121. feed_command('au BufNewFile *.c read Xtest.c')
  122. -- Will load Xtest.c.
  123. feed_command('e! foo.c')
  124. feed_command("au FileAppendPre *.out '[,']s/new/NEW/")
  125. feed_command('au FileAppendPost *.out !cat Xtest.c >test.out')
  126. -- Append it to the output file.
  127. feed_command('w>>test.out')
  128. -- Discard all prompts and messages.
  129. feed('<C-L>')
  130. expect([[
  131. /*
  132. * Here is a NEW .c file
  133. */]])
  134. end)
  135. it('FilterReadPre, FilterReadPost', function()
  136. -- Write a special input file for this test block.
  137. write_file('test.out', dedent([[
  138. startstart
  139. ]]) .. text1 .. dedent([[
  140. start of test.c
  141. /*
  142. * Here is a new .c file
  143. */
  144. end of test.c
  145. ]]) .. text1 .. dedent([[
  146. /*
  147. * Here is a NEW .c file
  148. */
  149. /*
  150. * Here is a new .c file
  151. */
  152. ]]) .. text1 .. dedent([[
  153. /*
  154. * Here is a new .c file
  155. */]]))
  156. -- Need temp files here.
  157. feed_command('set shelltemp')
  158. feed_command(
  159. 'au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")'
  160. )
  161. feed_command(
  162. 'au FilterReadPre *.out exe "silent !sed s/e/E/ " . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))'
  163. )
  164. feed_command(
  165. 'au FilterReadPre *.out exe "silent !rm " . shellescape(expand("<afile>")) . ".t"'
  166. )
  167. feed_command("au FilterReadPost *.out '[,']s/x/X/g")
  168. -- Edit the output file.
  169. feed_command('e! test.out')
  170. feed_command('23,$!cat')
  171. -- Discard all prompts and messages.
  172. feed('<C-L>')
  173. -- Remove CR for when sed adds them.
  174. feed_command([[23,$s/\r$//]])
  175. expect([[
  176. startstart
  177. start of testfile
  178. line 2 Abcdefghijklmnopqrstuvwxyz
  179. line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  180. line 4 Abcdefghijklmnopqrstuvwxyz
  181. line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  182. line 6 Abcdefghijklmnopqrstuvwxyz
  183. line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  184. line 8 Abcdefghijklmnopqrstuvwxyz
  185. line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  186. line 10 Abcdefghijklmnopqrstuvwxyz
  187. end of testfile
  188. start of test.c
  189. /*
  190. * Here is a new .c file
  191. */
  192. end of test.c
  193. start of testfile
  194. line 2 Abcdefghijklmnopqrstuvwxyz
  195. line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  196. line 4 Abcdefghijklmnopqrstuvwxyz
  197. linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  198. linE 6 AbcdefghijklmnopqrstuvwXyz
  199. linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  200. linE 8 AbcdefghijklmnopqrstuvwXyz
  201. linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  202. linE 10 AbcdefghijklmnopqrstuvwXyz
  203. End of testfile
  204. /*
  205. * HEre is a NEW .c file
  206. */
  207. /*
  208. * HEre is a new .c file
  209. */
  210. start of tEstfile
  211. linE 2 AbcdefghijklmnopqrstuvwXyz
  212. linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  213. linE 4 AbcdefghijklmnopqrstuvwXyz
  214. linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  215. linE 6 AbcdefghijklmnopqrstuvwXyz
  216. linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  217. linE 8 AbcdefghijklmnopqrstuvwXyz
  218. linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  219. linE 10 AbcdefghijklmnopqrstuvwXyz
  220. End of testfile
  221. /*
  222. * HEre is a new .c file
  223. */]])
  224. end)
  225. end)