Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/typeprof/cli/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def run_lsp
if @lsp_options[:stdio]
TypeProf::LSP::Server.start_stdio(@core_options)
else
TypeProf::LSP::Server.start_socket(@core_options)
TypeProf::LSP::Server.start_socket(@core_options, @lsp_options[:port])
end
rescue Exception
puts $!.detailed_message(highlight: false).gsub(/^/, "---")
Expand Down
4 changes: 2 additions & 2 deletions lib/typeprof/lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def self.start_stdio(core_options)
new(core_options, reader, writer).run
end

def self.start_socket(core_options)
Socket.tcp_server_sockets("localhost", nil) do |servs|
def self.start_socket(core_options, port = 0)
Socket.tcp_server_sockets("localhost", port) do |servs|
serv = servs[0].local_address
$stdout << JSON.generate({
host: serv.ip_address,
Expand Down
26 changes: 26 additions & 0 deletions test/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,32 @@ def test_e2e_no_show_stats
assert_not_include(result, "TypeProf Evaluation Statistics")
end

def test_lsp_port_option
# Find an available port from OS
tmp_server = TCPServer.new("localhost", 0)
port = tmp_server.addr[1]
tmp_server.close

read_pipe, write_pipe = IO.pipe
original_stdout = $stdout
$stdout = write_pipe

cli = TypeProf::CLI::CLI.new(["--lsp", "--port", port.to_s])
th = Thread.new { cli.run }

IO.select([read_pipe], nil, nil, 5)
output = read_pipe.read_nonblock(4096)
json = JSON.parse(output, symbolize_names: true)

assert_equal(port, json[:port])
ensure
th&.kill
th&.join(1)
$stdout = original_stdout
write_pipe&.close unless write_pipe&.closed?
read_pipe&.close unless read_pipe&.closed?
end

def test_lsp_options_with_lsp_mode
assert_nothing_raised { TypeProf::CLI::CLI.new(["--lsp", "--stdio"]) }
end
Expand Down