6
6
7
7
class ServerTest < ActiveSupport ::TestCase
8
8
setup do
9
- @server = RubyLsp ::Rails ::Server . new
9
+ @stdout = StringIO . new
10
+ @server = RubyLsp ::Rails ::Server . new ( stdout : @stdout , override_default_output_device : false )
10
11
end
11
12
12
13
test "returns nil if model doesn't exist" do
13
- response = @server . execute ( "model" , { name : "Foo" } )
14
+ @server . execute ( "model" , { name : "Foo" } )
14
15
assert_nil ( response . fetch ( :result ) )
15
16
end
16
17
17
18
test "returns nil if class is not a model" do
18
- response = @server . execute ( "model" , { name : "Time" } )
19
+ @server . execute ( "model" , { name : "Time" } )
19
20
assert_nil ( response . fetch ( :result ) )
20
21
end
21
22
22
23
test "returns nil if class is an abstract model" do
23
- response = @server . execute ( "model" , { name : "ApplicationRecord" } )
24
+ @server . execute ( "model" , { name : "ApplicationRecord" } )
24
25
assert_nil ( response . fetch ( :result ) )
25
26
end
26
27
27
28
test "returns nil if constant is not a class" do
28
- response = @server . execute ( "model" , { name : "RUBY_VERSION" } )
29
+ @server . execute ( "model" , { name : "RUBY_VERSION" } )
29
30
assert_nil ( response . fetch ( :result ) )
30
31
end
31
32
@@ -38,21 +39,21 @@ def <(other)
38
39
end
39
40
end
40
41
41
- response = @server . execute ( "model" , { name : "TestClassWithOverwrittenLessThan" } )
42
+ @server . execute ( "model" , { name : "TestClassWithOverwrittenLessThan" } )
42
43
assert_nil ( response . fetch ( :result ) )
43
44
end
44
45
45
46
test "handles older Rails version which don't have `schema_dump_path`" do
46
47
ActiveRecord ::Tasks ::DatabaseTasks . send ( :alias_method , :old_schema_dump_path , :schema_dump_path )
47
48
ActiveRecord ::Tasks ::DatabaseTasks . undef_method ( :schema_dump_path )
48
- response = @server . execute ( "model" , { name : "User" } )
49
+ @server . execute ( "model" , { name : "User" } )
49
50
assert_nil ( response . fetch ( :result ) [ :schema_file ] )
50
51
ensure
51
52
ActiveRecord ::Tasks ::DatabaseTasks . send ( :alias_method , :schema_dump_path , :old_schema_dump_path )
52
53
end
53
54
54
55
test "resolve association returns the location of the target class of a has_many association" do
55
- response = @server . execute (
56
+ @server . execute (
56
57
"association_target_location" ,
57
58
{ model_name : "Organization" , association_name : :memberships } ,
58
59
)
@@ -61,7 +62,7 @@ def <(other)
61
62
end
62
63
63
64
test "resolve association returns the location of the target class of a belongs_to association" do
64
- response = @server . execute (
65
+ @server . execute (
65
66
"association_target_location" ,
66
67
{ model_name : "Membership" , association_name : :organization } ,
67
68
)
@@ -70,7 +71,7 @@ def <(other)
70
71
end
71
72
72
73
test "resolve association returns the location of the target class of a has_one association" do
73
- response = @server . execute (
74
+ @server . execute (
74
75
"association_target_location" ,
75
76
{ model_name : "User" , association_name : :profile } ,
76
77
)
@@ -79,7 +80,7 @@ def <(other)
79
80
end
80
81
81
82
test "resolve association returns the location of the target class of a has_and_belongs_to_many association" do
82
- response = @server . execute (
83
+ @server . execute (
83
84
"association_target_location" ,
84
85
{ model_name : "Profile" , association_name : :labels } ,
85
86
)
@@ -88,23 +89,23 @@ def <(other)
88
89
end
89
90
90
91
test "resolve association handles invalid model name" do
91
- response = @server . execute (
92
+ @server . execute (
92
93
"association_target_location" ,
93
94
{ model_name : "NotHere" , association_name : :labels } ,
94
95
)
95
96
assert_nil ( response . fetch ( :result ) )
96
97
end
97
98
98
99
test "resolve association handles invalid association name" do
99
- response = @server . execute (
100
+ @server . execute (
100
101
"association_target_location" ,
101
102
{ model_name : "Membership" , association_name : :labels } ,
102
103
)
103
104
assert_nil ( response . fetch ( :result ) )
104
105
end
105
106
106
107
test "resolve association handles class_name option" do
107
- response = @server . execute (
108
+ @server . execute (
108
109
"association_target_location" ,
109
110
{ model_name : "User" , association_name : :location } ,
110
111
)
@@ -113,18 +114,18 @@ def <(other)
113
114
end
114
115
115
116
test "route location returns the location for a valid route" do
116
- response = @server . execute ( "route_location" , { name : "user_path" } )
117
+ @server . execute ( "route_location" , { name : "user_path" } )
117
118
location = response [ :result ] [ :location ]
118
119
assert_match %r{test/dummy/config/routes.rb:4$} , location
119
120
end
120
121
121
122
test "route location returns nil for invalid routes" do
122
- response = @server . execute ( "route_location" , { name : "invalid_path" } )
123
+ @server . execute ( "route_location" , { name : "invalid_path" } )
123
124
assert_nil response [ :result ]
124
125
end
125
126
126
127
test "route info" do
127
- response = @server . execute ( "route_info" , { controller : "UsersController" , action : "index" } )
128
+ @server . execute ( "route_info" , { controller : "UsersController" , action : "index" } )
128
129
129
130
result = response [ :result ]
130
131
@@ -159,7 +160,8 @@ def execute(request, params)
159
160
end
160
161
161
162
test "prints in the Rails application or server are automatically redirected to stderr" do
162
- server = RubyLsp ::Rails ::Server . new
163
+ stdout = StringIO . new
164
+ server = RubyLsp ::Rails ::Server . new ( stdout : stdout )
163
165
164
166
server . instance_eval do
165
167
def resolve_route_info ( requirements )
@@ -168,11 +170,18 @@ def resolve_route_info(requirements)
168
170
end
169
171
end
170
172
171
- stdout , stderr = capture_subprocess_io do
173
+ _ , stderr = capture_subprocess_io do
172
174
server . execute ( "route_info" , { controller : "UsersController" , action : "index" } )
173
175
end
174
176
175
- assert_empty ( stdout )
177
+ refute_match ( "Hello" , stdout . string )
176
178
assert_equal ( "Hello\n " , stderr )
177
179
end
180
+
181
+ private
182
+
183
+ def response
184
+ _headers , content = @stdout . string . split ( "\r \n \r \n " )
185
+ JSON . parse ( content , symbolize_names : true )
186
+ end
178
187
end
0 commit comments