Skip to content

Commit 5f4e2bb

Browse files
committed
Don't push group id to stack when encountering non-test classes
Some tests use classes instead of modules for namespacing, such us: ```ruby module Foo class Bar class MyTest < ActiveSupport::TestCase test "something" do 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.
1 parent 25f1189 commit 5f4e2bb

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

lib/ruby_lsp/ruby_lsp_rails/code_lens.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,17 @@ def on_class_node_enter(node)
8888
if class_name.end_with?("Test")
8989
command = "#{BASE_COMMAND} #{@path}"
9090
add_test_code_lens(node, name: class_name, command: command, kind: :group)
91+
@group_id_stack.push(@group_id)
92+
@group_id += 1
9193
end
92-
93-
@group_id_stack.push(@group_id)
94-
@group_id += 1
9594
end
9695

9796
sig { params(node: Prism::ClassNode).void }
9897
def on_class_node_leave(node)
99-
@group_id_stack.pop
98+
class_name = node.constant_path.slice
99+
if class_name.end_with?("Test")
100+
@group_id_stack.pop
101+
end
100102
end
101103

102104
private

test/ruby_lsp_rails/code_lens_test.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,60 @@ class NestedTest < ActiveSupport::TestCase
205205
assert_empty(data)
206206
end
207207

208+
test "recognizes nested class structure correctly" do
209+
response = generate_code_lens_for_source(<<~RUBY)
210+
module Foo
211+
class Bar
212+
class Test < ActiveSupport::TestCase
213+
test "an example" do
214+
# test body
215+
end
216+
end
217+
end
218+
219+
class AnotherTest < ActiveSupport::TestCase
220+
test "an example" do
221+
# test body
222+
end
223+
end
224+
end
225+
RUBY
226+
227+
data = response.map(&:data)
228+
229+
# Code lenses for `Test`
230+
explorer, terminal, debug = data.shift(3)
231+
assert_nil(explorer[:group_id])
232+
assert_nil(terminal[:group_id])
233+
assert_nil(debug[:group_id])
234+
assert_equal(1, explorer[:id])
235+
assert_equal(1, terminal[:id])
236+
assert_equal(1, debug[:id])
237+
238+
# Code lenses for `an example`
239+
explorer, terminal, debug = data.shift(3)
240+
assert_equal(1, explorer[:group_id])
241+
assert_equal(1, terminal[:group_id])
242+
assert_equal(1, debug[:group_id])
243+
244+
# Code lenses for `AnotherTest`
245+
explorer, terminal, debug = data.shift(3)
246+
assert_nil(explorer[:group_id])
247+
assert_nil(terminal[:group_id])
248+
assert_nil(debug[:group_id])
249+
assert_equal(2, explorer[:id])
250+
assert_equal(2, terminal[:id])
251+
assert_equal(2, debug[:id])
252+
253+
# Code lenses for `an example`
254+
explorer, terminal, debug = data.shift(3)
255+
assert_equal(2, explorer[:group_id])
256+
assert_equal(2, terminal[:group_id])
257+
assert_equal(2, debug[:group_id])
258+
259+
assert_empty(data)
260+
end
261+
208262
private
209263

210264
def generate_code_lens_for_source(source)

0 commit comments

Comments
 (0)