diff --git a/lua/guard-collection/formatter.lua b/lua/guard-collection/formatter.lua index ff61fe8..a597d4e 100644 --- a/lua/guard-collection/formatter.lua +++ b/lua/guard-collection/formatter.lua @@ -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 = { diff --git a/lua/guard-collection/linter/checkmake.lua b/lua/guard-collection/linter/checkmake.lua index af62824..565bc01 100644 --- a/lua/guard-collection/linter/checkmake.lua +++ b/lua/guard-collection/linter/checkmake.lua @@ -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 diff --git a/test/binary/checkmake_spec.lua b/test/binary/checkmake_spec.lua index adda48a..12b5166 100644 --- a/test/binary/checkmake_spec.lua +++ b/test/binary/checkmake_spec.lua @@ -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) diff --git a/test/binary/zsh_spec.lua b/test/binary/zsh_spec.lua index 78332d2..110a0ce 100644 --- a/test/binary/zsh_spec.lua +++ b/test/binary/zsh_spec.lua @@ -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) diff --git a/test/helper.lua b/test/helper.lua index d57204a..32e1226 100644 --- a/test/helper.lua +++ b/test/helper.lua @@ -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 diff --git a/test/npm/prettierd_spec.lua b/test/npm/prettierd_spec.lua index 61cecd6..a13b997 100644 --- a/test/npm/prettierd_spec.lua +++ b/test/npm/prettierd_spec.lua @@ -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) diff --git a/test/pip/cpplint_spec.lua b/test/pip/cpplint_spec.lua index cb0095a..3b6af6a 100644 --- a/test/pip/cpplint_spec.lua +++ b/test/pip/cpplint_spec.lua @@ -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)