Skip to content

Commit 30f8547

Browse files
Fix minitest spec code lens load path (#2706)
* Set load path based on test/spec directory Some codebases (particularly those using minitest/spec) have tests in the `spec` directory instead of the `test` directory. This commit adds support for detecting these paths and adjusting the load path accordingly. I opted to not include any directory by default. If the path contains `test` or `spec`, the appropriate directory is added to the load path. If the path doesn't contain either, then it seems unlikely that the `test` directory should be added to the load path. * Use File.fnmatch? to select test directory Co-authored-by: Vinicius Stock <[email protected]> --------- Co-authored-by: Vinicius Stock <[email protected]>
1 parent 23d8048 commit 30f8547

8 files changed

+212
-170
lines changed

lib/ruby_lsp/listeners/code_lens.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CodeLens
1515
"bundle exec ruby"
1616
rescue Bundler::GemfileNotFound
1717
"ruby"
18-
end + " -Itest ",
18+
end,
1919
String,
2020
)
2121
ACCESS_MODIFIERS = T.let([:public, :private, :protected], T::Array[Symbol])
@@ -229,7 +229,11 @@ def add_test_code_lens(node, name:, command:, kind:, id: name)
229229
).returns(String)
230230
end
231231
def generate_test_command(group_stack: [], spec_name: nil, method_name: nil)
232-
command = BASE_COMMAND + T.must(@path)
232+
path = T.must(@path)
233+
command = BASE_COMMAND
234+
command += " -Itest" if File.fnmatch?("**/test/**/*", path, File::FNM_PATHNAME)
235+
command += " -Ispec" if File.fnmatch?("**/spec/**/*", path, File::FNM_PATHNAME)
236+
command += " #{path}"
233237

234238
case @global_state.test_library
235239
when "minitest"

project-words

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ hostedtoolcache
3232
importmap
3333
indexables
3434
ipairs
35+
Ispec
3536
Itest
3637
ivar
3738
Jaro

test/expectations/code_lens/minitest_nested_classes_and_modules.exp.json

Lines changed: 45 additions & 45 deletions
Large diffs are not rendered by default.

test/expectations/code_lens/minitest_spec_tests.exp.json

Lines changed: 45 additions & 45 deletions
Large diffs are not rendered by default.

