Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Jan 29, 2025
1 parent 169bf19 commit 4f3570e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
17 changes: 17 additions & 0 deletions lib/ruby_lsp/tapioca/server_addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def execute(request, params)
).load_dsl_extensions_and_compilers
when "dsl"
fork { dsl(params[:constants]) }
when "gem"
# confirm if we need to fork or not
fork { gem(params[:added_or_modified_gems], params[:removed_gems]) }
when "route_dsl"
fork do
constants = ::Tapioca::Dsl::Compilers::UrlHelpers.gather_constants
Expand All @@ -46,6 +49,20 @@ def dsl(constants, *args)

::Tapioca::Cli.start(arguments)
end

def gem(added_or_modified_gems, removed_gems)
$stderr.puts "gem1"
# need?
load("tapioca/cli.rb") # Reload the CLI to reset thor defaults between requests

arguments = ["gem"]
arguments.push("--lsp_addon", "--workers=1")
arguments.concat(added_or_modified_gems)

# TODO: remove `removed_gems`

::Tapioca::Cli.start(arguments)
end
end
end
end
56 changes: 52 additions & 4 deletions spec/tapioca/addon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

module RubyLsp
module Tapioca
class AddonSpec < Minitest::HooksSpec
class AddonSpec < ::Tapioca::SpecWithProject
# The approach here is based on tests within the Ruby LSP Rails gem

it "generates DSL RBIs for a given constant" do
Expand All @@ -29,6 +29,51 @@ class AddonSpec < Minitest::HooksSpec
FileUtils.rm_rf("spec/dummy/sorbet/rbi")
end

it "generates gem RBIs" do
@project.write!("config/environment.rb", <<~RB)
require_relative "application.rb"
RB

@project.write!("config/application.rb", <<~RB)
require "rails"
module Test
class Application < Rails::Application
end
end
RB

@project.require_real_gem("rails")

content = File.read("spec/dummy/bin/rails")
@project.write!("bin/rails", content)
create_client(@project.absolute_path)
FOO_RB = <<~RB
module Foo
end
RB
foo = mock_gem("foo", "0.0.1") do
write!("lib/foo.rb", FOO_RB)
end
@project.write!("Gemfile", @project.tapioca_gemfile)
@project.require_mock_gem(foo)
@project.bundle_install!

@client.delegate_notification(
server_addon_name: "Tapioca",
request_name: "gem",
added_or_modified_gems: ["foo"],
removed_gems: ["bar"], # TODO: handle removal (separate test)
)
wait_until_exists(@project.absolute_path_to("sorbet/rbi/gems/[email protected]"))

shutdown_client

assert_path_exists(@project.absolute_path_to("sorbet/rbi/gems/[email protected]"))
ensure
FileUtils.rm_rf("spec/dummy/sorbet/rbi")
end

it "triggers route DSL generation if routes.rb is modified" do
create_client

Expand Down Expand Up @@ -70,9 +115,9 @@ class AddonSpec < Minitest::HooksSpec
private

# Starts a new client
def create_client
def create_client(path = "spec/dummy")
@outgoing_queue = Thread::Queue.new
@client = FileUtils.chdir("spec/dummy") do
@client = FileUtils.chdir(path) do
RubyLsp::Rails::RunnerClient.new(@outgoing_queue)
end

Expand All @@ -99,7 +144,10 @@ def wait_until_exists(path)
sleep(0.2) until File.exist?(path)
end
rescue Timeout::Error
flunk("#{path} was not created in time")
# flunk("#{path} was not created in time")
while (msg = @outgoing_queue.pop)
puts msg.params.message
end
end
end
end
Expand Down

0 comments on commit 4f3570e

Please sign in to comment.