Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only handle requests in _{spec,test}.rb files #6

Merged
merged 4 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rspec/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def deactivate; end
).returns(T.nilable(Listener[T::Array[Interface::CodeLens]]))
end
def create_code_lens_listener(uri, emitter, message_queue)
return nil unless uri.to_standardized_path&.end_with?("_test.rb") || uri.to_standardized_path&.end_with?("_spec.rb")
st0012 marked this conversation as resolved.
Show resolved Hide resolved

CodeLens.new(uri, emitter, message_queue)
end

Expand Down
4 changes: 1 addition & 3 deletions lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CodeLens < ::RubyLsp::Listener
sig { params(uri: URI::Generic, dispatcher: Prism::Dispatcher, message_queue: Thread::Queue).void }
def initialize(uri, dispatcher, message_queue)
@_response = T.let([], ResponseType)
@path = T.let(uri.to_standardized_path, T.nilable(String))
@path = T.let(T.must(uri.to_standardized_path), String) # Listener is only initialized if uri.to_standardized_path is valid
st0012 marked this conversation as resolved.
Show resolved Hide resolved
dispatcher.register(self, :on_call_node_enter)

@base_command = T.let(
Expand Down Expand Up @@ -78,8 +78,6 @@ def generate_name(node)

sig { params(node: Prism::Node, name: String, kind: Symbol).void }
def add_test_code_lens(node, name:, kind:)
return unless @path

line_number = node.location.start_line
command = "#{@base_command} #{@path}:#{line_number}"

Expand Down
42 changes: 34 additions & 8 deletions spec/ruby_lsp_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# frozen_string_literal: true

RSpec.describe RubyLsp::RSpec do
let(:uri) { URI("file:///fake.rb") }
let(:uri) { URI("file:///fake_spec.rb") }
let(:store) { RubyLsp::Store.new }
let(:message_queue) { Thread::Queue.new }

Expand Down Expand Up @@ -41,9 +41,9 @@

0.upto(2) do |i|
expect(response[i].command.arguments).to eq([
"/fake.rb",
"/fake_spec.rb",
"Foo",
"bundle exec rspec /fake.rb:1",
"bundle exec rspec /fake_spec.rb:1",
{ start_line: 0, start_column: 0, end_line: 5, end_column: 3 },
])
end
Expand All @@ -54,9 +54,9 @@

3.upto(5) do |i|
expect(response[i].command.arguments).to eq([
"/fake.rb",
"/fake_spec.rb",
"when something",
"bundle exec rspec /fake.rb:2",
"bundle exec rspec /fake_spec.rb:2",
{ start_line: 1, start_column: 2, end_line: 4, end_column: 5 },
])
end
Expand All @@ -67,9 +67,9 @@

6.upto(8) do |i|
expect(response[i].command.arguments).to eq([
"/fake.rb",
"/fake_spec.rb",
"does something",
"bundle exec rspec /fake.rb:3",
"bundle exec rspec /fake_spec.rb:3",
{ start_line: 2, start_column: 4, end_line: 3, end_column: 7 },
])
end
Expand Down Expand Up @@ -145,6 +145,32 @@
expect(response[15].command.arguments[1]).to eq("<var>")
end

context "when the file is not a test file" do
let(:uri) { URI("file:///not_spec_file.rb") }

it "ignores file" do
store.set(uri: uri, source: <<~RUBY, version: 1)
class FooBar
context "when something" do
end
end
RUBY

response = RubyLsp::Executor.new(store, message_queue).execute(
{
method: "textDocument/codeLens",
params: {
textDocument: { uri: uri },
position: { line: 0, character: 0 },
},
},
)

expect(response.error).to(be_nil)
expect(response.response.count).to eq(0)
end
end

context "when there's a binstub" do
let(:binstub_path) { File.expand_path("../bin/rspec", __dir__) }

Expand Down Expand Up @@ -179,7 +205,7 @@

response = response.response
expect(response.count).to eq(3)
expect(response[0].command.arguments[2]).to eq("bundle exec bin/rspec /fake.rb:1")
expect(response[0].command.arguments[2]).to eq("bundle exec bin/rspec /fake_spec.rb:1")
end
end
end
Loading