Skip to content

Commit 99604f5

Browse files
committed
Push test code lens to response builder on test discovery
1 parent ceede43 commit 99604f5

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

lib/ruby_lsp/ruby_lsp_rails/code_lens.rb

+17-9
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class CodeLens
7575
include Requests::Support::Common
7676
include ActiveSupportTestCaseHelper
7777

78-
#: (RunnerClient client, GlobalState global_state, ResponseBuilders::CollectionResponseBuilder[Interface::CodeLens] response_builder, URI::Generic uri, Prism::Dispatcher dispatcher) -> void
78+
#: (RunnerClient, GlobalState, ResponseBuilders::CollectionResponseBuilder[Interface::CodeLens], URI::Generic, Prism::Dispatcher) -> void
7979
def initialize(client, global_state, response_builder, uri, dispatcher)
8080
@client = client
8181
@global_state = global_state
@@ -98,8 +98,10 @@ def initialize(client, global_state, response_builder, uri, dispatcher)
9898

9999
#: (Prism::CallNode node) -> void
100100
def on_call_node_enter(node)
101-
content = extract_test_case_name(node)
101+
# Remove this method once the rollout is complete
102+
return if @global_state.enabled_feature?(:fullTestDiscovery)
102103

104+
content = extract_test_case_name(node)
103105
return unless content
104106

105107
line_number = node.location.start_line
@@ -110,12 +112,15 @@ def on_call_node_enter(node)
110112
# Although uncommon, Rails tests can be written with the classic "def test_name" syntax.
111113
#: (Prism::DefNode node) -> void
112114
def on_def_node_enter(node)
113-
method_name = node.name.to_s
114-
115-
if method_name.start_with?("test_")
116-
line_number = node.location.start_line
117-
command = "#{test_command} #{@path}:#{line_number}"
118-
add_test_code_lens(node, name: method_name, command: command, kind: :example)
115+
# Remove this entire unless block once the rollout is complete
116+
unless @global_state.enabled_feature?(:fullTestDiscovery)
117+
method_name = node.name.to_s
118+
119+
if method_name.start_with?("test_")
120+
line_number = node.location.start_line
121+
command = "#{test_command} #{@path}:#{line_number}"
122+
add_test_code_lens(node, name: method_name, command: command, kind: :example)
123+
end
119124
end
120125

121126
if controller?
@@ -134,7 +139,8 @@ def on_class_node_enter(node)
134139
# back in a controller context. This part is used in other places in the LSP
135140
@constant_name_stack << [class_name, superclass_name]
136141

137-
if class_name.end_with?("Test")
142+
# Remove this entire if block once the rollout is complete
143+
if class_name.end_with?("Test") && !@global_state.enabled_feature?(:fullTestDiscovery)
138144
fully_qualified_name = @constant_name_stack.map(&:first).join("::")
139145
command = "#{test_command} #{@path} --name \"/#{Shellwords.escape(fully_qualified_name)}(#|::)/\""
140146
add_test_code_lens(node, name: class_name, command: command, kind: :group)
@@ -155,6 +161,8 @@ def on_class_node_leave(node)
155161
if class_name.end_with?("Test")
156162
@group_id_stack.pop
157163
end
164+
# Remove everything but the `@constant_name_stack.pop` once the rollout is complete
165+
return if @global_state.enabled_feature?(:fullTestDiscovery)
158166

159167
@constant_name_stack.pop
160168
end

lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def on_class_node_enter(node)
7575
)
7676

7777
@response_builder.add(test_item)
78+
@response_builder.add_code_lens(test_item)
7879
end
7980
end
8081
end
@@ -127,13 +128,15 @@ def add_test_item(node, test_name)
127128
test_item = group_test_item
128129
return unless test_item
129130

130-
test_item.add(Requests::Support::TestItem.new(
131+
example_item = Requests::Support::TestItem.new(
131132
"#{test_item.id}##{test_name}",
132133
test_name,
133134
@uri,
134135
range_from_node(node),
135136
framework: :rails,
136-
))
137+
)
138+
test_item.add(example_item)
139+
@response_builder.add_code_lens(example_item)
137140
end
138141

139142
#: -> Requests::Support::TestItem?

test/ruby_lsp_rails/rails_test_style_test.rb

+43
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,49 @@ class SpecialCharsTest < ActiveSupport::TestCase
243243
end
244244
end
245245

246+
test "pushes code lenses to response builder" do
247+
source = <<~RUBY
248+
class SampleTest < ActiveSupport::TestCase
249+
test "first test" do
250+
assert true
251+
end
252+
253+
test "second test" do
254+
assert true
255+
end
256+
end
257+
RUBY
258+
259+
with_server(source, URI("/fake.rb")) do |server, uri|
260+
server.global_state.index.index_single(URI("/other_file.rb"), <<~RUBY)
261+
module Minitest
262+
class Test; end
263+
end
264+
265+
module ActiveSupport
266+
module Testing
267+
module Declarative
268+
end
269+
end
270+
271+
class TestCase < Minitest::Test
272+
extend Testing::Declarative
273+
end
274+
end
275+
RUBY
276+
277+
server.global_state.stubs(:enabled_feature?).returns(true)
278+
279+
server.process_message(id: 1, method: "textDocument/codeLens", params: {
280+
textDocument: { uri: uri },
281+
})
282+
283+
result = pop_result(server)
284+
items = result.response
285+
assert_equal(9, items.length)
286+
end
287+
end
288+
246289
private
247290

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

0 commit comments

Comments
 (0)