refactor(tests): clean up everywhere

This commit is contained in:
Oleksandr Smirnov 2025-03-22 20:12:25 +02:00
parent 1cb507fa40
commit b2780c8efb
No known key found for this signature in database
6 changed files with 30 additions and 7 deletions

View file

@ -16,9 +16,7 @@ local function do_the_test(fixture, pos)
child.cmd "write" child.cmd "write"
t.eq(t.readfile(rs.tmp), rs.fixtures.output) t.eq(t.readfile(rs.tmp), rs.fixtures.output)
t.cleanup(rs)
-- without it all other(not even from this module) tests are falling
t.deletefile(rs.tmp)
end end
T["comment"] = MiniTest.new_set {} T["comment"] = MiniTest.new_set {}

View file

@ -25,6 +25,7 @@ T["gotests"]["should add test for function under cursor"] = function()
child.cmd "GoTestAdd" child.cmd "GoTestAdd"
t.eq(rs.fixtures.output, read_testfile(rs.tmp)) t.eq(rs.fixtures.output, read_testfile(rs.tmp))
t.cleanup(rs)
end end
T["gotests"]["should add test for method under cursor"] = function() T["gotests"]["should add test for method under cursor"] = function()
@ -32,6 +33,7 @@ T["gotests"]["should add test for method under cursor"] = function()
child.cmd "GoTestAdd" child.cmd "GoTestAdd"
t.eq(rs.fixtures.output, read_testfile(rs.tmp)) t.eq(rs.fixtures.output, read_testfile(rs.tmp))
t.cleanup(rs)
end end
return T return T

View file

@ -16,6 +16,7 @@ T["iferr"]["works"] = function()
child.cmd "write" child.cmd "write"
t.eq(t.readfile(rs.tmp), rs.fixtures.output) t.eq(t.readfile(rs.tmp), rs.fixtures.output)
t.cleanup(rs)
end end
T["iferr"]["works with custom message"] = function() T["iferr"]["works with custom message"] = function()
@ -29,6 +30,7 @@ T["iferr"]["works with custom message"] = function()
child.cmd "write" child.cmd "write"
t.eq(t.readfile(rs.tmp), rs.fixtures.output) t.eq(t.readfile(rs.tmp), rs.fixtures.output)
t.cleanup(rs)
end end
return T return T

View file

@ -18,6 +18,7 @@ T["impl"]["works w io.Writer"] = function()
-- NOTE: since "impl" won't implement interface if it's already implemented i went with this hack -- NOTE: since "impl" won't implement interface if it's already implemented i went with this hack
local rhs = rs.fixtures.output:gsub("Test2", "Test") local rhs = rs.fixtures.output:gsub("Test2", "Test")
t.eq(t.readfile(rs.tmp), rhs) t.eq(t.readfile(rs.tmp), rhs)
t.cleanup(rs)
end end
T["impl"]["works r Read io.Reader"] = function() T["impl"]["works r Read io.Reader"] = function()
@ -27,6 +28,7 @@ T["impl"]["works r Read io.Reader"] = function()
local rhs = rs.fixtures.output:gsub("Read2", "Read") local rhs = rs.fixtures.output:gsub("Read2", "Read")
t.eq(t.readfile(rs.tmp), rhs) t.eq(t.readfile(rs.tmp), rhs)
t.cleanup(rs)
end end
T["impl"]["works io.Closer"] = function() T["impl"]["works io.Closer"] = function()
@ -36,6 +38,7 @@ T["impl"]["works io.Closer"] = function()
local rhs = rs.fixtures.output:gsub("Test2", "Test") local rhs = rs.fixtures.output:gsub("Test2", "Test")
t.eq(t.readfile(rs.tmp), rhs) t.eq(t.readfile(rs.tmp), rhs)
t.cleanup(rs)
end end
return T return T

View file

