-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathserver_test.rb
More file actions
285 lines (236 loc) · 9.28 KB
/
Copy pathserver_test.rb
File metadata and controls
285 lines (236 loc) · 9.28 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
# typed: true
# frozen_string_literal: true
require "test_helper"
require "ruby_lsp/ruby_lsp_rails/server"
class ServerTest < ActiveSupport::TestCase
setup do
@stdout = StringIO.new
@stderr = StringIO.new
@server = RubyLsp::Rails::Server.new(stdout: @stdout, stderr: @stderr, override_default_output_device: false)
end
test "returns nil if model doesn't exist" do
@server.execute("model", { name: "Foo" })
assert_nil(response.fetch(:result))
end
test "returns nil if class is not a model" do
@server.execute("model", { name: "Time" })
assert_nil(response.fetch(:result))
end
test "returns nil if class is an abstract model" do
@server.execute("model", { name: "ApplicationRecord" })
assert_nil(response.fetch(:result))
end
test "returns nil if constant is not a class" do
@server.execute("model", { name: "RUBY_VERSION" })
assert_nil(response.fetch(:result))
end
test "doesn't fail if the class overrides `<`" do
class TestClassWithOverwrittenLessThan
class << self
def <(other)
raise
end
end
end
@server.execute("model", { name: "TestClassWithOverwrittenLessThan" })
assert_nil(response.fetch(:result))
end
test "handles older Rails version which don't have `schema_dump_path`" do
ActiveRecord::Tasks::DatabaseTasks.send(:alias_method, :old_schema_dump_path, :schema_dump_path)
ActiveRecord::Tasks::DatabaseTasks.undef_method(:schema_dump_path)
@server.execute("model", { name: "User" })
assert_nil(response.fetch(:result)[:schema_file])
ensure
ActiveRecord::Tasks::DatabaseTasks.send(:alias_method, :schema_dump_path, :old_schema_dump_path)
end
test "resolve association returns the location of the target class of a has_many association" do
@server.execute(
"association_target",
{ model_name: "Organization", association_name: :memberships },
)
location = response[:result][:location]
name = response[:result][:name]
assert_equal "Membership", name
assert_match %r{test/dummy/app/models/membership.rb:3$}, location
end
test "resolve association returns the location of the target class of a belongs_to association" do
@server.execute(
"association_target",
{ model_name: "Membership", association_name: :organization },
)
location = response[:result][:location]
name = response[:result][:name]
assert_equal "Organization", name
assert_match %r{test/dummy/app/models/organization.rb:3$}, location
end
test "resolve association returns the location of the target class of a has_one association" do
@server.execute(
"association_target",
{ model_name: "User", association_name: :profile },
)
location = response[:result][:location]
name = response[:result][:name]
assert_equal "Profile", name
assert_match %r{test/dummy/app/models/profile.rb:3$}, location
end
test "resolve association returns the location of the target class of a has_and_belongs_to_many association" do
@server.execute(
"association_target",
{ model_name: "Profile", association_name: :labels },
)
location = response[:result][:location]
name = response[:result][:name]
assert_equal "Label", name
assert_match %r{test/dummy/app/models/label.rb:3$}, location
end
test "resolve association handles invalid model name" do
@server.execute(
"association_target",
{ model_name: "NotHere", association_name: :labels },
)
assert_nil(response.fetch(:result))
end
test "resolve association handles invalid association name" do
@server.execute(
"association_target",
{ model_name: "Membership", association_name: :labels },
)
assert_nil(response.fetch(:result))
end
test "resolve association handles class_name option" do
@server.execute(
"association_target",
{ model_name: "User", association_name: :location },
)
location = response[:result][:location]
name = response[:result][:name]
assert_equal "Country", name
assert_match %r{test/dummy/app/models/country.rb:3$}, location
end
test "resolve database configurations" do
@server.execute("db_configs", {})
migrations_paths = response[:result][:primary][:migrations_paths]
adapter_class = response[:result][:primary][:adapter_class]
assert_includes migrations_paths, "#{dummy_root}/db/migrate"
assert_equal "ActiveRecord::ConnectionAdapters::SQLite3Adapter", adapter_class
end
test "route location returns the location for a valid route" do
@server.execute("route_location", { name: "user_path" })
location = response[:result][:location]
assert_match %r{test/dummy/config/routes.rb:5$}, location
end
test "route location returns nil for invalid routes" do
@server.execute("route_location", { name: "invalid_path" })
assert_nil response[:result]
end
test "route info" do
@server.execute("route_info", { controller: "UsersController", action: "index" })
result = response[:result]
source_location_path, source_location_line = result[:source_location]
assert_equal "5", source_location_line
assert source_location_path.end_with?("config/routes.rb")
assert_equal "GET", result[:verb]
assert_equal "/users(.:format)", result[:path]
end
test "server add-ons" do
File.write("server_addon.rb", <<~RUBY)
class TapiocaServerAddon < RubyLsp::Rails::ServerAddon
def name
"Tapioca"
end
def execute(request, params)
send_message({ request:, params: })
end
end
RUBY
@server.execute("server_addon/register", server_addon_path: File.expand_path("server_addon.rb"))
@server.execute("server_addon/delegate", server_addon_name: "Tapioca", request_name: "dsl")
assert_equal(response, { params: {}, request: "dsl" })
ensure
FileUtils.rm("server_addon.rb")
end
test "checking for pending migrations" do
capture_subprocess_io do
system("bundle exec rails g migration CreateStudents name:string")
end
@server.execute("pending_migrations_message", {})
message = response.dig(:result, :pending_migrations_message)
assert_match("You have 1 pending migration", message)
assert_match(%r{db/migrate/[\d]+_create_students\.rb}, message)
ensure
FileUtils.rm_rf("db") if File.directory?("db")
end
test "running migrations happens in a child process" do
Open3.expects(:capture2)
.with({ "VERBOSE" => "true" }, "bundle exec rails db:migrate")
.returns(["Running migrations...", mock(exitstatus: 0)])
@server.execute("run_migrations", {})
assert_equal({ message: "Running migrations...", status: 0 }, response[:result])
end
test "shows error if there is a database connection error" do
@server.expects(:pending_migrations_message).raises(ActiveRecord::ConnectionNotEstablished)
@server.execute("pending_migrations_message", {})
assert_equal(
{ error: "Request pending_migrations_message failed because database connection was not established." }, response
)
end
test "shows error if database does not exist" do
@server.expects(:pending_migrations_message).raises(ActiveRecord::NoDatabaseError)
@server.execute("pending_migrations_message", {})
assert_equal(
{ error: "Request pending_migrations_message failed because the database does not exist." },
response,
)
end
test "send_message uses bytesize for content length with ASCII characters" do
@server.send(:send_message, { test: "hello" })
assert_equal "Content-Length: 16\r\n\r\n{\"test\":\"hello\"}", @stdout.string
end
test "send_message uses bytesize for content length with multibyte characters" do
@server.send(:send_message, { test: "こんにちは" }) # Japanese "hello"
expected = "Content-Length: 26\r\n\r\n"
expected += { test: "こんにちは" }.to_json.force_encoding(Encoding::ASCII_8BIT)
assert_equal expected, @stdout.string
end
test "log_message sends notification to client" do
@server.log_message("Hello")
expected_notification = {
method: "window/logMessage",
params: { type: 4, message: "Hello" },
}.to_json
assert_equal "Content-Length: #{expected_notification.bytesize}\r\n\r\n#{expected_notification}", @stderr.string
end
test "log_message allows server to define message type" do
@server.log_message("Hello", type: 1)
expected_notification = {
method: "window/logMessage",
params: { type: 1, message: "Hello" },
}.to_json
assert_equal "Content-Length: #{expected_notification.bytesize}\r\n\r\n#{expected_notification}", @stderr.string
end
test "regular prints become structured log messages" do
original_stdout = $stdout
stdout = StringIO.new
stderr = StringIO.new
server = RubyLsp::Rails::Server.new(stdout: stdout, stderr: stderr, override_default_output_device: true)
server.instance_eval do
def print_it!
puts "hello"
end
end
server #: as untyped
.print_it!
assert_match("Content-Length: 70\r\n\r\n", stderr.string)
assert_match(
"{\"method\":\"window/logMessage\",\"params\":{\"type\":4,\"message\":\"hello\\n\"}}",
stderr.string,
)
ensure
$> = original_stdout
end
private
def response
_headers, content = @stdout.string.split("\r\n\r\n")
JSON.parse(content, symbolize_names: true)
end
end