Skip to content

Commit

Permalink
Use regex to filter test runs for multiple classes in the same file (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Sep 9, 2024
1 parent 0ae85ad commit 72df24d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/ruby_lsp/ruby_lsp_rails/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,14 @@ def on_class_node_enter(node)
class_name = node.constant_path.slice
superclass_name = node.superclass&.slice

# We need to use a stack because someone could define a nested class
# inside a controller. When we exit that nested class declaration, we are
# back in a controller context. This part is used in other places in the LSP
@constant_name_stack << [class_name, superclass_name]

if class_name.end_with?("Test")
command = "#{test_command} #{@path}"
fully_qualified_name = @constant_name_stack.map(&:first).join("::")
command = "#{test_command} #{@path} --name \"/#{Shellwords.escape(fully_qualified_name)}(#|::)/\""
add_test_code_lens(node, name: class_name, command: command, kind: :group)
@group_id_stack.push(@group_id)
@group_id += 1
Expand All @@ -149,11 +155,6 @@ def on_class_node_enter(node)
command = "#{migrate_command} VERSION=#{migration_version}"
add_migrate_code_lens(node, name: class_name, command: command)
end

# We need to use a stack because someone could define a nested class
# inside a controller. When we exit that nested class declaration, we are
# back in a controller context. This part is used in other places in the LSP
@constant_name_stack << [class_name, superclass_name]
end

sig { params(node: Prism::ClassNode).void }
Expand Down
17 changes: 17 additions & 0 deletions test/ruby_lsp_rails/code_lens_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,23 @@ def test_example
assert_match("Debug", response[5].command.title)
end

test "uses regex to filter test classes defined in the same file" do
response = generate_code_lens_for_source(<<~RUBY)
class FirstTest < ActiveSupport::TestCase
def test_example
end
end
class SecondTest < ActiveSupport::TestCase
def test_example
end
end
RUBY

assert_match("bin/rails test /fake.rb --name \"/FirstTest(#|::)/\"", response[0].command.arguments[2])
assert_match("bin/rails test /fake.rb --name \"/SecondTest(#|::)/\"", response[7].command.arguments[2])
end

test "assigns the correct hierarchy to test structure" do
response = generate_code_lens_for_source(<<~RUBY)
class Test < ActiveSupport::TestCase
Expand Down

0 comments on commit 72df24d

Please sign in to comment.