Skip to content

Commit 7c1b294

Browse files
authored
Use -r instead of RUBYOPT to require LSP reporters (#3661)
1 parent c28f2bf commit 7c1b294

File tree

6 files changed

+67
-72
lines changed

6 files changed

+67
-72
lines changed

lib/ruby_lsp/listeners/test_style.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ def resolve_test_commands(items)
7575

7676
unless full_files.empty?
7777
specs, tests = full_files.partition { |path| spec?(path) }
78-
commands << "#{BASE_COMMAND} -Itest -e \"ARGV.each { |f| require f }\" #{tests.join(" ")}" if tests.any?
79-
commands << "#{BASE_COMMAND} -Ispec -e \"ARGV.each { |f| require f }\" #{specs.join(" ")}" if specs.any?
78+
79+
commands << "#{COMMAND} -Itest -e \"ARGV.each { |f| require f }\" #{tests.join(" ")}" if tests.any?
80+
commands << "#{COMMAND} -Ispec -e \"ARGV.each { |f| require f }\" #{specs.join(" ")}" if specs.any?
8081
end
8182

8283
commands
@@ -113,7 +114,7 @@ def handle_minitest_groups(file_path, groups_and_examples)
113114
end
114115

115116
load_path = spec?(file_path) ? "-Ispec" : "-Itest"
116-
"#{BASE_COMMAND} #{load_path} #{file_path} --name \"/#{regex}/\""
117+
"#{COMMAND} #{load_path} #{file_path} --name \"/#{regex}/\""
117118
end
118119

119120
#: (String, Hash[String, Hash[Symbol, untyped]]) -> Array[String]
@@ -124,7 +125,7 @@ def handle_test_unit_groups(file_path, groups_and_examples)
124125
Shellwords.escape(TestDiscovery::DYNAMIC_REFERENCE_MARKER),
125126
".*",
126127
)
127-
command = +"#{BASE_COMMAND} -Itest #{file_path} --testcase \"/^#{group_regex}\\$/\""
128+
command = +"#{COMMAND} -Itest #{file_path} --testcase \"/^#{group_regex}\\$/\""
128129

129130
unless examples.empty?
130131
command << if examples.length == 1
@@ -143,13 +144,14 @@ def handle_test_unit_groups(file_path, groups_and_examples)
143144

144145
MINITEST_REPORTER_PATH = File.expand_path("../test_reporters/minitest_reporter.rb", __dir__) #: String
145146
TEST_UNIT_REPORTER_PATH = File.expand_path("../test_reporters/test_unit_reporter.rb", __dir__) #: String
146-
ACCESS_MODIFIERS = [:public, :private, :protected].freeze
147147
BASE_COMMAND = begin
148148
Bundler.with_original_env { Bundler.default_lockfile }
149149
"bundle exec ruby"
150150
rescue Bundler::GemfileNotFound
151151
"ruby"
152152
end #: String
153+
COMMAND = "#{BASE_COMMAND} -r#{MINITEST_REPORTER_PATH} -r#{TEST_UNIT_REPORTER_PATH}" #: String
154+
ACCESS_MODIFIERS = [:public, :private, :protected].freeze
153155

154156
#: (ResponseBuilders::TestCollection, GlobalState, Prism::Dispatcher, URI::Generic) -> void
155157
def initialize(response_builder, global_state, dispatcher, uri)

lib/ruby_lsp/server.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,10 +1510,7 @@ def resolve_test_commands(message)
15101510

15111511
send_message(Result.new(
15121512
id: message[:id],
1513-
response: {
1514-
commands: commands,
1515-
reporterPaths: [Listeners::TestStyle::MINITEST_REPORTER_PATH, Listeners::TestStyle::TEST_UNIT_REPORTER_PATH],
1516-
},
1513+
response: { commands: commands },
15171514
))
15181515
end
15191516

lib/ruby_lsp/test_reporters/lsp_reporter.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ class LspReporter
2424
# https://code.visualstudio.com/api/references/vscode-api#StatementCoverage
2525
#: type statement_coverage = { executed: Integer, location: position, branches: Array[branch_coverage] }
2626

27-
#: bool
28-
attr_reader :invoked_shutdown
29-
3027
#: -> void
3128
def initialize
3229
dir_path = File.join(Dir.tmpdir, "ruby-lsp")
@@ -195,7 +192,7 @@ def at_coverage_exit
195192