@ -16,6 +16,7 @@ T["struct_tags"]["should add tag"] = function()
child.cmd "write" child.cmd "write"
t.eq(t.readfile(rs.tmp), rs.fixtures.output) t.eq(t.readfile(rs.tmp), rs.fixtures.output)
t.cleanup(rs)
end end
T["struct_tags"]["should remove tag"] = function() T["struct_tags"]["should remove tag"] = function()
@ -24,6 +25,7 @@ T["struct_tags"]["should remove tag"] = function()
child.cmd "write" child.cmd "write"
t.eq(t.readfile(rs.tmp), rs.fixtures.output) t.eq(t.readfile(rs.tmp), rs.fixtures.output)
t.cleanup(rs)
end end
T["struct_tags"]["should be able to handle many structs"] = function() T["struct_tags"]["should be able to handle many structs"] = function()
@ -32,6 +34,7 @@ T["struct_tags"]["should be able to handle many structs"] = function()
child.cmd "write" child.cmd "write"
t.eq(t.readfile(rs.tmp), rs.fixtures.output) t.eq(t.readfile(rs.tmp), rs.fixtures.output)
t.cleanup(rs)
end end
T["struct_tags"]["should clear struct"] = function() T["struct_tags"]["should clear struct"] = function()
@ -40,6 +43,7 @@ T["struct_tags"]["should clear struct"] = function()
child.cmd "write" child.cmd "write"
t.eq(t.readfile(rs.tmp), rs.fixtures.output) t.eq(t.readfile(rs.tmp), rs.fixtures.output)
t.cleanup(rs)
end end
T["struct_tags"]["should add more than one tag"] = function() T["struct_tags"]["should add more than one tag"] = function()
@ -60,6 +64,9 @@ T["struct_tags"]["should add more than one tag"] = function()
child.cmd "write" child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output) t.eq(t.readfile(tmp), fixtures.output)
---@diagnostic disable-next-line:missing-fields
t.cleanup { tmp = tmp }
end end
T["struct_tags"]["should add tags on var"] = function() T["struct_tags"]["should add tags on var"] = function()
@ -68,6 +75,7 @@ T["struct_tags"]["should add tags on var"] = function()
child.cmd "write" child.cmd "write"
t.eq(t.readfile(rs.tmp), rs.fixtures.output) t.eq(t.readfile(rs.tmp), rs.fixtures.output)
t.cleanup(rs)
end end
T["struct_tags"]["should add tags on short declr var"] = function() T["struct_tags"]["should add tags on short declr var"] = function()
@ -76,6 +84,7 @@ T["struct_tags"]["should add tags on short declr var"] = function()
child.cmd "write" child.cmd "write"
t.eq(t.readfile(rs.tmp), rs.fixtures.output) t.eq(t.readfile(rs.tmp), rs.fixtures.output)
t.cleanup(rs)
end end
return T return T

View file

@ -36,8 +36,12 @@ function testutils.deletefile(fpath)
vim.fn.delete(fpath) vim.fn.delete(fpath)
end end
---@class gopher.TestUtilsFixtures
---@field input string
---@field output string
---@param fixture string ---@param fixture string
---@return {input: string, output: string} ---@return gopher.TestUtilsFixtures
function testutils.get_fixtures(fixture) function testutils.get_fixtures(fixture)
return { return {
input = testutils.readfile(vim.fs.joinpath(testutils.fixtures_dir, fixture) .. "_input.go"), input = testutils.readfile(vim.fs.joinpath(testutils.fixtures_dir, fixture) .. "_input.go"),
@ -45,10 +49,14 @@ function testutils.get_fixtures(fixture)
} }
end end
---@class gopher.TestUtilsSetup
---@field tmp string
---@field fixtures gopher.TestUtilsFixtures
---@param fixture string ---@param fixture string
---@param child MiniTest.child ---@param child MiniTest.child
---@param pos? number[] ---@param pos? number[]
---@return {tmp: string, fixtures: {input: string, output: string}} ---@return gopher.TestUtilsSetup
function testutils.setup(fixture, child, pos) function testutils.setup(fixture, child, pos)
local tmp = testutils.tmpfile() local tmp = testutils.tmpfile()
local fixtures = testutils.get_fixtures(fixture) local fixtures = testutils.get_fixtures(fixture)
@ -66,8 +74,9 @@ function testutils.setup(fixture, child, pos)
} }
end end
function testutils.cleanup(tmp) ---@param inp gopher.TestUtilsSetup
testutils.deletefile(tmp) function testutils.cleanup(inp)
testutils.deletefile(inp.tmp)
end end
return testutils return testutils