add TSamLast command for reexecuting last running tests

This commit is contained in:
2025-12-25 14:30:34 +01:00
parent 51ec535eac
commit cbc3e201ae
8 changed files with 260 additions and 4 deletions

View File

@@ -7,6 +7,7 @@ local state = {
runners = {},
last_win = nil,
last_buf = nil,
last_command = nil,
autocmds_set = false,
}
@@ -50,6 +51,7 @@ end
function M.setup()
load_runners()
ensure_output_autocmds()
state.last_command = nil
end
function M.reload_runners()
@@ -298,6 +300,12 @@ local function run_cmd(cmd, cwd, handlers)
end
local function run_command(command)
if command and type(command.cmd) == "table" and #command.cmd > 0 then
state.last_command = {
cmd = vim.deepcopy(command.cmd),
cwd = command.cwd,
}
end
local cmd = command.cmd
local cwd = command.cwd or vim.loop.cwd()
@@ -350,6 +358,19 @@ local function run_command(command)
})
end
function M.run_last()
if not (state.last_command and type(state.last_command.cmd) == "table") then
vim.notify("[test-samurai] No previous test command", vim.log.levels.WARN)
return
end
local command = {
cmd = vim.deepcopy(state.last_command.cmd),
cwd = state.last_command.cwd,
}
run_command(command)
end
function M.run_nearest()
local bufnr = vim.api.nvim_get_current_buf()
local pos = vim.api.nvim_win_get_cursor(0)

View File

@@ -20,6 +20,10 @@ function M.test_all()
core.run_all()
end
function M.test_last()
core.run_last()
end
function M.show_output()
core.show_output()
end

View File

@@ -67,11 +67,16 @@ local function find_t_runs(lines, func)
return subtests
end
local function escape_go_regex(s)
s = s or ""
return (s:gsub("([\\.^$|()%%[%]{}*+?%-])", "\\\\%1"))
end
local function build_run_pattern(spec)
local name = spec.test_path or ""
local escaped = name:gsub("(%W)", "%%%1")
local escaped = escape_go_regex(name)
if spec.scope == "function" then
return "^" .. escaped .. "$|^" .. escaped .. "/"
return "^" .. escaped .. "($|/)"
else
return "^" .. escaped .. "$"
end