Skip to content

Commit

Permalink
Fix minitest spec code lens load path (#2706)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
thomasmarshall and vinistock authored Nov 26, 2024
1 parent 23d8048 commit 30f8547
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 170 deletions.
8 changes: 6 additions & 2 deletions lib/ruby_lsp/listeners/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CodeLens
"bundle exec ruby"
rescue Bundler::GemfileNotFound
"ruby"
end + " -Itest ",
end,
String,
)
ACCESS_MODIFIERS = T.let([:public, :private, :protected], T::Array[Symbol])
Expand Down Expand Up @@ -229,7 +229,11 @@ def add_test_code_lens(node, name:, command:, kind:, id: name)
).returns(String)
end
def generate_test_command(group_stack: [], spec_name: nil, method_name: nil)
command = BASE_COMMAND + T.must(@path)
path = T.must(@path)
command = BASE_COMMAND
command += " -Itest" if File.fnmatch?("**/test/**/*", path, File::FNM_PATHNAME)
command += " -Ispec" if File.fnmatch?("**/spec/**/*", path, File::FNM_PATHNAME)
command += " #{path}"

case @global_state.test_library
when "minitest"
Expand Down
1 change: 1 addition & 0 deletions project-words
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ hostedtoolcache
importmap
indexables
ipairs
Ispec
Itest
ivar
Jaro
Expand Down

Large diffs are not rendered by default.

90 changes: 45 additions & 45 deletions test/expectations/code_lens/minitest_spec_tests.exp.json

Large diffs are not rendered by default.

54 changes: 27 additions & 27 deletions test/expectations/code_lens/minitest_tests.exp.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name \"/^Test(#|::)/\"",
"bundle exec ruby /fixtures/minitest_tests.rb --name \"/^Test(#|::)/\"",
{
"start_line": 0,
"start_column": 0,
Expand Down Expand Up @@ -51,7 +51,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name \"/^Test(#|::)/\"",
"bundle exec ruby /fixtures/minitest_tests.rb --name \"/^Test(#|::)/\"",
{
"start_line": 0,
"start_column": 0,
Expand Down Expand Up @@ -85,7 +85,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name \"/^Test(#|::)/\"",
"bundle exec ruby /fixtures/minitest_tests.rb --name \"/^Test(#|::)/\"",
{
"start_line": 0,
"start_column": 0,
Expand Down Expand Up @@ -119,7 +119,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test#test_public",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public",
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public",
{
"start_line": 5,
"start_column": 2,
Expand Down Expand Up @@ -152,7 +152,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test#test_public",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public",
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public",
{
"start_line": 5,
"start_column": 2,
Expand Down Expand Up @@ -185,7 +185,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test#test_public",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public",
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public",
{
"start_line": 5,
"start_column": 2,
Expand Down Expand Up @@ -218,7 +218,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test#test_public_command",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public_command",
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public_command",
{
"start_line": 9,
"start_column": 9,
Expand Down Expand Up @@ -251,7 +251,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test#test_public_command",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public_command",
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public_command",
{
"start_line": 9,
"start_column": 9,
Expand Down Expand Up @@ -284,7 +284,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test#test_public_command",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public_command",
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public_command",
{
"start_line": 9,
"start_column": 9,
Expand Down Expand Up @@ -317,7 +317,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test#test_another_public",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_another_public",
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_another_public",
{
"start_line": 11,
"start_column": 9,
Expand Down Expand Up @@ -350,7 +350,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test#test_another_public",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_another_public",
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_another_public",
{
"start_line": 11,
"start_column": 9,
Expand Down Expand Up @@ -383,7 +383,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test#test_another_public",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_another_public",
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_another_public",
{
"start_line": 11,
"start_column": 9,
Expand Down Expand Up @@ -416,7 +416,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test#test_public_vcall",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public_vcall",
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public_vcall",
{
"start_line": 17,
"start_column": 2,
Expand Down Expand Up @@ -449,7 +449,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test#test_public_vcall",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public_vcall",
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public_vcall",
{
"start_line": 17,
"start_column": 2,
Expand Down Expand Up @@ -482,7 +482,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test#test_public_vcall",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_public_vcall",
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_public_vcall",
{
"start_line": 17,
"start_column": 2,
Expand Down Expand Up @@ -515,7 +515,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test#test_with_q?",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_with_q\\?",
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_with_q\\?",
{
"start_line": 19,
"start_column": 2,
Expand Down Expand Up @@ -548,7 +548,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test#test_with_q?",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_with_q\\?",
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_with_q\\?",
{
"start_line": 19,
"start_column": 2,
Expand Down Expand Up @@ -581,7 +581,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"Test#test_with_q?",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name Test#test_with_q\\?",
"bundle exec ruby /fixtures/minitest_tests.rb --name Test#test_with_q\\?",
{
"start_line": 19,
"start_column": 2,
Expand Down Expand Up @@ -614,7 +614,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"AnotherTest",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name \"/^AnotherTest(#|::)/\"",
"bundle exec ruby /fixtures/minitest_tests.rb --name \"/^AnotherTest(#|::)/\"",
{
"start_line": 24,
"start_column": 0,
Expand Down Expand Up @@ -648,7 +648,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"AnotherTest",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name \"/^AnotherTest(#|::)/\"",
"bundle exec ruby /fixtures/minitest_tests.rb --name \"/^AnotherTest(#|::)/\"",
{
"start_line": 24,
"start_column": 0,
Expand Down Expand Up @@ -682,7 +682,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"AnotherTest",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name \"/^AnotherTest(#|::)/\"",
"bundle exec ruby /fixtures/minitest_tests.rb --name \"/^AnotherTest(#|::)/\"",
{
"start_line": 24,
"start_column": 0,
Expand Down Expand Up @@ -716,7 +716,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"AnotherTest#test_public",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name AnotherTest#test_public",
"bundle exec ruby /fixtures/minitest_tests.rb --name AnotherTest#test_public",
{
"start_line": 25,
"start_column": 2,
Expand Down Expand Up @@ -749,7 +749,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"AnotherTest#test_public",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name AnotherTest#test_public",
"bundle exec ruby /fixtures/minitest_tests.rb --name AnotherTest#test_public",
{
"start_line": 25,
"start_column": 2,
Expand Down Expand Up @@ -782,7 +782,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"AnotherTest#test_public",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name AnotherTest#test_public",
"bundle exec ruby /fixtures/minitest_tests.rb --name AnotherTest#test_public",
{
"start_line": 25,
"start_column": 2,
Expand Down Expand Up @@ -815,7 +815,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"AnotherTest#test_public_2",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name AnotherTest#test_public_2",
"bundle exec ruby /fixtures/minitest_tests.rb --name AnotherTest#test_public_2",
{
"start_line": 31,
"start_column": 2,
Expand Down Expand Up @@ -848,7 +848,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"AnotherTest#test_public_2",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name AnotherTest#test_public_2",
"bundle exec ruby /fixtures/minitest_tests.rb --name AnotherTest#test_public_2",
{
"start_line": 31,
"start_column": 2,
Expand Down Expand Up @@ -881,7 +881,7 @@
"arguments": [
"/fixtures/minitest_tests.rb",
"AnotherTest#test_public_2",
"bundle exec ruby -Itest /fixtures/minitest_tests.rb --name AnotherTest#test_public_2",
"bundle exec ruby /fixtures/minitest_tests.rb --name AnotherTest#test_public_2",
{
"start_line": 31,
"start_column": 2,
Expand Down
Loading

0 comments on commit 30f8547

Please sign in to comment.