Skip to content

Fix NullClient#send_notification #426

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

Merged
merged 3 commits into from
Jul 31, 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
8 changes: 5 additions & 3 deletions lib/ruby_lsp/ruby_lsp_rails/runner_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def make_request(request, params = nil)
read_response
end

sig { params(request: String, params: T.nilable(T::Hash[Symbol, T.untyped])).void }
sig { overridable.params(request: String, params: T.nilable(T::Hash[Symbol, T.untyped])).void }
def send_message(request, params = nil)
message = { method: request }
message[:params] = params if params
Expand All @@ -181,9 +181,11 @@ def send_message(request, params = nil)
# The server connection died
end

alias_method :send_notification, :send_message
# Notifications are like messages, but one-way, with no response sent back.
sig { params(request: String, params: T.nilable(T::Hash[Symbol, T.untyped])).void }
def send_notification(request, params = nil) = send_message(request, params)
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 chose this over the alternatives, so subclasses don't have to remember to add their own alias/override.

It's unlikely that somebody would not want it to just mean the same thing as send_message, but if they did, they still have the freedom to override it different, if they would like.


sig { returns(T.nilable(T::Hash[Symbol, T.untyped])) }
sig { overridable.returns(T.nilable(T::Hash[Symbol, T.untyped])) }
def read_response
headers = @stdout.gets("\r\n\r\n")
raise IncompleteMessageError unless headers
Expand Down
28 changes: 28 additions & 0 deletions test/ruby_lsp_rails/runner_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,33 @@ class RunnerClientTest < ActiveSupport::TestCase
FileUtils.mv("test/dummy/config/application.rb.bak", "test/dummy/config/application.rb")
end
end

class NullClientTest < ActiveSupport::TestCase
setup { @client = NullClient.new }

test "#shutdown is a no-op" do
assert_nothing_raised { @client.shutdown }
end

test "#stopped? is always true" do
assert_predicate @client, :stopped?
end

test "#rails_root is just the current working directory" do
assert_equal Dir.pwd, @client.rails_root
end

test "#send_message is a no-op" do
assert_nothing_raised { @client.send(:send_message, "request", nil) }
end

test "#send_notification is a no-op" do
assert_nothing_raised { @client.send(:send_notification, "request", nil) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prior to the fix, this failed with:

Error:
RubyLsp::Rails::NullClientTest#test_#send_notification_is_a_no-op:
NoMethodError: undefined method `write' for nil
    lib/ruby_lsp/ruby_lsp_rails/runner_client.rb:180:in `send_message'
    test/ruby_lsp_rails/runner_client_test.rb:111:in `block (2 levels) in <class:NullClientTest>'
    /Users/alex/.gem/ruby/3.3.0/gems/activesupport-7.1.3.3/lib/active_support/testing/assertions.rb:49:in `assert_nothing_raised'
    test/ruby_lsp_rails/runner_client_test.rb:111:in `block in <class:NullClientTest>'

end

test "#read_response is a no-op" do
assert_nothing_raised { @client.send(:read_response) }
end
end
end
end
Loading