From 1bc720a4a36a9e450d02ee7cd3b37ea7d86b7fbf Mon Sep 17 00:00:00 2001 From: Sergii Getman Date: Sun, 11 Feb 2024 19:43:59 +0000 Subject: [PATCH] Remove quotes from nested tests names (#76) --- lua/neotest-go/init.lua | 8 +++-- lua/spec/neotest-go/init_spec.lua | 45 +++++++++++++++++++++++++++ neotest_go/three_level_nested_test.go | 20 ++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 neotest_go/three_level_nested_test.go diff --git a/lua/neotest-go/init.lua b/lua/neotest-go/init.lua index 8c97dc4..3fd561a 100644 --- a/lua/neotest-go/init.lua +++ b/lua/neotest-go/init.lua @@ -234,20 +234,22 @@ function adapter.prepare_results(tree, lines, go_root, go_module) } file_id = value.id else - local normalized_id = utils.normalize_id(value.id, go_root, go_module) + -- to remove quotes, for example three_level_nested_test.go::TestOdd::"odd"::5_is_odd + local value_id = value.id:gsub('%"', "") + local normalized_id = utils.normalize_id(value_id, go_root, go_module) local test_result = tests[normalized_id] -- file level node if test_result then local fname = async.fn.tempname() fn.writefile(test_result.output, fname) - results[value.id] = { + results[value_id] = { status = test_result.status, short = table.concat(test_result.output, ""), output = fname, } local errors = utils.get_errors_from_test(test_result, utils.get_filename_from_id(value.id)) if errors then - results[value.id].errors = errors + results[value_id].errors = errors end if test_result.status == test_statuses.fail and file_id then results[file_id].status = test_statuses.fail diff --git a/lua/spec/neotest-go/init_spec.lua b/lua/spec/neotest-go/init_spec.lua index a406c60..f020a1a 100644 --- a/lua/spec/neotest-go/init_spec.lua +++ b/lua/spec/neotest-go/init_spec.lua @@ -302,6 +302,51 @@ describe("prepare_results", function() end end ) + async.it( + "check that all nested results are in three_level_nested_test.go, quotes should be removed from keys", + -- three_level_nested_test.go::TestOdd::odd::5_is_odd + -- not three_level_nested_test.go::TestOdd::"odd"::5_is_odd + function() + local tests_folder = vim.loop.cwd() .. "/neotest_go" + local test_file = tests_folder .. "/three_level_nested_test.go" + local positions = plugin.discover_positions(test_file) + + local expected_keys = { + test_file, + test_file .. "::TestOdd", + test_file .. "::TestOdd::odd", + test_file .. "::TestOdd::odd::7_is_odd", + test_file .. "::TestOdd::odd::5_is_odd", + } + -- we should run test from module root + local command = { + "cd", + tests_folder, + "&&", + "go", + "test", + "-v", + "-json", + "", + "-count=1", + "-timeout=60s", + "./...", + } + local handle = io.popen(table.concat(command, " ")) + local result = handle:read("*a") + handle:close() + + local lines = {} + for s in result:gmatch("[^\r\n]+") do + table.insert(lines, s) + end + local processed_results = plugin.prepare_results(positions, lines, tests_folder, "neotest_go") + for _, v in pairs(expected_keys) do + assert.has_property(v, processed_results) + end + end + ) + async.it("check that we have correct file level test result status", function() local tests_folder = vim.loop.cwd() .. "/neotest_go" local test_cases = {} diff --git a/neotest_go/three_level_nested_test.go b/neotest_go/three_level_nested_test.go new file mode 100644 index 0000000..d6fd089 --- /dev/null +++ b/neotest_go/three_level_nested_test.go @@ -0,0 +1,20 @@ +package main + +import "testing" + +func TestOdd(t *testing.T) { + t.Run("odd", func(t *testing.T) { + t.Run("5 is odd", func(t *testing.T) { + if 5%2 != 1 { + t.Error("5 is actually odd") + } + }) + t.Run("7 is odd", func(t *testing.T) { + if 7%2 != 1 { + t.Error("7 is actually odd") + } + }) + + }) + +}