diff --git a/lua/go_to_test_file.lua b/lua/go_to_test_file.lua index 4e53e22..eb8d50b 100644 --- a/lua/go_to_test_file.lua +++ b/lua/go_to_test_file.lua @@ -102,16 +102,18 @@ go_to_test_file.find_test_or_source_file = function(git_root, current_file_abs_p else local test_folder_path = project.test_path_from_filepath(current_file_abs_path) local filename_no_ext = path.filename_no_ext(current_file_abs_path) - local match = list.match_one(test_folder_path, peer_dunder_tests.test_folder_names, ps, '/?$') - if match and match ~= '' then + local matches_test_folder = list.match_one(test_folder_path, peer_dunder_tests.test_folder_names, ps, '/?$') + if matches_test_folder and matches_test_folder ~= '' then local source_folder = path.dirname(test_folder_path) local filename = path.basename(current_file_abs_path) return {peer_dunder_tests.find_source_file(source_folder, filename), source_folder} elseif test_folder_path ~= '' then local project_root = root_tests.project_root_from_test_folder(test_folder_path) local test_foldername = path.basename(test_folder_path) + local test_file_dir = path.dirname(current_file_abs_path) + local path_in_test_folder = path.difference_between_ancestor_folder_and_sub_folder(test_folder_path, test_file_dir) local test_filename_without_test_identifiers = project_generic.remove_test_file_name_identifiers(filename_no_ext) - return {root_tests.find_source_file(project_root, test_foldername, test_filename_without_test_identifiers), test_folder_path} + return {root_tests.find_source_file(project_root, test_foldername, path_in_test_folder, test_filename_without_test_identifiers), test_folder_path} else local source_folder = path.dirname(current_file_abs_path) local filename = path.basename(current_file_abs_path) diff --git a/lua/go_to_test_file/root_tests.lua b/lua/go_to_test_file/root_tests.lua index 2574b67..2e275b4 100644 --- a/lua/go_to_test_file/root_tests.lua +++ b/lua/go_to_test_file/root_tests.lua @@ -20,9 +20,13 @@ root_tests.test_path_from_filepath = function(current_file_with_abs_path) end end -root_tests.find_source_file = function(project_root_abs_path, test_foldername, test_filename_without_test_identifiers) +root_tests.find_source_file = function(project_root_abs_path, test_foldername, path_in_test_folder, test_filename_without_test_identifiers) local ps = path.separator(system.name) - local cmmd = cmd.cd_string(project_root_abs_path) .. " && fd -t f -E '" .. test_foldername .. "' '" .. test_filename_without_test_identifiers .. "' | head -1" + local file_with_path = test_filename_without_test_identifiers + if path_in_test_folder ~= '' then + file_with_path = path.join(ps, path_in_test_folder, test_filename_without_test_identifiers) + end + local cmmd = cmd.cd_string(project_root_abs_path) .. " && fd -p -t f -E '" .. test_foldername .. "' '" .. file_with_path .. "([^" .. ps .. "]|$)' | head -1" local relative_path = vim.fn.trim(vim.fn.system(cmmd)):gsub("^." .. ps, "") return path.join(ps, project_root_abs_path, relative_path) end diff --git a/spec/go_to_test_file/root_tests_spec.lua b/spec/go_to_test_file/root_tests_spec.lua index 57d04fa..0e986df 100644 --- a/spec/go_to_test_file/root_tests_spec.lua +++ b/spec/go_to_test_file/root_tests_spec.lua @@ -23,13 +23,17 @@ describe('root_tests', function() end) describe('find_source_file', function() it('returns the coressponding source file from a root test folder project', function() + local ps = path.separator(system.name()) local file_folder_abs_path = path.script_path(system.name) + local test_file_dir = file_folder_abs_path local cmmd = cmd.cd_string(file_folder_abs_path) .. ' && git rev-parse --show-toplevel' local project_root_abs_path = vim.fn.trim(vim.fn.system(cmmd)) local test_foldername = 'spec' local test_filename_without_test_identifiers = 'root_tests' - local actual = root_tests.find_source_file(project_root_abs_path, test_foldername, test_filename_without_test_identifiers) + local test_folder_path = path.join(ps, project_root_abs_path, test_foldername) + local path_in_test_folder = path.difference_between_ancestor_folder_and_sub_folder(test_folder_path, test_file_dir) + local actual = root_tests.find_source_file(project_root_abs_path, test_foldername, path_in_test_folder, test_filename_without_test_identifiers) local expected = path.join(path.separator(system.name), project_root_abs_path, 'lua', 'go_to_test_file', 'root_tests.lua') assert.are.equal(expected, actual) end)