Skip to content

Commit

Permalink
feat: :TestEdit, file patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
klen committed Apr 5, 2022
1 parent 66248ed commit 6c91e2e
Show file tree
Hide file tree
Showing 51 changed files with 476 additions and 171 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/_
/nvim-treesitter
/todo.txt
/spec/lua/test/fixtures/**/target/
/spec/fixtures/**/target/
12 changes: 4 additions & 8 deletions .vimrc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ require("nvim-test").setup {
}

require("nvim-test.runners.vusted"):setup {
args = { "--helper=spec/lua/conftest.lua" },
args = { "--helper=spec/conftest.lua" },
env = {
VIRTUAL_ENV = "",
VUSTED_ARGS = "--headless --clean",
},

find_spec = function(filename)
local test_pattern = "_spec.lua"
if filename:sub(-#test_pattern) == test_pattern then
return filename
end

return string.format("spec/lua/test/%s_spec.lua", vim.fn.fnamemodify(filename, ":t:r"))
find_files = function(filename)
local path, _ = vim.fn.fnamemodify(filename, ":p:h"):gsub("lua/nvim%-test", "spec")
return string.format("%s/%s_spec.lua", path, vim.fn.fnamemodify(filename, ":t:r"))
end,
}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test t: nvim-treesitter
@echo $(CURDIR)
vusted \
--shuffle --lpath="./?.lua;./?/?.lua;./?/init.lua" \
--helper=$(CURDIR)/spec/lua/conftest.lua
--helper=$(CURDIR)/spec/conftest.lua
.PHONY: test t

RELEASE ?= patch
Expand Down
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ Test Runner for neovim

| Language | Test Runners |
| -------------: | :------------------------------- |
| **Go** | Go `go-test` |
| **Javascript** | Mocha `mocha`, Jest `jest` |
| **Lua** | Busted `busted` |
| **Python** | PyTest `pytest`, PyUnit `pyunit` |
| **Rust** | Cargo `cargo-test` |
| **Typescript** | TS-Mocha `ts-mocha` |
| **Go** | `go-test` |
| **Javascript** | `jest`, `mocha` |
| **Lua** | `busted`, `vusted` |
| **Python** | `pytest`, `pyunit` |
| **Rust** | `cargo-test` |
| **Typescript** | `jest`, `mocha`, `ts-mocha` |

## Install

Expand All @@ -37,10 +37,11 @@ use {

The plugin defines the commands:

- `TestNearest` - run the test nearest to the cursor
- `TestFile` - run all tests in the current file
- `TestSuite` - run the whole test suite
- `TestLast` - run the last test
- `TestFile` - run all tests for the current file
- `TestEdit` - edit tests for the current file
- `TestNearest` - run the test nearest to the cursor
- `TestLast` - rerun the latest test
- `TestVisit` - open the last run test in the current buffer
- `TestInfo` - show an information about the plugin

Expand Down Expand Up @@ -79,11 +80,14 @@ require('nvim-test').setup {
Setup a runner:
```lua
require('nvim-test.runners.jest'):setup {
command = "~/node_modules/.bin/jest", -- a command to run the test runner
args = { "--collectCoverage=false" }, -- default arguments
env = { CUSTOM_VAR = 'value' }, -- custom environment variables
command = "~/node_modules/.bin/jest", -- a command to run the test runner
args = { "--collectCoverage=false" }, -- default arguments
env = { CUSTOM_VAR = 'value' }, -- custom environment variables

file_pattern = "\\v(__tests__/.*|(spec|test))\\.(js|jsx|coffee|ts|tsx)$", -- determine whether a file is a testfile
find_files = { "{name}.test.{ext}", "{name}.spec.{ext}" }, -- find testfile for a file

filename_modifier = nil, -- modify filename before tests run (:h filename-modifiers)
working_directory = nil, -- set working directory (cwd by default)
filename_modifier = nil, -- modify filename before tests run (:h filename-modifiers)
working_directory = nil, -- set working directory (cwd by default)
}
```
150 changes: 126 additions & 24 deletions doc/nvim-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ INTRODUCTION *nvim-test-introduction*

Test Runner for neovim

------------------------------------------------------------------------------
COMMANDS *nvim-test-command*

:TestSuite *:TestSuite*
Run the whole test suite

:TestFile *:TestFile*
Run all tests in the current file

:TestEdit *:TestEdit*
Edit tests for the current file

:TestNearest *:TestNearest*
Run the test nearest to the cursor

:TestLast *:TestLast*
Rerun the latest test

:TestVisit *:TestVisit*
Open the last run test in the current buffer

:TestInfo *:TestInfo*
Show an information about plugin

------------------------------------------------------------------------------
SETUP *nvim-test-setup*

Expand Down Expand Up @@ -37,46 +61,124 @@ Setup the plugin (default values): >
Setup a runner: >
require('nvim-test.runners.jest'):setup {
command = "~/node_modules/.bin/jest", -- a command to run the test runner
args = { "--collectCoverage=false" }, -- default arguments
env = { CUSTOM_VAR = 'value' }, -- custom environment variables
command = "~/node_modules/.bin/jest", -- a command to run the test runner
args = { "--collectCoverage=false" }, -- default arguments
env = { CUSTOM_VAR = 'value' }, -- custom environment variables
filename_modifier = nil, -- modify filename before tests run (:h filename-modifiers)
working_directory = nil, -- set working directory (cwd by default)
file_pattern = "\\v(__tests__/.*|(spec|test))\\.(js|jsx|coffee|ts|tsx)$", -- determine whether a file is a testfile
find_files = { "{name}.test.{ext}", "{name}.spec.{ext}" }, -- find testfile for a file
filename_modifier = nil, -- modify filename before tests run (:h filename-modifiers)
working_directory = nil, -- set working directory (cwd by default)
}
------------------------------------------------------------------------------
COMMANDS *nvim-test-command*
RUNNERS *nvim-test-runners*

:TestNearest *:TestNearest*
Run the test nearest to the cursor
*nvim-test-busted*

:TestFile *:TestFile*
Run all tests in the current file
Default options: >
:TestSuite *:TestSuite*
Run the whole test suite
require('nvim-test.runners.busted'):setup {
command = "busted",
file_pattern = "\\v_spec\\.(lua|moon)$",
find_files = "{name}_spec.{ext}",
}
<
*nvim-test-cargo-test*

:TestLast *:TestLast*
Rerun the latest test
Cargotest runner may find the nearest crate root.

:TestVisit *:TestVisit*
Open the last run test in the current buffer
Default options: >
:TestInfo *:TestInfo*
Show an information about plugin
require('nvim-test.runners.cargo-test'):setup {
command = "cargo",
args = { "test" },
------------------------------------------------------------------------------
RUNNERS *nvim-test-runners*
package = false, -- Set to true, to find the nearest create root
}
<
*nvim-test-cypress*

*nvim-test-runners-cargo*
Default options: >
Cargotest runner may find the nearest crate root.
require('nvim-test.runners.cypress'):setup {
command = "go",
args = { "test", "-v" }
}
<
*nvim-test-go-test*

Enable `package` option: >
Default options: >
require('nvim-test.runners.cargo-test'):setup { package = true }
require('nvim-test.runners.go-test'):setup {
command = {"./node_modules/.bin/cypress", "cypress"},
args = { "run" },
file_pattern = "\\v(__tests__/.*|(spec|test))\\.(js|jsx|coffee|ts|tsx)$",
find_files = { "{name}.test.{ext}", "{name}.spec.{ext}" },
}
<
*nvim-test-jest*

Default options: >
require('nvim-test.runners.jest'):setup {
command = { "./node_modules/.bin/jest", "jest" },
file_pattern = "\\v(__tests__/.*|(spec|test))\\.(js|jsx|coffee|ts|tsx)$",
find_files = { "{name}.test.{ext}", "{name}.spec.{ext}" },
}
<
*nvim-test-mocha*

Default options: >
require('nvim-test.runners.mocha'):setup {
command = { "./node_modules/.bin/mocha", "mocha" },
file_pattern = "\\v(tests?/.*|test)\\.(js|jsx|coffee)$",
find_files = { "{name}.test.{ext}" },
}
<
*nvim-test-pytest*

Default options: >
require('nvim-test.runners.pytest'):setup {
command = { (vim.env.VIRTUAL_ENV or "venv") .. "/bin/pytest", "pytest" },
file_pattern = "\\v(test_[^.]+|[^.]+_test|tests)\\.py$",
find_files = { "test_{name}.py", "{name}_test.py", "tests.py" },
}
<
*nvim-test-pyunit*

Default options: >
require('nvim-test.runners.pytest'):setup {
command = { (vim.env.VIRTUAL_ENV or "venv") .. "/bin/python", "python" },
args = { "-m", "unittest" },
file_pattern = "\\v^test.*\\.py$",
find_files = { "test_{name}.py" },
}
<
*nvim-test-ts-mocha*

Default options: >
require('nvim-test.runners.ts-mocha'):setup {
command = { "./node_modules/.bin/ts-mocha", "ts-mocha" },
file_pattern = "\\v(tests?/.*|test)\\.(ts|tsx)$",
find_files = { "{name}.test.{ext}" },
}
<
*nvim-test-ts-vusted*

Default options: >
require('nvim-test.runners.vusted'):setup {
command = "vusted",
file_pattern = "\\v_spec\\.(lua|moon)$",
find_files = "{name}_spec.{ext}",
}
<

------------------------------------------------------------------------------
ABOUT *nvim-test-about*
Expand Down
36 changes: 29 additions & 7 deletions lua/nvim-test/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,34 @@ function M.run(scope)
local opts = {}
local filename = nil

-- Find tests
if scope == "nearest" then
opts.tests = runner:find_test(filetype)
end

-- Find file
if scope ~= "suite" then
filename = runner.config.find_spec(
vim.fn.expand("%" .. (runner.config.filename_modifier or M.config.filename_modifier))
filename = vim.fn.expand(
"%" .. (runner.config.filename_modifier or M.config.filename_modifier)
)
end
if scope == "nearest" then
opts.tests = runner:find_tests(filetype)
if not runner:is_testfile(filename) then
filename = runner:find_file(filename)
end
end

-- Prepare run context
local cmd = runner:build_cmd(filename, opts)
local cfg = { env = runner.config.env, working_directory = runner.config.working_directory }

-- Save last run
vim.g.test_latest = {
cmd = cmd,
cfg = runner.config,
cfg = cfg,
filename = filename,
line = api.nvim_win_get_cursor(0)[0],
}
return M.run_cmd(cmd, runner.config)

return M.run_cmd(cmd, cfg)
end
end

Expand Down Expand Up @@ -80,6 +91,16 @@ function M.visit()
return notifier:notify("No tests were run so far", 3)
end

function M.edit()
local runner = M.get_runner(vim.bo.filetype)
local filename = vim.fn.expand(
"%" .. (runner.config.filename_modifier or M.config.filename_modifier)
)
if not runner:is_testfile(filename) then
vim.api.nvim_command("edit " .. runner:find_file(filename, true))
end
end

--- Run the given command
---
---@param cmd table: a command to run
Expand Down Expand Up @@ -131,6 +152,7 @@ function M.setup(cfg)
api.nvim_command "command! TestSuite lua require'nvim-test'.run('suite')<CR>"
api.nvim_command "command! TestVisit lua require'nvim-test'.visit()<CR>"
api.nvim_command "command! TestInfo lua require'nvim-test.info'()<CR>"
api.nvim_command "command! TestEdit lua require'nvim-test'.edit()<CR>"
end
end

Expand Down
Loading

0 comments on commit 6c91e2e

Please sign in to comment.