Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions lib/ruby_lsp/requests/go_to_relevant_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ def perform

#: -> Array[String]
def find_relevant_paths
pattern = File.join(search_root, "**", relevant_filename_pattern)
candidate_paths = Dir.glob(pattern)
patterns = relevant_filename_patterns

candidate_paths = patterns.flat_map do |pattern|
Dir.glob(File.join(search_root, "**", pattern))
end

return [] if candidate_paths.empty?

Expand Down Expand Up @@ -77,18 +80,33 @@ def search_root
"."
end

#: -> String
def relevant_filename_pattern
input_basename = File.basename(@path, File.extname(@path))

relevant_basename_pattern =
if input_basename.match?(TEST_PATTERN)
input_basename.gsub(TEST_PATTERN, "")
#: -> Array[String]
def relevant_filename_patterns
extension = File.extname(@path)
input_basename = File.basename(@path, extension)

if input_basename.match?(TEST_PATTERN)
# Test file -> find implementation
base = input_basename.gsub(TEST_PATTERN, "")
parent_dir = File.basename(File.dirname(@path))

# If test file is in a directory matching the implementation name
# (e.g., go_to_relevant_file/test_go_to_relevant_file_a.rb)
# return patterns for both the base file name and the parent directory name
if base.include?(parent_dir) && base != parent_dir
["#{base}#{extension}", "#{parent_dir}#{extension}"]
else
"{{#{TEST_PREFIX_GLOB}}#{input_basename},#{input_basename}{#{TEST_SUFFIX_GLOB}}}"
["#{base}#{extension}"]
end

"#{relevant_basename_pattern}#{File.extname(@path)}"
else
# Implementation file -> find tests (including in matching directory)
[
"{#{TEST_PREFIX_GLOB}}#{input_basename}#{extension}",
"#{input_basename}{#{TEST_SUFFIX_GLOB}}#{extension}",
"#{input_basename}/{#{TEST_PREFIX_GLOB}}*#{extension}",
"#{input_basename}/*{#{TEST_SUFFIX_GLOB}}#{extension}",
]
end
end

# Using the Jaccard algorithm to determine the similarity between the
Expand Down
47 changes: 47 additions & 0 deletions test/requests/go_to_relevant_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,51 @@ def test_search_within_implementation_test_root
)
end
end

def test_finds_tests_in_matching_subdirectory
Dir.chdir(@workspace) do
lib_dir = File.join(@workspace, "lib")
test_root = File.join(@workspace, "test")
test_subdir = File.join(test_root, "user")

FileUtils.mkdir_p(lib_dir)
FileUtils.mkdir_p(test_subdir)

impl_file = File.join(lib_dir, "user.rb")
test_file1 = File.join(test_subdir, "create_user_test.rb")
test_file2 = File.join(test_subdir, "test_update_user.rb")

FileUtils.touch(impl_file)
FileUtils.touch(test_file1)
FileUtils.touch(test_file2)

result = RubyLsp::Requests::GoToRelevantFile.new(impl_file, @workspace).perform

assert_equal(
[test_file1, test_file2].sort,
result.sort,
)
end
end

def test_finds_implementation_from_nested_test_file
Dir.chdir(@workspace) do
lib_dir = File.join(@workspace, "lib")
test_root = File.join(@workspace, "test")
test_subdir = File.join(test_root, "go_to_relevant_file")

FileUtils.mkdir_p(lib_dir)
FileUtils.mkdir_p(test_subdir)

impl_file = File.join(lib_dir, "go_to_relevant_file.rb")
test_file = File.join(test_subdir, "go_to_relevant_file_a_test.rb")

FileUtils.touch(impl_file)
FileUtils.touch(test_file)

result = RubyLsp::Requests::GoToRelevantFile.new(test_file, @workspace).perform

assert_equal([impl_file], result)
end
end
end
Loading