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

Extract RubyLsp::TestHelper for addons to use #1833

Merged
merged 1 commit into from Mar 26, 2024

Conversation

andyw8
Copy link
Contributor

@andyw8 andyw8 commented Mar 26, 2024

This will allow us to use with_server in addons, e.g. Shopify/ruby-lsp-rails#301

@andyw8 andyw8 added chore Chore task server This pull request should be included in the server gem's release notes labels Mar 26, 2024
# NOTE: This module is intended to be used by addons for writing their own tests, so keep that in mind if changing.

module RubyLsp
module TestCase
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've made this a module rather than a class so that it can used from addons that don't use minitest, such as ruby-lsp-rspec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(maybe RubyLsp::TestHelpers would be a better name)

module TestCase
extend T::Sig

sig do
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we remove all the Sorbet aspects from this, so that we're not forcing addon authors to use it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually it doesn't matter, sorbet-runtime will be available already.

@andyw8 andyw8 requested a review from vinistock March 26, 2024 15:30
@andyw8 andyw8 force-pushed the andyw8/extract-ruby-lsp-test-case branch from e8e14ad to d6edc59 Compare March 26, 2024 15:52
# NOTE: This module is intended to be used by addons for writing their own tests, so keep that in mind if changing.

module RubyLsp
module TestCase
Copy link
Member

Choose a reason for hiding this comment

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

IMO, TestCase should be a class instead of a module. We can rename it to TestHelper if we prefer the module style. But personally I'd prefer it being a class and just inherit it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But if it's a class then we couldn't use it for https://github.com/st0012/ruby-lsp-rspec/blob/main/spec/ruby_lsp_rspec_spec.rb, right?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm that's a good point. Maybe we can learn from what Rails does? ActionDispatch::IntegrationTest can be used as a class, but most of its behaviours are defined in the Behavior module, which is then included by the class. And rspec-rails includes that module to provide the same behaviour as well.

In our case, RubyLsp::TestCase will be used in Ruby LSP itself and ruby-lsp-rspec. But in ruby-lsp-rspec I'd include the module like RubyLsp::TestHelper.

Of course, we can just rename this to RubyLsp::TestHelper for now. It's just one method and I won't insist on having a class atm.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, let's keep a module for now since it can be used with any test framework. But since it's a module, I'd vote for renaming it since TestCase is already commonly used for parent classes by ActiveSupport::TestCase and Test::Unit::TestCase.

I agree with TestHelper as suggested by Stan.

@andyw8 andyw8 force-pushed the andyw8/extract-ruby-lsp-test-case branch from d6edc59 to c5f8081 Compare March 26, 2024 16:52
Copy link
Member

@vinistock vinistock left a comment

Choose a reason for hiding this comment

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

Not strictly necessary to be included in this PR, but we should mention these test helpers in the addon documentation.

@@ -18,6 +18,7 @@
$VERBOSE = nil unless ENV["VERBOSE"] || ENV["CI"]

require_relative "../lib/ruby_lsp/internal"
require_relative "../lib/ruby_lsp/test_helper"
Copy link
Member

Choose a reason for hiding this comment

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

I know it's not related to this PR, but can we please update all of these to use regular requires? The lib directory gets inserted into the $LOAD_PATH a few lines above, there's no need to use relative requires.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#1834 because there's something I want to discuss about that approach.

@andyw8 andyw8 force-pushed the andyw8/extract-ruby-lsp-test-case branch 2 times, most recently from d6a4726 to dac14e9 Compare March 26, 2024 17:05
@andyw8 andyw8 changed the title Extract RubyLsp TestCase Extract RubyLsp::TestHelper for addons to use Mar 26, 2024
end
def with_server(source = nil, uri = Kernel.URI("file:///fake.rb"), &block)
server = RubyLsp::Server.new(test_mode: true)
server.process_message(method: "initialized")
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 added this as it's needed for some of the ruby-lsp-rails tests. Depending on the request it may not always be needed, but it doesn't seem to have much of an overhead.

@andyw8 andyw8 marked this pull request as ready for review March 26, 2024 17:11
@andyw8 andyw8 requested a review from a team as a code owner March 26, 2024 17:11
@andyw8 andyw8 requested review from Morriar and st0012 March 26, 2024 17:11
@andyw8 andyw8 enabled auto-merge (squash) March 26, 2024 17:37
@andyw8 andyw8 merged commit 099143b into main Mar 26, 2024
38 of 39 checks passed
@andyw8 andyw8 deleted the andyw8/extract-ruby-lsp-test-case branch March 26, 2024 19:48
@andyw8 andyw8 mentioned this pull request Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore Chore task server This pull request should be included in the server gem's release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants