Skip to content

Commit 2a0d13d

Browse files
committed
Support multiple test file matches in go to relevant file
1 parent 5802a0b commit 2a0d13d

File tree

2 files changed

+77
-12
lines changed

2 files changed

+77
-12
lines changed

lib/ruby_lsp/requests/go_to_relevant_file.rb

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ def perform
3535

3636
#: -> Array[String]
3737
def find_relevant_paths
38-
pattern = File.join(search_root, "**", relevant_filename_pattern)
39-
candidate_paths = Dir.glob(pattern)
38+
patterns = relevant_filename_patterns
39+
40+
candidate_paths = patterns.flat_map do |pattern|
41+
Dir.glob(File.join(search_root, "**", pattern))
42+
end
4043

4144
return [] if candidate_paths.empty?
4245

@@ -77,18 +80,33 @@ def search_root
7780
"."
7881
end
7982

80-
#: -> String
81-
def relevant_filename_pattern
82-
input_basename = File.basename(@path, File.extname(@path))
83-
84-
relevant_basename_pattern =
85-
if input_basename.match?(TEST_PATTERN)
86-
input_basename.gsub(TEST_PATTERN, "")
83+
#: -> Array[String]
84+
def relevant_filename_patterns
85+
extension = File.extname(@path)
86+
input_basename = File.basename(@path, extension)
87+
88+
if input_basename.match?(TEST_PATTERN)
89+
# Test file -> find implementation
90+
base = input_basename.gsub(TEST_PATTERN, "")
91+
parent_dir = File.basename(File.dirname(@path))
92+
93+
# If test file is in a directory matching the implementation name
94+
# (e.g., go_to_relevant_file/test_go_to_relevant_file_a.rb)
95+
# return patterns for both the base file name and the parent directory name
96+
if base.include?(parent_dir) && base != parent_dir
97+
["#{base}#{extension}", "#{parent_dir}#{extension}"]
8798
else
88-
"{{#{TEST_PREFIX_GLOB}}#{input_basename},#{input_basename}{#{TEST_SUFFIX_GLOB}}}"
99+
["#{base}#{extension}"]
89100
end
90-
91-
"#{relevant_basename_pattern}#{File.extname(@path)}"
101+
else
102+
# Implementation file -> find tests (including in matching directory)
103+
[
104+
"{#{TEST_PREFIX_GLOB}}#{input_basename}#{extension}",
105+
"#{input_basename}{#{TEST_SUFFIX_GLOB}}#{extension}",
106+
"#{input_basename}/{#{TEST_PREFIX_GLOB}}*#{extension}",
107+
"#{input_basename}/*{#{TEST_SUFFIX_GLOB}}#{extension}",
108+
]
109+
end
92110
end
93111

94112
# Using the Jaccard algorithm to determine the similarity between the

test/requests/go_to_relevant_file_test.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,51 @@ def test_search_within_implementation_test_root
117117
)
118118
end
119119
end
120+
121+
def test_finds_tests_in_matching_subdirectory
122+
Dir.chdir(@workspace) do
123+
lib_dir = File.join(@workspace, "lib")
124+
test_root = File.join(@workspace, "test")
125+
test_subdir = File.join(test_root, "user")
126+
127+
FileUtils.mkdir_p(lib_dir)
128+
FileUtils.mkdir_p(test_subdir)
129+
130+
impl_file = File.join(lib_dir, "user.rb")
131+
test_file1 = File.join(test_subdir, "create_user_test.rb")
132+
test_file2 = File.join(test_subdir, "test_update_user.rb")
133+
134+
FileUtils.touch(impl_file)
135+
FileUtils.touch(test_file1)
136+
FileUtils.touch(test_file2)
137+
138+
result = RubyLsp::Requests::GoToRelevantFile.new(impl_file, @workspace).perform
139+
140+
assert_equal(
141+
[test_file1, test_file2].sort,
142+
result.sort,
143+
)
144+
end
145+
end
146+
147+
def test_finds_implementation_from_nested_test_file
148+
Dir.chdir(@workspace) do
149+
lib_dir = File.join(@workspace, "lib")
150+
test_root = File.join(@workspace, "test")
151+
test_subdir = File.join(test_root, "go_to_relevant_file")
152+
153+
FileUtils.mkdir_p(lib_dir)
154+
FileUtils.mkdir_p(test_subdir)
155+
156+
impl_file = File.join(lib_dir, "go_to_relevant_file.rb")
157+
test_file = File.join(test_subdir, "go_to_relevant_file_a_test.rb")
158+
159+
FileUtils.touch(impl_file)
160+
FileUtils.touch(test_file)
161+
162+
result = RubyLsp::Requests::GoToRelevantFile.new(test_file, @workspace).perform
163+
164+
assert_equal([impl_file], result)
165+
end
166+
end
120167
end

0 commit comments

Comments
 (0)