robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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