@@ -8,6 +8,8 @@ class ServerTest < ActiveSupport::TestCase
88 setup do
99 @stdout = StringIO . new
1010 @stderr = StringIO . new
11+ RubyLsp ::Rails ::ServerAddon . instance_variable_set ( :@server_addon_classes , [ ] )
12+ RubyLsp ::Rails ::ServerAddon . instance_variable_set ( :@server_addons , { } )
1113 @server = RubyLsp ::Rails ::Server . new ( stdout : @stdout , stderr : @stderr , override_default_output_device : false )
1214 end
1315
@@ -268,6 +270,76 @@ def print_it!
268270 $> = original_stdout
269271 end
270272
273+ test "forked processes are named based on caller" do
274+ skip ( "Fork is not supported on Windows" ) if Gem . win_platform?
275+
276+ # ps_output = `ps -p #{Process.pid} -o comm=`.strip
277+ addon_path = File . expand_path ( "my_addon.rb" )
278+ File . write ( addon_path , <<~RUBY )
279+ class MyServerAddon < RubyLsp::Rails::ServerAddon
280+ def name
281+ "MyAddon"
282+ end
283+
284+ def execute(request, params)
285+ file = "process_name.txt"
286+
287+ # We can't directly send a message in these tests because we're using a StringIO as stdout instead of the
288+ # actual pipe, which means that the child process doesn't have access to the same object
289+ pid = fork { File.write(file, `ps -p \# {Process.pid} -o comm=`.strip) }
290+ Process.wait(pid)
291+
292+ send_message({ process_name: File.read(file) })
293+ File.delete(file)
294+ end
295+ end
296+ RUBY
297+
298+ begin
299+ @server . execute ( "server_addon/register" , server_addon_path : addon_path )
300+ @server . execute ( "server_addon/delegate" , server_addon_name : "MyAddon" , request_name : "dsl" )
301+ assert_equal ( response , { process_name : "ruby-lsp-rails: #{ addon_path } " } )
302+ ensure
303+ FileUtils . rm ( addon_path )
304+ end
305+ end
306+
307+ test "forked processes with no block are named based on caller" do
308+ skip ( "Fork is not supported on Windows" ) if Gem . win_platform?
309+
310+ addon_path = File . expand_path ( "my_other_addon.rb" )
311+ File . write ( addon_path , <<~RUBY )
312+ class MyOtherServerAddon < RubyLsp::Rails::ServerAddon
313+ def name
314+ "MyOtherAddon"
315+ end
316+
317+ def execute(request, params)
318+ file = "other_process_name.txt"
319+ pid = fork
320+
321+ if pid
322+ Process.wait(pid)
323+ send_message({ process_name: File.read(file) })
324+ File.delete(file)
325+ else
326+ File.write(file, `ps -p \# {Process.pid} -o comm=`.strip)
327+ # Exit from the child process or else we're stuck in the infinite loop of the server
328+ exit!
329+ end
330+ end
331+ end
332+ RUBY
333+
334+ begin
335+ @server . execute ( "server_addon/register" , server_addon_path : addon_path )
336+ @server . execute ( "server_addon/delegate" , server_addon_name : "MyOtherAddon" , request_name : "dsl" )
337+ assert_equal ( response , { process_name : "ruby-lsp-rails: #{ addon_path } " } )
338+ ensure
339+ FileUtils . rm ( addon_path )
340+ end
341+ end
342+
271343 private
272344
273345 def response
0 commit comments