Skip to content

Commit

Permalink
improves root_tests.find_source_file by including path
Browse files Browse the repository at this point in the history
  • Loading branch information
jtzero committed Aug 8, 2024
1 parent 2864610 commit e3cfee6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
8 changes: 5 additions & 3 deletions lua/go_to_test_file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions lua/go_to_test_file/root_tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion spec/go_to_test_file/root_tests_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e3cfee6

Please sign in to comment.