From 8be915d6c99de313d2519ca3fb6430b03ef31570 Mon Sep 17 00:00:00 2001 From: Thomas Marshall Date: Thu, 4 Dec 2025 13:05:28 +0000 Subject: [PATCH 1/2] Fix discovery of test methods in specs This commit ensures methods starting with "test_" are discovered as test methods in specs. --- lib/ruby_lsp/listeners/spec_style.rb | 26 ++++++++++++++++++++++++ test/requests/discover_tests_test.rb | 30 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/lib/ruby_lsp/listeners/spec_style.rb b/lib/ruby_lsp/listeners/spec_style.rb index 5f79a418a..8057b76d2 100644 --- a/lib/ruby_lsp/listeners/spec_style.rb +++ b/lib/ruby_lsp/listeners/spec_style.rb @@ -26,6 +26,7 @@ def initialize(response_builder, global_state, dispatcher, uri) register_events( dispatcher, :on_class_node_enter, + :on_def_node_enter, :on_call_node_enter, :on_call_node_leave, ) @@ -56,6 +57,31 @@ def on_module_node_leave(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerM super end + #: (Prism::DefNode) -> void + def on_def_node_enter(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod + name = node.name.to_s + return unless name.start_with?("test_") + + current_group = @spec_group_id_stack.last + return unless current_group.is_a?(DescribeGroup) + + parent = latest_group + return unless parent.is_a?(Requests::Support::TestItem) + + id = "#{parent.id}##{name}" + + test_item = Requests::Support::TestItem.new( + id, + name, + @uri, + range_from_node(node), + framework: :minitest, + ) + + parent.add(test_item) + @response_builder.add_code_lens(test_item) + end + #: (Prism::CallNode) -> void def on_call_node_enter(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod case node.name diff --git a/test/requests/discover_tests_test.rb b/test/requests/discover_tests_test.rb index d727dc0d7..9af1f9a61 100644 --- a/test/requests/discover_tests_test.rb +++ b/test/requests/discover_tests_test.rb @@ -822,6 +822,36 @@ module OtherNamespace end end + def test_discovers_test_methods + source = <<~RUBY + describe "MySpec" do + def test_foo; end + def helper_method; end + + describe "nested" do + def test_nested; end + end + end + + class NotASpec + def test_ignored; end + end + RUBY + + with_minitest_spec_configured(source) do |items| + assert_equal(["MySpec"], items.map { |i| i[:id] }) + assert_equal( + ["MySpec#test_foo", "MySpec::nested"], + items.dig(0, :children).map { |i| i[:id] }, + ) + assert_equal( + ["MySpec::nested#test_nested"], + items.dig(0, :children, 1, :children).map { |i| i[:id] }, + ) + assert_all_items_tagged_with(items, :minitest) + end + end + private def create_test_discovery_addon From 1adc1fe39c0676bfa866886534cbe632eb58116c Mon Sep 17 00:00:00 2001 From: Thomas Marshall Date: Mon, 8 Dec 2025 13:56:56 +0000 Subject: [PATCH 2/2] Ensure test methods in specs are not duplicated --- test/requests/discover_tests_test.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/requests/discover_tests_test.rb b/test/requests/discover_tests_test.rb index 9af1f9a61..01072f8be 100644 --- a/test/requests/discover_tests_test.rb +++ b/test/requests/discover_tests_test.rb @@ -836,10 +836,14 @@ def test_nested; end class NotASpec def test_ignored; end end + + class NotDescribeSpec < Minitest::Spec + def test_not_duplicate; end + end RUBY with_minitest_spec_configured(source) do |items| - assert_equal(["MySpec"], items.map { |i| i[:id] }) + assert_equal(["MySpec", "NotDescribeSpec"], items.map { |i| i[:id] }) assert_equal( ["MySpec#test_foo", "MySpec::nested"], items.dig(0, :children).map { |i| i[:id] }, @@ -848,6 +852,10 @@ def test_ignored; end ["MySpec::nested#test_nested"], items.dig(0, :children, 1, :children).map { |i| i[:id] }, ) + assert_equal( + ["NotDescribeSpec#test_not_duplicate"], + items.dig(1, :children).map { |i| i[:id] }, + ) assert_all_items_tagged_with(items, :minitest) end end