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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
local n = require('test.functional.testnvim')()
local t = require('test.testutil')
local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
local api = n.api
local eq = t.eq
local exec_capture = n.exec_capture
local exec_lua = n.exec_lua
local feed = n.feed
local fn = n.fn
local poke_eventloop = n.poke_eventloop
local fixtures = vim.fs.joinpath(t.paths.test_source_path, 'test/functional/fixtures/zip')
local old_samples = vim.fs.joinpath(t.paths.test_source_path, 'test/old/testdir/samples')
local function lines()
return api.nvim_buf_get_lines(0, 0, -1, false)
end
local function edit(path)
api.nvim_cmd({ cmd = 'edit', args = { path }, magic = { file = false, bar = false } }, {})
end
local function clear_zip()
n.clear({ args = { '--clean' } })
end
local function line_of(text)
for i, line in ipairs(lines()) do
if line == text then
return i
end
end
error(('missing line %q in %s'):format(text, vim.inspect(lines())))
end
describe('nvim.zip', function()
local root
--- Copy an archive into the test directory and return its path.
local function stage(source_dir, source, as)
local target = vim.fs.joinpath(root, as or source)
assert(vim.uv.fs_copyfile(vim.fs.joinpath(source_dir, source), target))
return target
end
--- Stage the browsing fixture, restart, and open it.
local function browse(as)
local archive = stage(fixtures, 'browser.zip', as)
clear_zip()
edit(archive)
return archive
end
before_each(function()
t.skip(vim.fn.executable('unzip') == 0, 'unzip not available')
root = vim.fs.normalize(t.tmpname(false) .. ' space%#')
t.mkdir(root)
end)
after_each(function()
n.rmdir(root)
end)
describe('activation', function()
it('uses zip.lua by default', function()
browse()
eq('folder/', lines()[1])
eq(true, exec_lua('return vim.g.loaded_nvim_zip_plugin == true'))
eq(false, exec_lua('return vim.g.loaded_zipPlugin ~= nil'))
end)
it('defers to zipPlugin.vim loaded before startup plugins', function()
local archive = stage(old_samples, 'test.zip', 'legacy.zip')
n.clear({ args = { '--clean', '--cmd', 'packadd old-zip' } })
edit(archive)
eq(true, lines()[1]:find('" zip.vim version', 1, true) ~= nil)
eq(0, fn.exists('#nvim.zip'))
eq(false, exec_lua('return vim.g.loaded_nvim_zip_plugin ~= nil'))
end)
it('yields to zipPlugin.vim loaded after startup', function()
local archive = stage(old_samples, 'test.zip', 'legacy.zip')
clear_zip()
exec_lua([[vim.cmd.packadd('old-zip')]])
edit(archive)
eq(true, lines()[1]:find('" zip.vim version', 1, true) ~= nil)
eq(1, fn.exists('#nvim.zip'))
eq(true, exec_lua('return vim.g.loaded_nvim_zip_plugin == true'))
end)
it('can be disabled', function()
local archive = stage(fixtures, 'browser.zip')
n.clear({
args = { '--clean', '--cmd', 'let g:loaded_nvim_zip_plugin = 1' },
})
edit(archive)
eq(0, fn.exists('#nvim.zip'))
eq('', api.nvim_get_option_value('filetype', { buf = 0 }))
end)
it('can source zip.lua repeatedly', function()
clear_zip()
-- Re-sourcing must reuse the augroup rather than stack duplicate autocmds.
eq(
2,
exec_lua(function()
vim.cmd.runtime('plugin/zip.lua')
vim.cmd.runtime('plugin/zip.lua')
local ids = {}
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ group = 'nvim.zip' })) do
ids[autocmd.id] = true
end
return vim.tbl_count(ids)
end)
)
end)
it('reports an unavailable backend without claiming the buffer', function()
local archive = stage(fixtures, 'browser.zip')
clear_zip()
exec_lua([[vim.env.PATH = '']])
edit(archive)
poke_eventloop()
eq(true, exec_capture('messages'):find('unzip executable not found', 1, true) ~= nil)
eq(false, exec_lua('return vim.b.nvim_dir ~= nil'))
end)
end)
describe('browsing', function()
it('opens zip-compatible file types', function()
browse('browser.jar')
eq('folder/', lines()[1])
eq('zip', api.nvim_get_option_value('filetype', { buf = 0 }))
end)
it('browses directories and opens entries', function()
browse()
eq({
'folder/',
'inner.zip',
'../escape.txt',
'/absolute.txt',
'crlf.txt',
'noeol.txt',
'names/',
}, lines())
eq('zip', api.nvim_get_option_value('filetype', { buf = 0 }))
eq(true, exec_capture('syntax list zipDirectory'):find('zipDirectory', 1, true) ~= nil)
feed('<CR>')
poke_eventloop()
eq({ 'nested/', 'root.txt', 'root.java' }, lines())
feed('<CR>')
poke_eventloop()
eq({ 'file.txt' }, lines())
feed('-')
poke_eventloop()
eq({ 'nested/', 'root.txt', 'root.java' }, lines())
eq('nested/', api.nvim_get_current_line())
api.nvim_win_set_cursor(0, { 2, 0 })
feed('<CR>')
poke_eventloop()
eq({ 'root text' }, lines())
eq('nowrite', api.nvim_get_option_value('buftype', { buf = 0 }))
eq(true, api.nvim_get_option_value('readonly', { buf = 0 }))
eq(false, api.nvim_get_option_value('modifiable', { buf = 0 }))
eq(false, api.nvim_get_option_value('swapfile', { buf = 0 }))
end)
it('opens the containing directory from the archive root', function()
browse()
feed('-')
poke_eventloop()
eq(root, vim.fs.normalize(api.nvim_buf_get_name(0)))
eq('browser.zip', api.nvim_get_current_line())
eq('directory', api.nvim_get_option_value('filetype', { buf = 0 }))
end)
it('opens entries at quickfix locations', function()
local archive = stage(fixtures, 'browser.zip')
clear_zip()
local uri = ('zip://%s/crlf.txt'):format(archive)
fn.setqflist({}, 'r', { items = { { filename = uri, lnum = 2, col = 1 } } })
api.nvim_cmd({ cmd = 'cfirst' }, {})
eq(uri, api.nvim_buf_get_name(0))
eq({ 2, 0 }, api.nvim_win_get_cursor(0))
eq('text', api.nvim_get_option_value('filetype', { buf = 0 }))
end)
it('does not reinterpret an entry ending in .zip as an archive', function()
browse()
api.nvim_win_set_cursor(0, { line_of('inner.zip'), 0 })
feed('<CR>')
poke_eventloop()
eq({ 'nested payload' }, lines())
eq(false, exec_lua('return vim.b.nvim_dir ~= nil'))
end)
it('preserves normal file reading details for entries', function()
local archive = stage(fixtures, 'browser.zip')
clear_zip()
edit(('zip://%s/crlf.txt'):format(archive))
eq({ 'one', 'two' }, lines())
eq('dos', api.nvim_get_option_value('fileformat', { buf = 0 }))
eq(true, api.nvim_get_option_value('endofline', { buf = 0 }))
edit(('zip://%s/noeol.txt'):format(archive))
eq({ 'no final newline' }, lines())
eq(false, api.nvim_get_option_value('endofline', { buf = 0 }))
end)
it('keeps suspicious entry paths visible and readable', function()
browse()
api.nvim_win_set_cursor(0, { line_of('../escape.txt'), 0 })
feed('<CR>')
poke_eventloop()
eq({ 'escape payload' }, lines())
end)
it('opens non-zip files normally', function()
local archive = vim.fs.joinpath(root, 'plain.zip')
t.write_file(archive, 'plain text', true)
clear_zip()
edit(archive)
eq({ 'plain text' }, lines())
eq(false, exec_lua('return vim.b.nvim_dir ~= nil'))
end)
it('lists a valid empty archive as empty', function()
local archive = vim.fs.joinpath(root, 'empty.zip')
local file = assert(io.open(archive, 'wb'))
-- An end-of-central-directory record with no entries: a valid, empty archive.
file:write('PK\5\6' .. string.rep('\0', 18))
file:close()
clear_zip()
edit(archive)
eq({ '' }, lines())
eq('zip', api.nvim_get_option_value('filetype', { buf = 0 }))
eq(true, exec_lua('return vim.b.nvim_zip ~= nil'))
end)
it('keeps the current level when reload fails', function()
local archive = browse()
feed('<CR>')
poke_eventloop()
eq({ 'nested/', 'root.txt', 'root.java' }, lines())
assert(
|