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