Skip to content

Commit

Permalink
feat: support env for runners, close #7
Browse files Browse the repository at this point in the history
  • Loading branch information
klen committed Apr 4, 2022
1 parent dac4b2e commit 6d7eef4
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .vimrc.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
require("nvim-test.runners.busted"):setup {
command = "vusted",
args = { "--helper=spec/lua/conftest.lua" },
env = {
VIRTUAL_ENV = "",
VUSTED_ARGS = "--headless --clean",
},
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,6 @@ Setup a runner:
require('nvim-test.runners.jest').setup {
command = "~/node_modules/.bin/jest",
args = { "--collectCoverage=false" },
env = { CUSTOM_VAR = 'value' },
}
```
1 change: 1 addition & 0 deletions doc/nvim-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Setup a runner: >
require('nvim-test.runners.jest').setup {
command = "~/node_modules/.bin/jest",
args = { "--collectCoverage=false" },
env = { CUSTOM_VAR = 'value' },
}
------------------------------------------------------------------------------
Expand Down
20 changes: 14 additions & 6 deletions lua/nvim-test/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,24 @@ function M.run(scope)
end

local cmd = runner:build_cmd(filename, opts)
vim.g.test_latest = { cmd = cmd, filename = filename, line = api.nvim_win_get_cursor(0)[0] }
return M.run_cmd(cmd)
local env = runner.config.env
vim.g.test_latest = {
cmd = cmd,
env = env,
filename = filename,
line = api.nvim_win_get_cursor(0)[0],
}
return M.run_cmd(cmd, env)
end
end

--- Repeat a latest test command
function M.run_last()
if not vim.g.test_latest then
local latest = vim.g.test_latest
if not latest then
return M.notifier:notify("No tests were run so far", "ErrorMsg")
end
return M.run_cmd(vim.g.test_latest.cmd)
return M.run_cmd(latest.cmd, latest.env)
end

---Get a runner by the given filetype
Expand Down Expand Up @@ -76,7 +83,8 @@ end
--- Run the given command
---
---@param cmd table: a command to run
function M.run_cmd(cmd)
---@param env table: an env
function M.run_cmd(cmd, env)
M.notifier:onotify(table.concat(cmd, " "))
if not M.config.run then
return
Expand All @@ -93,7 +101,7 @@ function M.run_cmd(cmd)
go_back = { opts.go_back, "boolean" },
-- stopinsert = { opts.stopinsert, "boolean" },
}
termExec(cmd, opts)
return termExec(cmd, env, opts)
end

-- Setup the plugin
Expand Down
17 changes: 11 additions & 6 deletions lua/nvim-test/terms/terminal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ local directionsMap = {
horizontal = "split",
}
local buffers = {}
local next = next

local exec = function(cmd, cfg)
return vim.fn.termopen(cmd, {
local exec = function(cmd, env, cfg)
local opts = {
on_exit = function(_, status)
if cfg.stopinsert == "auto" and status ~= 0 then
vim.cmd "stopinsert!"
end
end,
})
}
if env and next(env) then
opts.env = env
end
return vim.fn.termopen(cmd, opts)
end

return function(cmd, cfg)
return function(cmd, env, cfg)
if cfg.direction == "float" then
local bufnr = vim.api.nvim_create_buf(false, false)
vim.api.nvim_open_win(bufnr, true, {
Expand All @@ -26,7 +31,7 @@ return function(cmd, cfg)
style = "minimal",
border = "single",
})
return exec(cmd, cfg)
return exec(cmd, env, cfg)
end

local split = directionsMap[cfg.direction]
Expand All @@ -48,7 +53,7 @@ return function(cmd, cfg)
end

vim.cmd(string.format("botright %s new", split))
exec(cmd, cfg)
exec(cmd, env, cfg)

table.insert(buffers, vim.api.nvim_get_current_buf())
if cfg.stopinsert == true or cfg.go_back then
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-test/terms/toggleterm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local defaults = {
open = true, --- open boolean whether or not to open terminal window
}

return function(cmd, cfg)
return function(cmd, _, cfg)
cfg = vim.tbl_deep_extend("force", defaults, cfg)
local size = cfg.direction == "vertical" and cfg.width or cfg.height
toggleterm.exec(cmd, cfg.num, cfg.size or size, cfg.dir, cfg.direction, cfg.go_back, cfg.open)
Expand Down

0 comments on commit 6d7eef4

Please sign in to comment.