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).
15
local helpers, lfs = require('test.functional.helpers'), require('lfs')
16
local clear, execute, expect, eq, neq, dedent, write_file, feed =
17
helpers.clear, helpers.execute, helpers.expect, helpers.eq, helpers.neq,
18
helpers.dedent, helpers.write_file, helpers.feed
20
local function has_gzip()
21
return os.execute('gzip --help >/dev/null 2>&1') == 0
24
local function prepare_gz_file(name, text)
25
write_file(name, text..'\n')
26
-- Compress the file with gzip.
27
os.execute('gzip --force '..name)
28
-- This should create the .gz file and delete the original.
29
neq(nil, lfs.attributes(name..'.gz'))
30
eq(nil, lfs.attributes(name))
33
describe('file reading, writing and bufnew and filter autocommands', function()
34
local text1 = dedent([[
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
47
write_file('Xtest.c', [[
49
* Here is a new .c file
55
os.remove('Xtestfile.gz')
60
if not has_gzip() then
61
pending('skipped (missing `gzip` utility)', function() end)
64
it('FileReadPost (using gzip)', function()
65
prepare_gz_file('Xtestfile', text1)
66
execute('let $GZIP = ""')
67
--execute('au FileChangedShell * echo "caught FileChangedShell"')
69
execute("au FileReadPost *.gz '[,']!gzip -d")
70
-- Read and decompress the testfile.
71
execute('$r Xtestfile.gz')
75
it('BufReadPre, BufReadPost (using gzip)', function()
76
prepare_gz_file('Xtestfile', text1)
77
local gzip_data = io.open('Xtestfile.gz'):read('*all')
78
execute('let $GZIP = ""')
79
-- Setup autocommands to decompress before reading and re-compress afterwards.
80
execute("au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand('<afile>'))")
81
execute("au BufReadPre *.gz call rename(expand('<afile>:r'), expand('<afile>'))")
82
execute("au BufReadPost *.gz call rename(expand('<afile>'), expand('<afile>:r'))")
83
execute("au BufReadPost *.gz exe '!gzip ' . shellescape(expand('<afile>:r'))")
84
-- Edit compressed file.
85
execute('e! Xtestfile.gz')
86
-- Discard all prompts and messages.
88
-- Expect the decompressed file in the buffer.
90
-- Expect the original file to be unchanged.
91
eq(gzip_data, io.open('Xtestfile.gz'):read('*all'))
94
it('FileReadPre, FileReadPost', function()
95
prepare_gz_file('Xtestfile', text1)
96
execute('au! FileReadPre *.gz exe "silent !gzip -d " . shellescape(expand("<afile>"))')
97
execute('au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))')
98
execute("au! FileReadPost *.gz '[,']s/l/L/")
99
-- Read compressed file.
100
execute('$r Xtestfile.gz')
101
-- Discard all prompts and messages.
106
Line 2 Abcdefghijklmnopqrstuvwxyz
107
Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
108
Line 4 Abcdefghijklmnopqrstuvwxyz
109
Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
110
Line 6 Abcdefghijklmnopqrstuvwxyz
111
Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
112
Line 8 Abcdefghijklmnopqrstuvwxyz
113
Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
114
Line 10 Abcdefghijklmnopqrstuvwxyz
120
it('FileAppendPre, FileAppendPost', function()
121
execute('au BufNewFile *.c read Xtest.c')
122
-- Will load Xtest.c.
124
execute("au FileAppendPre *.out '[,']s/new/NEW/")
125
execute('au FileAppendPost *.out !cat Xtest.c >>test.out')
126
-- Append it to the output file.
127
execute('w>>test.out')
128
-- Discard all prompts and messages.
130
-- Expect the decompressed file in the buffer.
131
execute('e test.out')
135
* Here is a NEW .c file
139
it('FilterReadPre, FilterReadPost', function()
140
-- Write a special input file for this test block.
141
write_file('test.out', dedent([[
143
]]) .. text1 .. dedent([[
148
* Here is a new .c file
151
]]) .. text1 .. dedent([[
155
* Here is a NEW .c file
158
* Here is a new .c file
160
]]) .. text1 .. dedent([[
163
* Here is a new .c file
165
-- Need temp files here.
166
execute('set shelltemp')
167
execute('au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")')
168
execute('au FilterReadPre *.out exe "silent !sed s/e/E/ " . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))')
169
execute('au FilterReadPre *.out exe "silent !rm " . shellescape(expand("<afile>")) . ".t"')
170
execute("au FilterReadPost *.out '[,']s/x/X/g")
171
-- Edit the output file.
172
execute('e! test.out')
174
-- Discard all prompts and messages.
176
-- Remove CR for when sed adds them.
177
execute([[23,$s/\r$//]])
181
line 2 Abcdefghijklmnopqrstuvwxyz
182
line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
183
line 4 Abcdefghijklmnopqrstuvwxyz
184
line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
185
line 6 Abcdefghijklmnopqrstuvwxyz
186
line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
187
line 8 Abcdefghijklmnopqrstuvwxyz
188
line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
189
line 10 Abcdefghijklmnopqrstuvwxyz
194
* Here is a new .c file
198
line 2 Abcdefghijklmnopqrstuvwxyz
199
line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
200
line 4 Abcdefghijklmnopqrstuvwxyz
201
linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
202
linE 6 AbcdefghijklmnopqrstuvwXyz
203
linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
204
linE 8 AbcdefghijklmnopqrstuvwXyz
205
linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
206
linE 10 AbcdefghijklmnopqrstuvwXyz
210
* HEre is a NEW .c file
213
* HEre is a new .c file
216
linE 2 AbcdefghijklmnopqrstuvwXyz
217
linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
218
linE 4 AbcdefghijklmnopqrstuvwXyz
219
linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
220
linE 6 AbcdefghijklmnopqrstuvwXyz
221
linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
222
linE 8 AbcdefghijklmnopqrstuvwXyz
223
linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
224
linE 10 AbcdefghijklmnopqrstuvwXyz
227
* HEre is a new .c file