From ad97cd3095836f17c21b2aa99e7996d88f51b17b Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Thu, 20 Jun 2024 10:11:48 +0200 Subject: [PATCH] fix: go test pattern did not escape parenthesis --- lua/neotest-golang/convert.lua | 15 ++++++++++++++ lua/neotest-golang/runspec_test.lua | 8 +++----- tests/go/testname_spec.lua | 32 +++++++++++++++++++++++++---- tests/go/testname_test.go | 6 ++++++ 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/lua/neotest-golang/convert.lua b/lua/neotest-golang/convert.lua index 567509cc..185c181c 100644 --- a/lua/neotest-golang/convert.lua +++ b/lua/neotest-golang/convert.lua @@ -1,5 +1,20 @@ local M = {} +-- Converts the test name into a regexp-friendly pattern, for usage in +-- 'go test'. +---@param test_name string +---@return string +function M.to_gotest_regex_pattern(test_name) + local special_characters = { + "(", + ")", + } + for _, character in ipairs(special_characters) do + test_name = test_name:gsub("%" .. character, "\\" .. character) + end + return "^" .. test_name .. "$" +end + -- Converts the AST-detected Neotest node test name into the 'go test' command -- test name format. ---@param pos_id string diff --git a/lua/neotest-golang/runspec_test.lua b/lua/neotest-golang/runspec_test.lua index 0f2f62d0..c6b28c22 100644 --- a/lua/neotest-golang/runspec_test.lua +++ b/lua/neotest-golang/runspec_test.lua @@ -10,6 +10,8 @@ local M = {} function M.build(pos, strategy) --- @type string local test_name = convert.to_gotest_test_name(pos.id) + test_name = convert.to_gotest_regex_pattern(test_name) + --- @type string local test_folder_absolute_path = string.match(pos.path, "(.+)/") @@ -20,11 +22,7 @@ function M.build(pos, strategy) } --- @type table - local required_go_test_args = { - test_folder_absolute_path, - "-run", - "^" .. test_name .. "$", - } + local required_go_test_args = { test_folder_absolute_path, "-run", test_name } local combined_args = vim.list_extend( vim.deepcopy(options.get().go_test_args), diff --git a/tests/go/testname_spec.lua b/tests/go/testname_spec.lua index b71fe3ba..e878f93a 100644 --- a/tests/go/testname_spec.lua +++ b/tests/go/testname_spec.lua @@ -2,7 +2,7 @@ local nio = require("nio") local adapter = require("neotest-golang") local convert = require("neotest-golang.convert") -describe("Subtest name conversion", function() +describe("Neotest position to Go test name", function() -- Arrange local test_filepath = vim.loop.cwd() .. "/tests/go/testname_test.go" @@ -10,7 +10,7 @@ describe("Subtest name conversion", function() local tree = nio.tests.with_async_context(adapter.discover_positions, test_filepath) - it("Mixed case with space", function() + it("supports mixed case with space", function() local expected_subtest_name = '"Mixed case with space"' local expected_gotest_name = "TestNames/Mixed_case_with_space" @@ -27,7 +27,7 @@ describe("Subtest name conversion", function() ) end) - it("Special characters", function() + it("supports special characters", function() local expected_subtest_name = '"Period . comma , and apostrophy \' are ok to use"' local expected_gotest_name = @@ -46,7 +46,7 @@ describe("Subtest name conversion", function() ) end) - it("Brackets", function() + it("supports brackets", function() local expected_subtest_name = '"Brackets [1] (2) {3} are ok"' local expected_gotest_name = "TestNames/Brackets_[1]_(2)_{3}_are_ok" @@ -63,3 +63,27 @@ describe("Subtest name conversion", function() ) end) end) + +describe("Full Go test name conversion", function() + -- Arrange + local test_filepath = vim.loop.cwd() .. "/tests/go/testname_test.go" + + ---@type neotest.Tree + local tree = + nio.tests.with_async_context(adapter.discover_positions, test_filepath) + + it("escapes parenthesis", function() + local expected_subtest_name = '"Test(success)"' + local expected_gotest_name = "^TestNames/Test\\(success\\)$" + + -- Act + local pos = tree:node(7):data() + local test_name = convert.to_gotest_test_name(pos.id) + test_name = convert.to_gotest_regex_pattern(test_name) + + -- Assert + local actual_name = pos.name + assert.are.same(expected_subtest_name, actual_name) + assert.are.same(vim.inspect(expected_gotest_name), vim.inspect(test_name)) + end) +end) diff --git a/tests/go/testname_test.go b/tests/go/testname_test.go index 3d36a94c..926afecc 100644 --- a/tests/go/testname_test.go +++ b/tests/go/testname_test.go @@ -27,4 +27,10 @@ func TestNames(t *testing.T) { t.Fail() } }) + + t.Run("Test(success)", func(t *testing.T) { + if Add(1, 2) != 3 { + t.Fail() + } + }) }