196193
#: -> void
197194
def at_exit
198-
internal_shutdown unless invoked_shutdown
195+
internal_shutdown unless @invoked_shutdown
199196
end
200197

201198
class << self

test/requests/resolve_test_commands_test.rb

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
module RubyLsp
77
class ResolveTestCommandsMinitestTest < Minitest::Test
8+
COMMAND = Listeners::TestStyle::COMMAND
9+
810
def test_resolve_test_command_specific_examples
911
with_server do |server|
1012
server.process_message({
@@ -65,8 +67,8 @@ def test_resolve_test_command_specific_examples
6567
result = server.pop_response.response
6668
assert_equal(
6769
[
68-
"bundle exec ruby -Itest /test/server_test.rb --name \"/^ServerTest#test_server\\$/\"",
69-
"bundle exec ruby -Itest /test/store_test.rb --name \"/^StoreTest#test_store\\$/\"",
70+
"#{COMMAND} -Itest /test/server_test.rb --name \"/^ServerTest#test_server\\$/\"",
71+
"#{COMMAND} -Itest /test/store_test.rb --name \"/^StoreTest#test_store\\$/\"",
7072
],
7173
result[:commands],
7274
)
@@ -121,8 +123,8 @@ def test_resolve_test_command_group_mixed_with_examples
121123
result = server.pop_response.response
122124
assert_equal(
123125
[
124-
"bundle exec ruby -Itest /test/server_test.rb --name \"/^ServerTest(#|::)/\"",
125-
"bundle exec ruby -Itest /test/store_test.rb --name \"/^StoreTest#test_store\\$/\"",
126+
"#{COMMAND} -Itest /test/server_test.rb --name \"/^ServerTest(#|::)/\"",
127+
"#{COMMAND} -Itest /test/store_test.rb --name \"/^StoreTest#test_store\\$/\"",
126128
],
127129
result[:commands],
128130
)
@@ -177,7 +179,7 @@ def test_resolve_test_command_multiple_examples_from_same_group
177179
result = server.pop_response.response
178180
assert_equal(
179181
[
180-
"bundle exec ruby -Itest /test/server_test.rb --name \"/^ServerTest#(test_server|test_server_again)\\$/\"",
182+
"#{COMMAND} -Itest /test/server_test.rb --name \"/^ServerTest#(test_server|test_server_again)\\$/\"",
181183
],
182184
result[:commands],
183185
)
@@ -212,7 +214,7 @@ def test_resolve_test_command_entire_files
212214
result = server.pop_response.response
213215
assert_equal(
214216
[
215-
"bundle exec ruby -Itest -e \"ARGV.each { |f| require f }\" /test/server_test.rb /test/store_test.rb",
217+
"#{COMMAND} -Itest -e \"ARGV.each { |f| require f }\" /test/server_test.rb /test/store_test.rb",
216218
],
217219
result[:commands],
218220
)
@@ -255,7 +257,7 @@ def test_resolve_test_command_entire_directories
255257
result = server.pop_response.response
256258
assert_equal(
257259
[
258-
"bundle exec ruby -Itest -e \"ARGV.each { |f| require f }\" /other/test/fake_test.rb " \
260+
"#{COMMAND} -Itest -e \"ARGV.each { |f| require f }\" /other/test/fake_test.rb " \
259261
"/other/test/fake_test2.rb /test/server_test.rb /test/store_test.rb",
260262
],
261263
result[:commands],
@@ -299,7 +301,7 @@ def test_resolve_test_command_multiple_test_groups
299301
result = server.pop_response.response
300302
assert_equal(
301303
[
302-
"bundle exec ruby -Itest /test/server_test.rb --name \"/(^ServerTest(#|::)|^OtherServerTest(#|::))/\"",
304+
"#{COMMAND} -Itest /test/server_test.rb --name \"/(^ServerTest(#|::)|^OtherServerTest(#|::))/\"",
303305
],
304306
result[:commands],
305307
)
@@ -388,9 +390,9 @@ def test_resolve_test_command_complex_case
388390
result = server.pop_response.response
389391
assert_equal(
390392
[
391-
"bundle exec ruby -Itest /test/server_test.rb --name " \
393+
"#{COMMAND} -Itest /test/server_test.rb --name " \
392394
"\"/(^ServerTest#(test_server|test_server_again)\\$|^OtherServerTest(#|::))/\"",
393-
"bundle exec ruby -Itest /test/store_test.rb --name \"/^StoreTest#test_store\\$/\"",
395+
"#{COMMAND} -Itest /test/store_test.rb --name \"/^StoreTest#test_store\\$/\"",
394396
],
395397
result[:commands],
396398
)
@@ -445,8 +447,8 @@ def test_resolve_test_command_examples_with_dynamic_references
445447
result = server.pop_response.response
446448
assert_equal(
447449
[
448-
"bundle exec ruby -Itest /test/server_test.rb --name \"/^.*::ServerTest#test_server\\$/\"",
449-
"bundle exec ruby -Itest /test/store_test.rb --name \"/^.*::StoreTest(#|::)/\"",
450+
"#{COMMAND} -Itest /test/server_test.rb --name \"/^.*::ServerTest#test_server\\$/\"",
451+
"#{COMMAND} -Itest /test/store_test.rb --name \"/^.*::StoreTest(#|::)/\"",
450452
],
451453
result[:commands],
452454
)
@@ -483,7 +485,7 @@ def test_resolve_test_command_examples_with_no_parent_items
483485
result = server.pop_response.response
484486
assert_equal(
485487
[
486-
"bundle exec ruby -Itest /test/requests/completion_test.rb " \
488+
"#{COMMAND} -Itest /test/requests/completion_test.rb " \
487489
"--name \"/^CompletionTest#(test_with_typed_false|test_with_typed_true)\\$/\"",
488490
],
489491
result[:commands],
@@ -528,7 +530,7 @@ def test_resolve_test_command_nested_test_groups
528530
result = server.pop_response.response
529531
assert_equal(
530532
[
531-
"bundle exec ruby -Itest /test/server_test.rb --name \"/^ServerTest::MainTest::NestedTest(#|::)/\"",
533+
"#{COMMAND} -Itest /test/server_test.rb --name \"/^ServerTest::MainTest::NestedTest(#|::)/\"",
532534
],
533535
result[:commands],
534536
)
@@ -568,8 +570,8 @@ def test_resolve_test_command_mix_of_directories_and_examples
568570
result = server.pop_response.response
569571
assert_equal(
570572
[
571-
"bundle exec ruby -Itest /test/server_test.rb --name \"/^ServerTest#test_server\\$/\"",
572-
"bundle exec ruby -Itest -e \"ARGV.each { |f| require f }\" /test/unit/fake_test.rb " \
573+
"#{COMMAND} -Itest /test/server_test.rb --name \"/^ServerTest#test_server\\$/\"",
574+
"#{COMMAND} -Itest -e \"ARGV.each { |f| require f }\" /test/unit/fake_test.rb " \
573575
"/test/unit/fake_test2.rb",
574576
],
575577
result[:commands],
@@ -625,8 +627,8 @@ def test_resolve_test_command_for_minitest_spec
625627
result = server.pop_response.response
626628
assert_equal(
627629
[
628-
"bundle exec ruby -Ispec /spec/server_spec.rb --name \"/^ServerSpec#test_\\d{4}_something\\$/\"",
629-
"bundle exec ruby -Ispec -e \"ARGV.each { |f| require f }\" /spec/other_spec.rb",
630+
"#{COMMAND} -Ispec /spec/server_spec.rb --name \"/^ServerSpec#test_\\d{4}_something\\$/\"",
631+
"#{COMMAND} -Ispec -e \"ARGV.each { |f| require f }\" /spec/other_spec.rb",
630632
],
631633
result[:commands],
632634
)
@@ -655,7 +657,7 @@ def test_resolve_test_command_for_minitest_spec_directory
655657
result = server.pop_response.response
656658
assert_equal(
657659
[
658-
"bundle exec ruby -Ispec -e \"ARGV.each { |f| require f }\" /other/spec/fake_spec.rb " \
660+
"#{COMMAND} -Ispec -e \"ARGV.each { |f| require f }\" /other/spec/fake_spec.rb " \
659661
"/other/spec/fake2_spec.rb",
660662
],
661663
result[:commands],
@@ -700,7 +702,7 @@ def test_resolve_test_command_for_nested_spec_in_class
700702
result = server.pop_response.response
701703
assert_equal(
702704
[
703-
"bundle exec ruby -Ispec /spec/server_spec.rb --name \"/^ServerSpec::foo(#|::)/\"",
705+
"#{COMMAND} -Ispec /spec/server_spec.rb --name \"/^ServerSpec::foo(#|::)/\"",
704706
],
705707
result[:commands],
706708
)
@@ -744,7 +746,7 @@ def test_resolve_test_command_for_minitest_spec_with_backticks
744746
result = server.pop_response.response
745747
assert_equal(
746748
[
747-
"bundle exec ruby -Ispec /spec/server_spec.rb --name " \
749+
"#{COMMAND} -Ispec /spec/server_spec.rb --name " \
748750
"\"/^ServerSpec#test_\\d{4}_uses\\ \\`SomeClass\\`\\ to\\ do\\ something\\$/\"",
749751
],
750752
result[:commands],
@@ -754,6 +756,8 @@ def test_resolve_test_command_for_minitest_spec_with_backticks
754756
end
755757

756758
class ResolveTestCommandsTestUnitTest < Minitest::Test
759+
COMMAND = Listeners::TestStyle::COMMAND
760+
757761
def test_resolve_test_command_specific_examples
758762
with_server do |server|
759763
server.process_message({
@@ -814,8 +818,8 @@ def test_resolve_test_command_specific_examples
814818
result = server.pop_response.response
815819
assert_equal(
816820
[
817-
"bundle exec ruby -Itest /test/server_test.rb --testcase \"/^ServerTest\\$/\" --name \"/test_server\\$/\"",
818-
"bundle exec ruby -Itest /test/store_test.rb --testcase \"/^StoreTest\\$/\" --name \"/test_store\\$/\"",
821+
"#{COMMAND} -Itest /test/server_test.rb --testcase \"/^ServerTest\\$/\" --name \"/test_server\\$/\"",
822+
"#{COMMAND} -Itest /test/store_test.rb --testcase \"/^StoreTest\\$/\" --name \"/test_store\\$/\"",
819823
],
820824
result[:commands],
821825
)
@@ -870,8 +874,8 @@ def test_resolve_test_command_group_mixed_with_examples
870874
result = server.pop_response.response
871875
assert_equal(
872876
[
873-
"bundle exec ruby -Itest /test/server_test.rb --testcase \"/^ServerTest\\$/\"",
874-
"bundle exec ruby -Itest /test/store_test.rb --testcase \"/^StoreTest\\$/\" --name \"/test_store\\$/\"",
877+
"#{COMMAND} -Itest /test/server_test.rb --testcase \"/^ServerTest\\$/\"",
878+
"#{COMMAND} -Itest /test/store_test.rb --testcase \"/^StoreTest\\$/\" --name \"/test_store\\$/\"",
875879
],
876880
result[:commands],
877881
)
@@ -926,7 +930,7 @@ def test_resolve_test_command_multiple_examples_from_same_group
926930
result = server.pop_response.response
927931
assert_equal(
928932
[
929-
"bundle exec ruby -Itest /test/server_test.rb --testcase \"/^ServerTest\\$/\" " \
933+
"#{COMMAND} -Itest /test/server_test.rb --testcase \"/^ServerTest\\$/\" " \
930934
"--name \"/(test_server|test_server_again)\\$/\"",
931935
],
932936
result[:commands],
@@ -970,8 +974,8 @@ def test_resolve_test_command_multiple_test_groups
970974
result = server.pop_response.response
971975
assert_equal(
972976
[
973-
"bundle exec ruby -Itest /test/server_test.rb --testcase \"/^ServerTest\\$/\"",
974-
"bundle exec ruby -Itest /test/server_test.rb --testcase \"/^OtherServerTest\\$/\"",
977+
"#{COMMAND} -Itest /test/server_test.rb --testcase \"/^ServerTest\\$/\"",
978+
"#{COMMAND} -Itest /test/server_test.rb --testcase \"/^OtherServerTest\\$/\"",
975979
],
976980
result[:commands],
977981
)
@@ -1060,10 +1064,10 @@ def test_resolve_test_command_complex_case
10601064
result = server.pop_response.response
10611065
assert_equal(
10621066
[
1063-
"bundle exec ruby -Itest /test/server_test.rb --testcase \"/^ServerTest\\$/\" " \
1067+
"#{COMMAND} -Itest /test/server_test.rb --testcase \"/^ServerTest\\$/\" " \
10641068
"--name \"/(test_server|test_server_again)\\$/\"",
1065-
"bundle exec ruby -Itest /test/server_test.rb --testcase \"/^OtherServerTest\\$/\"",
1066-
"bundle exec ruby -Itest /test/store_test.rb --testcase \"/^StoreTest\\$/\" --name \"/test_store\\$/\"",
1069+
"#{COMMAND} -Itest /test/server_test.rb --testcase \"/^OtherServerTest\\$/\"",
1070+
"#{COMMAND} -Itest /test/store_test.rb --testcase \"/^StoreTest\\$/\" --name \"/test_store\\$/\"",
10671071
],
10681072
result[:commands],
10691073
)
@@ -1118,9 +1122,9 @@ def test_resolve_test_command_examples_with_dynamic_references
11181122
result = server.pop_response.response
11191123
assert_equal(
11201124
[
1121-
"bundle exec ruby -Itest /test/server_test.rb --testcase \"/^.*::ServerTest\\$/\" " \
1125+
"#{COMMAND} -Itest /test/server_test.rb --testcase \"/^.*::ServerTest\\$/\" " \
11221126
"--name \"/test_server\\$/\"",
1123-
"bundle exec ruby -Itest /test/store_test.rb --testcase \"/^.*::StoreTest\\$/\"",
1127+
"#{COMMAND} -Itest /test/store_test.rb --testcase \"/^.*::StoreTest\\$/\"",
11241128
],
11251129
result[:commands],
11261130
)
@@ -1157,7 +1161,7 @@ def test_resolve_test_command_examples_with_no_parent_items
11571161
result = server.pop_response.response
11581162
assert_equal(
11591163
[
1160-
"bundle exec ruby -Itest /test/requests/completion_test.rb --testcase \"/^CompletionTest\\$/\" " \
1164+
"#{COMMAND} -Itest /test/requests/completion_test.rb --testcase \"/^CompletionTest\\$/\" " \
11611165
"--name \"/(test_with_typed_false|test_with_typed_true)\\$/\"",
11621166
],
11631167
result[:commands],
@@ -1202,7 +1206,7 @@ def test_resolve_test_command_nested_test_groups
12021206
result = server.pop_response.response
12031207
assert_equal(
12041208
[
1205-
"bundle exec ruby -Itest /test/server_test.rb --testcase \"/^ServerTest::MainTest::NestedTest\\$/\"",
1209+
"#{COMMAND} -Itest /test/server_test.rb --testcase \"/^ServerTest::MainTest::NestedTest\\$/\"",
12061210
],
12071211
result[:commands],
12081212
)
@@ -1242,8 +1246,8 @@ def test_resolve_test_command_mix_of_directories_and_examples
12421246
result = server.pop_response.response
12431247
assert_equal(
12441248
[
1245-
"bundle exec ruby -Itest /test/server_test.rb --testcase \"/^ServerTest\\$/\" --name \"/test_server\\$/\"",
1246-
"bundle exec ruby -Itest -e \"ARGV.each { |f| require f }\" /test/unit/fake_test.rb " \
1249+
"#{COMMAND} -Itest /test/server_test.rb --testcase \"/^ServerTest\\$/\" --name \"/test_server\\$/\"",
1250+
"#{COMMAND} -Itest -e \"ARGV.each { |f| require f }\" /test/unit/fake_test.rb " \
12471251
"/test/unit/fake_test2.rb",
12481252
],
12491253
result[:commands],

test/server_test.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,21 +1376,6 @@ def test_modifying_files_during_initial_indexing_does_not_duplicate_entries
13761376
end
13771377
end
13781378

1379-
def test_resolve_test_commands_returns_custom_reporters
1380-
@server.process_message({
1381-
id: 1,
1382-
method: "rubyLsp/resolveTestCommands",
1383-
params: {
1384-
items: [],
1385-
},
1386-
})
1387-
result = find_message(RubyLsp::Result, id: 1)
1388-
reporters = result.response[:reporterPaths]
1389-
1390-
assert_includes(reporters, File.expand_path("../lib/ruby_lsp/test_reporters/minitest_reporter.rb", __dir__))
1391-
assert_includes(reporters, File.expand_path("../lib/ruby_lsp/test_reporters/test_unit_reporter.rb", __dir__))
1392-
end
1393-
13941379
def test_requests_code_lens_refresh_after_indexing
13951380
@server.process_message({
13961381
id: 1,

0 commit comments

Comments
 (0)