Skip to content

Commit 4847475

Browse files
authored
Fix test hierarchy for nested groups (#612)
* Upgrade Ruby LSP version * Fix test hierarchy for nested groups
1 parent 42b9b84 commit 4847475

File tree

2 files changed

+63
-13
lines changed

2 files changed

+63
-13
lines changed

lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def resolve_test_commands(items)
5454
def initialize(response_builder, global_state, dispatcher, uri)
5555
super(response_builder, global_state, dispatcher, uri)
5656

57+
@parent_stack = [@response_builder] #: Array[(Requests::Support::TestItem | ResponseBuilders::TestCollection)?]
58+
5759
dispatcher.register(
5860
self,
5961
:on_class_node_enter,
@@ -74,12 +76,32 @@ def on_class_node_enter(node)
7476
framework: :rails,
7577
)
7678

77-
@response_builder.add(test_item)
79+
last_test_group.add(test_item)
7880
@response_builder.add_code_lens(test_item)
81+
@parent_stack << test_item
82+
else
83+
@parent_stack << nil
7984
end
8085
end
8186
end
8287

88+
#: (Prism::ClassNode node) -> void
89+
def on_class_node_leave(node)
90+
@parent_stack.pop
91+
super
92+
end
93+
94+
#: (Prism::ModuleNode node) -> void
95+
def on_module_node_enter(node)
96+
@parent_stack << nil
97+
end
98+
99+
#: (Prism::ModuleNode node) -> void
100+
def on_module_node_leave(node)
101+
@parent_stack.pop
102+
super
103+
end
104+
83105
#: (Prism::CallNode node) -> void
84106
def on_call_node_enter(node)
85107
return unless node.name == :test
@@ -125,28 +147,24 @@ def declarative_minitest?(attached_ancestors, fully_qualified_name)
125147

126148
#: (Prism::Node node, String test_name) -> void
127149
def add_test_item(node, test_name)
128-
test_item = group_test_item
129-
return unless test_item
150+
parent = @parent_stack.last
151+
return unless parent.is_a?(Requests::Support::TestItem)
130152

131153
example_item = Requests::Support::TestItem.new(
132-
"#{test_item.id}##{test_name}",
154+
"#{parent.id}##{test_name}",
133155
test_name,
134156
@uri,
135157
range_from_node(node),
136158
framework: :rails,
137159
)
138-
test_item.add(example_item)
160+
parent.add(example_item)
139161
@response_builder.add_code_lens(example_item)
140162
end
141163

142-
#: -> Requests::Support::TestItem?
143-
def group_test_item
144-
current_group_name = RubyIndexer::Index.actual_nesting(@nesting, nil).join("::")
145-
146-
# If we're finding a test method, but for the wrong framework, then the group test item will not have been
147-
# previously pushed and thus we return early and avoid adding items for a framework this listener is not
148-
# interested in
149-
@response_builder[current_group_name]
164+
#: -> (Requests::Support::TestItem | ResponseBuilders::TestCollection)
165+
def last_test_group
166+
index = @parent_stack.rindex { |i| i } #: as !nil
167+
@parent_stack[index] #: as Requests::Support::TestItem | ResponseBuilders::TestCollection
150168
end
151169
end
152170
end

test/ruby_lsp_rails/rails_test_style_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,38 @@ class TestCase < Minitest::Test
286286
end
287287
end
288288

289+
test "nested test groups" do
290+
source = <<~RUBY
291+
class SampleTest < ActiveSupport::TestCase
292+
class InnerTest < ActiveSupport::TestCase
293+
test "first" do
294+
end
295+
end
296+
297+
class AnotherGroupTest < ActiveSupport::TestCase
298+
test "second" do
299+
end
300+
end
301+
end
302+
RUBY
303+
304+
with_active_support_declarative_tests(source) do |items|
305+
assert_equal(["SampleTest"], items.map { |i| i[:id] })
306+
assert_equal(
307+
["SampleTest::InnerTest", "SampleTest::AnotherGroupTest"],
308+
items.dig(0, :children).map { |i| i[:label] },
309+
)
310+
assert_equal(
311+
["SampleTest::InnerTest#test_first"],
312+
items.dig(0, :children, 0, :children).map { |i| i[:id] },
313+
)
314+
assert_equal(
315+
["SampleTest::AnotherGroupTest#test_second"],
316+
items.dig(0, :children, 1, :children).map { |i| i[:id] },
317+
)
318+
end
319+
end
320+
289321
private
290322

291323
def with_active_support_declarative_tests(source, file: "/fake.rb", &block)

0 commit comments

Comments
 (0)