Skip to content

Commit b7f0b90

Browse files
authored
Merge pull request #1 from st0012/detect-binstub
Use rspec binstub when it's present
2 parents 8975e10 + 6ece89a commit b7f0b90

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class CodeLens < ::RubyLsp::Listener
1010
include ::RubyLsp::Requests::Support::Common
1111

1212
ResponseType = type_member { { fixed: T::Array[::RubyLsp::Interface::CodeLens] } }
13-
BASE_COMMAND = T.let((File.exist?("Gemfile.lock") ? "bundle exec rspec" : "rspec"), String)
1413

1514
sig { override.returns(ResponseType) }
1615
attr_reader :_response
@@ -21,6 +20,23 @@ def initialize(uri, emitter, message_queue)
2120
@path = T.let(uri.to_standardized_path, T.nilable(String))
2221
emitter.register(self, :on_command, :on_command_call, :on_call)
2322

23+
@base_command = T.let(
24+
begin
25+
cmd = if File.exist?(File.join(Dir.pwd, "bin", "rspec"))
26+
"bin/rspec"
27+
else
28+
"rspec"
29+
end
30+
31+
if File.exist?("Gemfile.lock")
32+
"bundle exec #{cmd}"
33+
else
34+
cmd
35+
end
36+
end,
37+
String,
38+
)
39+
2440
super(emitter, message_queue)
2541
end
2642

@@ -103,7 +119,7 @@ def add_test_code_lens(node, name:, kind:)
103119
return unless @path
104120

105121
line_number = node.location.start_line
106-
command = "#{BASE_COMMAND} #{@path}:#{line_number}"
122+
command = "#{@base_command} #{@path}:#{line_number}"
107123

108124
arguments = [
109125
@path,

spec/ruby_lsp_rspec_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,42 @@
105105
response = response.response
106106
expect(response.count).to eq(12)
107107
end
108+
109+
context "when there's a binstub" do
110+
let(:binstub_path) { File.expand_path("../bin/rspec", __dir__) }
111+
112+
before do
113+
File.write(binstub_path, <<~RUBY)
114+
#!/usr/bin/env ruby
115+
puts "binstub is called"
116+
RUBY
117+
end
118+
119+
after do
120+
FileUtils.rm(binstub_path) if File.exist?(binstub_path)
121+
end
122+
123+
it "uses the binstub" do
124+
store.set(uri: uri, source: <<~RUBY, version: 1)
125+
RSpec.describe(Foo::Bar) do
126+
end
127+
RUBY
128+
129+
response = RubyLsp::Executor.new(store, message_queue).execute(
130+
{
131+
method: "textDocument/codeLens",
132+
params: {
133+
textDocument: { uri: uri },
134+
position: { line: 0, character: 0 },
135+
},
136+
},
137+
)
138+
139+
expect(response.error).to(be_nil)
140+
141+
response = response.response
142+
expect(response.count).to eq(3)
143+
expect(response[0].command.arguments[2]).to eq("bundle exec bin/rspec /fake.rb:1")
144+
end
145+
end
108146
end

0 commit comments

Comments
 (0)