Files
test-samurai.nvim/tests/test_samurai_go_spec.lua

148 lines
4.3 KiB
Lua

local go_runner = require("test-samurai.runners.go")
local util = require("test-samurai.util")
describe("test-samurai go runner", function()
it("detects Go test files by suffix", function()
local bufnr1 = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr1, "/tmp/go_suffix_test.go")
assert.is_true(go_runner.is_test_file(bufnr1))
local bufnr2 = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr2, "/tmp/go_main.go")
assert.is_false(go_runner.is_test_file(bufnr2))
end)
it("finds subtest when cursor is inside t.Run block", function()
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr, "/tmp/go_subtest_test.go")
local lines = {
"package main",
"import \"testing\"",
"",
"func TestFoo(t *testing.T) {",
" t.Run(\"first\", func(t *testing.T) {",
" -- inside first",
" })",
"",
" t.Run(\"second\", func(t *testing.T) {",
" -- inside second",
" })",
"}",
}
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
local orig_fs_find = vim.fs.find
vim.fs.find = function(markers, opts)
return { "/tmp/go.mod" }
end
local row_inside_first = 5
local spec, err = go_runner.find_nearest(bufnr, row_inside_first, 0)
vim.fs.find = orig_fs_find
assert.is_nil(err)
assert.is_not_nil(spec)
assert.equals("TestFoo/first", spec.test_path)
assert.equals("subtest", spec.scope)
assert.is_true(spec.file:match("go_subtest_test%.go$") ~= nil)
assert.is_true(spec.cwd:match("tmp$") ~= nil)
end)
it("falls back to whole test function when between subtests", function()
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr, "/tmp/go_between_test.go")
local lines = {
"package main",
"import \"testing\"",
"",
"func TestFoo(t *testing.T) {",
" t.Run(\"first\", func(t *testing.T) {",
" -- inside first",
" })",
"",
" t.Run(\"second\", func(t *testing.T) {",
" -- inside second",
" })",
"}",
}
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
local orig_fs_find = vim.fs.find
vim.fs.find = function(markers, opts)
return { "/tmp/go.mod" }
end
local row_between = 7
local spec, err = go_runner.find_nearest(bufnr, row_between, 0)
vim.fs.find = orig_fs_find
assert.is_nil(err)
assert.is_not_nil(spec)
assert.equals("TestFoo", spec.test_path)
assert.equals("function", spec.scope)
assert.is_true(spec.file:match("go_between_test%.go$") ~= nil)
assert.is_true(spec.cwd:match("tmp$") ~= nil)
end)
it("build_command uses current package and correct run pattern", function()
local spec_sub = {
file = "/tmp/project/pkg/foo_test.go",
cwd = "/tmp/project",
test_path = "TestFoo/first",
scope = "subtest",
}
local cmd_spec_sub = go_runner.build_command(spec_sub)
assert.are.same(
{ "go", "test", "-json", "./pkg", "-run", "^TestFoo/first$" },
cmd_spec_sub.cmd
)
local spec_func = {
file = "/tmp/project/foo_test.go",
cwd = "/tmp/project",
test_path = "TestFoo",
scope = "function",
}
local cmd_spec_func = go_runner.build_command(spec_func)
assert.are.same(
{ "go", "test", "-json", "./", "-run", "^TestFoo($|/)" },
cmd_spec_func.cmd
)
end)
it("build_file_command uses exact test names from current file", function()
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr, "/tmp/project/get_test.go")
local lines = {
"package main",
"import \"testing\"",
"",
"func TestHandleGet(t *testing.T) {",
" t.Run(\"returns_200\", func(t *testing.T) {",
" -- inside test",
" })",
"}",
}
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
local orig_find_root = util.find_root
util.find_root = function(path, markers)
return "/tmp/project"
end
local cmd_spec = go_runner.build_file_command(bufnr)
util.find_root = orig_find_root
assert.are.same(
{ "go", "test", "-json", "./", "-run", "^(TestHandleGet)$" },
cmd_spec.cmd
)
assert.equals("/tmp/project", cmd_spec.cwd)
end)
end)