-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathrunner_client_test.rb
More file actions
331 lines (277 loc) · 11.9 KB
/
Copy pathrunner_client_test.rb
File metadata and controls
331 lines (277 loc) · 11.9 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
# typed: true
# frozen_string_literal: true
require "test_helper"
require "ruby_lsp/ruby_lsp_rails/runner_client"
module RubyLsp
module Rails
class RunnerClientTest < ActiveSupport::TestCase
setup do
@outgoing_queue = Thread::Queue.new
@global_state = GlobalState.new
@global_state.apply_options({
capabilities: {
window: {
workDoneProgress: true,
},
},
})
@client = RunnerClient.new(@outgoing_queue, @global_state) #: RunnerClient
end
teardown do
@client.shutdown
# On Windows, the server process sometimes takes a lot longer to shutdown and may end up getting force killed,
# which makes this assertion flaky
assert_predicate(@client, :stopped?) unless Gem.win_platform?
@outgoing_queue.close
end
# These are integration tests which start the server. For the more fine-grained tests, see `server_test.rb`.
test "#model returns information for the requested model" do
# These columns are from the schema in the dummy app: test/dummy/db/schema.rb
# The default values behavior changed in https://github.com/rails/rails/pull/54683
columns = if Gem::Version.new(::Rails.version) < Gem::Version.new("8.1.0.alpha")
[
["id", "integer", nil, false],
["first_name", "string", "", true],
["last_name", "string", nil, true],
["age", "integer", "0", true],
["created_at", "datetime", nil, false],
["updated_at", "datetime", nil, false],
["country_id", "integer", nil, false],
["active", "boolean", "1", false],
]
else
[
["id", "integer", nil, false],
["first_name", "string", "", true],
["last_name", "string", nil, true],
["age", "integer", 0, true],
["created_at", "datetime", nil, false],
["updated_at", "datetime", nil, false],
["country_id", "integer", nil, false],
["active", "boolean", true, false],
]
end
foreign_keys = ["country_id"]
indexes = [
{ name: "users_unique_complex", columns: "COALESCE(country_id, 0), ltrim(first_name)", unique: true },
{ name: "index_users_on_country_id", columns: ["country_id"], unique: false },
]
response = @client.model("User") #: as !nil
assert_equal(columns, response.fetch(:columns))
assert_equal(foreign_keys, response.fetch(:foreign_keys))
assert_equal(indexes.sort_by { |i| i[:name] }, response.fetch(:indexes).sort_by { |i| i[:name] })
assert_match(%r{db/schema\.rb$}, response.fetch(:schema_file))
end
test "returns nil if the request returns a nil response" do
assert_nil @client.model("ApplicationRecord") # ApplicationRecord is abstract
end
test "falls back to null client when bin/rails is not found" do
FileUtils.mv("bin/rails", "bin/rails_backup")
outgoing_queue = Thread::Queue.new
client = RunnerClient.create_client(outgoing_queue, @global_state)
begin
assert_instance_of(NullClient, client)
assert_nil(client.model("User"))
assert_predicate(client, :stopped?)
log = pop_log_notification(outgoing_queue, RubyLsp::Constant::MessageType::WARNING)
assert_instance_of(RubyLsp::Notification, log)
assert_match("Ruby LSP Rails failed to locate bin/rails in the current directory", log.params.message)
ensure
outgoing_queue.close
FileUtils.mv("bin/rails_backup", "bin/rails")
end
end
test "failing to spawn server creates a null client" do
FileUtils.mv("test/dummy/config/application.rb", "test/dummy/config/application.rb.bak")
outgoing_queue = Thread::Queue.new
client = RunnerClient.create_client(outgoing_queue, @global_state)
begin
assert_instance_of(NullClient, client)
assert_nil(client.model("User"))
assert_predicate(client, :stopped?)
log = pop_log_notification(outgoing_queue, RubyLsp::Constant::MessageType::ERROR)
assert_instance_of(RubyLsp::Notification, log)
assert_match("Ruby LSP Rails failed to initialize server", log.params.message)
ensure
outgoing_queue.close
FileUtils.mv("test/dummy/config/application.rb.bak", "test/dummy/config/application.rb")
end
end
test "is resilient to extra output being printed during boot" do
content = File.read("test/dummy/config/application.rb")
FileUtils.mv("test/dummy/config/application.rb", "test/dummy/config/application.rb.bak")
junk = %{\nputs "1\r\n\r\nhello"}
File.write("test/dummy/config/application.rb", content + junk)
outgoing_queue = Thread::Queue.new
client = RunnerClient.create_client(outgoing_queue, @global_state)
response = client.model("User") #: as !nil
begin
assert(response.key?(:columns))
ensure
outgoing_queue.close
FileUtils.mv("test/dummy/config/application.rb.bak", "test/dummy/config/application.rb")
end
end
test "fetches database configurations" do
assert_equal(
{
primary: {
migrations_paths: ["db/migrate"],
adapter_class: "ActiveRecord::ConnectionAdapters::SQLite3Adapter",
},
},
@client.db_configs,
)
end
test "delegate notification" do
@client.expects(:send_notification).with(
"server_addon/delegate",
server_addon_name: "My Add-on",
request_name: "do_something",
id: 5,
)
@client.delegate_notification(server_addon_name: "My Add-on", request_name: "do_something", id: 5)
end
test "delegate request" do
@client.expects(:make_request).with(
"server_addon/delegate",
server_addon_name: "My Add-on",
request_name: "do_something",
id: 5,
)
@client.delegate_request(server_addon_name: "My Add-on", request_name: "do_something", id: 5)
end
test "RUBY_LSP_RAILS_RUNNER environment variable is accessible in spawned server process" do
File.write("env_check_addon.rb", <<~RUBY)
class EnvCheckAddon < RubyLsp::Rails::ServerAddon
def name
"EnvCheck"
end
def execute(request, params)
send_result({ env_var: ENV["RUBY_LSP_RAILS_RUNNER"] })
end
end
RUBY
@client.register_server_addon(File.expand_path("env_check_addon.rb"))
response = @client.delegate_request(server_addon_name: "EnvCheck", request_name: "check") #: as !nil
assert_equal("true", response[:env_var])
ensure
FileUtils.rm_f("env_check_addon.rb")
end
test "server add-ons can log messages with the editor" do
File.write("server_addon.rb", <<~RUBY)
class TapiocaServerAddon < RubyLsp::Rails::ServerAddon
def name
"Tapioca"
end
def execute(request, params)
log_message("Hello!")
send_result({ request: request, params: params })
end
end
RUBY
@client.register_server_addon(File.expand_path("server_addon.rb"))
@client.delegate_notification(server_addon_name: "Tapioca", request_name: "dsl")
# Started booting server
pop_log_notification(@outgoing_queue, RubyLsp::Constant::MessageType::LOG)
# Finished booting server
pop_log_notification(@outgoing_queue, RubyLsp::Constant::MessageType::LOG)
log = @outgoing_queue.pop
# Sometimes we get warnings concerning deprecations and they mess up this expectation
3.times do
unless log.dig(:params, :message).match?(/Hello!/)
log = @outgoing_queue.pop
end
end
assert_match("Hello!", log.dig(:params, :message))
ensure
FileUtils.rm("server_addon.rb")
end
test "server add-ons can report progress" do
File.write("server_addon.rb", <<~RUBY)
class TapiocaServerAddon < RubyLsp::Rails::ServerAddon
def name
"Tapioca"
end
def execute(request, params)
begin_progress("my-progress-id", "Doing something expensive")
report_progress("my-progress-id", message: "Made some progress!")
end_progress("my-progress-id")
end
end
RUBY
@client.register_server_addon(File.expand_path("server_addon.rb"))
@client.delegate_notification(server_addon_name: "Tapioca", request_name: "dsl")
# Started booting server
pop_log_notification(@outgoing_queue, RubyLsp::Constant::MessageType::LOG)
# Finished booting server
pop_log_notification(@outgoing_queue, RubyLsp::Constant::MessageType::LOG)
messages = []
# Sometimes we get warnings concerning deprecations and they mess up this expectation
until messages.length == 4
message = @outgoing_queue.pop
messages << message if message.dig(:params, :token) == "my-progress-id"
end
assert_equal("window/workDoneProgress/create", messages.dig(0, :method))
assert_equal("begin", messages.dig(1, :params, :value, :kind))
assert_equal("report", messages.dig(2, :params, :value, :kind))
assert_equal("end", messages.dig(3, :params, :value, :kind))
ensure
FileUtils.rm("server_addon.rb")
end
test "server add-ons can report progress through block API" do
File.write("server_addon.rb", <<~RUBY)
class TapiocaServerAddon < RubyLsp::Rails::ServerAddon
def name
"Tapioca"
end
def execute(request, params)
with_progress("my-progress-id", "Doing something expensive") do |progress|
progress.report(message: "Made some progress!")
end
end
end
RUBY
@client.register_server_addon(File.expand_path("server_addon.rb"))
@client.delegate_notification(server_addon_name: "Tapioca", request_name: "dsl")
# Started booting server
pop_log_notification(@outgoing_queue, RubyLsp::Constant::MessageType::LOG)
# Finished booting server
pop_log_notification(@outgoing_queue, RubyLsp::Constant::MessageType::LOG)
messages = []
# Sometimes we get warnings concerning deprecations and they mess up this expectation
until messages.length == 4
message = @outgoing_queue.pop
messages << message if message.dig(:params, :token) == "my-progress-id"
end
assert_equal("window/workDoneProgress/create", messages.dig(0, :method))
assert_equal("begin", messages.dig(1, :params, :value, :kind))
assert_equal("report", messages.dig(2, :params, :value, :kind))
assert_equal("end", messages.dig(3, :params, :value, :kind))
ensure
FileUtils.rm("server_addon.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") }
end
test "#send_notification is a no-op" do
assert_nothing_raised { @client.send(:send_notification, "request") }
end
test "#read_response is a no-op" do
assert_nothing_raised { @client.send(:read_response) }
end
end
end
end