Skip to content

Commit

Permalink
Add launch mode integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Oct 23, 2024
1 parent 5ac10ef commit 30423f0
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 5 deletions.
3 changes: 3 additions & 0 deletions exe/ruby-lsp-launcher
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ locked_bundler_version_file = File.join(".ruby-lsp", "locked_bundler_version")
# Composed the Ruby LSP bundle in a forked process so that we can require gems without polluting the main process
# `$LOAD_PATH` and `Gem.loaded_specs`
pid = fork do
# Activate our own dependencies in the forked process only, so that it doesn't pollute the main process. We need to be
# able to require gems in the forked process to set up the composed bundle
Gem::Specification.find_by_name("ruby-lsp").activate
require_relative "../lib/ruby_lsp/setup_bundler"
require "json"
require "uri"
Expand Down
4 changes: 3 additions & 1 deletion lib/ruby_lsp/global_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ def dot_rubocop_yml_present
sig { returns(T::Array[String]) }
def gather_direct_dependencies
Bundler.with_original_env { Bundler.default_gemfile }
Bundler.locked_gems.dependencies.keys + gemspec_dependencies

dependencies = Bundler.locked_gems&.dependencies&.keys || []
dependencies + gemspec_dependencies
rescue Bundler::GemfileNotFound
[]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ def workspace_dependencies(message)
}
end
end
rescue Bundler::GemfileNotFound
rescue Bundler::GemfileNotFound, Bundler::GitError
[]
end

Expand Down
4 changes: 4 additions & 0 deletions project-words
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ autoloaded
autorun
bigdecimal
bindir
binmode
binread
Bizt
Bizw
bufnr
bytesize
byteslice
codepoint
codepoints
Expand All @@ -18,6 +20,7 @@ dont
eglot
Eglot
eruby
exitstatus
EXTGLOB
FIXEDENCODING
Floo
Expand Down Expand Up @@ -46,6 +49,7 @@ nargs
nodoc
noreturn
nvim
popen
qtlzwssomeking
quickfixes
quxx
Expand Down
103 changes: 100 additions & 3 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,125 @@
# frozen_string_literal: true

require "test_helper"
require "open3"

class IntegrationTest < Minitest::Test
def setup
def test_ruby_lsp_doctor_works
skip("CI only") unless ENV["CI"]
end

def test_ruby_lsp_doctor_works
in_isolation do
system("bundle exec ruby-lsp --doctor")
assert_equal(0, $CHILD_STATUS)
end
end

def test_ruby_lsp_check_works
skip("CI only") unless ENV["CI"]

in_isolation do
system("bundle exec ruby-lsp-check")
assert_equal(0, $CHILD_STATUS)
end
end

def test_launch_mode_with_no_gemfile
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
Bundler.with_unbundled_env do
launch_and_shutdown(dir)
end
end
end
end

def test_launch_mode_with_missing_lockfile
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
File.write(File.join(dir, "Gemfile"), <<~RUBY)
source "https://rubygems.org"
gem "stringio"
RUBY

Bundler.with_unbundled_env do
launch_and_shutdown(dir)
end
end
end
end

def test_launch_mode_with_full_bundle
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
File.write(File.join(dir, "Gemfile"), <<~RUBY)
source "https://rubygems.org"
gem "stringio"
RUBY

lockfile_contents = <<~LOCKFILE
GEM
remote: https://rubygems.org/
specs:
stringio (3.1.0)
PLATFORMS
arm64-darwin-23
ruby
DEPENDENCIES
stringio
BUNDLED WITH
2.5.7
LOCKFILE
File.write(File.join(dir, "Gemfile.lock"), lockfile_contents)

Bundler.with_unbundled_env do
launch_and_shutdown(dir)
end
end
end
end

private

def launch_and_shutdown(workspace_path)
stdin, stdout, stderr, wait_thr = Open3.popen3("#{Bundler.root}/exe/ruby-lsp --use-launcher")
stdin.binmode
stdin.sync = true
stdout.binmode
stdout.sync = true
stderr.binmode
stderr.sync = true

initialize_request = {
id: 1,
method: "initialize",
params: {
initializationOptions: {},
capabilities: { general: { positionEncodings: ["utf-8"] } },
workspaceFolders: [{ uri: URI::Generic.from_path(path: workspace_path).to_s }],
},
}.to_json
stdin.write("Content-Length: #{initialize_request.bytesize}\r\n\r\n#{initialize_request}")

shutdown_request = {
id: 2,
method: "shutdown",
}.to_json
stdin.write("Content-Length: #{shutdown_request.bytesize}\r\n\r\n#{shutdown_request}")

exit_notification = {
method: "exit",
}.to_json
stdin.write("Content-Length: #{exit_notification.bytesize}\r\n\r\n#{exit_notification}")

assert_equal(0, T.unsafe(wait_thr.value).exitstatus, "server crashed: #{stderr.read}")

stdin.close
stdout.close
stderr.close
end

def in_isolation(&block)
gem_path = Bundler.root
Dir.mktmpdir do |dir|
Expand Down

0 comments on commit 30423f0

Please sign in to comment.