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

@@ -95,7 +95,7 @@ describe("test-samurai go runner", function()
local cmd_spec_sub = go_runner.build_command(spec_sub)
assert.are.same(
{ "go", "test", "-v", "./pkg", "-run", "^TestFoo%/first$" },
{ "go", "test", "-v", "./pkg", "-run", "^TestFoo/first$" },
cmd_spec_sub.cmd
)
@@ -108,7 +108,7 @@ describe("test-samurai go runner", function()
local cmd_spec_func = go_runner.build_command(spec_func)
assert.are.same(
{ "go", "test", "-v", "./", "-run", "^TestFoo$|^TestFoo/" },
{ "go", "test", "-v", "./", "-run", "^TestFoo($|/)" },
cmd_spec_func.cmd
)
end)

View File

@@ -0,0 +1,126 @@
local test_samurai = require("test-samurai")
local core = require("test-samurai.core")
local function mkbuf(path, ft, lines)
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr, path)
vim.bo[bufnr].filetype = ft
if lines then
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
end
return bufnr
end
local function capture_jobstart()
local calls = {}
local orig = vim.fn.jobstart
vim.fn.jobstart = function(cmd, opts)
table.insert(calls, { cmd = cmd, opts = opts })
return 1
end
return calls, orig
end
describe("TSamLast", function()
before_each(function()
test_samurai.setup()
end)
it("reruns last Go command", function()
local calls, orig_jobstart = capture_jobstart()
local bufnr = mkbuf("/tmp/project/foo_test.go", "go", {
"package main",
"import \"testing\"",
"",
"func TestFoo(t *testing.T) {",
" t.Run(\"first\", func(t *testing.T) {",
" -- inside first",
" })",
"}",
})
vim.api.nvim_set_current_buf(bufnr)
vim.api.nvim_win_set_cursor(0, { 6, 0 })
core.run_nearest()
core.run_last()
vim.fn.jobstart = orig_jobstart
assert.equals(2, #calls)
assert.are.same({ "go", "test", "-v", "./", "-run", "^TestFoo/first$" }, calls[1].cmd)
assert.are.same(calls[1].cmd, calls[2].cmd)
assert.equals(calls[1].opts.cwd, calls[2].opts.cwd)
end)
it("reruns last JS command", function()
local calls, orig_jobstart = capture_jobstart()
local bufnr = mkbuf("/tmp/project/foo_last.test.ts", "typescript", {
'describe("outer", function() {',
' it("inner 1", function() {',
" -- inside 1",
" })",
"",
' it("inner 2", function() {',
" -- inside 2",
" })",
"})",
})
vim.api.nvim_set_current_buf(bufnr)
vim.api.nvim_win_set_cursor(0, { 7, 0 })
core.run_nearest()
core.run_last()
vim.fn.jobstart = orig_jobstart
assert.equals(2, #calls)
assert.are.same(
{ "npx", "jest", "/tmp/project/foo_last.test.ts", "-t", "inner 2" },
calls[1].cmd
)
assert.are.same(calls[1].cmd, calls[2].cmd)
assert.equals(calls[1].opts.cwd, calls[2].opts.cwd)
end)
it("reruns last Lua command", function()
local calls, orig_jobstart = capture_jobstart()
local bufnr = mkbuf("/tmp/project/foo_last_spec.lua", "lua", {
"describe('outer', function()",
" it('inner 1', function()",
" local x = 1",
" end)",
"",
" it('inner 2', function()",
" local y = 2",
" end)",
"end)",
})
vim.api.nvim_set_current_buf(bufnr)
vim.api.nvim_win_set_cursor(0, { 7, 0 })
core.run_nearest()
core.run_last()
vim.fn.jobstart = orig_jobstart
assert.equals(2, #calls)
assert.are.same({
"nvim",
"--headless",
"-u",
"/tmp/project/tests/minimal_init.lua",
"-c",
'PlenaryBustedFile /tmp/project/foo_last_spec.lua { busted_args = { "--filter", "inner 2" } }',
"-c",
"qa",
}, calls[1].cmd)
assert.are.same(calls[1].cmd, calls[2].cmd)
assert.equals(calls[1].opts.cwd, calls[2].opts.cwd)
end)
end)

View File

@@ -61,4 +61,19 @@ describe("test-samurai public API", function()
assert.is_true(called)
end)
it("delegates test_last to core.run_last", function()
local called = false
local orig = core.run_last
core.run_last = function()
called = true
end
test_samurai.test_last()
core.run_last = orig
assert.is_true(called)
end)
end)