From 9f3ee54f6c66b722021593c7572a4e3e986c4d29 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Sat, 21 Sep 2024 15:24:47 +0200 Subject: [PATCH] refactor: distinguish between path and package --- lua/neotest-golang/lib/cmd.lua | 3 +- lua/neotest-golang/options.lua | 36 ++++++++++++++++++------ lua/neotest-golang/runspec/dir.lua | 8 ++++-- lua/neotest-golang/runspec/file.lua | 14 ++++++--- lua/neotest-golang/runspec/namespace.lua | 2 +- lua/neotest-golang/runspec/test.lua | 2 +- 6 files changed, 48 insertions(+), 17 deletions(-) diff --git a/lua/neotest-golang/lib/cmd.lua b/lua/neotest-golang/lib/cmd.lua index cb3f1610..f1ef3662 100644 --- a/lua/neotest-golang/lib/cmd.lua +++ b/lua/neotest-golang/lib/cmd.lua @@ -45,7 +45,8 @@ function M.golist_command() end --- @class TestCommandData ---- @field package_or_path string The Go package or the full path to test. +--- @field absolute_folder_path string | nil The absolute folder path to the test file. +--- @field package_name string | nil The Go package name. --- @field position neotest.Position The position of the test. --- @field regexp string | nil The regular expression to filter tests. diff --git a/lua/neotest-golang/options.lua b/lua/neotest-golang/options.lua index 0faf3239..1780fe74 100644 --- a/lua/neotest-golang/options.lua +++ b/lua/neotest-golang/options.lua @@ -31,11 +31,21 @@ local default_runners = { go_test_args = go_test_args() end local required_go_test_args = {} - if cmd_data.regexp ~= nil then + if + cmd_data.position.type == "test" + or cmd_data.position.type == "namespace" + then required_go_test_args = - { cmd_data.package_or_path, "-run", cmd_data.regexp } - else - required_go_test_args = { cmd_data.package_or_path } + { cmd_data.absolute_folder_path, "-run", cmd_data.regexp } + elseif cmd_data.position.type == "file" then + if cmd_data.regexp ~= nil then + required_go_test_args = + { cmd_data.package_name, "-run", cmd_data.regexp } + else + required_go_test_args = { cmd_data.package_name } + end + elseif cmd_data.position.type == "dir" then + required_go_test_args = { cmd_data.package_name } end cmd = vim.list_extend(vim.deepcopy(cmd), go_test_args) cmd = vim.list_extend(vim.deepcopy(cmd), required_go_test_args) @@ -55,11 +65,21 @@ local default_runners = { go_test_args = go_test_args() end local required_go_test_args = {} - if cmd_data.regexp ~= nil then + if + cmd_data.position.type == "test" + or cmd_data.position.type == "namespace" + then required_go_test_args = - { cmd_data.package_or_path, "-run", cmd_data.regexp } - else - required_go_test_args = { cmd_data.package_or_path } + { cmd_data.absolute_folder_path, "-run", cmd_data.regexp } + elseif cmd_data.position.type == "file" then + if cmd_data.regexp ~= nil then + required_go_test_args = + { cmd_data.package_name, "-run", cmd_data.regexp } + else + required_go_test_args = { cmd_data.package_name } + end + elseif cmd_data.position.type == "dir" then + required_go_test_args = { cmd_data.package_name } end cmd = vim.list_extend(vim.deepcopy(cmd), gotestsum_args) cmd = vim.list_extend(vim.deepcopy(cmd), { "--" }) diff --git a/lua/neotest-golang/runspec/dir.lua b/lua/neotest-golang/runspec/dir.lua index 8a06231f..f42610c4 100644 --- a/lua/neotest-golang/runspec/dir.lua +++ b/lua/neotest-golang/runspec/dir.lua @@ -47,8 +47,12 @@ function M.build(pos) end end - local cmd_data = - { package_or_path = package_name, position = pos, regexp = nil } + local cmd_data = { + package_name = package_name, + position = pos, + regexp = nil, + } + local test_cmd, json_filepath = lib.cmd.test_command(cmd_data) --- @type RunspecContext diff --git a/lua/neotest-golang/runspec/file.lua b/lua/neotest-golang/runspec/file.lua index 8b5c6571..61254026 100644 --- a/lua/neotest-golang/runspec/file.lua +++ b/lua/neotest-golang/runspec/file.lua @@ -55,13 +55,19 @@ function M.build(pos, tree) local json_filepath = nil local regexp = M.get_regexp(pos.path) if regexp ~= nil then - local cmd_data = - { package_or_path = package_name, position = pos, regexp = regexp } + local cmd_data = { + package_name = package_name, + position = pos, + regexp = regexp, + } test_cmd, json_filepath = lib.cmd.test_command(cmd_data) else -- fallback: run all tests in the package - local cmd_data = - { package_or_path = package_name, position = pos, regexp = nil } + local cmd_data = { + package_name = package_name, + position = pos, + regexp = nil, + } test_cmd, json_filepath = lib.cmd.test_command_in_package_with_regexp(cmd_data) -- NOTE: could also fall back to running on a per-test basis by using a bare return diff --git a/lua/neotest-golang/runspec/namespace.lua b/lua/neotest-golang/runspec/namespace.lua index 029bb058..3301f038 100644 --- a/lua/neotest-golang/runspec/namespace.lua +++ b/lua/neotest-golang/runspec/namespace.lua @@ -27,7 +27,7 @@ function M.build(pos) local test_name_regexp = lib.convert.to_gotest_regex_pattern(test_name) local cmd_data = { - package_or_path = test_folder_absolute_path, + absolute_folder_path = test_folder_absolute_path, position = pos, regexp = test_name_regexp, } diff --git a/lua/neotest-golang/runspec/test.lua b/lua/neotest-golang/runspec/test.lua index 6cb0b3ae..febcb83a 100644 --- a/lua/neotest-golang/runspec/test.lua +++ b/lua/neotest-golang/runspec/test.lua @@ -29,7 +29,7 @@ function M.build(pos, strategy) local test_name_regexp = lib.convert.to_gotest_regex_pattern(test_name) local cmd_data = { - package_or_path = test_folder_absolute_path, + absolute_folder_path = test_folder_absolute_path, position = pos, regexp = test_name_regexp, }