Allow ENV to override GlobalState#test_library #1928
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
In our project we have a small amount of rspec tests and a large amount of minitest tests. The test library detection method prefers rspec over minitest and as a result, the testing code lenses don't work for the vast majority of our tests.
Having multiple test frameworks in-use in a single project is certainly not standard. Ours is a monorepo-ish application born years ago, being converted from engines to packwerk, so it's got a sprinkling of various things in it. We're looking for some way to be able to override the test library detection to be able to force it to
"rails"
.Implementation
Potential solutions I've dreamed up:
Addon
that inherits fromRubyLsp::Rails::Addon
but removes this restriction: https://github.com/Shopify/ruby-lsp-rails/blob/8c0e73a2e938c5cda5e7ff20098d78d132f09853/lib/ruby_lsp/ruby_lsp_rails/addon.rb#L50Same as (1) except we fork the RubyLsp::Rails repo and remove the line.
Allow an ENV var to specify the testing library
.ruby-lsp.yml
config file #1434)monkey-patchingbehavior modifications. Something like RubyLsp looks forruby-lsp.rb
or something at the root dir and justload
s it as part of its booting process. In my case, I could use it to prepend my own module to the library, but I could also see this as a way to make small Addons where creating a full gem seems like a lot. Sort of a use-at-your-own-risk escape hatch.ruby-lsp.rb
in my home directory that uses that uses theconst_defined
callback to modifyRubyLsp::GlobalState
as its loaded. Then Iexport RUBYOPT="-r ${HOME}/ruby-lsp"
to get my fingers into every single ruby process and I convince my coworkers to do the same.In this PR I've gone with option 3.
Though again, I can understand if this whole minitest/rspec living together is not a use-case you care to support. Which is one reason I actually kind of want option 4 (some way to hook into the the various ruby-lsp processes being run). It would be a simple outlet for us if you don't want to support things like this and it would enable us to deal with any other issues that arise immediately while we see about upstreaming solutions.
Automated Tests
I added the test. It's a bit annoying to mutate the ENV and put it back correctly. It's also not thread-safe. But it works.
Manual Tests
Make a rails project that contains both rspec and minitest gems and tests. The test runners will be present for the rspec tests and not for the minitest tests. Then,
export RUBY_LSP_TEST_LIBRARY="minitest"
and reload the window. The opposite will now be true.