From 9af369e20e2885112e99a2f5404ab92dc46f55e3 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sun, 31 Jul 2022 02:16:51 +0900 Subject: [PATCH] [Fix #10771] Make server mode aware of `--cache-root` command line option Fix #10771. This PR makes server mode aware of `--cache-root` command line option. The logic for getting `--cache-root` from command line option is original to make the implementation lighter. Because the client of server mode should be lightweight. #10842 needs a different solution than `--cache-root` option. I'm working on it separately from solving this issue. --- ...e_server_mode_aware_of_command_line_option | 1 + lib/rubocop/server/cache.rb | 9 +++++- lib/rubocop/server/cli.rb | 20 +++++++++++- spec/rubocop/server/cache_spec.rb | 31 +++++++++++++++++++ spec/rubocop/server/cli_spec.rb | 18 +++++++++++ 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_make_server_mode_aware_of_command_line_option create mode 100644 spec/rubocop/server/cache_spec.rb diff --git a/changelog/fix_make_server_mode_aware_of_command_line_option b/changelog/fix_make_server_mode_aware_of_command_line_option new file mode 100644 index 00000000000..8f2f3dc4e6d --- /dev/null +++ b/changelog/fix_make_server_mode_aware_of_command_line_option @@ -0,0 +1 @@ +* [#10771](https://github.com/rubocop/rubocop/issues/10771): Make server mode aware of `--cache-root` command line option. ([@koic][]) diff --git a/lib/rubocop/server/cache.rb b/lib/rubocop/server/cache.rb index 4752429672a..e199b81f7f5 100644 --- a/lib/rubocop/server/cache.rb +++ b/lib/rubocop/server/cache.rb @@ -19,6 +19,8 @@ class Cache GEMFILE_NAMES = %w[Gemfile gems.rb].freeze class << self + attr_accessor :cache_root_path + # Searches for Gemfile or gems.rb in the current dir or any parent dirs def project_dir current_dir = Dir.pwd @@ -38,12 +40,17 @@ def project_dir_cache_key end def dir - cache_path = File.expand_path('~/.cache/rubocop_cache/server') Pathname.new(File.join(cache_path, project_dir_cache_key)).tap do |d| d.mkpath unless d.exist? end end + def cache_path + cache_root_dir = cache_root_path || File.join(Dir.home, '.cache') + + File.expand_path(File.join(cache_root_dir, 'rubocop_cache', 'server')) + end + def port_path dir.join('port') end diff --git a/lib/rubocop/server/cli.rb b/lib/rubocop/server/cli.rb index ae9bde42aca..93716e8d555 100644 --- a/lib/rubocop/server/cli.rb +++ b/lib/rubocop/server/cli.rb @@ -29,6 +29,7 @@ def initialize @exit = false end + # rubocop:disable Metrics/MethodLength def run(argv = ARGV) unless Server.support_server? return error('RuboCop server is not supported by this Ruby.') if use_server_option?(argv) @@ -36,6 +37,7 @@ def run(argv = ARGV) return STATUS_SUCCESS end + Cache.cache_root_path = fetch_cache_root_path_from(argv) deleted_server_arguments = delete_server_argument_from(argv) if deleted_server_arguments.size >= 2 @@ -44,7 +46,7 @@ def run(argv = ARGV) server_command = deleted_server_arguments.first - if EXCLUSIVE_OPTIONS.include?(server_command) && argv.count >= 2 + if EXCLUSIVE_OPTIONS.include?(server_command) && argv.count > allowed_option_count return error("#{server_command} cannot be combined with other options.") end @@ -52,6 +54,7 @@ def run(argv = ARGV) STATUS_SUCCESS end + # rubocop:enable Metrics/MethodLength def exit? @exit @@ -82,6 +85,17 @@ def run_command(server_command) end # rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength: + def fetch_cache_root_path_from(arguments) + cache_root = arguments.detect { |argument| argument.start_with?('--cache-root') } + return unless cache_root + + if cache_root.start_with?('--cache-root=') + cache_root.split('=')[1] + else + arguments[arguments.index(cache_root) + 1] + end + end + def delete_server_argument_from(all_arguments) SERVER_OPTIONS.each_with_object([]) do |server_option, server_arguments| server_arguments << all_arguments.delete(server_option) @@ -92,6 +106,10 @@ def use_server_option?(argv) (argv & SERVER_OPTIONS).any? end + def allowed_option_count + Cache.cache_root_path ? 2 : 1 + end + def error(message) @exit = true warn Rainbow(message).red diff --git a/spec/rubocop/server/cache_spec.rb b/spec/rubocop/server/cache_spec.rb new file mode 100644 index 00000000000..f146aeb5544 --- /dev/null +++ b/spec/rubocop/server/cache_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +RSpec.describe RuboCop::Server::Cache do + subject(:cache_class) { described_class } + + describe '.cache_path' do + context 'when cache root path is not specified as default' do + before do + cache_class.cache_root_path = nil + end + + it 'is the default path' do + expect(cache_class.cache_path).to eq("#{Dir.home}/.cache/rubocop_cache/server") + end + end + + context 'when cache root path is specified path' do + before do + cache_class.cache_root_path = '/tmp' + end + + it 'is the specified path' do + if RuboCop::Platform.windows? + expect(cache_class.cache_path).to eq('D:/tmp/rubocop_cache/server') + else + expect(cache_class.cache_path).to eq('/tmp/rubocop_cache/server') + end + end + end + end +end diff --git a/spec/rubocop/server/cli_spec.rb b/spec/rubocop/server/cli_spec.rb index 555a25aa598..07c4f7f002b 100644 --- a/spec/rubocop/server/cli_spec.rb +++ b/spec/rubocop/server/cli_spec.rb @@ -145,6 +145,24 @@ expect($stderr.string).to eq "--server-status cannot be combined with other options.\n" end end + + context 'when using server option with `--cache-root path` option' do + it 'returns exit status 0 and display an error message' do + expect(cli.run(['--server-status', '--cache-root', '/tmp'])).to eq(0) + expect(cli.exit?).to be(true) + expect($stdout.string).to eq "RuboCop server is not running.\n" + expect($stderr.string).not_to eq "--server-status cannot be combined with other options.\n" + end + end + + context 'when using server option with `--cache-root=path` option' do + it 'returns exit status 0 and display an information message' do + expect(cli.run(['--server-status', '--cache-root=/tmp'])).to eq(0) + expect(cli.exit?).to be(true) + expect($stdout.string).to eq "RuboCop server is not running.\n" + expect($stderr.string).not_to eq "--server-status cannot be combined with other options.\n" + end + end else context 'when using `--server` option' do it 'returns exit status 2 and display an error message' do