test/expectations/code_lens/minitest_tests.exp.json

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"arguments": [
1818
"/fixtures/minitest_tests.rb",
1919
"Test",
20-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name \"/^Test(#|::)/\"",
20+
"bundle exec ruby /fixtures/minitest_tests.rb --name \"/^Test(#|::)/\"",
2121
{
2222
"start_line": 0,
2323
"start_column": 0,
@@ -51,7 +51,7 @@
5151
"arguments": [
5252
"/fixtures/minitest_tests.rb",
5353
"Test",
54-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name \"/^Test(#|::)/\"",
54+
"bundle exec ruby /fixtures/minitest_tests.rb --name \"/^Test(#|::)/\"",
5555
{
5656
"start_line": 0,
5757
"start_column": 0,
@@ -85,7 +85,7 @@
8585
"arguments": [
8686
"/fixtures/minitest_tests.rb",
8787
"Test",
88-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name \"/^Test(#|::)/\"",
88+
"bundle exec ruby /fixtures/minitest_tests.rb --name \"/^Test(#|::)/\"",
8989
{
9090
"start_line": 0,
9191
"start_column": 0,
@@ -119,7 +119,7 @@
119119
"arguments": [
120120
"/fixtures/minitest_tests.rb",
121121
"Test#test_public",
122-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public",
122+
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public",
123123
{
124124
"start_line": 5,
125125
"start_column": 2,
@@ -152,7 +152,7 @@
152152
"arguments": [
153153
"/fixtures/minitest_tests.rb",
154154
"Test#test_public",
155-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public",
155+
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public",
156156
{
157157
"start_line": 5,
158158
"start_column": 2,
@@ -185,7 +185,7 @@
185185
"arguments": [
186186
"/fixtures/minitest_tests.rb",
187187
"Test#test_public",
188-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public",
188+
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public",
189189
{
190190
"start_line": 5,
191191
"start_column": 2,
@@ -218,7 +218,7 @@
218218
"arguments": [
219219
"/fixtures/minitest_tests.rb",
220220
"Test#test_public_command",
221-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public_command",
221+
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public_command",
222222
{
223223
"start_line": 9,
224224
"start_column": 9,
@@ -251,7 +251,7 @@
251251
"arguments": [
252252
"/fixtures/minitest_tests.rb",
253253
"Test#test_public_command",
254-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public_command",
254+
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public_command",
255255
{
256256
"start_line": 9,
257257
"start_column": 9,
@@ -284,7 +284,7 @@
284284
"arguments": [
285285
"/fixtures/minitest_tests.rb",
286286
"Test#test_public_command",
287-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public_command",
287+
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public_command",
288288
{
289289
"start_line": 9,
290290
"start_column": 9,
@@ -317,7 +317,7 @@
317317
"arguments": [
318318
"/fixtures/minitest_tests.rb",
319319
"Test#test_another_public",
320-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_another_public",
320+
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_another_public",
321321
{
322322
"start_line": 11,
323323
"start_column": 9,
@@ -350,7 +350,7 @@
350350
"arguments": [
351351
"/fixtures/minitest_tests.rb",
352352
"Test#test_another_public",
353-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_another_public",
353+
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_another_public",
354354
{
355355
"start_line": 11,
356356
"start_column": 9,
@@ -383,7 +383,7 @@
383383
"arguments": [
384384
"/fixtures/minitest_tests.rb",
385385
"Test#test_another_public",
386-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_another_public",
386+
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_another_public",
387387
{
388388
"start_line": 11,
389389
"start_column": 9,
@@ -416,7 +416,7 @@
416416
"arguments": [
417417
"/fixtures/minitest_tests.rb",
418418
"Test#test_public_vcall",
419-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public_vcall",
419+
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public_vcall",
420420
{
421421
"start_line": 17,
422422
"start_column": 2,
@@ -449,7 +449,7 @@
449449
"arguments": [
450450
"/fixtures/minitest_tests.rb",
451451
"Test#test_public_vcall",
452-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public_vcall",
452+
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public_vcall",
453453
{
454454
"start_line": 17,
455455
"start_column": 2,
@@ -482,7 +482,7 @@
482482
"arguments": [
483483
"/fixtures/minitest_tests.rb",
484484
"Test#test_public_vcall",
485-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public_vcall",
485+
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public_vcall",
486486
{
487487
"start_line": 17,
488488
"start_column": 2,
@@ -515,7 +515,7 @@
515515
"arguments": [
516516
"/fixtures/minitest_tests.rb",
517517
"Test#test_with_q?",
518-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_with_q\\?",
518+
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_with_q\\?",
519519
{
520520
"start_line": 19,
521521
"start_column": 2,
@@ -548,7 +548,7 @@
548548
"arguments": [
549549
"/fixtures/minitest_tests.rb",
550550
"Test#test_with_q?",
551-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_with_q\\?",
551+
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_with_q\\?",
552552
{
553553
"start_line": 19,
554554
"start_column": 2,
@@ -581,7 +581,7 @@
581581
"arguments": [
582582
"/fixtures/minitest_tests.rb",
583583
"Test#test_with_q?",
584-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_with_q\\?",
584+
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_with_q\\?",
585585
{
586586
"start_line": 19,
587587
"start_column": 2,
@@ -614,7 +614,7 @@
614614
"arguments": [
615615
"/fixtures/minitest_tests.rb",
616616
"AnotherTest",
617-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name \"/^AnotherTest(#|::)/\"",
617+
"bundle exec ruby /fixtures/minitest_tests.rb --name \"/^AnotherTest(#|::)/\"",
618618
{
619619
"start_line": 24,
620620
"start_column": 0,
@@ -648,7 +648,7 @@
648648
"arguments": [
649649
"/fixtures/minitest_tests.rb",
650650
"AnotherTest",
651-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name \"/^AnotherTest(#|::)/\"",
651+
"bundle exec ruby /fixtures/minitest_tests.rb --name \"/^AnotherTest(#|::)/\"",
652652
{
653653
"start_line": 24,
654654
"start_column": 0,
@@ -682,7 +682,7 @@
682682
"arguments": [
683683
"/fixtures/minitest_tests.rb",
684684
"AnotherTest",
685-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name \"/^AnotherTest(#|::)/\"",
685+
"bundle exec ruby /fixtures/minitest_tests.rb --name \"/^AnotherTest(#|::)/\"",
686686
{
687687
"start_line": 24,
688688
"start_column": 0,
@@ -716,7 +716,7 @@
716716
"arguments": [
717717
"/fixtures/minitest_tests.rb",
718718
"AnotherTest#test_public",
719-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name AnotherTest#test_public",
719+
"bundle exec ruby /fixtures/minitest_tests.rb --name AnotherTest#test_public",
720720
{
721721
"start_line": 25,
722722
"start_column": 2,
@@ -749,7 +749,7 @@
749749
"arguments": [
750750
"/fixtures/minitest_tests.rb",
751751
"AnotherTest#test_public",
752-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name AnotherTest#test_public",
752+
"bundle exec ruby /fixtures/minitest_tests.rb --name AnotherTest#test_public",
753753
{
754754
"start_line": 25,
755755
"start_column": 2,
@@ -782,7 +782,7 @@
782782
"arguments": [
783783
"/fixtures/minitest_tests.rb",
784784
"AnotherTest#test_public",
785-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name AnotherTest#test_public",
785+
"bundle exec ruby /fixtures/minitest_tests.rb --name AnotherTest#test_public",
786786
{
787787
"start_line": 25,
788788
"start_column": 2,
@@ -815,7 +815,7 @@
815815
"arguments": [
816816
"/fixtures/minitest_tests.rb",
817817
"AnotherTest#test_public_2",
818-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name AnotherTest#test_public_2",
818+
"bundle exec ruby /fixtures/minitest_tests.rb --name AnotherTest#test_public_2",
819819
{
820820
"start_line": 31,
821821
"start_column": 2,
@@ -848,7 +848,7 @@
848848
"arguments": [
849849
"/fixtures/minitest_tests.rb",
850850
"AnotherTest#test_public_2",
851-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name AnotherTest#test_public_2",
851+
"bundle exec ruby /fixtures/minitest_tests.rb --name AnotherTest#test_public_2",
852852
{
853853
"start_line": 31,
854854
"start_column": 2,
@@ -881,7 +881,7 @@
881881
"arguments": [
882882
"/fixtures/minitest_tests.rb",
883883
"AnotherTest#test_public_2",
884-
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name AnotherTest#test_public_2",
884+
"bundle exec ruby /fixtures/minitest_tests.rb --name AnotherTest#test_public_2",
885885
{
886886
"start_line": 31,
887887
"start_column": 2,

0 commit comments

Comments
 (0)