Skip to content

Commit

Permalink
[Fix rubocop#11164] Suppress a server mode message
Browse files Browse the repository at this point in the history
Fixes rubocop#11164

This PR suppresses "RuboCop server starting..." message with `--server --format json`
because JSON format does not expected output message when IDE integration with server mode.
  • Loading branch information
koic committed Nov 9, 2022
1 parent cb29d08 commit bf3d4e4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_suppress_a_server_mode_message.md
@@ -0,0 +1 @@
* [#11164](https://github.com/rubocop/rubocop/issues/11164): Suppress "RuboCop server starting..." message with `--server --format json`. ([@koic][])
15 changes: 15 additions & 0 deletions lib/rubocop/server/core.rb
Expand Up @@ -17,6 +17,8 @@ module Server
# The core of server process. It starts TCP server and perform socket communication.
# @api private
class Core
JSON_FORMATS = %w[json j].freeze

def self.token
@token ||= SecureRandom.hex(4)
end
Expand Down Expand Up @@ -57,6 +59,10 @@ def server_mode?
def start_server(host, port)
@server = TCPServer.open(host, port)

# JSON format does not expected output message when IDE integration with server mode.
# See: https://github.com/rubocop/rubocop/issues/11164
return if use_json_format?

output_stream = ARGV.include?('--stderr') ? $stderr : $stdout
output_stream.puts "RuboCop server starting on #{@server.addr[3]}:#{@server.addr[1]}."
end
Expand All @@ -76,6 +82,15 @@ def read_socket(socket)
ensure
socket.close
end

def use_json_format?
return true if ARGV.include?('--format=json') || ARGV.include?('--format=j')
return false unless (index = ARGV.index('--format'))

format = ARGV[index + 1]

JSON_FORMATS.include?(format)
end
end
end
end
58 changes: 58 additions & 0 deletions spec/rubocop/server/rubocop_server_spec.rb
Expand Up @@ -67,6 +67,64 @@
end
end

context 'when using `--server` and json is specified as the format' do
context 'when `--format=json`' do
it 'does not display the server start message' do
create_file('example.rb', <<~RUBY)
puts 0
RUBY

stdout, _stderr, _status = Open3.capture3(
'ruby', '-I', '.',
rubocop, '--server', '--format=json', '--stdin', 'example.rb', stdin_data: 'puts 0'
)
expect(stdout).not_to start_with 'RuboCop server starting on '
end
end

context 'when `--format=j`' do
it 'does not display the server start message' do
create_file('example.rb', <<~RUBY)
puts 0
RUBY

stdout, _stderr, _status = Open3.capture3(
'ruby', '-I', '.',
rubocop, '--server', '--format=j', '--stdin', 'example.rb', stdin_data: 'puts 0'
)
expect(stdout).not_to start_with 'RuboCop server starting on '
end
end

context 'when `--format json`' do
it 'does not display the server start message' do
create_file('example.rb', <<~RUBY)
puts 0
RUBY

stdout, _stderr, _status = Open3.capture3(
'ruby', '-I', '.',
rubocop, '--server', '--format', 'json', '--stdin', 'example.rb', stdin_data: 'puts 0'
)
expect(stdout).not_to start_with 'RuboCop server starting on '
end
end

context 'when `--format j`' do
it 'does not display the server start message' do
create_file('example.rb', <<~RUBY)
puts 0
RUBY

stdout, _stderr, _status = Open3.capture3(
'ruby', '-I', '.',
rubocop, '--server', '--format', 'j', '--stdin', 'example.rb', stdin_data: 'puts 0'
)
expect(stdout).not_to start_with 'RuboCop server starting on '
end
end
end

context 'when using `--server` option after running server and updating configuration' do
it 'applies .rubocop.yml configuration changes even during server startup' do
create_file('example.rb', <<~RUBY)
Expand Down

0 comments on commit bf3d4e4

Please sign in to comment.