Skip to content

Commit cc279ea

Browse files
authored
Centralize rspec command resolution (#61)
1 parent 6502ef2 commit cc279ea

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

lib/ruby_lsp/ruby_lsp_rspec/addon.rb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ module RSpec
1414
class Addon < ::RubyLsp::Addon
1515
extend T::Sig
1616

17-
sig { returns(T.nilable(String)) }
18-
attr_reader :rspec_command
19-
2017
sig { returns(T::Boolean) }
2118
attr_reader :debug
2219

@@ -32,7 +29,7 @@ def activate(global_state, message_queue)
3229
@index = T.let(global_state.index, T.nilable(RubyIndexer::Index))
3330

3431
settings = global_state.settings_for_addon(name)
35-
@rspec_command = T.let(settings&.dig(:rspecCommand), T.nilable(String))
32+
@rspec_command = rspec_command(settings)
3633
@debug = settings&.dig(:debug) || false
3734
end
3835

@@ -55,7 +52,7 @@ def version
5552
def create_code_lens_listener(response_builder, uri, dispatcher)
5653
return unless uri.to_standardized_path&.end_with?("_test.rb") || uri.to_standardized_path&.end_with?("_spec.rb")
5754

58-
CodeLens.new(response_builder, uri, dispatcher, rspec_command: rspec_command, debug: debug)
55+
CodeLens.new(response_builder, uri, dispatcher, T.must(@rspec_command), debug: debug)
5956
end
6057

6158
sig do
@@ -89,6 +86,24 @@ def create_definition_listener(response_builder, uri, node_context, dispatcher)
8986
def name
9087
"Ruby LSP RSpec"
9188
end
89+
90+
sig { params(settings: T.nilable(T::Hash[Symbol, T.untyped])).returns(String) }
91+
def rspec_command(settings)
92+
@rspec_command ||= settings&.dig(:rspecCommand) || begin
93+
cmd = if File.exist?(File.join(Dir.pwd, "bin", "rspec"))
94+
"bin/rspec"
95+
else
96+
"rspec"
97+
end
98+
99+
begin
100+
Bundler.with_original_env { Bundler.default_lockfile }
101+
"bundle exec #{cmd}"
102+
rescue Bundler::GemfileNotFound
103+
cmd
104+
end
105+
end
106+
end
92107
end
93108
end
94109
end

lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,21 @@ class CodeLens
1313
response_builder: ResponseBuilders::CollectionResponseBuilder[Interface::CodeLens],
1414
uri: URI::Generic,
1515
dispatcher: Prism::Dispatcher,
16-
rspec_command: T.nilable(String),
16+
rspec_command: String,
1717
debug: T::Boolean,
1818
).void
1919
end
20-
def initialize(response_builder, uri, dispatcher, rspec_command: nil, debug: false)
20+
def initialize(response_builder, uri, dispatcher, rspec_command, debug: false)
2121
@response_builder = response_builder
2222
# Listener is only initialized if uri.to_standardized_path is valid
2323
@path = T.let(T.must(uri.to_standardized_path), String)
2424
@group_id = T.let(1, Integer)
2525
@group_id_stack = T.let([], T::Array[Integer])
26+
@rspec_command = rspec_command
2627
@anonymous_example_count = T.let(0, Integer)
2728
dispatcher.register(self, :on_call_node_enter, :on_call_node_leave)
2829

2930
@debug = debug
30-
@base_command = T.let(
31-
# The user-configured command takes precedence over inferred command default
32-
rspec_command || begin
33-
cmd = if File.exist?(File.join(Dir.pwd, "bin", "rspec"))
34-
"bin/rspec"
35-
else
36-
"rspec"
37-
end
38-
39-
if File.exist?("Gemfile.lock")
40-
"bundle exec #{cmd}"
41-
else
42-
cmd
43-
end
44-
end,
45-
String,
46-
)
4731
end
4832

4933
sig { params(node: Prism::CallNode).void }
@@ -111,7 +95,7 @@ def generate_name(node)
11195
sig { params(node: Prism::Node, name: String, kind: Symbol).void }
11296
def add_test_code_lens(node, name:, kind:)
11397
line_number = node.location.start_line
114-
command = "#{@base_command} #{@path}:#{line_number}"
98+
command = "#{@rspec_command} #{@path}:#{line_number}"
11599

116100
log_message("Full command: `#{command}`") if @debug
117101

0 commit comments

Comments
 (0)