Skip to content
Closed
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ jobs:
- uses: ./.github/actions/test-setup
- uses: cachix/install-nix-action@v27
- name: Install tools
run: nix profile install nixpkgs#nixfmt-rfc-style
run: nix profile install nixpkgs#nixfmt
- name: Run tests
run: |
export LUA_PATH="lua/?.lua;lua/?/init.lua;$LUA_PATH"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
- [latexindent](https://github.com/cmhughes/latexindent.pl)
- [mdformat](https://github.com/executablebooks/mdformat)
- [mixformat](https://github.com/elixir-lang/elixir/)
- [nixfmt](https://github.com/serokell/nixfmt)
- [nixfmt](https://github.com/NixOS/nixfmt)
- [npm_groovy_lint](https://github.com/nvuillam/npm-groovy-lint) as `npm-groovy-lint --format`
- [npm_groovy_lint_fix](https://github.com/nvuillam/npm-groovy-lint) as `npm-groovy-lint --fix`
- [ormolu](https://hackage.haskell.org/package/ormolu)
Expand Down
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
12 changes: 3 additions & 9 deletions test/binary/zsh_spec.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
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)
local helper = require('test.helper')
local bufnr, diagnostics = helper.run_lint_fn('zsh', 'zsh', { 'if true; then' })
assert.is_true(#diagnostics > 0)
for _, d in ipairs(diagnostics) do
assert.equal(bufnr, d.bufnr)
assert.equal('zsh', d.source)
helper.assert_diag(d, { bufnr = bufnr, source = 'zsh' })
assert.is_number(d.lnum)
assert.is_string(d.message)
end
Expand Down
22 changes: 22 additions & 0 deletions test/helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ function M.run_lint(name, ft, input, opts)
return bufnr, diags
end

function M.run_lint_fn(name, ft, input, opts)
opts = opts or {}
local linter = require('guard-collection.linter')[name]
assert(linter, 'unknown linter: ' .. name)
assert(linter.fn, name .. ' does not use custom fn')
local dir = opts.tmpdir or '/tmp'
local tmpfile = dir .. '/guard-test.' .. ft
vim.fn.writefile(input, tmpfile)
local bufnr = api.nvim_create_buf(false, true)
local output
local co = coroutine.create(function()
output = linter.fn(nil, tmpfile)
end)
coroutine.resume(co)
vim.wait(5000, function()
return output ~= nil
end)
assert(output ~= nil, name .. ' fn timed out')
local diags = linter.parse(output, bufnr)
return bufnr, diags
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)
13 changes: 3 additions & 10 deletions test/pip/cpplint_spec.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
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)
local helper = require('test.helper')
local bufnr, diagnostics = helper.run_lint_fn('cpplint', 'cpp', { [[int main(){int x=1;}]] })
assert.is_true(#diagnostics > 0)
for _, d in ipairs(diagnostics) do
assert.equal(bufnr, d.bufnr)
assert.equal('cpplint', d.source)
helper.assert_diag(d, { bufnr = bufnr, source = 'cpplint' })
assert.is_number(d.lnum)
assert.is_string(d.message)
end
Expand Down
Loading