From 1bc720a4a36a9e450d02ee7cd3b37ea7d86b7fbf Mon Sep 17 00:00:00 2001 From: Sergii Getman Date: Sun, 11 Feb 2024 19:43:59 +0000 Subject: [PATCH 01/10] 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") + } + }) + + }) + +} From 89b601a1447ec2c54c8ce5331c2418134115d601 Mon Sep 17 00:00:00 2001 From: Sergii Getman Date: Sun, 11 Feb 2024 20:51:12 +0000 Subject: [PATCH 02/10] Remove quotes from normalised id (#77) --- lua/neotest-go/init.lua | 4 +- lua/spec/neotest-go/init_spec.lua | 77 +++++++++++++++---------------- 2 files changed, 38 insertions(+), 43 deletions(-) diff --git a/lua/neotest-go/init.lua b/lua/neotest-go/init.lua index 3fd561a..08e490f 100644 --- a/lua/neotest-go/init.lua +++ b/lua/neotest-go/init.lua @@ -242,14 +242,14 @@ function adapter.prepare_results(tree, lines, go_root, go_module) 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 f020a1a..80bfb75 100644 --- a/lua/spec/neotest-go/init_spec.lua +++ b/lua/spec/neotest-go/init_spec.lua @@ -302,50 +302,45 @@ 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) + async.it("check that all nested results are in three_level_nested_test.go", 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 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 + 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" From 6f8ddead0e771e5c0df8c38af86fa3df05a0fda0 Mon Sep 17 00:00:00 2001 From: Sergii Getman Date: Mon, 12 Feb 2024 11:36:04 +0000 Subject: [PATCH 03/10] Revert "Remove quotes from normalised id (#77)" (#78) This reverts commit 89b601a1447ec2c54c8ce5331c2418134115d601. --- lua/neotest-go/init.lua | 4 +- lua/spec/neotest-go/init_spec.lua | 77 ++++++++++++++++--------------- 2 files changed, 43 insertions(+), 38 deletions(-) diff --git a/lua/neotest-go/init.lua b/lua/neotest-go/init.lua index 08e490f..3fd561a 100644 --- a/lua/neotest-go/init.lua +++ b/lua/neotest-go/init.lua @@ -242,14 +242,14 @@ function adapter.prepare_results(tree, lines, go_root, go_module) 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 80bfb75..f020a1a 100644 --- a/lua/spec/neotest-go/init_spec.lua +++ b/lua/spec/neotest-go/init_spec.lua @@ -302,45 +302,50 @@ describe("prepare_results", function() end end ) - async.it("check that all nested results are in three_level_nested_test.go", 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) + 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 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) + 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 - end) + ) async.it("check that we have correct file level test result status", function() local tests_folder = vim.loop.cwd() .. "/neotest_go" From ba5d536304ed6971f00d16b48ec26997622ffb43 Mon Sep 17 00:00:00 2001 From: Sergii Getman Date: Mon, 12 Feb 2024 11:36:27 +0000 Subject: [PATCH 04/10] Revert "Remove quotes from nested tests names (#76)" (#79) This reverts commit 1bc720a4a36a9e450d02ee7cd3b37ea7d86b7fbf. --- 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, 3 insertions(+), 70 deletions(-) delete mode 100644 neotest_go/three_level_nested_test.go diff --git a/lua/neotest-go/init.lua b/lua/neotest-go/init.lua index 3fd561a..8c97dc4 100644 --- a/lua/neotest-go/init.lua +++ b/lua/neotest-go/init.lua @@ -234,22 +234,20 @@ function adapter.prepare_results(tree, lines, go_root, go_module) } file_id = value.id else - -- 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 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 f020a1a..a406c60 100644 --- a/lua/spec/neotest-go/init_spec.lua +++ b/lua/spec/neotest-go/init_spec.lua @@ -302,51 +302,6 @@ 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 deleted file mode 100644 index d6fd089..0000000 --- a/neotest_go/three_level_nested_test.go +++ /dev/null @@ -1,20 +0,0 @@ -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") - } - }) - - }) - -} From 97c2785d09edea0e444a1acddde1424ad9873fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Michal=C3=ADk?= Date: Tue, 20 Feb 2024 21:29:57 +0100 Subject: [PATCH 05/10] feat: use --run flag when executing single test Run the go test command with --run flag when executing single test. Support for neotest `type=test` was removed in https://github.com/nvim-neotest/neotest-go/pull/72 This is unfortunate but i believe in wisdom of the maintainers. Anyway i really need this functionality. Otherwise the plugin is unusable on projects with e2e/integration tests. I can't execute the whole suite each time, that is job for the CI. --- lua/neotest-go/init.lua | 8 ++++++++ lua/spec/neotest-go/init_spec.lua | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lua/neotest-go/init.lua b/lua/neotest-go/init.lua index 8c97dc4..4df0259 100644 --- a/lua/neotest-go/init.lua +++ b/lua/neotest-go/init.lua @@ -161,6 +161,12 @@ function adapter.build_spec(args) if fn.isdirectory(position.path) ~= 1 then location = fn.fnamemodify(position.path, ":h") end + + local run_flag = {} + if position.type == "test" then + run_flag = { "--run", "\\^" .. utils.get_prefix(args.tree, position.name) .. "\\$" } + end + local command = vim.tbl_flatten({ "cd", location, @@ -171,8 +177,10 @@ function adapter.build_spec(args) "-json", utils.get_build_tags(), vim.list_extend(get_args(), args.extra_args or {}), + run_flag, dir, }) + return { command = table.concat(command, " "), context = { diff --git a/lua/spec/neotest-go/init_spec.lua b/lua/spec/neotest-go/init_spec.lua index a406c60..1c8912d 100644 --- a/lua/spec/neotest-go/init_spec.lua +++ b/lua/spec/neotest-go/init_spec.lua @@ -355,6 +355,20 @@ describe("build_spec", function() assert.are.same(expected_command, result.command) assert.are.same(path, result.context.file) end) + async.it("build specification for single test", function() + local path = vim.loop.cwd() .. "/neotest_go/main_test.go" + local tree = plugin.discover_positions(path):children()[1] + + local args = { tree = tree } + local expected_command = "cd " + .. vim.loop.cwd() + .. "/neotest_go && go test -v -json -count=1 -timeout=60s --run \\^TestAddOne\\$ ./" + local result = plugin.build_spec(args) + assert.are.same(expected_command, result.command) + assert.are.same(path, result.context.file) + end) + +-- This test is overwriting plugin global state, keep it at end of the file or face the consequences ¯\_(ツ)_/¯ async.it("build specification for many_table_test.go recuresive run", function() local plugin_with_recursive_run = require("neotest-go")({ recursive_run = true }) local path = vim.loop.cwd() .. "/neotest_go/many_table_test.go" From dd03b1a0a703079196290f32265982aad9f94a25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Michal=C3=ADk?= Date: Sat, 24 Feb 2024 20:59:54 +0100 Subject: [PATCH 06/10] feat: build arbitraty deeply nested test names --- lua/neotest-go/init.lua | 4 ++-- lua/neotest-go/utils.lua | 21 ++++++++++++++------- lua/spec/neotest-go/init_spec.lua | 2 +- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lua/neotest-go/init.lua b/lua/neotest-go/init.lua index 4df0259..e2a03b2 100644 --- a/lua/neotest-go/init.lua +++ b/lua/neotest-go/init.lua @@ -108,7 +108,7 @@ function adapter.discover_positions(path) field: (field_identifier) @test.field.name1 (#eq? @test.field.name @test.field.name1)))))))) -;; query for map table tests +;; query for map table tests (block (short_var_declaration left: (expression_list @@ -164,7 +164,7 @@ function adapter.build_spec(args) local run_flag = {} if position.type == "test" then - run_flag = { "--run", "\\^" .. utils.get_prefix(args.tree, position.name) .. "\\$" } + run_flag = { "--run", "^" .. utils.get_prefix(args.tree) .. "$" } end local command = vim.tbl_flatten({ diff --git a/lua/neotest-go/utils.lua b/lua/neotest-go/utils.lua index 23beae8..546844b 100644 --- a/lua/neotest-go/utils.lua +++ b/lua/neotest-go/utils.lua @@ -157,15 +157,22 @@ function utils.get_errors_from_test(test, file_name) end ---@param tree neotest.Tree ----@param name string ---@return string -function utils.get_prefix(tree, name) - local parent_tree = tree:parent() - if not parent_tree or parent_tree:data().type == "file" then - return name +function utils.get_prefix(tree) + local parts = {} + + -- build test name for potentially deeply nested tests + while tree and tree:data().type == "test" do + local name = tree:data().name + name = name:gsub("[\"']", "") -- Remove quotes + name = name:gsub("%s", "_") -- Replace spaces with underscores + + table.insert(parts, 1, name) + + tree = tree:parent() end - local parent_name = parent_tree:data().name - return parent_name .. "/" .. name + + return table.concat(parts, "/") end return utils diff --git a/lua/spec/neotest-go/init_spec.lua b/lua/spec/neotest-go/init_spec.lua index 1c8912d..46c05d5 100644 --- a/lua/spec/neotest-go/init_spec.lua +++ b/lua/spec/neotest-go/init_spec.lua @@ -362,7 +362,7 @@ describe("build_spec", function() local args = { tree = tree } local expected_command = "cd " .. vim.loop.cwd() - .. "/neotest_go && go test -v -json -count=1 -timeout=60s --run \\^TestAddOne\\$ ./" + .. "/neotest_go && go test -v -json -count=1 -timeout=60s --run ^TestAddOne$ ./" local result = plugin.build_spec(args) assert.are.same(expected_command, result.command) assert.are.same(path, result.context.file) From b955680287aad3331a5193b6565845c3d977f9ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Michal=C3=ADk?= Date: Tue, 27 Feb 2024 19:38:04 +0100 Subject: [PATCH 07/10] chore: use single dash run flag --- lua/neotest-go/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/neotest-go/init.lua b/lua/neotest-go/init.lua index e2a03b2..ab0b5e3 100644 --- a/lua/neotest-go/init.lua +++ b/lua/neotest-go/init.lua @@ -164,7 +164,7 @@ function adapter.build_spec(args) local run_flag = {} if position.type == "test" then - run_flag = { "--run", "^" .. utils.get_prefix(args.tree) .. "$" } + run_flag = { "-run", "^" .. utils.get_prefix(args.tree) .. "$" } end local command = vim.tbl_flatten({ From 70a38abfaa7d20132318e2c438f3bbd643b8208a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Michal=C3=ADk?= Date: Tue, 27 Feb 2024 20:17:18 +0100 Subject: [PATCH 08/10] chore: test for nested test, test commands --- lua/spec/neotest-go/init_spec.lua | 51 +++++++++++++++++++++++++------ neotest_go/cases_test.go | 6 ++++ 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/lua/spec/neotest-go/init_spec.lua b/lua/spec/neotest-go/init_spec.lua index 46c05d5..7a87796 100644 --- a/lua/spec/neotest-go/init_spec.lua +++ b/lua/spec/neotest-go/init_spec.lua @@ -134,7 +134,7 @@ describe("discover_positions", function() id = vim.loop.cwd() .. "/neotest_go/cases_test.go", name = "cases_test.go", path = vim.loop.cwd() .. "/neotest_go/cases_test.go", - range = { 0, 0, 49, 0 }, + range = { 0, 0, 55, 0 }, type = "file", }, { @@ -169,7 +169,7 @@ describe("discover_positions", function() id = vim.loop.cwd() .. "/neotest_go/cases_test.go::TestAdd", name = "TestAdd", path = vim.loop.cwd() .. "/neotest_go/cases_test.go", - range = { 35, 0, 48, 1 }, + range = { 35, 0, 54, 1 }, type = "test", }, { @@ -190,6 +190,24 @@ describe("discover_positions", function() type = "test", }, }, + { + { + id = vim.loop.cwd() .. "/neotest_go/cases_test.go::TestAdd::test_three", + name = '"test three"', + path = vim.loop.cwd() .. "/neotest_go/cases_test.go", + range = { 44, 1, 48, 3 }, + type = "test", + }, + { + { + id = vim.loop.cwd() .. "/neotest_go/cases_test.go::TestAdd::\"test three\"::test_four", + name = '"test four"', + path = vim.loop.cwd() .. "/neotest_go/cases_test.go", + range = { 45, 2, 47, 4 }, + type = "test", + }, + }, + }, }, } @@ -334,7 +352,7 @@ describe("prepare_results", function() table.insert(lines, s) end local processed_results = - plugin.prepare_results(positions, lines, tests_folder, "neotest_go") + plugin.prepare_results(positions, lines, tests_folder, "neotest_go") assert.equals(test_result.status, processed_results[test_file].status) end) @@ -349,8 +367,8 @@ describe("build_spec", function() local args = { tree = tree } local expected_command = "cd " - .. vim.loop.cwd() - .. "/neotest_go && go test -v -json -count=1 -timeout=60s ./" + .. vim.loop.cwd() + .. "/neotest_go && go test -v -json -count=1 -timeout=60s ./" local result = plugin.build_spec(args) assert.are.same(expected_command, result.command) assert.are.same(path, result.context.file) @@ -361,14 +379,27 @@ describe("build_spec", function() local args = { tree = tree } local expected_command = "cd " - .. vim.loop.cwd() - .. "/neotest_go && go test -v -json -count=1 -timeout=60s --run ^TestAddOne$ ./" + .. vim.loop.cwd() + .. "/neotest_go && go test -v -json -count=1 -timeout=60s -run ^TestAddOne$ ./" local result = plugin.build_spec(args) assert.are.same(expected_command, result.command) assert.are.same(path, result.context.file) end) --- This test is overwriting plugin global state, keep it at end of the file or face the consequences ¯\_(ツ)_/¯ + async.it("build specification for single nested test", function() + local path = vim.loop.cwd() .. "/neotest_go/cases_test.go" + local tree = plugin.discover_positions(path) + local test_tree = tree:children()[2]:children()[3]:children()[1] + + local args = { tree = test_tree } + local expected_command = "cd " + .. vim.loop.cwd() + .. "/neotest_go && go test -v -json -count=1 -timeout=60s -run ^TestAdd/test_three/test_four$ ./" + local result = plugin.build_spec(args) + assert.are.same(expected_command, result.command) + assert.are.same(path, result.context.file) + end) + -- This test is overwriting plugin global state, keep it at end of the file or face the consequences ¯\_(ツ)_/¯ async.it("build specification for many_table_test.go recuresive run", function() local plugin_with_recursive_run = require("neotest-go")({ recursive_run = true }) local path = vim.loop.cwd() .. "/neotest_go/many_table_test.go" @@ -376,8 +407,8 @@ describe("build_spec", function() local args = { tree = tree } local expected_command = "cd " - .. vim.loop.cwd() - .. "/neotest_go && go test -v -json -count=1 -timeout=60s ./..." + .. vim.loop.cwd() + .. "/neotest_go && go test -v -json -count=1 -timeout=60s ./..." local result = plugin_with_recursive_run.build_spec(args) assert.are.same(expected_command, result.command) assert.are.same(path, result.context.file) diff --git a/neotest_go/cases_test.go b/neotest_go/cases_test.go index 7c39201..b9e47bf 100644 --- a/neotest_go/cases_test.go +++ b/neotest_go/cases_test.go @@ -42,6 +42,12 @@ func TestAdd(t *testing.T) { assert.Equal(t, 5, add(1, 2)) }) + t.Run("test three", func(t *testing.T) { + t.Run("test four", func(t *testing.T) { + assert.Equal(t, 3, add(1, 2)) + }) + }) + variable := "string" t.Run(variable, func(t *testing.T) { assert.Equal(t, 3, add(1, 2)) From 3760d921ed838888237c38c79e84a55f61aa7c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Michal=C3=ADk?= Date: Tue, 27 Feb 2024 20:24:59 +0100 Subject: [PATCH 09/10] chore: rename get_prefix to get_test_id --- lua/neotest-go/init.lua | 2 +- lua/neotest-go/utils.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/neotest-go/init.lua b/lua/neotest-go/init.lua index ab0b5e3..ea0ea52 100644 --- a/lua/neotest-go/init.lua +++ b/lua/neotest-go/init.lua @@ -164,7 +164,7 @@ function adapter.build_spec(args) local run_flag = {} if position.type == "test" then - run_flag = { "-run", "^" .. utils.get_prefix(args.tree) .. "$" } + run_flag = { "-run", "^" .. utils.get_test_id(args.tree) .. "$" } end local command = vim.tbl_flatten({ diff --git a/lua/neotest-go/utils.lua b/lua/neotest-go/utils.lua index 546844b..c10874a 100644 --- a/lua/neotest-go/utils.lua +++ b/lua/neotest-go/utils.lua @@ -158,7 +158,7 @@ end ---@param tree neotest.Tree ---@return string -function utils.get_prefix(tree) +function utils.get_test_id(tree) local parts = {} -- build test name for potentially deeply nested tests From be07d75d6831cebe252a9b5f9cc235b1307e1f7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Michal=C3=ADk?= Date: Mon, 29 Apr 2024 21:03:49 +0200 Subject: [PATCH 10/10] fix: stylua runned --- lua/neotest-go/utils.lua | 2 +- lua/spec/neotest-go/init_spec.lua | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lua/neotest-go/utils.lua b/lua/neotest-go/utils.lua index c10874a..aeac1ce 100644 --- a/lua/neotest-go/utils.lua +++ b/lua/neotest-go/utils.lua @@ -165,7 +165,7 @@ function utils.get_test_id(tree) while tree and tree:data().type == "test" do local name = tree:data().name name = name:gsub("[\"']", "") -- Remove quotes - name = name:gsub("%s", "_") -- Replace spaces with underscores + name = name:gsub("%s", "_") -- Replace spaces with underscores table.insert(parts, 1, name) diff --git a/lua/spec/neotest-go/init_spec.lua b/lua/spec/neotest-go/init_spec.lua index 7a87796..df11555 100644 --- a/lua/spec/neotest-go/init_spec.lua +++ b/lua/spec/neotest-go/init_spec.lua @@ -200,7 +200,7 @@ describe("discover_positions", function() }, { { - id = vim.loop.cwd() .. "/neotest_go/cases_test.go::TestAdd::\"test three\"::test_four", + id = vim.loop.cwd() .. '/neotest_go/cases_test.go::TestAdd::"test three"::test_four', name = '"test four"', path = vim.loop.cwd() .. "/neotest_go/cases_test.go", range = { 45, 2, 47, 4 }, @@ -352,7 +352,7 @@ describe("prepare_results", function() table.insert(lines, s) end local processed_results = - plugin.prepare_results(positions, lines, tests_folder, "neotest_go") + plugin.prepare_results(positions, lines, tests_folder, "neotest_go") assert.equals(test_result.status, processed_results[test_file].status) end) @@ -367,8 +367,8 @@ describe("build_spec", function() local args = { tree = tree } local expected_command = "cd " - .. vim.loop.cwd() - .. "/neotest_go && go test -v -json -count=1 -timeout=60s ./" + .. vim.loop.cwd() + .. "/neotest_go && go test -v -json -count=1 -timeout=60s ./" local result = plugin.build_spec(args) assert.are.same(expected_command, result.command) assert.are.same(path, result.context.file) @@ -379,8 +379,8 @@ describe("build_spec", function() local args = { tree = tree } local expected_command = "cd " - .. vim.loop.cwd() - .. "/neotest_go && go test -v -json -count=1 -timeout=60s -run ^TestAddOne$ ./" + .. vim.loop.cwd() + .. "/neotest_go && go test -v -json -count=1 -timeout=60s -run ^TestAddOne$ ./" local result = plugin.build_spec(args) assert.are.same(expected_command, result.command) assert.are.same(path, result.context.file) @@ -393,8 +393,8 @@ describe("build_spec", function() local args = { tree = test_tree } local expected_command = "cd " - .. vim.loop.cwd() - .. "/neotest_go && go test -v -json -count=1 -timeout=60s -run ^TestAdd/test_three/test_four$ ./" + .. vim.loop.cwd() + .. "/neotest_go && go test -v -json -count=1 -timeout=60s -run ^TestAdd/test_three/test_four$ ./" local result = plugin.build_spec(args) assert.are.same(expected_command, result.command) assert.are.same(path, result.context.file) @@ -407,8 +407,8 @@ describe("build_spec", function() local args = { tree = tree } local expected_command = "cd " - .. vim.loop.cwd() - .. "/neotest_go && go test -v -json -count=1 -timeout=60s ./..." + .. vim.loop.cwd() + .. "/neotest_go && go test -v -json -count=1 -timeout=60s ./..." local result = plugin_with_recursive_run.build_spec(args) assert.are.same(expected_command, result.command) assert.are.same(path, result.context.file)