diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7b4364e..b6d6755 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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" diff --git a/README.md b/README.md index a2734e4..d7780da 100644 --- a/README.md +++ b/README.md @@ -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) 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..5e40c7c 100644 --- a/test/binary/zsh_spec.lua +++ b/test/binary/zsh_spec.lua @@ -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 diff --git a/test/helper.lua b/test/helper.lua index d57204a..1b9b55b 100644 --- a/test/helper.lua +++ b/test/helper.lua @@ -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 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..452f3ad 100644 --- a/test/pip/cpplint_spec.lua +++ b/test/pip/cpplint_spec.lua @@ -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