Skip to content

Commit 72df24d

Browse files
authored
Use regex to filter test runs for multiple classes in the same file (#448)
1 parent 0ae85ad commit 72df24d

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

lib/ruby_lsp/ruby_lsp_rails/code_lens.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,14 @@ def on_class_node_enter(node)
138138
class_name = node.constant_path.slice
139139
superclass_name = node.superclass&.slice
140140

141+
# We need to use a stack because someone could define a nested class
142+
# inside a controller. When we exit that nested class declaration, we are
143+
# back in a controller context. This part is used in other places in the LSP
144+
@constant_name_stack << [class_name, superclass_name]
145+
141146
if class_name.end_with?("Test")
142-
command = "#{test_command} #{@path}"
147+
fully_qualified_name = @constant_name_stack.map(&:first).join("::")
148+
command = "#{test_command} #{@path} --name \"/#{Shellwords.escape(fully_qualified_name)}(#|::)/\""
143149
add_test_code_lens(node, name: class_name, command: command, kind: :group)
144150
@group_id_stack.push(@group_id)
145151
@group_id += 1
@@ -149,11 +155,6 @@ def on_class_node_enter(node)
149155
command = "#{migrate_command} VERSION=#{migration_version}"
150156
add_migrate_code_lens(node, name: class_name, command: command)
151157
end
152-
153-
# We need to use a stack because someone could define a nested class
154-
# inside a controller. When we exit that nested class declaration, we are
155-
# back in a controller context. This part is used in other places in the LSP
156-
@constant_name_stack << [class_name, superclass_name]
157158
end
158159

159160
sig { params(node: Prism::ClassNode).void }

test/ruby_lsp_rails/code_lens_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,23 @@ def test_example
150150
assert_match("Debug", response[5].command.title)
151151
end
152152

153+
test "uses regex to filter test classes defined in the same file" do
154+
response = generate_code_lens_for_source(<<~RUBY)
155+
class FirstTest < ActiveSupport::TestCase
156+
def test_example
157+
end
158+
end
159+
160+
class SecondTest < ActiveSupport::TestCase
161+
def test_example
162+
end
163+
end
164+
RUBY
165+
166+
assert_match("bin/rails test /fake.rb --name \"/FirstTest(#|::)/\"", response[0].command.arguments[2])
167+
assert_match("bin/rails test /fake.rb --name \"/SecondTest(#|::)/\"", response[7].command.arguments[2])
168+
end
169+
153170
test "assigns the correct hierarchy to test structure" do
154171
response = generate_code_lens_for_source(<<~RUBY)
155172
class Test < ActiveSupport::TestCase

0 commit comments

Comments
 (0)