From bf3d4e41e97ac252004c13c900ce24c464ad4c3f Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 9 Nov 2022 18:20:56 +0900 Subject: [PATCH] [Fix #11164] Suppress a server mode message Fixes #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. --- .../fix_suppress_a_server_mode_message.md | 1 + lib/rubocop/server/core.rb | 15 +++++ spec/rubocop/server/rubocop_server_spec.rb | 58 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 changelog/fix_suppress_a_server_mode_message.md diff --git a/changelog/fix_suppress_a_server_mode_message.md b/changelog/fix_suppress_a_server_mode_message.md new file mode 100644 index 00000000000..72f9befeef1 --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/server/core.rb b/lib/rubocop/server/core.rb index 3e171d2f733..70a18f09379 100644 --- a/lib/rubocop/server/core.rb +++ b/lib/rubocop/server/core.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/rubocop/server/rubocop_server_spec.rb b/spec/rubocop/server/rubocop_server_spec.rb index 11b0865db0f..226d875052b 100644 --- a/spec/rubocop/server/rubocop_server_spec.rb +++ b/spec/rubocop/server/rubocop_server_spec.rb @@ -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)