st0012 / ruby-lsp-rspec

A Ruby LSP addon for RSpec tests
MIT License
127 stars 7 forks source link

Only handle requests in _{spec,test}.rb files #6

Closed rohitpaulk closed 1 year ago

rohitpaulk commented 1 year ago

Ran into this issue today where I saw rspec markers in a non-spec file:

Screenshot 2023-10-28 at 21 43 51

This PR fixes that behaviour by restricting the codelens listener to only listen on files that are named _test.rb or _spec.rb.

Potential Issues

Rspec's default file pattern is _spec.rb, but that can be configured.

I vaguely remember coming across codebases that use _test.rb for specs, hence the added pattern. This can backfire if people use both rspec & minitest. Minitest has a spec syntax too, so ruby-lsp-rspec will end up creating incorrect markers on minitest files. Won't affect minitest-only users since I assume they won't have this addon installed.

I suspect the only accurate way to fix these issues would be to get the exact value of rspec's file pattern - either through static analysis, or by having a configuration option passed in. Doubt that either of these options are easy to do, but open to suggestions!

Other approaches

My first instinct was to improve how context is detected, by ignoring usages like context.foo. Thought about this a bit more, and dropped it because even a non-test file could have usages like context "foo" {} if it was using a non-rspec DSL.

rohitpaulk commented 1 year ago

Thanks for cleaning up the lint errors!