diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..65a2e82 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,262 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + coverage: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Check test coverage + run: ./scripts/check-coverage + + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: JohnnyMorganz/stylua-action@v4 + with: + token: ${{ secrets.GITHUB_TOKEN }} + version: 2.0.2 + args: --check . + + test-pip: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: rhysd/action-setup-vim@v1 + with: + neovim: true + version: nightly + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - uses: leso-kn/gh-actions-lua@master + with: + luaVersion: "5.1" + - uses: hishamhm/gh-actions-luarocks@master + - name: Install tools + run: | + pip install -q autopep8 black cmake-format cpplint docformatter flake8 ruff sqlfluff yapf + luarocks install busted --local + luarocks install nlua --local + - name: Clone guard.nvim + run: git clone --depth 1 https://github.com/nvimdev/guard.nvim && mv guard.nvim/lua/guard lua/ + - name: Run tests + run: | + export LUA_PATH="lua/?.lua;lua/?/init.lua;$LUA_PATH" + busted --lua nlua test/pip/*_spec.lua + + test-npm: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: rhysd/action-setup-vim@v1 + with: + neovim: true + version: nightly + - uses: actions/setup-node@v4 + with: + node-version: 20 + - uses: leso-kn/gh-actions-lua@master + with: + luaVersion: "5.1" + - uses: hishamhm/gh-actions-luarocks@master + - name: Install tools + run: | + npm install -g prettier @biomejs/biome sql-formatter @taplo/cli + luarocks install busted --local + luarocks install nlua --local + - name: Clone guard.nvim + run: git clone --depth 1 https://github.com/nvimdev/guard.nvim && mv guard.nvim/lua/guard lua/ + - name: Run tests + run: | + export LUA_PATH="lua/?.lua;lua/?/init.lua;$LUA_PATH" + busted --lua nlua test/npm/*_spec.lua + + test-go: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: rhysd/action-setup-vim@v1 + with: + neovim: true + version: nightly + - uses: actions/setup-go@v5 + with: + go-version: stable + cache: false + - uses: leso-kn/gh-actions-lua@master + with: + luaVersion: "5.1" + - uses: hishamhm/gh-actions-luarocks@master + - name: Install tools + run: | + go install mvdan.cc/gofumpt@latest + go install github.com/segmentio/golines@latest + luarocks install busted --local + luarocks install nlua --local + - name: Clone guard.nvim + run: git clone --depth 1 https://github.com/nvimdev/guard.nvim && mv guard.nvim/lua/guard lua/ + - name: Run tests + run: | + export PATH="$HOME/go/bin:$PATH" + export LUA_PATH="lua/?.lua;lua/?/init.lua;$LUA_PATH" + busted --lua nlua test/go/*_spec.lua + + test-rust: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: rhysd/action-setup-vim@v1 + with: + neovim: true + version: nightly + - uses: dtolnay/rust-toolchain@stable + with: + components: rustfmt + - uses: dtolnay/rust-toolchain@nightly + with: + components: rustfmt + - uses: leso-kn/gh-actions-lua@master + with: + luaVersion: "5.1" + - uses: hishamhm/gh-actions-luarocks@master + - name: Install test tools + run: | + luarocks install busted --local + luarocks install nlua --local + - name: Clone guard.nvim + run: git clone --depth 1 https://github.com/nvimdev/guard.nvim && mv guard.nvim/lua/guard lua/ + - name: Run tests + run: | + export LUA_PATH="lua/?.lua;lua/?/init.lua;$LUA_PATH" + busted --lua nlua test/rust/*_spec.lua + + test-lua: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: rhysd/action-setup-vim@v1 + with: + neovim: true + version: nightly + - uses: leso-kn/gh-actions-lua@master + with: + luaVersion: "5.1" + - uses: hishamhm/gh-actions-luarocks@master + - name: Install tools + run: | + luarocks install busted --local + luarocks install nlua --local + luarocks install luacheck --local + mkdir -p $HOME/.local/bin + wget -q https://github.com/Kampfkarren/selene/releases/download/0.28.0/selene-0.28.0-linux.zip + unzip -q selene-0.28.0-linux.zip -d $HOME/.local/bin + chmod +x $HOME/.local/bin/selene + wget -q https://github.com/JohnnyMorganz/StyLua/releases/download/v2.0.2/stylua-linux-x86_64.zip + unzip -q stylua-linux-x86_64.zip -d $HOME/.local/bin + chmod +x $HOME/.local/bin/stylua + - name: Clone guard.nvim + run: git clone --depth 1 https://github.com/nvimdev/guard.nvim && mv guard.nvim/lua/guard lua/ + - name: Run tests + run: | + export PATH="$HOME/.local/bin:$PATH" + export LUA_PATH="lua/?.lua;lua/?/init.lua;$LUA_PATH" + busted --lua nlua test/lua/*_spec.lua + + test-binary: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: rhysd/action-setup-vim@v1 + with: + neovim: true + version: nightly + - uses: leso-kn/gh-actions-lua@master + with: + luaVersion: "5.1" + - uses: hishamhm/gh-actions-luarocks@master + - name: Install tools + run: | + sudo apt-get install -y zsh + luarocks install busted --local + luarocks install nlua --local + mkdir -p $HOME/.local/bin + cd $HOME/.local/bin + wget -q https://github.com/kamadorueda/alejandra/releases/download/4.0.0/alejandra-x86_64-unknown-linux-musl -O alejandra && chmod +x alejandra + wget -q https://github.com/bufbuild/buf/releases/download/v1.47.2/buf-Linux-x86_64 -O buf && chmod +x buf + wget -q https://github.com/mrtazz/checkmake/releases/download/0.2.2/checkmake-0.2.2.linux.amd64 -O checkmake && chmod +x checkmake + wget -q https://github.com/denoland/deno/releases/download/v2.1.4/deno-x86_64-unknown-linux-gnu.zip && unzip -q deno-x86_64-unknown-linux-gnu.zip && chmod +x deno + wget -q https://github.com/cmhughes/latexindent.pl/releases/download/V3.24.4/latexindent-linux -O latexindent && chmod +x latexindent + wget -q https://github.com/nicklockwood/SwiftFormat/releases/download/0.55.3/swiftformat_linux.zip && unzip -q swiftformat_linux.zip && mv swiftformat_linux swiftformat && chmod +x swiftformat + - name: Clone guard.nvim + run: git clone --depth 1 https://github.com/nvimdev/guard.nvim && mv guard.nvim/lua/guard lua/ + - name: Run tests + run: | + export PATH="$HOME/.local/bin:$PATH" + export LUA_PATH="lua/?.lua;lua/?/init.lua;$LUA_PATH" + busted --lua nlua test/binary/*_spec.lua + + test-clang: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: rhysd/action-setup-vim@v1 + with: + neovim: true + version: nightly + - uses: leso-kn/gh-actions-lua@master + with: + luaVersion: "5.1" + - uses: hishamhm/gh-actions-luarocks@master + - name: Install tools + run: | + sudo apt-get install -y clang-format clang-tidy + luarocks install busted --local + luarocks install nlua --local + - name: Clone guard.nvim + run: git clone --depth 1 https://github.com/nvimdev/guard.nvim && mv guard.nvim/lua/guard lua/ + - name: Run tests + run: | + export LUA_PATH="lua/?.lua;lua/?/init.lua;$LUA_PATH" + for f in test/clang/*_spec.lua; do busted --lua nlua "$f"; done + + test-haskell: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: rhysd/action-setup-vim@v1 + with: + neovim: true + version: nightly + - uses: haskell-actions/setup@v2 + with: + ghc-version: '9.8' + cabal-version: latest + - uses: actions/cache@v4 + with: + path: ~/.cabal + key: cabal-${{ runner.os }}-9.8-hlint + restore-keys: cabal-${{ runner.os }}-9.8- + - uses: leso-kn/gh-actions-lua@master + with: + luaVersion: "5.1" + - uses: hishamhm/gh-actions-luarocks@master + - name: Install tools + run: | + cabal update + cabal install hlint --overwrite-policy=always + luarocks install busted --local + luarocks install nlua --local + - name: Clone guard.nvim + run: git clone --depth 1 https://github.com/nvimdev/guard.nvim && mv guard.nvim/lua/guard lua/ + - name: Run tests + run: | + export PATH="$HOME/.cabal/bin:$PATH" + export LUA_PATH="lua/?.lua;lua/?/init.lua;$LUA_PATH" + busted --lua nlua test/haskell/*_spec.lua diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml deleted file mode 100644 index ca8ae6c..0000000 --- a/.github/workflows/test.yml +++ /dev/null @@ -1,75 +0,0 @@ -name: CI - -on: [push, pull_request] - -jobs: - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Stylua - uses: JohnnyMorganz/stylua-action@v3 - with: - token: ${{ secrets.GITHUB_TOKEN }} - version: latest - args: --check . - - test: - name: Run Test - runs-on: ubuntu-latest - permissions: - contents: write - steps: - - uses: actions/checkout@v3 - - - name: Test Setup (Neovim Nightly) - uses: rhysd/action-setup-vim@v1 - id: vim - with: - neovim: true - version: nightly - - - name: Test Setup (Lua) - uses: leafo/gh-actions-lua@v10 - with: - luaVersion: "luajit-openresty" - - - name: Test Setup (Luarocks) - uses: leafo/gh-actions-luarocks@v4 - - - name: Package Manager Setup (Python) - uses: actions/setup-python@v4 - - - name: Package Manager Setup (Rust) - uses: actions-rs/toolchain@v1 - with: - toolchain: nightly - components: rustfmt, cargo - - - name: Package Manager Setup (npm) - uses: actions/setup-node@v3 - with: - node-version: 18 - - - name: Package Manager Setup (homebrew) - id: set-up-homebrew - uses: Homebrew/actions/setup-homebrew@master - - - name: Install Tools - shell: bash - run: bash ./test/setup.sh - - - name: Formatter Test - shell: bash - run: | - source ./test/env.sh - vusted ./test/formatter - - - name: Linter Test - shell: bash - if: always() - run: | - source ./test/env.sh - for linter_test in ./test/linter/*_spec.lua; do - vusted $linter_test - done diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ceb2b98 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +CLAUDE.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index cad0f7e..dc8c013 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,107 +1,71 @@ # Contributing to guard-collection - Add your config to `formatter.lua` or `linter/.lua`, if it's a linter don't forget to export it in `linter/init.lua` -- Write a test. If it's a formatter, show that the config works by giving an example input and verify that the file did got formatted as intended. For example: +- Write a test in the appropriate `test//` directory. Tests run tools synchronously via `vim.system():wait()`, bypassing guard.nvim's async pipeline. + +Formatter example: ```lua describe('black', function() it('can format', function() - -- pre-test setup - local ft = require('guard.filetype') - ft('python'):fmt('black') - -- Giving example input to the helper - -- the helper creates a new buffer with it, formats, and returns the formatted output - local formatted = require('test.formatter.helper').test_with('python', { - -- The input code should somewhat reflect the filetype + local formatted = require('test.helper').run_fmt('black', 'python', { [[def foo(n):]], [[ if n in (1,2,3):]], [[ return n+1]], - [[a, b = 1, 2]], - [[b, a = a, b]], - [[print( f"The factorial of {a} is: {foo(a)}")]], }) - -- Show that the input is indeed formatted as intended assert.are.same({ [[def foo(n):]], [[ if n in (1, 2, 3):]], [[ return n + 1]], - [[]], - [[]], - [[a, b = 1, 2]], - [[b, a = a, b]], - [[print(f"The factorial of {a} is: {foo(a)}")]], }, formatted) end) end) ``` -- Or if it's a linter, show that the linter's output is converted correctly into neovim diagnostics +Linter example: ```lua -describe('selene', function() +describe('flake8', function() it('can lint', function() - -- pre-test setup - local helper = require('test.linter.helper') - local ns = helper.namespace - local ft = require('guard.filetype') - ft('lua'):lint('selene') - require('guard').setup() - -- Giving example input to the helper - -- the helper creates a new buffer with it, requires lint, and returns the diagnostics - local buf, diagnostics = require('test.linter.helper').test_with('lua', { - -- Make sure the input actually has some problems that the linter detects - [[local M = {}]], - [[function M.foo()]], - [[ print("foo")]], - [[end]], - [[U.bar()]], - [[return M]], + local helper = require('test.helper') + local buf, diagnostics = helper.run_lint('flake8', 'python', { + [[import os]], }) - -- Show that the diagnostics is indeed valid - assert.are.same({ - { - bufnr = buf, - col = 0, - end_col = 0, - end_lnum = 4, - lnum = 4, - message = '`U` is not defined [undefined_variable]', - -- sometimes the namespace is not fixed - namespace = ns, - severity = 1, - source = 'selene', - }, - }, diagnostics) + assert.is_true(#diagnostics > 0) + for _, d in ipairs(diagnostics) do + assert.equal(buf, d.bufnr) + assert.equal('flake8', d.source) + assert.is_number(d.lnum) + assert.is_string(d.message) + end end) end) - ``` -- To run the test you just created, install [vusted](https://github.com/notomo/vusted) - ```shell - luarocks --lua-version=5.1 install vusted - ``` -- Create a symlink so that vusted recognizes guard.nvim namespaces +For linters with a custom `fn` (cpplint, checkmake, zsh), run the command directly and test `parse()`: -```shell -ln -s ~/.local/share/nvim/lazy/guard.nvim/lua/guard lua +```lua +describe('cpplint', function() + it('can lint', function() + local linter = require('test.helper').get_linter('cpplint') + local tmpfile = '/tmp/guard-test.cpp' + vim.fn.writefile({ [[int main(){int x=1;}]] }, tmpfile) + local bufnr = vim.api.nvim_create_buf(false, true) + local result = vim.system({ 'cpplint', '--filter=-legal/copyright', tmpfile }, {}):wait() + local diagnostics = linter.parse(result.stderr or '', bufnr) + assert.is_true(#diagnostics > 0) + end) +end) ``` -- Run the test and make sure it passes - +- Add your tool to CI in `.github/workflows/ci.yaml` under the appropriate job +- Run the test locally: ```shell - - vusted ./test/formatter/_spec.lua - # or - vusted ./test/linter/\_spec.lua - - ok 1 - can format - ok 1 - can lint + # requires: neovim, lua 5.1, luarocks, busted, nlua + # also requires guard.nvim cloned: git clone --depth 1 https://github.com/nvimdev/guard.nvim && mv guard.nvim/lua/guard lua/ + make test-pip # or whichever category ``` - -- Modify `test/setup.sh` so that CI installs your tool - -- Optionally, format the code with stylua +- Format with stylua before submitting: ```shell stylua . ``` diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f2453ac --- /dev/null +++ b/Makefile @@ -0,0 +1,36 @@ +LUA_PATH := lua/?.lua;lua/?/init.lua;$(LUA_PATH) +export LUA_PATH + +.PHONY: lint test check-coverage test-pip test-npm test-go test-rust test-lua test-binary test-clang test-haskell + +lint: + stylua --check . + +check-coverage: + ./scripts/check-coverage + +test: test-pip test-npm test-go test-rust test-lua test-binary test-clang test-haskell + +test-pip: + busted --lua nlua test/pip/*_spec.lua + +test-npm: + busted --lua nlua test/npm/*_spec.lua + +test-go: + busted --lua nlua test/go/*_spec.lua + +test-rust: + busted --lua nlua test/rust/*_spec.lua + +test-lua: + busted --lua nlua test/lua/*_spec.lua + +test-binary: + busted --lua nlua test/binary/*_spec.lua + +test-clang: + @for f in test/clang/*_spec.lua; do busted --lua nlua "$$f"; done + +test-haskell: + busted --lua nlua test/haskell/*_spec.lua diff --git a/lua/guard-collection/linter/cpplint.lua b/lua/guard-collection/linter/cpplint.lua new file mode 100644 index 0000000..4054c95 --- /dev/null +++ b/lua/guard-collection/linter/cpplint.lua @@ -0,0 +1,33 @@ +local lint = require('guard.lint') + +return { + fn = function(_, fname) + local co = assert(coroutine.running()) + vim.system({ 'cpplint', '--filter=-legal/copyright', fname }, {}, function(result) + coroutine.resume(co, result.stderr or '') + end) + return coroutine.yield() + end, + parse = function(result, bufnr) + local diags = {} + for line in result:gmatch('[^\n]+') do + local lnum, msg, cat, sev = line:match('^[^:]+:(%d+):%s*(.-)%s+%[([^%]]+)%]%s+%[(%d+)%]$') + if lnum then + local severity = tonumber(sev) >= 3 and vim.diagnostic.severity.ERROR + or vim.diagnostic.severity.WARN + table.insert( + diags, + lint.diag_fmt( + bufnr, + tonumber(lnum) - 1, + 0, + ('[%s] %s'):format(cat, msg), + severity, + 'cpplint' + ) + ) + end + end + return diags + end, +} diff --git a/lua/guard-collection/linter/init.lua b/lua/guard-collection/linter/init.lua index 51a6599..1a377de 100644 --- a/lua/guard-collection/linter/init.lua +++ b/lua/guard-collection/linter/init.lua @@ -3,6 +3,7 @@ return { checkmake = require('guard-collection.linter.checkmake'), ['clang-tidy'] = require('guard-collection.linter.clang-tidy'), codespell = require('guard-collection.linter.codespell'), + cpplint = require('guard-collection.linter.cpplint'), detekt = require('guard-collection.linter.detekt'), eslint = require('guard-collection.linter.eslint'), eslint_d = require('guard-collection.linter.eslint_d'), @@ -23,4 +24,5 @@ return { mypy = require('guard-collection.linter.mypy').mypy, mypyc = require('guard-collection.linter.mypy').mypyc, dmypy = require('guard-collection.linter.mypy').dmypy, + zsh = require('guard-collection.linter.zsh'), } diff --git a/lua/guard-collection/linter/zsh.lua b/lua/guard-collection/linter/zsh.lua new file mode 100644 index 0000000..24a2e6b --- /dev/null +++ b/lua/guard-collection/linter/zsh.lua @@ -0,0 +1,24 @@ +local lint = require('guard.lint') + +return { + fn = function(_, fname) + local co = assert(coroutine.running()) + vim.system({ 'zsh', '-n', fname }, {}, function(result) + coroutine.resume(co, result.stderr or '') + end) + return coroutine.yield() + end, + parse = function(result, bufnr) + local diags = {} + for line in result:gmatch('[^\n]+') do + local lnum, msg = line:match(':(%d+): (.+)$') + if lnum then + table.insert( + diags, + lint.diag_fmt(bufnr, tonumber(lnum) - 1, 0, msg, vim.diagnostic.severity.ERROR, 'zsh') + ) + end + end + return diags + end, +} diff --git a/scripts/check-coverage b/scripts/check-coverage new file mode 100755 index 0000000..831d2f7 --- /dev/null +++ b/scripts/check-coverage @@ -0,0 +1,96 @@ +#!/bin/sh +set -eu + +red='\033[0;31m' +green='\033[0;32m' +yellow='\033[0;33m' +bold='\033[1m' +dim='\033[2m' +reset='\033[0m' + +root="$(cd "$(dirname "$0")/.." && pwd)" +fmt_src="$root/lua/guard-collection/formatter.lua" +lint_src="$root/lua/guard-collection/linter/init.lua" +test_dir="$root/test" + +fmt_names=$( + sed -n "s/^M\.\([a-zA-Z_][a-zA-Z0-9_]*\) =.*/\1/p" "$fmt_src" + sed -n "s/^M\['\([^']*\)'\] =.*/\1/p" "$fmt_src" +) + +lint_names=$( + sed -n "s/^ \([a-zA-Z_][a-zA-Z0-9_]*\) = require.*/\1/p" "$lint_src" + sed -n "s/^ \['\([^']*\)'\] = require.*/\1/p" "$lint_src" +) + +tested="" +for f in "$test_dir"/*/*_spec.lua; do + [ -f "$f" ] || continue + name="${f##*/}" + name="${name%_spec.lua}" + tested="$tested $name" +done + +has_test() { + for t in $tested; do + [ "$t" = "$1" ] && return 0 + done + return 1 +} + +total_fmt=0 +covered_fmt=0 +missing_fmt="" +for name in $fmt_names; do + total_fmt=$((total_fmt + 1)) + if has_test "$name"; then + covered_fmt=$((covered_fmt + 1)) + else + missing_fmt="$missing_fmt $name" + fi +done + +total_lint=0 +covered_lint=0 +missing_lint="" +for name in $lint_names; do + total_lint=$((total_lint + 1)) + if has_test "$name"; then + covered_lint=$((covered_lint + 1)) + else + missing_lint="$missing_lint $name" + fi +done + +total=$((total_fmt + total_lint)) +covered=$((covered_fmt + covered_lint)) +missing=$((total - covered)) + +printf "\n" +printf "${bold}guard-collection test coverage${reset}\n" +printf "${dim}────────────────────────────────${reset}\n" +printf " formatters: ${bold}%d${reset}/%d\n" "$covered_fmt" "$total_fmt" +printf " linters: ${bold}%d${reset}/%d\n" "$covered_lint" "$total_lint" +printf " total: ${bold}%d${reset}/%d\n" "$covered" "$total" +printf "${dim}────────────────────────────────${reset}\n" + +if [ "$missing" -gt 0 ]; then + if [ -n "$(echo $missing_fmt)" ]; then + printf "\n${yellow}formatters missing tests:${reset}\n" + for name in $missing_fmt; do + printf " ${red}✗${reset} %s\n" "$name" + done + fi + + if [ -n "$(echo $missing_lint)" ]; then + printf "\n${yellow}linters missing tests:${reset}\n" + for name in $missing_lint; do + printf " ${red}✗${reset} %s\n" "$name" + done + fi + + printf "\n${red}${bold}FAIL: %d tools have no test${reset}\n\n" "$missing" + exit 1 +fi + +printf "\n${green}${bold}OK: all tools have tests${reset}\n\n" diff --git a/test/binary/alejandra_spec.lua b/test/binary/alejandra_spec.lua new file mode 100644 index 0000000..897e41a --- /dev/null +++ b/test/binary/alejandra_spec.lua @@ -0,0 +1,10 @@ +describe('alejandra', function() + it('can format', function() + local formatted = require('test.helper').run_fmt('alejandra', 'nix', { + [[{inputs={nixpkgs.url="github:NixOS/nixpkgs/nixos-unstable";};}]], + }) + assert.are.same({ + [[{inputs = {nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";};}]], + }, formatted) + end) +end) diff --git a/test/binary/checkmake_spec.lua b/test/binary/checkmake_spec.lua new file mode 100644 index 0000000..adda48a --- /dev/null +++ b/test/binary/checkmake_spec.lua @@ -0,0 +1,28 @@ +describe('checkmake', function() + it('can lint', function() + local linter = require('test.helper').get_linter('checkmake') + local tmpfile = '/tmp/guard-test.make' + local input = { + [[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('checkmake', d.source) + assert.is_number(d.lnum) + assert.is_string(d.message) + end + end) +end) diff --git a/test/formatter/deno_fmt_spec.lua b/test/binary/deno_fmt_spec.lua similarity index 79% rename from test/formatter/deno_fmt_spec.lua rename to test/binary/deno_fmt_spec.lua index 1b359c4..90860ee 100644 --- a/test/formatter/deno_fmt_spec.lua +++ b/test/binary/deno_fmt_spec.lua @@ -1,9 +1,6 @@ describe('deno fmt', function() it('can format', function() - local ft = require('guard.filetype') - ft('typescript'):fmt('deno_fmt') - - local formatted = require('test.formatter.helper').test_with('typescript', { + local formatted = require('test.helper').run_fmt('deno_fmt', 'typescript', { [[function ]], [[ fibonacci(num:]], [[ number):]], diff --git a/test/formatter/latexindent_spec.lua b/test/binary/latexindent_spec.lua similarity index 80% rename from test/formatter/latexindent_spec.lua rename to test/binary/latexindent_spec.lua index c4bba5d..4f22713 100644 --- a/test/formatter/latexindent_spec.lua +++ b/test/binary/latexindent_spec.lua @@ -1,9 +1,6 @@ describe('latexindent', function() it('can format', function() - local ft = require('guard.filetype') - ft('latex'):fmt('latexindent') - - local formatted = require('test.formatter.helper').test_with('latex', { + local formatted = require('test.helper').run_fmt('latexindent', 'latex', { [[\documentclass{article}]], [[\begin{document}]], [[Shopping list]], diff --git a/test/formatter/swiftformat.lua b/test/binary/swiftformat_spec.lua similarity index 64% rename from test/formatter/swiftformat.lua rename to test/binary/swiftformat_spec.lua index 2580c0e..1c6b640 100644 --- a/test/formatter/swiftformat.lua +++ b/test/binary/swiftformat_spec.lua @@ -1,9 +1,6 @@ describe('swiftformat', function() it('can format', function() - local ft = require('guard.filetype') - ft('swift'):fmt('swiftformat') - - local formatted = require('test.formatter.helper').test_with('swift', { + local formatted = require('test.helper').run_fmt('swiftformat', 'swift', { [[func myFunc() { ]], [[print("hello") ]], [[ }]], diff --git a/test/binary/zsh_spec.lua b/test/binary/zsh_spec.lua new file mode 100644 index 0000000..78332d2 --- /dev/null +++ b/test/binary/zsh_spec.lua @@ -0,0 +1,18 @@ +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 + end) +end) diff --git a/test/formatter/clang-format_spec.lua b/test/clang/clang-format_spec.lua similarity index 81% rename from test/formatter/clang-format_spec.lua rename to test/clang/clang-format_spec.lua index 2ca886c..c5bb344 100644 --- a/test/formatter/clang-format_spec.lua +++ b/test/clang/clang-format_spec.lua @@ -1,9 +1,6 @@ describe('clang-format', function() it('can format', function() - local ft = require('guard.filetype') - ft('c'):fmt('clang-format') - - local formatted = require('test.formatter.helper').test_with('c', { + local formatted = require('test.helper').run_fmt('clang-format', 'c', { [[#include ]], [[int main() {]], [[ int x = 0x87654321]], diff --git a/test/clang/clang-tidy_spec.lua b/test/clang/clang-tidy_spec.lua new file mode 100644 index 0000000..d3ef122 --- /dev/null +++ b/test/clang/clang-tidy_spec.lua @@ -0,0 +1,21 @@ +describe('clang-tidy', function() + it('can lint', function() + local helper = require('test.helper') + local buf, diagnostics = helper.run_lint('clang-tidy', 'c', { + [[#include ]], + [[int main() {]], + [[ int x = 10;]], + [[ int y = 0;]], + [[ printf("%d", x / y);]], + [[ return 0;]], + [[}]], + }) + assert.is_true(#diagnostics > 0) + for _, d in ipairs(diagnostics) do + assert.equal(buf, d.bufnr) + assert.equal('clang-tidy', d.source) + assert.is_number(d.lnum) + assert.is_string(d.message) + end + end) +end) diff --git a/test/env.sh b/test/env.sh deleted file mode 100644 index a9788c8..0000000 --- a/test/env.sh +++ /dev/null @@ -1,4 +0,0 @@ -#! /bin/bash -export PATH="/home/linuxbrew/.linuxbrew/opt/clang-format/bin:$PATH" -export PATH="$HOME/.local/bin:$PATH" -export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH" diff --git a/test/formatter/alejandra_spec.lua b/test/formatter/alejandra_spec.lua deleted file mode 100644 index 5282dbb..0000000 --- a/test/formatter/alejandra_spec.lua +++ /dev/null @@ -1,17 +0,0 @@ -describe('alejandra', function() - it('can format', function() - -- pre-test setup - local ft = require('guard.filetype') - ft('nix'):fmt('alejandra') - -- Giving example input to the helper - -- the helper creates a new buffer with it, formats, and returns the formatted output - local formatted = require('test.formatter.helper').test_with('nix', { - -- The input code should somewhat reflect the filetype - [[{inputs={nixpkgs.url="github:NixOS/nixpkgs/nixos-unstable";};}]], - }) - -- Show that the input is indeed formatted as intended - assert.are.same({ - [[{inputs = {nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";};}]], - }, formatted) - end) -end) diff --git a/test/formatter/buf_spec.lua b/test/formatter/buf_spec.lua deleted file mode 100644 index 1ea646a..0000000 --- a/test/formatter/buf_spec.lua +++ /dev/null @@ -1,17 +0,0 @@ -describe('buf', function() - it('can format', function() - local ft = require('guard.filetype') - ft('proto'):fmt('buf') - - local formatted = require('test.formatter.helper').test_with('proto', { - [[syntax = "proto3"; message Foo { string bar = 1; }]], - }) - assert.are.same({ - [[syntax = "proto3";]], - [[]], - [[message Foo {]], - [[ string bar = 1;]], - [[}]], - }, formatted) - end) -end) diff --git a/test/formatter/helper.lua b/test/formatter/helper.lua deleted file mode 100644 index 28292fd..0000000 --- a/test/formatter/helper.lua +++ /dev/null @@ -1,18 +0,0 @@ -local M = {} -local api = vim.api - -function M.test_with(ft, input) - local cmd = require('guard.filetype')(ft).formatter[1].cmd - assert(not cmd or vim.fn.executable(cmd) == 1) - local bufnr = api.nvim_create_buf(true, false) - vim.bo[bufnr].filetype = ft - api.nvim_set_current_buf(bufnr) - api.nvim_buf_set_lines(bufnr, 0, -1, false, input) - -- To provide fname - vim.cmd('silent! write! /tmp/file.' .. ft) - require('guard.format').do_fmt(bufnr) - vim.wait(3000) - return api.nvim_buf_get_lines(bufnr, 0, -1, false) -end - -return M diff --git a/test/formatter/sqlfluff_spec.lua b/test/formatter/sqlfluff_spec.lua deleted file mode 100644 index 393713c..0000000 --- a/test/formatter/sqlfluff_spec.lua +++ /dev/null @@ -1,42 +0,0 @@ -describe('sqlfluff', function() - it('can format', function() - local ft = require('guard.filetype') - local tool = ft('sql'):fmt('sqlfluff') - tool.formatter[1].args = vim.list_extend(tool.formatter[1].args, { '--dialect', 'ansi' }) - - local formatted = require('test.formatter.helper').test_with('sql', { - [[SELECT *]], - [[FROM]], - [[ World ]], - [[WHERE "Someone"]], - [[ LIKE '%YOU%']], - }) - assert.are.same({ - [[SELECT *]], - [[FROM]], - [[ World]], - [[WHERE]], - [[ "Someone"]], - [[ LIKE '%YOU%']], - }, formatted) - end) - - it('can fix', function() - local ft = require('guard.filetype') - local tool = ft('sql'):fmt('sqlfluff_fix') - tool.formatter[1].args = vim.list_extend(tool.formatter[1].args, { '--dialect', 'ansi' }) - - local formatted = require('test.formatter.helper').test_with('sql', { - [[SELECT]], - [[ a + b AS foo,]], - [[ c AS bar]], - [[FROM my_table]], - }) - assert.are.same({ - [[SELECT]], - [[ c AS bar,]], - [[ a + b AS foo]], - [[FROM my_table]], - }, formatted) - end) -end) diff --git a/test/formatter/gofmt_spec.lua b/test/go/gofmt_spec.lua similarity index 76% rename from test/formatter/gofmt_spec.lua rename to test/go/gofmt_spec.lua index a17d93d..8f3f213 100644 --- a/test/formatter/gofmt_spec.lua +++ b/test/go/gofmt_spec.lua @@ -1,9 +1,6 @@ describe('gofmt', function() it('can format', function() - local ft = require('guard.filetype') - ft('go'):fmt('gofmt') - - local formatted = require('test.formatter.helper').test_with('go', { + local formatted = require('test.helper').run_fmt('gofmt', 'go', { [[package main]], [[]], [[ import "fmt" ]], diff --git a/test/formatter/gofumpt_spec.lua b/test/go/gofumpt_spec.lua similarity index 76% rename from test/formatter/gofumpt_spec.lua rename to test/go/gofumpt_spec.lua index 933403c..86911ce 100644 --- a/test/formatter/gofumpt_spec.lua +++ b/test/go/gofumpt_spec.lua @@ -1,9 +1,6 @@ describe('gofumpt', function() it('can format', function() - local ft = require('guard.filetype') - ft('go'):fmt('gofumpt') - - local formatted = require('test.formatter.helper').test_with('go', { + local formatted = require('test.helper').run_fmt('gofumpt', 'go', { [[package main]], [[]], [[ import "fmt" ]], diff --git a/test/formatter/golines_spec.lua b/test/go/golines_spec.lua similarity index 76% rename from test/formatter/golines_spec.lua rename to test/go/golines_spec.lua index 935b61e..0cd4a9b 100644 --- a/test/formatter/golines_spec.lua +++ b/test/go/golines_spec.lua @@ -1,9 +1,6 @@ describe('golines', function() it('can format', function() - local ft = require('guard.filetype') - ft('go'):fmt('golines') - - local formatted = require('test.formatter.helper').test_with('go', { + local formatted = require('test.helper').run_fmt('golines', 'go', { [[package main]], [[]], [[ import "fmt" ]], diff --git a/test/haskell/hlint_spec.lua b/test/haskell/hlint_spec.lua new file mode 100644 index 0000000..9584413 --- /dev/null +++ b/test/haskell/hlint_spec.lua @@ -0,0 +1,17 @@ +describe('hlint', function() + it('can lint', function() + local helper = require('test.helper') + local buf, diagnostics = helper.run_lint('hlint', 'haskell', { + [[concat $ map escapeC s]], + [[ftable ++ (map (\ (c, x) -> (toUpper c, urlEncode x)) ftable)]], + [[mapM (delete_line (fn2fp f) line) old]], + }) + assert.is_true(#diagnostics > 0) + for _, d in ipairs(diagnostics) do + assert.equal(buf, d.bufnr) + assert.equal('hlint', d.source) + assert.is_number(d.lnum) + assert.is_string(d.message) + end + end) +end) diff --git a/test/helper.lua b/test/helper.lua new file mode 100644 index 0000000..ef63342 --- /dev/null +++ b/test/helper.lua @@ -0,0 +1,58 @@ +local M = {} +local api = vim.api + +function M.run_fmt(name, ft, input) + local config = require('guard-collection.formatter')[name] + assert(config, 'unknown formatter: ' .. name) + assert(not config.fn, name .. ' uses custom fn, not testable this way') + local cmd = vim.list_extend({ config.cmd }, config.args or {}) + local tmpfile = '/tmp/guard-test.' .. ft + if config.fname then + vim.fn.writefile(input, tmpfile) + table.insert(cmd, tmpfile) + end + local result = vim + .system(cmd, { + stdin = config.stdin and (table.concat(input, '\n') .. '\n') or nil, + }) + :wait() + assert(result.code == 0, name .. ' exited ' .. result.code .. ': ' .. (result.stderr or '')) + if config.stdin then + return vim.split(result.stdout, '\n', { trimempty = true }) + else + return vim.fn.readfile(tmpfile) + end +end + +function M.run_lint(name, ft, input) + local linter = require('guard-collection.linter')[name] + assert(linter, 'unknown linter: ' .. name) + assert(not linter.fn, name .. ' uses custom fn — test parse() directly instead') + local input_str = table.concat(input, '\n') .. '\n' + local tmpfile = '/tmp/guard-test.' .. ft + vim.fn.writefile(input, tmpfile) + local bufnr = api.nvim_create_buf(false, true) + local cmd = vim.list_extend({ linter.cmd }, linter.args or {}) + if linter.fname then + table.insert(cmd, tmpfile) + end + local result = vim + .system(cmd, { + stdin = linter.stdin and input_str or nil, + }) + :wait() + local output = result.stdout or '' + if output == '' then + output = result.stderr or '' + end + local diags = linter.parse(output, bufnr) + return bufnr, diags +end + +function M.get_linter(name) + local linter = require('guard-collection.linter')[name] + assert(linter, 'unknown linter: ' .. name) + return linter +end + +return M diff --git a/test/linter/buf_spec.lua b/test/linter/buf_spec.lua deleted file mode 100644 index 7fe56eb..0000000 --- a/test/linter/buf_spec.lua +++ /dev/null @@ -1,25 +0,0 @@ -describe('buf', function() - it('can lint', function() - local helper = require('test.linter.helper') - local ns = helper.namespace - local ft = require('guard.filetype') - ft('proto'):lint('buf') - - local buf, diagnostics = helper.test_with('proto', { - [[syntax = "proto3"; message Foo { string bar = 1 }]], - }) - assert.are.same({ - { - bufnr = buf, - col = 48, - end_col = 48, - end_lnum = 0, - lnum = 0, - message = "syntax error: expecting ';'", - namespace = ns, - severity = 4, - source = 'buf', - }, - }, diagnostics) - end) -end) diff --git a/test/linter/checkmake_spec.lua b/test/linter/checkmake_spec.lua deleted file mode 100644 index 2964cd9..0000000 --- a/test/linter/checkmake_spec.lua +++ /dev/null @@ -1,48 +0,0 @@ -describe('checkmake', function() - it('can lint', function() - local helper = require('test.linter.helper') - local ns = helper.namespace - local ft = require('guard.filetype') - ft('make'):lint('checkmake') - - local buf, diagnostics = helper.test_with('make', { - [[all:]], - [[\techo hello]], - }) - assert.are.same({ - { - bufnr = buf, - col = 0, - end_col = 0, - end_lnum = 0, - lnum = 0, - message = '[minphony] Missing required phony target "all"', - namespace = ns, - severity = 2, - source = 'checkmake', - }, - { - bufnr = buf, - col = 0, - end_col = 0, - end_lnum = 0, - lnum = 0, - message = '[minphony] Missing required phony target "clean"', - namespace = ns, - severity = 2, - source = 'checkmake', - }, - { - bufnr = buf, - col = 0, - end_col = 0, - end_lnum = 0, - lnum = 0, - message = '[minphony] Missing required phony target "test"', - namespace = ns, - severity = 2, - source = 'checkmake', - }, - }, diagnostics) - end) -end) diff --git a/test/linter/clang-tidy_spec.lua b/test/linter/clang-tidy_spec.lua deleted file mode 100644 index 9eee7bd..0000000 --- a/test/linter/clang-tidy_spec.lua +++ /dev/null @@ -1,31 +0,0 @@ -describe('clang-tidy', function() - it('can lint', function() - local helper = require('test.linter.helper') - local ns = helper.namespace - local ft = require('guard.filetype') - ft('c'):lint('clang-tidy') - - local buf, diagnostics = helper.test_with('c', { - [[#include ]], - [[int main() {]], - [[ int x = 10;]], - [[ int y = 0;]], - [[ printf("%d", x / y);]], - [[ return 0;]], - [[}]], - }) - assert.are.same({ - { - bufnr = buf, - col = 19, - end_col = 19, - end_lnum = 4, - lnum = 4, - message = 'Division by zero[clang-analyzer-core.DivideZero]', - namespace = ns, - severity = 2, - source = 'clang-tidy', - }, - }, diagnostics) - end) -end) diff --git a/test/linter/flake8_spec.lua b/test/linter/flake8_spec.lua deleted file mode 100644 index e12b780..0000000 --- a/test/linter/flake8_spec.lua +++ /dev/null @@ -1,107 +0,0 @@ -describe('flake8', function() - it('can lint', function() - local helper = require('test.linter.helper') - local ns = helper.namespace - local ft = require('guard.filetype') - ft('python'):lint('flake8') - - local buf, diagnostics = helper.test_with('python', { - [[import os]], - [[]], - [[def foo(n):]], - [[ if n == 0:]], - [[ return bar]], - [[print("it's too long sentence to be displayed in one line, blah blah blah blah")]], - }) - assert.are.same({ - { - bufnr = buf, - col = 0, - end_col = 0, - end_lnum = 0, - lnum = 0, - message = "'os' imported but unused[401]", - namespace = ns, - severity = 3, - source = 'flake8', - }, - { - bufnr = buf, - col = 0, - end_col = 0, - end_lnum = 2, - lnum = 2, - message = 'expected 2 blank lines, found 1[302]', - namespace = ns, - severity = 1, - source = 'flake8', - }, - { - bufnr = buf, - col = 9, - end_col = 9, - end_lnum = 4, - lnum = 4, - message = 'indentation is not a multiple of 4[111]', - namespace = ns, - severity = 1, - source = 'flake8', - }, - { - bufnr = buf, - col = 9, - end_col = 9, - end_lnum = 4, - lnum = 4, - message = 'over-indented[117]', - namespace = ns, - severity = 1, - source = 'flake8', - }, - { - bufnr = buf, - col = 15, - end_col = 15, - end_lnum = 4, - lnum = 4, - message = 'multiple spaces after keyword[271]', - namespace = ns, - severity = 1, - source = 'flake8', - }, - { - bufnr = buf, - col = 17, - end_col = 17, - end_lnum = 4, - lnum = 4, - message = "undefined name 'bar'[821]", - namespace = ns, - severity = 3, - source = 'flake8', - }, - { - bufnr = buf, - col = 0, - end_col = 0, - end_lnum = 5, - lnum = 5, - message = 'expected 2 blank lines after class or function definition, found 0[305]', - namespace = ns, - severity = 1, - source = 'flake8', - }, - { - bufnr = buf, - col = 79, - end_col = 79, - end_lnum = 5, - lnum = 5, - message = 'line too long (80 > 79 characters)[501]', - namespace = ns, - severity = 1, - source = 'flake8', - }, - }, diagnostics) - end) -end) diff --git a/test/linter/helper.lua b/test/linter/helper.lua deleted file mode 100644 index fb43a42..0000000 --- a/test/linter/helper.lua +++ /dev/null @@ -1,22 +0,0 @@ -local M = {} -local api = vim.api -local lint = require('guard.lint') -M.namespace = api.nvim_get_namespaces().Guard - -function M.test_with(ft, input) - local cmd = require('guard.filetype')(ft).linter[1].cmd - assert(vim.fn.executable(cmd) == 1) - - local bufnr = api.nvim_create_buf(true, false) - vim.bo[bufnr].filetype = ft - api.nvim_set_current_buf(bufnr) - api.nvim_buf_set_lines(bufnr, 0, -1, false, input) - -- To make linters happy - vim.cmd('silent! write! /tmp/test.' .. ft) - - lint.do_lint(bufnr) - vim.wait(3000) - return bufnr, vim.diagnostic.get(bufnr) -end - -return M diff --git a/test/linter/hlint_spec.lua b/test/linter/hlint_spec.lua deleted file mode 100644 index 02864da..0000000 --- a/test/linter/hlint_spec.lua +++ /dev/null @@ -1,49 +0,0 @@ -describe('hlint', function() - it('can lint', function() - local helper = require('test.linter.helper') - local ns = helper.namespace - local ft = require('guard.filetype') - ft('haskell'):lint('hlint') - - local buf, diagnostics = helper.test_with('haskell', { - [[concat $ map escapeC s]], - [[ftable ++ (map (\ (c, x) -> (toUpper c, urlEncode x)) ftable)]], - [[mapM (delete_line (fn2fp f) line) old]], - }) - assert.are.same({ - { - bufnr = buf, - col = 0, - end_col = 0, - end_lnum = 0, - lnum = 0, - message = 'Use concatMap: concatMap escapeC s', - namespace = ns, - severity = 2, - source = 'hlint', - }, - { - bufnr = buf, - col = 10, - end_col = 10, - end_lnum = 1, - lnum = 1, - message = [[Redundant bracket: ftable ++ map (\ (c, x) -> (toUpper c, urlEncode x)) ftable]], - namespace = ns, - severity = 3, - source = 'hlint', - }, - { - bufnr = buf, - col = 16, - end_col = 16, - end_lnum = 1, - lnum = 1, - message = 'Use bimap: Data.Bifunctor.bimap toUpper urlEncode', - namespace = ns, - severity = 3, - source = 'hlint', - }, - }, diagnostics) - end) -end) diff --git a/test/linter/luacheck_spec.lua b/test/linter/luacheck_spec.lua deleted file mode 100644 index 0731c74..0000000 --- a/test/linter/luacheck_spec.lua +++ /dev/null @@ -1,30 +0,0 @@ -describe('luacheck', function() - it('can lint', function() - local helper = require('test.linter.helper') - local ns = helper.namespace - local ft = require('guard.filetype') - ft('lua'):lint('luacheck') - - local buf, diagnostics = helper.test_with('lua', { - [[local M = {}]], - [[function M.foo()]], - [[ print("foo")]], - [[end]], - [[U.bar()]], - [[return M]], - }) - assert.are.same({ - { - bufnr = buf, - col = 0, - end_col = 0, - end_lnum = 4, - lnum = 4, - message = "accessing undefined variable 'U'[113]", - namespace = ns, - severity = 2, - source = 'luacheck', - }, - }, diagnostics) - end) -end) diff --git a/test/linter/selene_spec.lua b/test/linter/selene_spec.lua deleted file mode 100644 index 9be18f9..0000000 --- a/test/linter/selene_spec.lua +++ /dev/null @@ -1,25 +0,0 @@ -describe('selene', function() - it('can lint', function() - local helper = require('test.linter.helper') - local ns = helper.namespace - local ft = require('guard.filetype') - ft('lua'):lint('selene') - - local buf, diagnostics = helper.test_with('lua', { - [[print(a)]], - }) - assert.are.same({ - { - bufnr = buf, - col = 6, - end_col = 0, - end_lnum = 0, - lnum = 0, - message = '`a` is not defined[undefined_variable]', - namespace = ns, - severity = 1, - source = 'selene', - }, - }, diagnostics) - end) -end) diff --git a/test/lua/luacheck_spec.lua b/test/lua/luacheck_spec.lua new file mode 100644 index 0000000..e043ea7 --- /dev/null +++ b/test/lua/luacheck_spec.lua @@ -0,0 +1,20 @@ +describe('luacheck', function() + it('can lint', function() + local helper = require('test.helper') + local buf, diagnostics = helper.run_lint('luacheck', 'lua', { + [[local M = {}]], + [[function M.foo()]], + [[ print("foo")]], + [[end]], + [[U.bar()]], + [[return M]], + }) + assert.is_true(#diagnostics > 0) + for _, d in ipairs(diagnostics) do + assert.equal(buf, d.bufnr) + assert.equal('luacheck', d.source) + assert.is_number(d.lnum) + assert.is_string(d.message) + end + end) +end) diff --git a/test/lua/selene_spec.lua b/test/lua/selene_spec.lua new file mode 100644 index 0000000..04d25d5 --- /dev/null +++ b/test/lua/selene_spec.lua @@ -0,0 +1,15 @@ +describe('selene', function() + it('can lint', function() + local helper = require('test.helper') + local buf, diagnostics = helper.run_lint('selene', 'lua', { + [[print(a)]], + }) + assert.is_true(#diagnostics > 0) + for _, d in ipairs(diagnostics) do + assert.equal(buf, d.bufnr) + assert.equal('selene', d.source) + assert.is_number(d.lnum) + assert.is_string(d.message) + end + end) +end) diff --git a/test/formatter/stylua_spec.lua b/test/lua/stylua_spec.lua similarity index 65% rename from test/formatter/stylua_spec.lua rename to test/lua/stylua_spec.lua index a98d5b1..0e2b054 100644 --- a/test/formatter/stylua_spec.lua +++ b/test/lua/stylua_spec.lua @@ -1,9 +1,6 @@ describe('stylua', function() it('can format', function() - local ft = require('guard.filetype') - ft('lua'):fmt('stylua') - - local formatted = require('test.formatter.helper').test_with('lua', { + local formatted = require('test.helper').run_fmt('stylua', 'lua', { [[local M={}]], [[ M.foo =]], [[ "foo"]], diff --git a/test/formatter/biome_spec.lua b/test/npm/biome_spec.lua similarity index 67% rename from test/formatter/biome_spec.lua rename to test/npm/biome_spec.lua index dedb733..a697946 100644 --- a/test/formatter/biome_spec.lua +++ b/test/npm/biome_spec.lua @@ -1,9 +1,6 @@ describe('biome', function() it('can format json', function() - local ft = require('guard.filetype') - ft('json'):fmt('biome') - - local formatted = require('test.formatter.helper').test_with('json', { + local formatted = require('test.helper').run_fmt('biome', 'json', { [[{"name": "dove" , "age":10 ]], [[,"gender": "male"}]], }) @@ -13,10 +10,7 @@ describe('biome', function() end) it('can format javascript', function() - local ft = require('guard.filetype') - ft('js'):fmt('biome') - - local formatted = require('test.formatter.helper').test_with('js', { + local formatted = require('test.helper').run_fmt('biome', 'js', { [[ const randomNumber = Math.floor(]], [[ Math.random() * 10]], [[ ) + 1]], diff --git a/test/formatter/prettier_spec.lua b/test/npm/prettier_spec.lua similarity index 70% rename from test/formatter/prettier_spec.lua rename to test/npm/prettier_spec.lua index aa381af..2be27a7 100644 --- a/test/formatter/prettier_spec.lua +++ b/test/npm/prettier_spec.lua @@ -1,9 +1,6 @@ describe('prettier', function() it('can format', function() - local ft = require('guard.filetype') - ft('javascript'):fmt('prettier') - - local formatted = require('test.formatter.helper').test_with('javascript', { + local formatted = require('test.helper').run_fmt('prettier', 'javascript', { [[ const randomNumber = Math.floor(]], [[ Math.random() * 10]], [[ ) + 1]], diff --git a/test/formatter/sql-formatter_spec.lua b/test/npm/sql-formatter_spec.lua similarity index 66% rename from test/formatter/sql-formatter_spec.lua rename to test/npm/sql-formatter_spec.lua index 2063998..7d03101 100644 --- a/test/formatter/sql-formatter_spec.lua +++ b/test/npm/sql-formatter_spec.lua @@ -1,9 +1,6 @@ describe('sql-formatter', function() it('can format', function() - local ft = require('guard.filetype') - ft('sql'):fmt('sql-formatter') - - local formatted = require('test.formatter.helper').test_with('sql', { + local formatted = require('test.helper').run_fmt('sql-formatter', 'sql', { [[SELECT *]], [[FROM]], [[World]], @@ -17,8 +14,6 @@ describe('sql-formatter', function() [[ World]], [[WHERE]], [[ "Someone" LIKE '%YOU%']], - -- /> no results! - -- /> :sob }, formatted) end) end) diff --git a/test/formatter/taplo_spec.lua b/test/npm/taplo_spec.lua similarity index 79% rename from test/formatter/taplo_spec.lua rename to test/npm/taplo_spec.lua index bdada26..d92bec6 100644 --- a/test/formatter/taplo_spec.lua +++ b/test/npm/taplo_spec.lua @@ -1,9 +1,6 @@ describe('taplo', function() it('can format', function() - local ft = require('guard.filetype') - ft('toml'):fmt('taplo') - - local formatted = require('test.formatter.helper').test_with('toml', { + local formatted = require('test.helper').run_fmt('taplo', 'toml', { [[ [dependencies] ]], [[ async-process = "^1.7"]], [[strum = { version = "^0.25", features = ["derive"] } ]], diff --git a/test/formatter/autopep8_spec.lua b/test/pip/autopep8_spec.lua similarity index 78% rename from test/formatter/autopep8_spec.lua rename to test/pip/autopep8_spec.lua index 7f89f4a..3e96edf 100644 --- a/test/formatter/autopep8_spec.lua +++ b/test/pip/autopep8_spec.lua @@ -1,9 +1,6 @@ describe('autopep8', function() it('can format', function() - local ft = require('guard.filetype') - ft('python'):fmt('autopep8') - - local formatted = require('test.formatter.helper').test_with('python', { + local formatted = require('test.helper').run_fmt('autopep8', 'python', { [[def foo(n):]], [[ if n in (1,2,3):]], [[ return n+1]], diff --git a/test/formatter/black_spec.lua b/test/pip/black_spec.lua similarity index 78% rename from test/formatter/black_spec.lua rename to test/pip/black_spec.lua index 2312e76..1deffcd 100644 --- a/test/formatter/black_spec.lua +++ b/test/pip/black_spec.lua @@ -1,9 +1,6 @@ describe('black', function() it('can format', function() - local ft = require('guard.filetype') - ft('python'):fmt('black') - - local formatted = require('test.formatter.helper').test_with('python', { + local formatted = require('test.helper').run_fmt('black', 'python', { [[def foo(n):]], [[ if n in (1,2,3):]], [[ return n+1]], diff --git a/test/formatter/cmake-format_spec.lua b/test/pip/cmake-format_spec.lua similarity index 69% rename from test/formatter/cmake-format_spec.lua rename to test/pip/cmake-format_spec.lua index 2e5a817..25af6e5 100644 --- a/test/formatter/cmake-format_spec.lua +++ b/test/pip/cmake-format_spec.lua @@ -1,9 +1,6 @@ describe('cmake-format', function() it('can format', function() - local ft = require('guard.filetype') - ft('cmake'):fmt('cmake-format') - - local formatted = require('test.formatter.helper').test_with('cmake', { + local formatted = require('test.helper').run_fmt('cmake-format', 'cmake', { [[cmake_minimum_required(VERSION 3.10)]], [[project(test)]], [[add_executable(test main.cpp)]], diff --git a/test/pip/cpplint_spec.lua b/test/pip/cpplint_spec.lua new file mode 100644 index 0000000..cb0095a --- /dev/null +++ b/test/pip/cpplint_spec.lua @@ -0,0 +1,19 @@ +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 + end) +end) diff --git a/test/formatter/docformatter_spec.lua b/test/pip/docformatter_spec.lua similarity index 69% rename from test/formatter/docformatter_spec.lua rename to test/pip/docformatter_spec.lua index 977d80d..1871572 100644 --- a/test/formatter/docformatter_spec.lua +++ b/test/pip/docformatter_spec.lua @@ -1,9 +1,6 @@ describe('docformatter', function() it('can format', function() - local ft = require('guard.filetype') - ft('python'):fmt('docformatter') - - local formatted = require('test.formatter.helper').test_with('python', { + local formatted = require('test.helper').run_fmt('docformatter', 'python', { [[def foo():]], [[ """]], [[ Hello foo.]], diff --git a/test/pip/flake8_spec.lua b/test/pip/flake8_spec.lua new file mode 100644 index 0000000..fab866a --- /dev/null +++ b/test/pip/flake8_spec.lua @@ -0,0 +1,21 @@ +describe('flake8', function() + it('can lint', function() + local helper = require('test.helper') + local buf, diagnostics = helper.run_lint('flake8', 'python', { + [[import os]], + [[]], + [[def foo(n):]], + [[ if n == 0:]], + [[ return bar]], + [[print("it's too long sentence to be displayed in one line, blah blah blah blah")]], + }) + assert.is_true(#diagnostics > 0) + for _, d in ipairs(diagnostics) do + assert.equal(buf, d.bufnr) + assert.equal('flake8', d.source) + assert.is_number(d.lnum) + assert.is_number(d.col) + assert.is_string(d.message) + end + end) +end) diff --git a/test/formatter/ruff_spec.lua b/test/pip/ruff_spec.lua similarity index 78% rename from test/formatter/ruff_spec.lua rename to test/pip/ruff_spec.lua index 11dcb07..866b13f 100644 --- a/test/formatter/ruff_spec.lua +++ b/test/pip/ruff_spec.lua @@ -1,9 +1,6 @@ describe('ruff', function() it('can format', function() - local ft = require('guard.filetype') - ft('python'):fmt('ruff') - - local formatted = require('test.formatter.helper').test_with('python', { + local formatted = require('test.helper').run_fmt('ruff', 'python', { [[def foo(n):]], [[ if n in (1,2,3):]], [[ return n+1]], diff --git a/test/pip/sqlfluff_spec.lua b/test/pip/sqlfluff_spec.lua new file mode 100644 index 0000000..cb8452b --- /dev/null +++ b/test/pip/sqlfluff_spec.lua @@ -0,0 +1,54 @@ +describe('sqlfluff', function() + it('can format', function() + local config = require('guard-collection.formatter').sqlfluff + local cmd = vim.list_extend({ config.cmd }, config.args) + vim.list_extend(cmd, { '--dialect', 'ansi' }) + local input = { + [[SELECT *]], + [[FROM]], + [[ World ]], + [[WHERE "Someone"]], + [[ LIKE '%YOU%']], + } + local result = vim + .system(cmd, { + stdin = table.concat(input, '\n') .. '\n', + }) + :wait() + assert(result.code == 0, 'sqlfluff exited ' .. result.code .. ': ' .. (result.stderr or '')) + local formatted = vim.split(result.stdout, '\n', { trimempty = true }) + assert.are.same({ + [[SELECT *]], + [[FROM]], + [[ World]], + [[WHERE]], + [[ "Someone"]], + [[ LIKE '%YOU%']], + }, formatted) + end) + + it('can fix', function() + local config = require('guard-collection.formatter').sqlfluff_fix + local cmd = vim.list_extend({ config.cmd }, config.args) + vim.list_extend(cmd, { '--dialect', 'ansi' }) + local input = { + [[SELECT]], + [[ a + b AS foo,]], + [[ c AS bar]], + [[FROM my_table]], + } + local result = vim + .system(cmd, { + stdin = table.concat(input, '\n') .. '\n', + }) + :wait() + assert(result.code == 0, 'sqlfluff_fix exited ' .. result.code .. ': ' .. (result.stderr or '')) + local formatted = vim.split(result.stdout, '\n', { trimempty = true }) + assert.are.same({ + [[SELECT]], + [[ c AS bar,]], + [[ a + b AS foo]], + [[FROM my_table]], + }, formatted) + end) +end) diff --git a/test/formatter/yapf_spec.lua b/test/pip/yapf_spec.lua similarity index 78% rename from test/formatter/yapf_spec.lua rename to test/pip/yapf_spec.lua index a1678a4..69ff808 100644 --- a/test/formatter/yapf_spec.lua +++ b/test/pip/yapf_spec.lua @@ -1,9 +1,6 @@ describe('yapf', function() it('can format', function() - local ft = require('guard.filetype') - ft('python'):fmt('yapf') - - local formatted = require('test.formatter.helper').test_with('python', { + local formatted = require('test.helper').run_fmt('yapf', 'python', { [[def foo(n):]], [[ if n in (1,2,3):]], [[ return n+1]], diff --git a/test/formatter/rustfmt_nightly_spec.lua b/test/rust/rustfmt_nightly_spec.lua similarity index 74% rename from test/formatter/rustfmt_nightly_spec.lua rename to test/rust/rustfmt_nightly_spec.lua index 584c279..d37b01e 100644 --- a/test/formatter/rustfmt_nightly_spec.lua +++ b/test/rust/rustfmt_nightly_spec.lua @@ -1,9 +1,6 @@ describe('rustfmt_nightly', function() it('can format', function() - local ft = require('guard.filetype') - ft('rust'):fmt('rustfmt_nightly') - - local formatted = require('test.formatter.helper').test_with('rust', { + local formatted = require('test.helper').run_fmt('rustfmt_nightly', 'rust', { [[use std::{collections::HashMap, collections::HashSet};]], [[fn main() {]], [[let var:usize=1;]], diff --git a/test/formatter/rustfmt_spec.lua b/test/rust/rustfmt_spec.lua similarity index 65% rename from test/formatter/rustfmt_spec.lua rename to test/rust/rustfmt_spec.lua index bec9e5b..6782c85 100644 --- a/test/formatter/rustfmt_spec.lua +++ b/test/rust/rustfmt_spec.lua @@ -1,9 +1,6 @@ describe('rustfmt', function() it('can format', function() - local ft = require('guard.filetype') - ft('rust'):fmt('rustfmt') - - local formatted = require('test.formatter.helper').test_with('rust', { + local formatted = require('test.helper').run_fmt('rustfmt', 'rust', { [[use std::{collections::HashMap, collections::HashSet};]], [[fn main() {]], [[let var:usize=1;]], @@ -11,7 +8,7 @@ describe('rustfmt', function() [[}]], }) assert.are.same({ - [[use std::{collections::HashMap, collections::HashSet};]], + [[use std::collections::{HashMap, HashSet};]], [[fn main() {]], [[ let var: usize = 1;]], [[ println!("{var}");]], diff --git a/test/setup.sh b/test/setup.sh deleted file mode 100644 index d1db3e1..0000000 --- a/test/setup.sh +++ /dev/null @@ -1,62 +0,0 @@ -#! /bin/bash -# Setup homebrew -eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" - -# Install packages with package managers -luarocks install luacheck & - -pip -qqq install autopep8 black djhtml docformatter flake8 isort pylint yapf codespell ruff sqlfluff clang-tidy mypy & - -npm install -g --silent \ - prettier @fsouza/prettierd sql-formatter shellcheck shfmt @taplo/cli @biomejs/biome & - -brew install \ - hlint ormolu clang-format golines gofumpt detekt swiftformat & - -# Install standalone binary packages -bin="$HOME/.local/bin" -gh="https://github.com" -mkdir -p $bin - -# alejandra -wget -q $gh"/kamadorueda/alejandra/releases/download/4.0.0/alejandra-x86_64-unknown-linux-musl" -mv ./alejandra-x86_64-unknown-linux-musl $bin/alejandra -chmod +x $bin/alejandra - -# cbfmt -wget -q $gh"/lukas-reineke/cbfmt/releases/download/v0.2.0/cbfmt_linux-x86_64_v0.2.0.tar.gz" -tar -xvf cbfmt_linux-x86_64_v0.2.0.tar.gz -mv ./cbfmt_linux-x86_64_v0.2.0/cbfmt $bin -chmod +x $bin/cbfmt - -# selene -wget -q "$gh/Kampfkarren/selene/releases/download/0.28.0/selene-0.28.0-linux.zip" -unzip selene-0.28.0-linux.zip -d $bin -chmod +x $bin/selene - -# stylua -wget -q "$gh/JohnnyMorganz/StyLua/releases/download/v0.18.0/stylua-linux.zip" -unzip stylua-linux.zip -d $bin -chmod +x $bin/stylua - -# latexindent -wget -q "$gh/cmhughes/latexindent.pl/releases/download/V3.22.2/latexindent-linux" -chmod +x latexindent-linux -mv latexindent-linux $bin/latexindent - -# nixfmt -wget -q "$gh/serokell/nixfmt/releases/download/v0.5.0/nixfmt" -chmod +x nixfmt -mv nixfmt $bin/nixfmt - -# ktlint -wget -q "$gh/pinterest/ktlint/releases/download/1.0.0/ktlint" -chmod +x ktlint -mv ktlint $bin/ktlint - -# test setup -export PATH="$HOME/.local/bin:$PATH" -luarocks install vusted -git clone "$gh/nvimdev/guard.nvim" "$HOME/guard.nvim" -mv "$HOME/guard.nvim/lua/guard" ./lua/ -wait