Skip to content

Commit

Permalink
Don't push group id to stack when encountering non-test classes (#1891)
Browse files Browse the repository at this point in the history
Some tests use classes instead of modules for namespacing, such us:

```ruby
module Foo
  class Bar
    class MyTest < Minitest::Test
      def test_something
        assert true
      end
    end
  end
end
```

And when this happens, the current implementation would still push the
group id to the stack, which would cause the first test group (`MyTest`)
to already have non-nil group id. This will make the extension to think
there's another parent test group, which is not the case and will cause
no code lens to be shown.

This commit fixes this by not pushing the group id to the stack when
encountering non-test classes, and only pop the group id when the
encountered class is a test class.
  • Loading branch information
st0012 committed Apr 5, 2024
1 parent 971ba89 commit ee5f44d
Show file tree
Hide file tree
Showing 3 changed files with 284 additions and 78 deletions.
13 changes: 9 additions & 4 deletions lib/ruby_lsp/listeners/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,22 @@ def on_class_node_enter(node)
command: generate_test_command(group_stack: @group_stack),
kind: :group,
)
end

@group_id_stack.push(@group_id)
@group_id += 1
@group_id_stack.push(@group_id)
@group_id += 1
end
end

sig { params(node: Prism::ClassNode).void }
def on_class_node_leave(node)
@visibility_stack.pop
@group_stack.pop
@group_id_stack.pop

class_name = node.constant_path.slice

if @path && class_name.end_with?("Test")
@group_id_stack.pop
end
end

sig { params(node: Prism::DefNode).void }
Expand Down

0 comments on commit ee5f44d

Please sign in to comment.