e61e950d30
A registry with a stray space or newline passed validation and was concatenated verbatim into the archive URL, so curl saw the malformed address instead of the user seeing the required-registry error. Trim the value before matching and reject any whitespace left inside it; apply the same to a spec's explicit url.
212 lines
7.4 KiB
Lua
212 lines
7.4 KiB
Lua
local t = require('tests.harness')
|
|
local spec = require('arti-pack.spec')
|
|
|
|
local REGISTRY = 'https://artifacts.example.com/artifactory/github-remote'
|
|
|
|
t.describe('spec.parse', function()
|
|
t.it('parses a full table spec', function()
|
|
local s = assert(spec.parse({ 'nvim-lua/plenary.nvim', tag = 'v0.1.4', opt = true }))
|
|
t.eq('nvim-lua', s.owner)
|
|
t.eq('plenary.nvim', s.repo)
|
|
t.eq('plenary.nvim', s.name)
|
|
t.eq('v0.1.4', s.tag)
|
|
t.eq('zip', s.ext)
|
|
t.eq(true, s.opt)
|
|
end)
|
|
|
|
t.it('defaults opt to false', function()
|
|
local s = assert(spec.parse({ 'tpope/vim-fugitive', tag = 'v3.7' }))
|
|
t.eq(false, s.opt)
|
|
end)
|
|
|
|
t.it('honours an "as" override for the install directory', function()
|
|
local s = assert(spec.parse({ 'folke/tokyonight.nvim', tag = 'v4.10.0', as = 'tokyonight' }))
|
|
t.eq('tokyonight', s.name)
|
|
end)
|
|
|
|
t.it('accepts a source key instead of the positional entry', function()
|
|
local s = assert(spec.parse({ source = 'owner/repo', tag = 'v1' }))
|
|
t.eq('owner/repo', s.source)
|
|
end)
|
|
|
|
t.it('rejects a bare string with no tag', function()
|
|
local s, err = spec.parse('owner/repo')
|
|
t.falsy(s)
|
|
t.matches('missing a tag', err)
|
|
end)
|
|
|
|
t.it('rejects a source without an owner', function()
|
|
local s, err = spec.parse({ 'plenary.nvim', tag = 'v1' })
|
|
t.falsy(s)
|
|
t.matches('expected "owner/repo"', err)
|
|
end)
|
|
|
|
t.it('rejects a source with extra path segments', function()
|
|
local s, err = spec.parse({ 'a/b/c', tag = 'v1' })
|
|
t.falsy(s)
|
|
t.matches('expected "owner/repo"', err)
|
|
end)
|
|
|
|
t.it('rejects a tag containing a path separator', function()
|
|
local s, err = spec.parse({ 'owner/repo', tag = '../../etc' })
|
|
t.falsy(s)
|
|
t.matches('invalid tag', err)
|
|
end)
|
|
|
|
t.it('rejects an unsupported extension', function()
|
|
local s, err = spec.parse({ 'owner/repo', tag = 'v1', ext = 'rar' })
|
|
t.falsy(s)
|
|
t.matches('unsupported ext', err)
|
|
end)
|
|
|
|
t.it('rejects a traversing "as" name', function()
|
|
local s, err = spec.parse({ 'owner/repo', tag = 'v1', as = '..' })
|
|
t.falsy(s)
|
|
t.matches('unusable directory name', err)
|
|
end)
|
|
|
|
t.it('rejects a non-table, non-string spec', function()
|
|
local s, err = spec.parse(42)
|
|
t.falsy(s)
|
|
t.matches('must be a string or a table', err)
|
|
end)
|
|
|
|
t.it('trims surrounding whitespace from the source and tag', function()
|
|
local s = assert(spec.parse({ ' owner/repo ', tag = ' v1.0.0 ' }))
|
|
t.eq('owner/repo', s.source)
|
|
t.eq('v1.0.0', s.tag)
|
|
end)
|
|
|
|
t.it('trims surrounding whitespace from an explicit url', function()
|
|
local s = assert(spec.parse({ 'owner/repo', tag = 'v1', url = ' https://example.test/a.zip\n' }))
|
|
t.eq('https://example.test/a.zip', s.url)
|
|
end)
|
|
|
|
t.it('rejects a url with whitespace inside it', function()
|
|
for _, bad in ipairs({ 'https://example.test/a b.zip', 'https://example\n.test/a.zip', ' ' }) do
|
|
local s, err = spec.parse({ 'owner/repo', tag = 'v1', url = bad })
|
|
t.falsy(s, 'rejects ' .. vim.inspect(bad))
|
|
t.matches('invalid url', err)
|
|
end
|
|
end)
|
|
end)
|
|
|
|
t.describe('spec.url', function()
|
|
t.it('builds a tag archive url', function()
|
|
local s = assert(spec.parse({ 'nvim-lua/plenary.nvim', tag = 'v0.1.4' }))
|
|
t.eq(REGISTRY .. '/nvim-lua/plenary.nvim/archive/refs/tags/v0.1.4.zip', spec.url(s, REGISTRY))
|
|
end)
|
|
|
|
t.it('honours the tar.gz extension', function()
|
|
local s = assert(spec.parse({ 'nvim-lua/plenary.nvim', tag = 'v0.1.4', ext = 'tar.gz' }))
|
|
t.matches('/v0%.1%.4%.tar%.gz$', spec.url(s, REGISTRY))
|
|
end)
|
|
|
|
t.it('does not double up on a trailing registry slash', function()
|
|
local s = assert(spec.parse({ 'owner/repo', tag = 'v1' }))
|
|
t.eq(REGISTRY .. '/owner/repo/archive/refs/tags/v1.zip', spec.url(s, REGISTRY .. '///'))
|
|
end)
|
|
|
|
t.it('never builds a url containing whitespace', function()
|
|
local s = assert(spec.parse({ 'owner/repo', tag = 'v1' }))
|
|
local want = REGISTRY .. '/owner/repo/archive/refs/tags/v1.zip'
|
|
t.eq(want, spec.url(s, REGISTRY .. ' '))
|
|
t.eq(want, spec.url(s, REGISTRY .. '\n'))
|
|
t.eq(want, spec.url(s, ' ' .. REGISTRY))
|
|
|
|
for _, bad in ipairs({ 'https://arti facts.example.com/gh', 'https://artifacts\n.example.com/gh', ' ' }) do
|
|
local url, err = spec.url(s, bad)
|
|
t.falsy(url, 'rejects ' .. vim.inspect(bad))
|
|
t.matches('is not an http%(s%) URL', err)
|
|
end
|
|
end)
|
|
|
|
t.it('passes an explicit url straight through', function()
|
|
local s = assert(spec.parse({ 'owner/repo', tag = 'v1', url = 'https://example.test/a.zip' }))
|
|
t.eq('https://example.test/a.zip', spec.url(s, REGISTRY))
|
|
end)
|
|
|
|
t.it('errors when no registry is configured', function()
|
|
local s = assert(spec.parse({ 'owner/repo', tag = 'v1' }))
|
|
local url, err = spec.url(s, nil)
|
|
t.falsy(url)
|
|
t.matches('registry is not configured', err)
|
|
t.matches("setup{ registry = 'https://", err, 'the error says how to set it')
|
|
end)
|
|
|
|
t.it('errors instead of handing a bad registry to curl', function()
|
|
local s = assert(spec.parse({ 'owner/repo', tag = 'v1' }))
|
|
for _, bad in ipairs({ '', 'https://', 'artifacts.example.com/gh', 'ftp://artifacts.example.com', 42 }) do
|
|
local url, err = spec.url(s, bad)
|
|
t.falsy(url, 'rejects ' .. vim.inspect(bad))
|
|
t.matches('is not an http%(s%) URL', err)
|
|
end
|
|
end)
|
|
end)
|
|
|
|
t.describe('spec.check_registry', function()
|
|
t.it('returns the registry with trailing slashes stripped', function()
|
|
t.eq(REGISTRY, spec.check_registry(REGISTRY))
|
|
t.eq(REGISTRY, spec.check_registry(REGISTRY .. '/'))
|
|
t.eq(REGISTRY, spec.check_registry(REGISTRY .. '///'))
|
|
end)
|
|
|
|
t.it('accepts a plain http registry', function()
|
|
t.eq('http://artifacts.example.com/gh', spec.check_registry('http://artifacts.example.com/gh'))
|
|
end)
|
|
|
|
t.it('trims leading whitespace', function()
|
|
t.eq(REGISTRY, spec.check_registry(' ' .. REGISTRY))
|
|
end)
|
|
|
|
t.it('trims trailing whitespace, including a newline', function()
|
|
t.eq(REGISTRY, spec.check_registry(REGISTRY .. ' '))
|
|
t.eq(REGISTRY, spec.check_registry(REGISTRY .. '\n'))
|
|
t.eq(REGISTRY, spec.check_registry(' \t' .. REGISTRY .. '/ \n'))
|
|
end)
|
|
|
|
t.it('rejects whitespace embedded in the registry', function()
|
|
for _, bad in ipairs({ 'https://artifacts.example.com/g h', 'https://artifacts\n.example.com/gh' }) do
|
|
local base, err = spec.check_registry(bad)
|
|
t.falsy(base, 'rejects ' .. vim.inspect(bad))
|
|
t.matches('is not an http%(s%) URL', err)
|
|
end
|
|
end)
|
|
|
|
t.it('rejects a whitespace-only registry', function()
|
|
for _, bad in ipairs({ ' ', ' ', '\n', ' \t\n ' }) do
|
|
local base, err = spec.check_registry(bad)
|
|
t.falsy(base, 'rejects ' .. vim.inspect(bad))
|
|
t.matches('is not an http%(s%) URL', err)
|
|
end
|
|
end)
|
|
end)
|
|
|
|
t.describe('spec.parse_all', function()
|
|
t.it('collects specs and reports per-entry errors', function()
|
|
local specs, errors = spec.parse_all({
|
|
{ 'a/one', tag = 'v1' },
|
|
{ 'bad-source', tag = 'v1' },
|
|
{ 'b/two', tag = 'v2', opt = true },
|
|
})
|
|
t.eq(2, #specs)
|
|
t.eq(1, #errors)
|
|
t.matches('plugins%[2%]', errors[1])
|
|
end)
|
|
|
|
t.it('rejects two specs that install to the same directory', function()
|
|
local specs, errors = spec.parse_all({
|
|
{ 'a/shared', tag = 'v1' },
|
|
{ 'b/shared', tag = 'v2' },
|
|
})
|
|
t.eq(1, #specs)
|
|
t.matches('duplicate plugin name', errors[1])
|
|
end)
|
|
|
|
t.it('returns empty results for a nil plugin list', function()
|
|
local specs, errors = spec.parse_all(nil)
|
|
t.eq({}, specs)
|
|
t.eq({}, errors)
|
|
end)
|
|
end)
|