Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect rails as the testing framework if bin/rails exists #1896

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/ruby_lsp/global_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ def detect_test_library
if direct_dependency?(/^rspec/)
"rspec"
# A Rails app may have a dependency on minitest, but we would instead want to use the Rails test runner provided
# by ruby-lsp-rails.
elsif direct_dependency?(/^rails$/)
# by ruby-lsp-rails. A Rails app doesn't need to depend on the rails gem itself, individual components like
# activestorage may be added to the gemfile so that other components aren't downloaded. Check for the presence
# of bin/rails to support these cases.
elsif File.exist?(File.join(workspace_path, "bin/rails"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
elsif File.exist?(File.join(workspace_path, "bin/rails"))
elsif File.exist?(File.join(workspace_path, "bin", "rails"))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry I clicked merge at the same time 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's apply this change. We can't use forward slashes for Windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, is that really the fix then? https://ruby-doc.org/3.3.0/File.html#method-c-join doesn't mention this being platform-aware and seems to always use / anyways.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then maybe we should be using Pathname instead and need to switch this to

Suggested change
elsif File.exist?(File.join(workspace_path, "bin/rails"))
elsif Pathname.new(workspace_path).join("bin").join("rails").exist?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, maybe Ruby already takes care of that under the hood https://github.com/ruby/ruby/blob/b09604e1fd5768daf31aaa4f8130fa8cd9b8d240/file.c#L252.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine, yeah. I wanted to properly check in my VM but I don't think I'm going through the bother of properly setting ruby up, so a boring irb session will have to do:

image

"rails"
# NOTE: Intentionally ends with $ to avoid mis-matching minitest-reporters, etc. in a Rails app.
elsif direct_dependency?(/^minitest$/)
Expand Down
8 changes: 5 additions & 3 deletions test/global_state_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ def test_detects_dependencies_in_gemspecs
assert(GlobalState.new.direct_dependency?(/^prism$/))
end

def test_detects_rails_if_both_rails_and_minitest_are_present
stub_dependencies("minitest" => "1.2.3", "rails" => "1.2.3")
def test_detects_rails_if_minitest_is_present_and_bin_rails_exists
stub_dependencies("minitest" => "1.2.3")
File.expects(:exist?).with("#{Dir.pwd}/bin/rails").once.returns(true)

assert_equal("rails", GlobalState.new.test_library)
end

def test_detects_rspec_if_both_rails_and_rspec_are_present
stub_dependencies("rspec" => "1.2.3", "rails" => "1.2.3")
stub_dependencies("rspec" => "1.2.3")
File.expects(:exist?).never

assert_equal("rspec", GlobalState.new.test_library)
end
Expand Down
Loading