File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -125,7 +125,7 @@ def run_lsp
125125 if @lsp_options [ :stdio ]
126126 TypeProf ::LSP ::Server . start_stdio ( @core_options )
127127 else
128- TypeProf ::LSP ::Server . start_socket ( @core_options )
128+ TypeProf ::LSP ::Server . start_socket ( @core_options , @lsp_options [ :port ] )
129129 end
130130 rescue Exception
131131 puts $!. detailed_message ( highlight : false ) . gsub ( /^/ , "---" )
Original file line number Diff line number Diff line change @@ -21,8 +21,8 @@ def self.start_stdio(core_options)
2121 new ( core_options , reader , writer ) . run
2222 end
2323
24- def self . start_socket ( core_options )
25- Socket . tcp_server_sockets ( "localhost" , nil ) do |servs |
24+ def self . start_socket ( core_options , port = 0 )
25+ Socket . tcp_server_sockets ( "localhost" , port ) do |servs |
2626 serv = servs [ 0 ] . local_address
2727 $stdout << JSON . generate ( {
2828 host : serv . ip_address ,
Original file line number Diff line number Diff line change @@ -186,6 +186,32 @@ def test_e2e_no_show_stats
186186 assert_not_include ( result , "TypeProf Evaluation Statistics" )
187187 end
188188
189+ def test_lsp_port_option
190+ # Find an available port from OS
191+ tmp_server = TCPServer . new ( "localhost" , 0 )
192+ port = tmp_server . addr [ 1 ]
193+ tmp_server . close
194+
195+ read_pipe , write_pipe = IO . pipe
196+ original_stdout = $stdout
197+ $stdout = write_pipe
198+
199+ cli = TypeProf ::CLI ::CLI . new ( [ "--lsp" , "--port" , port . to_s ] )
200+ th = Thread . new { cli . run }
201+
202+ IO . select ( [ read_pipe ] , nil , nil , 5 )
203+ output = read_pipe . read_nonblock ( 4096 )
204+ json = JSON . parse ( output , symbolize_names : true )
205+
206+ assert_equal ( port , json [ :port ] )
207+ ensure
208+ th &.kill
209+ th &.join ( 1 )
210+ $stdout = original_stdout
211+ write_pipe &.close unless write_pipe &.closed?
212+ read_pipe &.close unless read_pipe &.closed?
213+ end
214+
189215 def test_lsp_options_with_lsp_mode
190216 assert_nothing_raised { TypeProf ::CLI ::CLI . new ( [ "--lsp" , "--stdio" ] ) }
191217 end
You can’t perform that action at this time.
0 commit comments