Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 3 additions & 23 deletions lua/guard-collection/formatter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -186,29 +186,9 @@ M.prettier = {
}

M.prettierd = {
fn = function(buf, range)
if vim.fn.has('nvim-0.10') ~= 1 then
vim.notify('[guard-collection]: prettierd uses vim.system introduced in nvim 0.10', 4)
return
end
local srow = range and range['start'][1] or 0
local erow = range and range['end'][1] or -1
local handle = vim.system(
{ 'prettierd', vim.api.nvim_buf_get_name(buf) },
{
stdin = true,
},
vim.schedule_wrap(function(result)
if result.code ~= 0 then
return
end
vim.api.nvim_buf_set_lines(buf, srow, erow, false, vim.split(result.stdout, '\r?\n'))
vim.cmd('silent! noautocmd write!')
end)
)
handle:write(table.concat(vim.api.nvim_buf_get_lines(buf, srow, erow, false), '\n'))
handle:write(nil)
end,
cmd = 'prettierd',
stdin = true,
fname = true,
}

M.rubocop = {
Expand Down
14 changes: 3 additions & 11 deletions lua/guard-collection/linter/checkmake.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
local lint = require('guard.lint')

return {
fn = function(_, fname)
local co = assert(coroutine.running())
vim.system({
'checkmake',
'--format={{.FileName}}:{{.LineNumber}}: [{{.Rule}}] {{.Violation}}\n',
fname,
}, {}, function(result)
coroutine.resume(co, result.stdout or '')
end)
return coroutine.yield()
end,
cmd = 'checkmake',
args = { '--format={{.FileName}}:{{.LineNumber}}: [{{.Rule}}] {{.Violation}}\n' },
fname = true,
parse = function(result, bufnr)
local diags = {}
for line in result:gmatch('[^\n]+') do
Expand Down
20 changes: 4 additions & 16 deletions test/binary/checkmake_spec.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
describe('checkmake', function()
it('can lint', function()
local linter = require('test.helper').get_linter('checkmake')
local tmpfile = '/tmp/guard-test.make'
local input = {
local helper = require('test.helper')
local buf, diagnostics = helper.run_lint('checkmake', 'make', {
[[all:]],
[[\techo hello]],
}
vim.fn.writefile(input, tmpfile)
local bufnr = vim.api.nvim_create_buf(false, true)
local result = vim
.system({
'checkmake',
'--format={{.FileName}}:{{.LineNumber}}: [{{.Rule}}] {{.Violation}}\n',
tmpfile,
}, {})
:wait()
local output = result.stdout or ''
local diagnostics = linter.parse(output, bufnr)
})
assert.is_true(#diagnostics > 0)
for _, d in ipairs(diagnostics) do
assert.equal(bufnr, d.bufnr)
assert.equal(buf, d.bufnr)
assert.equal('checkmake', d.source)
assert.is_number(d.lnum)
assert.is_string(d.message)
Expand Down
23 changes: 9 additions & 14 deletions test/binary/zsh_spec.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
describe('zsh', function()
it('can lint', function()
local linter = require('test.helper').get_linter('zsh')
local tmpfile = '/tmp/guard-test.zsh'
vim.fn.writefile({ 'if true; then' }, tmpfile)
local bufnr = vim.api.nvim_create_buf(false, true)
local result = vim.system({ 'zsh', '-n', tmpfile }, {}):wait()
local output = result.stderr or ''
local diagnostics = linter.parse(output, bufnr)
assert.is_true(#diagnostics > 0)
for _, d in ipairs(diagnostics) do
assert.equal(bufnr, d.bufnr)
assert.equal('zsh', d.source)
assert.is_number(d.lnum)
assert.is_string(d.message)
end
local helper = require('test.helper')
helper.run_lint_fn('zsh', 'zsh', { 'if true; then' }, function(bufnr, diagnostics)
assert.is_true(#diagnostics > 0)
for _, d in ipairs(diagnostics) do
helper.assert_diag(d, { bufnr = bufnr, source = 'zsh' })
assert.is_number(d.lnum)
assert.is_string(d.message)
end
end)
end)
end)
21 changes: 21 additions & 0 deletions test/helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ function M.run_lint(name, ft, input, opts)
return bufnr, diags
end

function M.run_lint_fn(name, ft, input, assert_fn)
local linter = require('guard-collection.linter')[name]
assert(linter, 'unknown linter: ' .. name)
assert(linter.fn, name .. ' does not use custom fn')
local tmpfile = '/tmp/guard-test.' .. ft
vim.fn.writefile(input, tmpfile)
local bufnr = api.nvim_create_buf(false, true)
local done = false
local co = coroutine.create(function()
local output = linter.fn(nil, tmpfile)
local diags = linter.parse(output, bufnr)
assert_fn(bufnr, diags)
done = true
end)
coroutine.resume(co)
vim.wait(5000, function()
return done
end)
assert(done, name .. ' fn timed out')
end

function M.assert_diag(d, expect)
local a = require('luassert')
if expect.bufnr then
Expand Down
16 changes: 4 additions & 12 deletions test/npm/prettierd_spec.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
describe('prettierd', function()
it('can format', function()
local tmpfile = '/tmp/guard-test.js'
local input = {
local formatted = require('test.helper').run_fmt('prettierd', 'js', {
'const x={a:1,b:2,c:3}',
'const y = [1,2,3,4,5]',
}
vim.fn.writefile(input, tmpfile)
local result = vim
.system({ 'prettierd', tmpfile }, { stdin = table.concat(input, '\n') })
:wait()
assert.equal(0, result.code)
local expected = table.concat({
})
assert.are.same({
'const x = { a: 1, b: 2, c: 3 };',
'const y = [1, 2, 3, 4, 5];',
'',
}, '\n')
assert.equal(expected, result.stdout)
}, formatted)
end)
end)
24 changes: 9 additions & 15 deletions test/pip/cpplint_spec.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
describe('cpplint', function()
it('can lint', function()
local linter = require('test.helper').get_linter('cpplint')
local tmpfile = '/tmp/guard-test.cpp'
local input = { [[int main(){int x=1;}]] }
vim.fn.writefile(input, tmpfile)
local bufnr = vim.api.nvim_create_buf(false, true)
local result = vim.system({ 'cpplint', '--filter=-legal/copyright', tmpfile }, {}):wait()
local output = result.stderr or ''
local diagnostics = linter.parse(output, bufnr)
assert.is_true(#diagnostics > 0)
for _, d in ipairs(diagnostics) do
assert.equal(bufnr, d.bufnr)
assert.equal('cpplint', d.source)
assert.is_number(d.lnum)
assert.is_string(d.message)
end
local helper = require('test.helper')
helper.run_lint_fn('cpplint', 'cpp', { [[int main(){int x=1;}]] }, function(bufnr, diagnostics)
assert.is_true(#diagnostics > 0)
for _, d in ipairs(diagnostics) do
helper.assert_diag(d, { bufnr = bufnr, source = 'cpplint' })
assert.is_number(d.lnum)
assert.is_string(d.message)
end
end)
end)
end)
Loading