Add Lua test helpers
This commit is contained in:
parent
528d4575aa
commit
c76f2ca656
106
nvimrc
106
nvimrc
|
@ -142,6 +142,12 @@ nnoremap <leader>s :split<cr>
|
||||||
nnoremap <leader>rp "_dd<bar>P
|
nnoremap <leader>rp "_dd<bar>P
|
||||||
|
|
||||||
lua <<EOF
|
lua <<EOF
|
||||||
|
function _G.dump(...)
|
||||||
|
local objects = vim.tbl_map(vim.inspect, {...})
|
||||||
|
print(unpack(objects))
|
||||||
|
return ...
|
||||||
|
end
|
||||||
|
|
||||||
function _G.insert_uuid()
|
function _G.insert_uuid()
|
||||||
local uuid = vim.call("system", "uuidprint")
|
local uuid = vim.call("system", "uuidprint")
|
||||||
vim.call("setreg", "u", uuid)
|
vim.call("setreg", "u", uuid)
|
||||||
|
@ -151,26 +157,108 @@ end
|
||||||
vim.api.nvim_set_keymap("n", "<leader>uu", [[:lua _G.insert_uuid()<cr>]], {noremap = true, silent = true})
|
vim.api.nvim_set_keymap("n", "<leader>uu", [[:lua _G.insert_uuid()<cr>]], {noremap = true, silent = true})
|
||||||
vim.api.nvim_set_keymap("i", "<c-d><c-u>", [[<esc>:lua _G.insert_uuid()<cr>a]], {noremap = true, silent = true})
|
vim.api.nvim_set_keymap("i", "<c-d><c-u>", [[<esc>:lua _G.insert_uuid()<cr>a]], {noremap = true, silent = true})
|
||||||
|
|
||||||
_G.run_tests = function()
|
_G._test_cmd_to_wins = {}
|
||||||
local bufnr = vim.call("bufnr", "%")
|
|
||||||
local filetype = vim.call("getbufvar", bufnr, "&filetype", "ERROR")
|
|
||||||
|
|
||||||
|
_G._build_test_cmd = function()
|
||||||
|
local bufnr = vim.call("bufnr", "%")
|
||||||
|
local filetype = vim.call("getbufvar", bufnr, "&filetype")
|
||||||
if filetype == "" then
|
if filetype == "" then
|
||||||
print("cannot run tests for filetype:", filetype)
|
print("cannot build test command for filetype:", filetype)
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local path = vim.call("expand", "%:p")
|
||||||
|
|
||||||
|
-- TODO: allow line number to be passed
|
||||||
|
-- TODO: check file exists before running command
|
||||||
|
if filetype == "ruby" then
|
||||||
|
if not path:find("_spec.rb") then
|
||||||
|
path = path:gsub("/app/", "/spec/"):gsub([[.rb$]], "_spec.rb")
|
||||||
|
end
|
||||||
|
|
||||||
|
return "bundle exec rspec --format=progress " .. path
|
||||||
|
elseif filetype == "go" then
|
||||||
|
return "go test " .. path:gsub("^(.*)/(.*go)$", "%1/...")
|
||||||
|
else
|
||||||
|
print("filetype not supported:", filetype)
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
_G.run_tests = function()
|
||||||
|
local cmd = _G._build_test_cmd()
|
||||||
|
if cmd == nil then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if filetype == "ruby" then
|
vim.api.nvim_command([[silent up]])
|
||||||
local path = vim.call("expand", "%:p")
|
|
||||||
local altpath = path:gsub("/app/", "/spec/"):gsub([[.rb$]], "_spec.rb")
|
local winid = _G._test_cmd_to_wins[cmd]
|
||||||
vim.api.nvim_command([[10split +term\ bundle\ exec\ rspec\ ]] .. altpath)
|
if winid == nil or not vim.api.nvim_win_is_valid(winid) or not vim.call("win_gotoid", winid) == -1 then
|
||||||
|
vim.api.nvim_command([[10split]])
|
||||||
|
winid = vim.call("win_getid")
|
||||||
|
_G._test_cmd_to_wins[cmd] = winid
|
||||||
|
end
|
||||||
|
|
||||||
|
local buf = vim.api.nvim_create_buf(false, true)
|
||||||
|
vim.api.nvim_buf_set_option(buf, "bufhidden", "delete")
|
||||||
|
vim.api.nvim_win_set_buf(winid, buf)
|
||||||
|
|
||||||
|
vim.call("termopen", cmd)
|
||||||
vim.api.nvim_command([[normal! G]])
|
vim.api.nvim_command([[normal! G]])
|
||||||
vim.api.nvim_command([[wincmd p]])
|
vim.api.nvim_command([[wincmd p]])
|
||||||
|
end
|
||||||
|
|
||||||
|
_G.focus_tests = function()
|
||||||
|
local cmd = _G._build_test_cmd()
|
||||||
|
if cmd == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local winid = _G._test_cmd_to_wins[cmd]
|
||||||
|
if winid == nil or not vim.api.nvim_win_is_valid(winid) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.call("win_gotoid", winid)
|
||||||
|
end
|
||||||
|
|
||||||
|
_G.close_tests = function()
|
||||||
|
local current_winid = vim.api.nvim_get_current_win()
|
||||||
|
for _, winid in pairs(_G._test_cmd_to_wins) do
|
||||||
|
if current_winid == winid and vim.api.nvim_win_is_valid(winid) then
|
||||||
|
vim.api.nvim_win_close(winid, false)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_set_keymap("n", "<leader>rt", [[:lua _G.run_tests()<cr>]], {noremap = true, silent = true})
|
local cmd = _G._build_test_cmd()
|
||||||
|
if cmd == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local winid = _G._test_cmd_to_wins[cmd]
|
||||||
|
if winid == nil or not vim.api.nvim_win_is_valid(winid) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_win_close(winid, false)
|
||||||
|
end
|
||||||
|
|
||||||
|
_G.copy_test_cmd = function()
|
||||||
|
local cmd = _G._build_test_cmd()
|
||||||
|
if cmd == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.call("setreg", "*", cmd)
|
||||||
|
print("Copied: " .. cmd)
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_set_keymap("n", "<leader>cr", [[:lua _G.run_tests()<cr>]], {noremap = true, silent = true})
|
||||||
|
vim.api.nvim_set_keymap("n", "<leader>cf", [[:lua _G.focus_tests()<cr>]], {noremap = true, silent = true})
|
||||||
|
vim.api.nvim_set_keymap("n", "<leader>cq", [[:lua _G.close_tests()<cr>]], {noremap = true, silent = true})
|
||||||
|
vim.api.nvim_set_keymap("n", "<leader>cc", [[:lua _G.copy_test_cmd()<cr>]], {noremap = true, silent = true})
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
" Git mappings
|
" Git mappings
|
||||||
|
|
Loading…
Reference in New Issue