Skip to content

Use regex to filter test runs for multiple classes in the same file #448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2024
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
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
Loading