ci: format
This commit is contained in:
commit
e49a664d48
30 changed files with 1612 additions and 0 deletions
38
spec/commands_spec.lua
Normal file
38
spec/commands_spec.lua
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
local helpers = require('spec.helpers')
|
||||
|
||||
describe('commands', function()
|
||||
before_each(function()
|
||||
helpers.reset_config()
|
||||
end)
|
||||
|
||||
describe('setup', function()
|
||||
it('creates the :Render command', function()
|
||||
require('render.commands').setup()
|
||||
local cmds = vim.api.nvim_get_commands({})
|
||||
assert.is_not_nil(cmds.Render)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('dispatch', function()
|
||||
it('does not error on :Render with no provider', function()
|
||||
require('render.commands').setup()
|
||||
assert.has_no.errors(function()
|
||||
vim.cmd('Render compile')
|
||||
end)
|
||||
end)
|
||||
|
||||
it('does not error on :Render stop', function()
|
||||
require('render.commands').setup()
|
||||
assert.has_no.errors(function()
|
||||
vim.cmd('Render stop')
|
||||
end)
|
||||
end)
|
||||
|
||||
it('does not error on :Render status', function()
|
||||
require('render.commands').setup()
|
||||
assert.has_no.errors(function()
|
||||
vim.cmd('Render status')
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
176
spec/compiler_spec.lua
Normal file
176
spec/compiler_spec.lua
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
local helpers = require('spec.helpers')
|
||||
|
||||
describe('compiler', function()
|
||||
local compiler
|
||||
|
||||
before_each(function()
|
||||
helpers.reset_config()
|
||||
compiler = require('render.compiler')
|
||||
end)
|
||||
|
||||
describe('compile', function()
|
||||
it('spawns a process and tracks it in active table', function()
|
||||
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
||||
vim.api.nvim_buf_set_name(bufnr, '/tmp/render_test.txt')
|
||||
vim.bo[bufnr].modified = false
|
||||
|
||||
local provider = { cmd = { 'echo', 'ok' } }
|
||||
local ctx = {
|
||||
bufnr = bufnr,
|
||||
file = '/tmp/render_test.txt',
|
||||
root = '/tmp',
|
||||
ft = 'text',
|
||||
}
|
||||
|
||||
compiler.compile(bufnr, 'echo', provider, ctx)
|
||||
local active = compiler._test.active
|
||||
assert.is_not_nil(active[bufnr])
|
||||
assert.are.equal('echo', active[bufnr].provider)
|
||||
|
||||
vim.wait(2000, function()
|
||||
return active[bufnr] == nil
|
||||
end, 50)
|
||||
|
||||
assert.is_nil(active[bufnr])
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('fires RenderCompileStarted event', function()
|
||||
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
||||
vim.api.nvim_buf_set_name(bufnr, '/tmp/render_test_event.txt')
|
||||
vim.bo[bufnr].modified = false
|
||||
|
||||
local fired = false
|
||||
vim.api.nvim_create_autocmd('User', {
|
||||
pattern = 'RenderCompileStarted',
|
||||
once = true,
|
||||
callback = function()
|
||||
fired = true
|
||||
end,
|
||||
})
|
||||
|
||||
local provider = { cmd = { 'echo', 'ok' } }
|
||||
local ctx = {
|
||||
bufnr = bufnr,
|
||||
file = '/tmp/render_test_event.txt',
|
||||
root = '/tmp',
|
||||
ft = 'text',
|
||||
}
|
||||
|
||||
compiler.compile(bufnr, 'echo', provider, ctx)
|
||||
assert.is_true(fired)
|
||||
|
||||
vim.wait(2000, function()
|
||||
return compiler._test.active[bufnr] == nil
|
||||
end, 50)
|
||||
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('fires RenderCompileSuccess on exit code 0', function()
|
||||
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
||||
vim.api.nvim_buf_set_name(bufnr, '/tmp/render_test_success.txt')
|
||||
vim.bo[bufnr].modified = false
|
||||
|
||||
local succeeded = false
|
||||
vim.api.nvim_create_autocmd('User', {
|
||||
pattern = 'RenderCompileSuccess',
|
||||
once = true,
|
||||
callback = function()
|
||||
succeeded = true
|
||||
end,
|
||||
})
|
||||
|
||||
local provider = { cmd = { 'true' } }
|
||||
local ctx = {
|
||||
bufnr = bufnr,
|
||||
file = '/tmp/render_test_success.txt',
|
||||
root = '/tmp',
|
||||
ft = 'text',
|
||||
}
|
||||
|
||||
compiler.compile(bufnr, 'truecmd', provider, ctx)
|
||||
|
||||
vim.wait(2000, function()
|
||||
return succeeded
|
||||
end, 50)
|
||||
|
||||
assert.is_true(succeeded)
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('fires RenderCompileFailed on non-zero exit', function()
|
||||
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
||||
vim.api.nvim_buf_set_name(bufnr, '/tmp/render_test_fail.txt')
|
||||
vim.bo[bufnr].modified = false
|
||||
|
||||
local failed = false
|
||||
vim.api.nvim_create_autocmd('User', {
|
||||
pattern = 'RenderCompileFailed',
|
||||
once = true,
|
||||
callback = function()
|
||||
failed = true
|
||||
end,
|
||||
})
|
||||
|
||||
local provider = { cmd = { 'false' } }
|
||||
local ctx = {
|
||||
bufnr = bufnr,
|
||||
file = '/tmp/render_test_fail.txt',
|
||||
root = '/tmp',
|
||||
ft = 'text',
|
||||
}
|
||||
|
||||
compiler.compile(bufnr, 'falsecmd', provider, ctx)
|
||||
|
||||
vim.wait(2000, function()
|
||||
return failed
|
||||
end, 50)
|
||||
|
||||
assert.is_true(failed)
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('stop', function()
|
||||
it('does nothing when no process is active', function()
|
||||
assert.has_no.errors(function()
|
||||
compiler.stop(999)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('status', function()
|
||||
it('returns idle for buffer with no process', function()
|
||||
local s = compiler.status(42)
|
||||
assert.is_false(s.compiling)
|
||||
end)
|
||||
|
||||
it('returns compiling during active process', function()
|
||||
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
||||
vim.api.nvim_buf_set_name(bufnr, '/tmp/render_test_status.txt')
|
||||
vim.bo[bufnr].modified = false
|
||||
|
||||
local provider = { cmd = { 'sleep', '10' } }
|
||||
local ctx = {
|
||||
bufnr = bufnr,
|
||||
file = '/tmp/render_test_status.txt',
|
||||
root = '/tmp',
|
||||
ft = 'text',
|
||||
}
|
||||
|
||||
compiler.compile(bufnr, 'sleepcmd', provider, ctx)
|
||||
local s = compiler.status(bufnr)
|
||||
assert.is_true(s.compiling)
|
||||
assert.are.equal('sleepcmd', s.provider)
|
||||
|
||||
compiler.stop(bufnr)
|
||||
|
||||
vim.wait(2000, function()
|
||||
return compiler._test.active[bufnr] == nil
|
||||
end, 50)
|
||||
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
128
spec/diagnostic_spec.lua
Normal file
128
spec/diagnostic_spec.lua
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
local helpers = require('spec.helpers')
|
||||
|
||||
describe('diagnostic', function()
|
||||
local diagnostic
|
||||
|
||||
before_each(function()
|
||||
helpers.reset_config()
|
||||
diagnostic = require('render.diagnostic')
|
||||
end)
|
||||
|
||||
describe('clear', function()
|
||||
it('clears diagnostics for a buffer', function()
|
||||
local bufnr = helpers.create_buffer({ 'line1', 'line2' })
|
||||
local ns = diagnostic.get_namespace()
|
||||
vim.diagnostic.set(ns, bufnr, {
|
||||
{ lnum = 0, col = 0, message = 'test error', severity = vim.diagnostic.severity.ERROR },
|
||||
})
|
||||
|
||||
local diags = vim.diagnostic.get(bufnr, { namespace = ns })
|
||||
assert.are.equal(1, #diags)
|
||||
|
||||
diagnostic.clear(bufnr)
|
||||
|
||||
diags = vim.diagnostic.get(bufnr, { namespace = ns })
|
||||
assert.are.equal(0, #diags)
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('set', function()
|
||||
it('sets diagnostics from error_parser output', function()
|
||||
local bufnr = helpers.create_buffer({ 'line1', 'line2' })
|
||||
local ns = diagnostic.get_namespace()
|
||||
|
||||
local parser = function()
|
||||
return {
|
||||
{ lnum = 0, col = 0, message = 'syntax error', severity = vim.diagnostic.severity.ERROR },
|
||||
}
|
||||
end
|
||||
|
||||
local ctx = { bufnr = bufnr, file = '/tmp/test.typ', root = '/tmp', ft = 'typst' }
|
||||
diagnostic.set(bufnr, 'typst', parser, 'error on line 1', ctx)
|
||||
|
||||
local diags = vim.diagnostic.get(bufnr, { namespace = ns })
|
||||
assert.are.equal(1, #diags)
|
||||
assert.are.equal('syntax error', diags[1].message)
|
||||
assert.are.equal('typst', diags[1].source)
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('sets source to provider name when not specified', function()
|
||||
local bufnr = helpers.create_buffer({ 'line1' })
|
||||
local ns = diagnostic.get_namespace()
|
||||
|
||||
local parser = function()
|
||||
return {
|
||||
{ lnum = 0, col = 0, message = 'err', severity = vim.diagnostic.severity.ERROR },
|
||||
}
|
||||
end
|
||||
|
||||
local ctx = { bufnr = bufnr, file = '/tmp/test.tex', root = '/tmp', ft = 'tex' }
|
||||
diagnostic.set(bufnr, 'latexmk', parser, 'error', ctx)
|
||||
|
||||
local diags = vim.diagnostic.get(bufnr, { namespace = ns })
|
||||
assert.are.equal('latexmk', diags[1].source)
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('preserves existing source from parser', function()
|
||||
local bufnr = helpers.create_buffer({ 'line1' })
|
||||
local ns = diagnostic.get_namespace()
|
||||
|
||||
local parser = function()
|
||||
return {
|
||||
{
|
||||
lnum = 0,
|
||||
col = 0,
|
||||
message = 'err',
|
||||
severity = vim.diagnostic.severity.ERROR,
|
||||
source = 'custom',
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
local ctx = { bufnr = bufnr, file = '/tmp/test.tex', root = '/tmp', ft = 'tex' }
|
||||
diagnostic.set(bufnr, 'latexmk', parser, 'error', ctx)
|
||||
|
||||
local diags = vim.diagnostic.get(bufnr, { namespace = ns })
|
||||
assert.are.equal('custom', diags[1].source)
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('handles parser failure gracefully', function()
|
||||
local bufnr = helpers.create_buffer({ 'line1' })
|
||||
local ns = diagnostic.get_namespace()
|
||||
|
||||
local parser = function()
|
||||
error('parser exploded')
|
||||
end
|
||||
|
||||
local ctx = { bufnr = bufnr, file = '/tmp/test.tex', root = '/tmp', ft = 'tex' }
|
||||
|
||||
assert.has_no.errors(function()
|
||||
diagnostic.set(bufnr, 'latexmk', parser, 'error', ctx)
|
||||
end)
|
||||
|
||||
local diags = vim.diagnostic.get(bufnr, { namespace = ns })
|
||||
assert.are.equal(0, #diags)
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('does nothing when parser returns empty list', function()
|
||||
local bufnr = helpers.create_buffer({ 'line1' })
|
||||
local ns = diagnostic.get_namespace()
|
||||
|
||||
local parser = function()
|
||||
return {}
|
||||
end
|
||||
|
||||
local ctx = { bufnr = bufnr, file = '/tmp/test.tex', root = '/tmp', ft = 'tex' }
|
||||
diagnostic.set(bufnr, 'latexmk', parser, 'error', ctx)
|
||||
|
||||
local diags = vim.diagnostic.get(bufnr, { namespace = ns })
|
||||
assert.are.equal(0, #diags)
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
27
spec/helpers.lua
Normal file
27
spec/helpers.lua
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
local plugin_dir = vim.fn.getcwd()
|
||||
vim.opt.runtimepath:prepend(plugin_dir)
|
||||
vim.opt.packpath = {}
|
||||
|
||||
local M = {}
|
||||
|
||||
function M.create_buffer(lines, ft)
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines or {})
|
||||
if ft then
|
||||
vim.bo[bufnr].filetype = ft
|
||||
end
|
||||
return bufnr
|
||||
end
|
||||
|
||||
function M.delete_buffer(bufnr)
|
||||
if bufnr and vim.api.nvim_buf_is_valid(bufnr) then
|
||||
vim.api.nvim_buf_delete(bufnr, { force = true })
|
||||
end
|
||||
end
|
||||
|
||||
function M.reset_config()
|
||||
vim.g.render = nil
|
||||
require('render')._test.reset()
|
||||
end
|
||||
|
||||
return M
|
||||
116
spec/init_spec.lua
Normal file
116
spec/init_spec.lua
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
local helpers = require('spec.helpers')
|
||||
|
||||
describe('render', function()
|
||||
local render
|
||||
|
||||
before_each(function()
|
||||
helpers.reset_config()
|
||||
render = require('render')
|
||||
end)
|
||||
|
||||
describe('config', function()
|
||||
it('accepts nil config', function()
|
||||
vim.g.render = nil
|
||||
assert.has_no.errors(function()
|
||||
render.get_config()
|
||||
end)
|
||||
end)
|
||||
|
||||
it('applies default values', function()
|
||||
vim.g.render = nil
|
||||
local config = render.get_config()
|
||||
assert.is_false(config.debug)
|
||||
assert.are.same({}, config.providers)
|
||||
assert.are.same({}, config.providers_by_ft)
|
||||
end)
|
||||
|
||||
it('merges user config with defaults', function()
|
||||
vim.g.render = { debug = true }
|
||||
helpers.reset_config()
|
||||
local config = require('render').get_config()
|
||||
assert.is_true(config.debug)
|
||||
assert.are.same({}, config.providers)
|
||||
end)
|
||||
|
||||
it('accepts full provider config', function()
|
||||
vim.g.render = {
|
||||
providers = {
|
||||
typst = {
|
||||
cmd = { 'typst', 'compile' },
|
||||
args = { '%s' },
|
||||
},
|
||||
},
|
||||
providers_by_ft = {
|
||||
typst = 'typst',
|
||||
},
|
||||
}
|
||||
helpers.reset_config()
|
||||
local config = require('render').get_config()
|
||||
assert.is_not_nil(config.providers.typst)
|
||||
assert.are.equal('typst', config.providers_by_ft.typst)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('resolve_provider', function()
|
||||
before_each(function()
|
||||
vim.g.render = {
|
||||
providers = {
|
||||
typst = { cmd = { 'typst', 'compile' } },
|
||||
},
|
||||
providers_by_ft = {
|
||||
typst = 'typst',
|
||||
},
|
||||
}
|
||||
helpers.reset_config()
|
||||
render = require('render')
|
||||
end)
|
||||
|
||||
it('returns provider name for mapped filetype', function()
|
||||
local bufnr = helpers.create_buffer({}, 'typst')
|
||||
local name = render.resolve_provider(bufnr)
|
||||
assert.are.equal('typst', name)
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('returns nil for unmapped filetype', function()
|
||||
local bufnr = helpers.create_buffer({}, 'lua')
|
||||
local name = render.resolve_provider(bufnr)
|
||||
assert.is_nil(name)
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('returns nil when provider name maps to missing config', function()
|
||||
vim.g.render = {
|
||||
providers = {},
|
||||
providers_by_ft = { typst = 'typst' },
|
||||
}
|
||||
helpers.reset_config()
|
||||
local bufnr = helpers.create_buffer({}, 'typst')
|
||||
local name = require('render').resolve_provider(bufnr)
|
||||
assert.is_nil(name)
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('build_context', function()
|
||||
it('builds context from buffer', function()
|
||||
local bufnr = helpers.create_buffer({}, 'typst')
|
||||
local ctx = render.build_context(bufnr)
|
||||
assert.are.equal(bufnr, ctx.bufnr)
|
||||
assert.are.equal('typst', ctx.ft)
|
||||
assert.is_string(ctx.file)
|
||||
assert.is_string(ctx.root)
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('status', function()
|
||||
it('returns idle when nothing is compiling', function()
|
||||
local bufnr = helpers.create_buffer({})
|
||||
local s = render.status(bufnr)
|
||||
assert.is_false(s.compiling)
|
||||
assert.is_nil(s.provider)
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
4
spec/minimal_init.lua
Normal file
4
spec/minimal_init.lua
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
vim.cmd([[set runtimepath=$VIMRUNTIME]])
|
||||
vim.opt.runtimepath:append('.')
|
||||
vim.opt.packpath = {}
|
||||
vim.opt.loadplugins = false
|
||||
Loading…
Add table
Add a link
Reference in a new issue