Skip to content

Commit

Permalink
Allow ENV to override GlobalState#test_library
Browse files Browse the repository at this point in the history
In our project we have a very small amount of rspec tests and a large
amount of minitest tests. The test detection method prefers rspec over
minitest and as a result, the testing code lenses don't work for the
vast majority of our tests.

This change allows a user to set an ENV variable to force the
test_library to be what they choose.
  • Loading branch information
petekinnecom committed Apr 12, 2024
1 parent 9136779 commit d180c21
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/ruby_lsp/global_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ def detect_formatter

sig { returns(String) }
def detect_test_library
if direct_dependency?(/^rspec/)
if ENV.key?("RUBY_LSP_TEST_LIBRARY")
ENV["RUBY_LSP_TEST_LIBRARY"]
elsif 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. A Rails app doesn't need to depend on the rails gem itself, individual components like
Expand Down
14 changes: 14 additions & 0 deletions test/global_state_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ def test_detects_rspec_if_both_rails_and_rspec_are_present
assert_equal("rspec", GlobalState.new.test_library)
end

def test_detects_library_from_env_if_present
original_env = ENV.fetch("RUBY_LSP_TEST_LIBRARY", :not_present)
ENV["RUBY_LSP_TEST_LIBRARY"] = "minitest"
stub_dependencies("test-unit" => "1.2.3")
stub_dependencies("rspec" => "1.2.3")
assert_equal("minitest", GlobalState.new.test_library)
ensure
if original_env == :not_present
ENV.delete("RUBY_LSP_TEST_LIBRARY")
else
ENV["RUBY_LSP_TEST_LIBRARY"] = original_env
end
end

def test_direct_dependency_returns_false_outside_of_bundle
File.expects(:file?).at_least_once.returns(false)
stub_dependencies({})
Expand Down

0 comments on commit d180c21

Please sign in to comment.