Skip to content

Do not fail on first activation #40

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 2 commits into from
Apr 21, 2023
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
2 changes: 2 additions & 0 deletions lib/ruby-lsp-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# frozen_string_literal: true

require "sorbet-runtime"
require "pathname"

require "ruby_lsp_rails/version"
require "ruby_lsp_rails/railtie"

Expand Down
15 changes: 13 additions & 2 deletions lib/ruby_lsp/ruby_lsp_rails/rails_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module RubyLsp
module Rails
class RailsClient
class ServerNotRunningError < StandardError; end
class NeedsRestartError < StandardError; end

extend T::Sig
include Singleton
Expand All @@ -20,9 +21,19 @@ class ServerNotRunningError < StandardError; end

sig { void }
def initialize
@root = T.let(Dir.exist?("test/dummy") ? File.join(Dir.pwd, "test", "dummy") : Dir.pwd, String)
base_uri = File.read("#{@root}/tmp/app_uri.txt").chomp
project_root = Pathname.new(ENV["BUNDLE_GEMFILE"]).dirname
dummy_path = File.join(project_root, "test", "dummy")
@root = T.let(Dir.exist?(dummy_path) ? dummy_path : project_root.to_s, String)
app_uri_path = "#{@root}/tmp/app_uri.txt"

unless File.exist?(app_uri_path)
raise NeedsRestartError, <<~MESSAGE
The Ruby LSP Rails extension needs to be initialized. Please restart the Rails server and the Ruby LSP
to get Rails features in the editor
MESSAGE
end

base_uri = File.read(app_uri_path).chomp
@uri = T.let("#{base_uri}/ruby_lsp_rails", String)
end

Expand Down
17 changes: 17 additions & 0 deletions test/lib/rails_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ class RailsClientTest < ActiveSupport::TestCase
stub_http_request("200", expected_response.to_json)
assert_equal(expected_response, RailsClient.instance.model("User"))
end

test "raises during instantiation if app_uri file doesn't exist" do
project_root = Pathname.new(ENV["BUNDLE_GEMFILE"]).dirname
app_uri_path = "#{project_root}/test/dummy/tmp/app_uri.txt"
FileUtils.rm(app_uri_path)

# If the RailsClient singleton was initialized in a different test successfully, then there would be no chance
# for this assertion to pass. We need to reset the singleton instance in order to force `initialize` to be
# executed again
Singleton.send(:__init__, RailsClient)
Copy link
Contributor

Choose a reason for hiding this comment

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

TIL (was looking for something like this recently).


assert_raises(RailsClient::NeedsRestartError) do
RailsClient.instance
end
ensure
File.write(T.must(app_uri_path), "http://localhost:3000")
end
end
end
end