-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathintegration_test.rb
More file actions
479 lines (390 loc) · 12.7 KB
/
integration_test.rb
File metadata and controls
479 lines (390 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# typed: true
# frozen_string_literal: true
require "test_helper"
class IntegrationTest < Minitest::Test
def setup
@root = Bundler.root
end
def test_ruby_lsp_doctor_works
skip("CI only") unless ENV["CI"]
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_activation_script_succeeds_even_on_binary_encoding
ENV["LC_ALL"] = "C"
ENV["LANG"] = "C"
ENV["PS1"] = "\xE2\x96\xB7".b
_stdout, stderr, status = Open3.capture3(
"ruby",
"-EUTF-8:UTF-8",
File.join(__dir__, "..", "vscode", "activation.rb"),
)
assert_equal(0, status.exitstatus, stderr)
match = /RUBY_LSP_ACTIVATION_SEPARATOR(.*)RUBY_LSP_ACTIVATION_SEPARATOR/m.match(stderr) #: as !nil
activation_string = match[1] #: as !nil
version, gem_path, yjit, *fields = activation_string.split("RUBY_LSP_FS")
assert_equal(RUBY_VERSION, version)
refute_nil(gem_path)
assert(yjit)
assert_includes(fields, "PS1RUBY_LSP_VS#{ENV["PS1"]}")
fields.each do |field|
key, value = field.split("RUBY_LSP_VS")
refute_equal(key.encoding, Encoding::BINARY) if key
refute_equal(value.encoding, Encoding::BINARY) if value
end
end
def test_chruby_activation_script
_stdout, stderr, status = Open3.capture3(
"ruby",
"-EUTF-8:UTF-8",
File.join(__dir__, "..", "vscode", "chruby_activation.rb"),
RUBY_VERSION,
)
assert_equal(0, status.exitstatus, stderr)
default_gems, gem_home, yjit, version = stderr.split("RUBY_LSP_ACTIVATION_SEPARATOR")
assert_equal(RUBY_VERSION, version)
# These may be switched in CI due to Bundler settings, so we use simpler assertions
assert(yjit)
assert(gem_home)
assert(default_gems)
end
def test_activation_script_succeeds_on_invalid_unicode
ENV["LC_ALL"] = "C"
ENV["LANG"] = "C"
ENV["INVALID_UTF8"] = "\xE2\x80".b
_stdout, stderr, status = Open3.capture3(
"ruby",
"-EUTF-8:UTF-8",
File.join(__dir__, "..", "vscode", "activation.rb"),
)
assert_equal(0, status.exitstatus, stderr)
match = /RUBY_LSP_ACTIVATION_SEPARATOR(.*)RUBY_LSP_ACTIVATION_SEPARATOR/m.match(stderr) #: as !nil
activation_string = match[1] #: as !nil
version, gem_path, yjit, *fields = activation_string.split("RUBY_LSP_FS")
assert_equal(RUBY_VERSION, version)
refute_nil(gem_path)
assert(yjit)
fields.each do |field|
key, value = field.split("RUBY_LSP_VS")
refute_equal(key.encoding, Encoding::BINARY) if key
refute_equal(value.encoding, Encoding::BINARY) if value
end
end
def test_uses_same_bundler_version_as_main_app
in_temp_dir do |dir|
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.7)
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
capture_subprocess_io do
system("bundle install")
end
end
Bundler.with_unbundled_env do
launch(dir, "ruby-lsp")
end
assert_match(/BUNDLED WITH\n\s*2.5.7/, File.read(File.join(dir, ".ruby-lsp", "Gemfile.lock")))
end
end
def test_does_not_use_custom_binstubs_if_they_are_in_the_path
in_temp_dir do |dir|
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.7)
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
capture_subprocess_io do
system("bundle install")
end
end
bin_path = File.join(dir, "bin")
FileUtils.mkdir(bin_path)
File.write(File.join(bin_path, "bundle"), <<~RUBY)
#!/usr/bin/env ruby
raise "This should not be called"
RUBY
FileUtils.chmod(0o755, File.join(bin_path, "bundle"))
Bundler.with_unbundled_env do
launch(dir, "ruby-lsp", { "PATH" => "#{bin_path}#{File::PATH_SEPARATOR}#{ENV["PATH"]}" })
end
assert_match(/BUNDLED WITH\n\s*2.5.7/, File.read(File.join(dir, ".ruby-lsp", "Gemfile.lock")))
end
end
def test_launch_mode_with_no_gemfile
in_temp_dir do |dir|
Bundler.with_unbundled_env do
launch(dir)
end
end
end
def test_launch_mode_with_missing_lockfile
in_temp_dir do |dir|
File.write(File.join(dir, "Gemfile"), <<~RUBY)
source "https://rubygems.org"
gem "stringio"
RUBY
Bundler.with_unbundled_env do
launch(dir)
end
end
end
def test_launch_mode_with_full_bundle
in_temp_dir do |dir|
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(dir)
end
end
end
def test_launch_mode_with_no_gemfile_and_bundle_path
in_temp_dir do |dir|
Bundler.with_unbundled_env do
system("bundle config --local path #{File.join("vendor", "bundle")}")
assert_path_exists(File.join(dir, ".bundle", "config"))
launch(dir)
end
end
end
def test_composed_bundle_includes_debug
in_temp_dir do |dir|
Bundler.with_unbundled_env do
launch(dir)
_stdout, stderr = capture_subprocess_io do
system(
{ "BUNDLE_GEMFILE" => File.join(dir, ".ruby-lsp", "Gemfile") },
"bundle exec ruby -e 'require \"debug\"'",
)
end
refute_match(/cannot load such file/, stderr)
end
end
end
def test_launch_mode_with_bundle_package
in_temp_dir do |dir|
File.write(File.join(dir, "Gemfile"), <<~RUBY)
source "https://rubygems.org"
gem "stringio"
RUBY
Bundler.with_unbundled_env do
capture_subprocess_io do
system("bundle", "install")
system("bundle", "package")
end
cached_gems = Dir.glob("#{dir}/vendor/cache/*.gem")
refute_empty(cached_gems)
launch(dir)
assert_empty(Dir.glob("#{dir}/vendor/cache/*.gem") - cached_gems)
end
end
end
def test_launch_mode_retries_if_setup_failed_after_successful_install
# RubyGems asks for confirmation when uninstalling a gem, so we need to be able to write the `y` to stdin
uninstall_rails = ->() {
stdin, _stdout, _stderr, wait_thread = Open3.popen3("gem", "uninstall", "rails")
stdin.write("y\n")
wait_thread.join
}
in_temp_dir do |dir|
File.write(File.join(dir, "Gemfile"), <<~RUBY)
source "https://rubygems.org"
gem "rails"
RUBY
Bundler.with_unbundled_env do
# Generate a lockfile first
capture_subprocess_io { system("bundle", "install") }
# Uninstall the gem so that composing the bundle has to install it
uninstall_rails.call
# Preemptively create the bundle_env file and acquire an exclusive lock on it, so that composing the bundle will
# have to pause immediately after bundle installing, but before invoking Bundler.setup
bundle_env_path = File.join(dir, ".ruby-lsp", "bundle_env")
FileUtils.mkdir_p(File.dirname(bundle_env_path))
FileUtils.touch(bundle_env_path)
thread = Thread.new do
File.open(bundle_env_path) do |f|
f.flock(File::LOCK_EX)
# Give the bundle compose enough time to finish and get stuck on the lock
sleep(2)
# Uninstall Rails after successfully bundle installing and before invoking Bundler.setup, which will cause
# it to fail with `Bundler::GemNotFound`. This triggers our retry mechanism
uninstall_rails.call
end
end
launch(dir)
thread.join
end
end
end
def test_update_server_command
in_temp_dir do |dir|
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.7)
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
stdout, _ = capture_subprocess_io do
system(File.join(@root, "exe", "ruby-lsp"), "--update-server")
end
assert_includes(stdout, "Executing server update...")
end
end
end
private
def launch(workspace_path, exec = "ruby-lsp-launcher", extra_env = {})
specification = Gem::Specification.find_by_name("ruby-lsp")
paths = [specification.full_gem_path]
paths.concat(specification.dependencies.filter_map { |dep| dep.to_spec&.full_gem_path })
load_path = $LOAD_PATH.filter_map do |path|
next unless paths.any? { |gem_path| path.start_with?(gem_path) } || !path.start_with?(Bundler.bundle_path.to_s)
["-I", File.expand_path(path)]
end.uniq.flatten
stdin, stdout, stderr, wait_thr = Open3 #: as untyped
.popen3(
extra_env,
Gem.ruby,
*load_path,
File.join(@root, "exe", exec),
)
stdin.sync = true
stdin.binmode
stdout.sync = true
stdout.binmode
stderr.sync = true
stderr.binmode
send_message(stdin, {
id: 1,
method: "initialize",
params: {
initializationOptions: {},
capabilities: { general: { positionEncodings: ["utf-8"] } },
workspaceFolders: [{ uri: URI::Generic.from_path(path: workspace_path).to_s }],
},
})
# First message is the log of initializing Ruby LSP
read_message(stdout)
# Verify that initialization didn't fail
initialize_response = read_message(stdout)
refute(initialize_response[:error], initialize_response.dig(:error, :message))
send_message(stdin, { id: 2, method: "shutdown" })
send_message(stdin, { method: "exit" })
# Wait until the process exits
wait_thr.join
# If the child process failed, it is really difficult to diagnose what's happening unless we read what was printed
# to stderr
unless wait_thr.value.success?
require "timeout"
Timeout.timeout(5) do
flunk("Process failed\n#{stderr.read}")
end
end
assert_path_exists(File.join(workspace_path, ".ruby-lsp", "Gemfile"))
assert_path_exists(File.join(workspace_path, ".ruby-lsp", "Gemfile.lock"))
refute_path_exists(File.join(workspace_path, ".ruby-lsp", "install_error"))
end
def send_message(stdin, message)
json_message = message.to_json
stdin.write("Content-Length: #{json_message.bytesize}\r\n\r\n#{json_message}")
stdin.flush
end
def read_message(stdout)
headers = stdout.gets("\r\n\r\n")
length = headers[/Content-Length: (\d+)/i, 1].to_i
JSON.parse(stdout.read(length), symbolize_names: true)
end
def in_temp_dir
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
yield(dir)
end
end
end
def in_isolation(&block)
gem_path = Bundler.root
in_temp_dir do |dir|
File.write(File.join(dir, "Gemfile"), <<~GEMFILE)
source "https://rubygems.org"
gem "ruby-lsp", path: "#{gem_path}"
GEMFILE
Bundler.with_unbundled_env do
capture_subprocess_io do
system("bundle install")
# Only do this after `bundle install` as to not change the lockfile
File.write(File.join(dir, "Gemfile"), <<~GEMFILE, mode: "a+")
# This causes ruby-lsp to run in its own directory without
# all the supplementary gems like rubocop
Dir.chdir("#{gem_path}")
GEMFILE
yield
end
end
end
end
end