Files
arti-pack/tests/fs_spec.lua
T
unkin-agent 46517953e5
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
Add arti-pack plugin manager scaffold
- add lua modules for spec parsing, lock state, fs helpers and the installer
- add ArtiPackSync/ArtiPackClean/ArtiPackList commands and help doc
- add headless nvim test harness and specs
- add make test/lint/fmt/pre-commit and patch/minor/major bump targets
- add woodpecker test, lint and pre-commit pipelines
2026-09-08 23:22:15 +10:00

100 lines
2.9 KiB
Lua

local t = require('tests.harness')
local fs = require('arti-pack.fs')
local function tmpdir()
local dir = vim.fn.tempname()
vim.fn.mkdir(dir, 'p')
return dir
end
local function write(path, contents)
local fd = assert(io.open(path, 'wb'))
fd:write(contents)
fd:close()
return path
end
t.describe('fs.archive_root', function()
t.it('strips the single wrapper directory a tag archive unpacks to', function()
local dir = tmpdir()
vim.fn.mkdir(dir .. '/plenary.nvim-0.1.4/lua', 'p')
t.eq(dir .. '/plenary.nvim-0.1.4', fs.archive_root(dir))
end)
t.it('keeps the directory when the archive unpacked flat', function()
local dir = tmpdir()
vim.fn.mkdir(dir .. '/lua', 'p')
write(dir .. '/README.md', 'hi')
t.eq(dir, fs.archive_root(dir))
end)
t.it('keeps the directory when its only entry is a file', function()
local dir = tmpdir()
write(dir .. '/init.lua', 'return {}')
t.eq(dir, fs.archive_root(dir))
end)
end)
t.describe('fs.verify_archive', function()
t.it('accepts a zip magic header', function()
local path = write(tmpdir() .. '/a.zip', 'PK\3\4rest of the file')
t.truthy(fs.verify_archive(path, 'zip'))
end)
t.it('accepts a gzip magic header', function()
local path = write(tmpdir() .. '/a.tar.gz', '\31\139\8\0padding')
t.truthy(fs.verify_archive(path, 'tar.gz'))
end)
t.it('rejects an empty download', function()
local path = write(tmpdir() .. '/a.zip', '')
local ok, err = fs.verify_archive(path, 'zip')
t.falsy(ok)
t.matches('empty', err)
end)
t.it('rejects an html error page served in place of a zip', function()
local path = write(tmpdir() .. '/a.zip', '<!DOCTYPE html><html>404</html>')
local ok, err = fs.verify_archive(path, 'zip')
t.falsy(ok)
t.matches('not a zip', err)
end)
t.it('rejects a zip when a tar.gz was expected', function()
local path = write(tmpdir() .. '/a.tar.gz', 'PK\3\4rest')
local ok, err = fs.verify_archive(path, 'tar.gz')
t.falsy(ok)
t.matches('not a tar%.gz', err)
end)
t.it('rejects a missing file', function()
local ok, err = fs.verify_archive(tmpdir() .. '/absent.zip', 'zip')
t.falsy(ok)
t.matches('empty', err)
end)
end)
t.describe('fs.extract_cmd', function()
t.it('uses unzip into the destination for zips', function()
t.eq({ 'unzip', '-q', '-o', '/a.zip', '-d', '/dest' }, fs.extract_cmd('/a.zip', '/dest', 'zip'))
end)
t.it('uses tar for gzipped tarballs', function()
t.eq({ 'tar', '-xzf', '/a.tar.gz', '-C', '/dest' }, fs.extract_cmd('/a.tar.gz', '/dest', 'tar.gz'))
end)
end)
t.describe('fs.rm_rf', function()
t.it('removes a populated directory tree', function()
local dir = tmpdir()
vim.fn.mkdir(dir .. '/a/b', 'p')
write(dir .. '/a/b/c.txt', 'x')
fs.rm_rf(dir .. '/a')
t.falsy(fs.exists(dir .. '/a'))
end)
t.it('is a no-op on a path that is not there', function()
fs.rm_rf(tmpdir() .. '/never-existed')
end)
end)