Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
klen committed Apr 4, 2022
1 parent 99b9fff commit ce3d068
Show file tree
Hide file tree
Showing 35 changed files with 277 additions and 124 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/_
/nvim-treesitter
/todo.txt
/spec/lua/test/fixtures/**/target/
2 changes: 1 addition & 1 deletion .vimrc.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require("nvim-test.runners.busted"):setup {
command = "vusted",
args = " --helper=spec/lua/conftest.lua",
args = { "--helper=spec/lua/conftest.lua" },
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ require('nvim-test').setup {
width = 96, -- terminal's width (for vertical|float)
height = 24, -- terminal's height (for horizontal|float)
go_back = false, -- return focus to original window after executing
stopinsert = false, -- exit from insert mode
stopinsert = false, -- exit from insert mode (true|false|"auto")
keep_one = true, -- only for term 'terminal', use only one buffer for testing
},
runners = { -- setup tests runners
Expand All @@ -79,6 +79,6 @@ Setup a runner:
```lua
require('nvim-test.runners.jest').setup {
command = "~/node_modules/.bin/jest",
args = " --collectCoverage=false ",
args = { "--collectCoverage=false" },
}
```
18 changes: 15 additions & 3 deletions doc/nvim-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ Setup the plugin (default values): >
commands_create = true, -- create commands (TestFile, TestLast, ...)
silent = false, -- less notifications
run = true, -- run test commands
term = "terminal", -- a terminal to run (terminal|toggleterm)
term = "terminal", -- a terminal to run ("terminal"|"toggleterm")
termOpts = {
direction = "vertical", -- terminal's direction (horizontal|vertical|float)
width = 96, -- terminal's width (for vertical|float)
height = 24, -- terminal's height (for horizontal|float)
go_back = false, -- return focus to original window after executing
stopinsert = false, -- exit from insert mode
stopinsert = false, -- exit from insert mode (true|false|"auto")
keep_one = true, -- only for term 'terminal', use only one buffer for testing
},
runners = { -- setup tests runners
Expand All @@ -37,7 +37,7 @@ Setup a runner: >
require('nvim-test.runners.jest').setup {
command = "~/node_modules/.bin/jest",
args = " --collectCoverage=false ",
args = { "--collectCoverage=false" },
}
------------------------------------------------------------------------------
Expand All @@ -61,6 +61,18 @@ COMMANDS *nvim-test-command*
:TestInfo *:TestInfo*
Show an information about plugin

------------------------------------------------------------------------------
RUNNERS *nvim-test-runners*

*nvim-test-runners-cargo*

Cargotest runner may find the nearest crate root.

Enable `package` option: >
require('nvim-test.runners.cargo-test').setup { package = true }
------------------------------------------------------------------------------
ABOUT *nvim-test-about*

Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-test/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ return {
width = 96,
height = 24,
go_back = false,
stopinsert = false,
stopinsert = "auto",
keep_one = true,
},
}
10 changes: 6 additions & 4 deletions lua/nvim-test/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local Notifier = require "nvim-test.notify"
local api = vim.api
local suite_runner
local Runner = require "nvim-test.runner"
local M = {
config = vim.deepcopy(require "nvim-test.config"),
notifier = nil,
Expand Down Expand Up @@ -45,7 +46,8 @@ function M.run_last()
return M.run_cmd(vim.g.test_latest.cmd)
end

--- Get a runner by the given filetype
---Get a runner by the given filetype
---@return Runner runner
function M.get_runner(filetype, default)
local runner_module = M.runners[filetype]
if runner_module then
Expand Down Expand Up @@ -73,9 +75,9 @@ end

--- Run the given command
---
---@param cmd string: a command to run
---@param cmd table: a command to run
function M.run_cmd(cmd)
M.notifier:onotify(cmd)
M.notifier:onotify(table.concat(cmd, " "))
if not M.config.run then
return
end
Expand All @@ -89,7 +91,7 @@ function M.run_cmd(cmd)
width = { opts.width, "number" },
height = { opts.height, "number" },
go_back = { opts.go_back, "boolean" },
stopinsert = { opts.stopinsert, "boolean" },
-- stopinsert = { opts.stopinsert, "boolean" },
}
termExec(cmd, opts)
end
Expand Down
28 changes: 18 additions & 10 deletions lua/nvim-test/runner.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
---@diagnostic disable: unused-local
local utils = require "nvim-test.utils"
local ts_utils = require "nvim-treesitter.ts_utils"

---@class Runner
local Runner = {
config = { args = "" },
config = { args = {} },
}
Runner.__index = Runner

Expand Down Expand Up @@ -52,23 +54,29 @@ function Runner:is_test(name)
return true
end

-- Build command list
---
---@return table cmd command list
function Runner:build_cmd(filename, opts)
return string.format("%s%s", self.config.command, self:build_args(filename, opts or {}))
local args = utils.concat({}, self.config.args)
self:build_args(args, filename, opts or {})
table.insert(args, 1, self.config.command)
return args
end

function Runner:build_args(filename, opts)
local args = self.config.args
-- Build arguments
---
---@return table args arguments list
function Runner:build_args(args, filename, opts)
if filename then
args = args .. " " .. filename
table.insert(args, filename)
end
if opts.tests and #opts.tests > 0 then
args = args .. self:build_test_args(opts.tests)
self:build_test_args(args, opts.tests)
end
return args
end

function Runner:build_test_args(tests)
return ""
end
---@return table test_args test arguments list
function Runner:build_test_args(args, tests) end

return Runner
6 changes: 4 additions & 2 deletions lua/nvim-test/runners/busted.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ function busted:parse_name(name)
return name:gsub("^[\"']", ""):gsub("[\"']$", "")
end

function busted:build_test_args(tests)
return string.format(" --filter '%s'", table.concat(tests, " "))
function busted:build_test_args(args, tests)
table.insert(args, "--filter")
table.insert(args, table.concat(tests, " "))
return args
end

return busted
44 changes: 26 additions & 18 deletions lua/nvim-test/runners/cargo-test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
--
local Runner = require "nvim-test.runner"

local cargotest = Runner:init({ command = "cargo test" }, {
local cargotest = Runner:init({ command = "cargo", args = { "test" }, package = false }, {
rust = [[
(
(mod_item
Expand All @@ -19,31 +19,39 @@ function cargotest:is_test(name)
return string.match(name, "[Tt]est") and true
end

function cargotest:build_args(filename, opts)
function cargotest:build_args(args, filename, opts)
-- for whole suite do nothing
local args = self.config.args
if not filename then
return args
return
end

local parts = vim.fn.split(vim.fn.fnamemodify(filename, ":.:r"), "/")
if parts[#parts] == "main" or parts[#parts] == "lib" or parts[#parts] == "mod" then
parts[#parts] = nil
end
if parts[1] == "src" then
table.remove(parts, 1)
end

local modname = (#parts > 0) and table.concat(parts, "::")
if modname then
modname = " " .. vim.fn.shellescape(modname .. "::")
-- Find a package
if self.config.package then
local crate = vim.fn.findfile("Cargo.toml", vim.fn.fnamemodify(filename, ":p") .. ";")
if crate and #crate > 0 then
table.insert(args, "-p")
table.insert(args, vim.fn.fnamemodify(crate, ":p:h:t"))
end
end

if opts.tests and #opts.tests > 0 then
return args .. " " .. table.concat(opts.tests, "::") .. " -- --exact"
table.insert(args, table.concat(opts.tests, "::"))
table.insert(args, "--")
table.insert(args, "--exact")
else
local parts = vim.fn.split(vim.fn.fnamemodify(filename, ":.:r"), "/")
if parts[#parts] == "main" or parts[#parts] == "lib" or parts[#parts] == "mod" then
parts[#parts] = nil
end
if parts[1] == "src" then
table.remove(parts, 1)
end

local modname = (#parts > 0) and table.concat(parts, "::")
if modname then
table.insert(args, modname .. "::")
end
end

return args .. (modname or "")
end

return cargotest
27 changes: 13 additions & 14 deletions lua/nvim-test/runners/go-test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
--
local Runner = require "nvim-test.runner"

local gotest = Runner:init({ command = "go test" }, {
local gotest = Runner:init({ command = "go", args = { "test", "-v" } }, {
go = [[
((function_declaration
name: (identifier) @function-name) @scope-root)
Expand All @@ -13,20 +13,19 @@ function gotest:is_test(name)
return string.match(name, "^Test") or string.match(name, "^Example")
end

function gotest:build_args(filename, opts)
local args = self.config.args
if not filename then
return args .. " ./..."
function gotest:build_args(args, filename, opts)
if filename then
local path = vim.fn.fnamemodify(filename, ":.:h")
if path ~= "." then
table.insert(args, string.format("./%s/...", path))
end
if opts.tests and #opts.tests > 0 then
table.insert(args, "-run")
table.insert(args, opts.tests[1] .. "$")
end
else
table.insert(args, "./...")
end

local path = vim.fn.fnamemodify(filename, ":.:h")
if path ~= "." then
args = string.format("%s ./%s/...", args, path)
end
if opts.tests then
args = string.format(" -run %s ", vim.fn.shellescape(opts.tests[1] .. "$")) .. args
end
return args
end

return gotest
5 changes: 3 additions & 2 deletions lua/nvim-test/runners/jest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ function jest:parse_name(name)
return name:gsub("^[\"'`]", ""):gsub("[\"'`]$", "")
end

function jest:build_test_args(tests)
return string.format(" -t '%s'", table.concat(tests, " "))
function jest:build_test_args(args, tests)
table.insert(args, "-t")
table.insert(args, table.concat(tests, " "))
end

return jest
5 changes: 3 additions & 2 deletions lua/nvim-test/runners/mocha.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ function mocha:parse_name(name)
return name:gsub("^[\"'`]", ""):gsub("[\"'`]$", "")
end

function mocha:build_test_args(tests)
return string.format(" -f '%s'", table.concat(tests, " "))
function mocha:build_test_args(args, tests)
table.insert(args, "-f")
table.insert(args, table.concat(tests, " "))
end

return mocha
4 changes: 2 additions & 2 deletions lua/nvim-test/runners/pytest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ function pytest:is_test(name)
return string.match(name, "[Tt]est") and true
end

function pytest:build_test_args(tests)
return "::" .. table.concat(tests, "::")
function pytest:build_test_args(args, tests)
args[#args] = args[#args] .. "::" .. table.concat(tests, "::")
end

return pytest
14 changes: 6 additions & 8 deletions lua/nvim-test/runners/pyunit.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
local Runner = require "nvim-test.runner"

local cmd = vim.env.VIRTUAL_ENV and vim.env.VIRTUAL_ENV .. "/bin/python" or "python"

local pyunit = Runner:init({
command = cmd .. " -m unittest",
command = vim.env.VIRTUAL_ENV and vim.env.VIRTUAL_ENV .. "/bin/python" or "python",
args = { "-m", "unittest" },
}, {
python = [[
; Class
Expand All @@ -20,15 +19,14 @@ function pyunit:is_test(name)
return string.match(name, "[Tt]est") and true
end

function pyunit:build_args(filename, opts)
local args = self.config.args
function pyunit:build_args(args, filename, opts)
if filename then
args = args .. " " .. vim.fn.fnamemodify(filename, ":.:r"):gsub("/", ".")
local path, _ = vim.fn.fnamemodify(filename, ":.:r"):gsub("/", ".")
table.insert(args, path)
end
if opts.tests and #opts.tests > 0 then
args = args .. "." .. table.concat(opts.tests, ".")
args[#args] = args[#args] .. "." .. table.concat(opts.tests, ".")
end
return args
end

return pyunit
5 changes: 3 additions & 2 deletions lua/nvim-test/runners/ts-mocha.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ function tsmocha:parse_name(name)
return name:gsub("^[\"'`]", ""):gsub("[\"'`]$", "")
end

function tsmocha:build_test_args(tests)
return string.format(" -f '%s'", table.concat(tests, " "))
function tsmocha:build_test_args(args, tests)
table.insert(args, "-f")
table.insert(args, table.concat(tests, " "))
end

return tsmocha
Loading

0 comments on commit ce3d068

Please sign in to comment.