diff --git a/lib/ruby_lsp/ruby_lsp_rails/code_lens.rb b/lib/ruby_lsp/ruby_lsp_rails/code_lens.rb index 12537a56..5ce4e13a 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/code_lens.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/code_lens.rb @@ -75,7 +75,7 @@ class CodeLens include Requests::Support::Common include ActiveSupportTestCaseHelper - #: (RunnerClient client, GlobalState global_state, ResponseBuilders::CollectionResponseBuilder[Interface::CodeLens] response_builder, URI::Generic uri, Prism::Dispatcher dispatcher) -> void + #: (RunnerClient, GlobalState, ResponseBuilders::CollectionResponseBuilder[Interface::CodeLens], URI::Generic, Prism::Dispatcher) -> void def initialize(client, global_state, response_builder, uri, dispatcher) @client = client @global_state = global_state @@ -98,8 +98,10 @@ def initialize(client, global_state, response_builder, uri, dispatcher) #: (Prism::CallNode node) -> void def on_call_node_enter(node) - content = extract_test_case_name(node) + # Remove this method once the rollout is complete + return if @global_state.enabled_feature?(:fullTestDiscovery) + content = extract_test_case_name(node) return unless content line_number = node.location.start_line @@ -110,12 +112,15 @@ def on_call_node_enter(node) # Although uncommon, Rails tests can be written with the classic "def test_name" syntax. #: (Prism::DefNode node) -> void def on_def_node_enter(node) - method_name = node.name.to_s - - if method_name.start_with?("test_") - line_number = node.location.start_line - command = "#{test_command} #{@path}:#{line_number}" - add_test_code_lens(node, name: method_name, command: command, kind: :example) + # Remove this entire unless block once the rollout is complete + unless @global_state.enabled_feature?(:fullTestDiscovery) + method_name = node.name.to_s + + if method_name.start_with?("test_") + line_number = node.location.start_line + command = "#{test_command} #{@path}:#{line_number}" + add_test_code_lens(node, name: method_name, command: command, kind: :example) + end end if controller? @@ -134,7 +139,8 @@ def on_class_node_enter(node) # 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") + # Remove this entire if block once the rollout is complete + if class_name.end_with?("Test") && !@global_state.enabled_feature?(:fullTestDiscovery) 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) @@ -155,6 +161,8 @@ def on_class_node_leave(node) if class_name.end_with?("Test") @group_id_stack.pop end + # Remove everything but the `@constant_name_stack.pop` once the rollout is complete + return if @global_state.enabled_feature?(:fullTestDiscovery) @constant_name_stack.pop end diff --git a/lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb b/lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb index e9d7b65e..de5bb81a 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb @@ -75,6 +75,7 @@ def on_class_node_enter(node) ) @response_builder.add(test_item) + @response_builder.add_code_lens(test_item) end end end @@ -127,13 +128,15 @@ def add_test_item(node, test_name) test_item = group_test_item return unless test_item - test_item.add(Requests::Support::TestItem.new( + example_item = Requests::Support::TestItem.new( "#{test_item.id}##{test_name}", test_name, @uri, range_from_node(node), framework: :rails, - )) + ) + test_item.add(example_item) + @response_builder.add_code_lens(example_item) end #: -> Requests::Support::TestItem? diff --git a/test/ruby_lsp_rails/rails_test_style_test.rb b/test/ruby_lsp_rails/rails_test_style_test.rb index 9150e7eb..8fcd9493 100644 --- a/test/ruby_lsp_rails/rails_test_style_test.rb +++ b/test/ruby_lsp_rails/rails_test_style_test.rb @@ -243,6 +243,49 @@ class SpecialCharsTest < ActiveSupport::TestCase end end + test "pushes code lenses to response builder" do + source = <<~RUBY + class SampleTest < ActiveSupport::TestCase + test "first test" do + assert true + end + + test "second test" do + assert true + end + end + RUBY + + with_server(source, URI("/fake.rb")) do |server, uri| + server.global_state.index.index_single(URI("/other_file.rb"), <<~RUBY) + module Minitest + class Test; end + end + + module ActiveSupport + module Testing + module Declarative + end + end + + class TestCase < Minitest::Test + extend Testing::Declarative + end + end + RUBY + + server.global_state.stubs(:enabled_feature?).returns(true) + + server.process_message(id: 1, method: "textDocument/codeLens", params: { + textDocument: { uri: uri }, + }) + + result = pop_result(server) + items = result.response + assert_equal(9, items.length) + end + end + private def with_active_support_declarative_tests(source, file: "/fake.rb", &block)