Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10771] Make server mode aware of --cache-root command line option #10849

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
* [#10771](https://github.com/rubocop/rubocop/issues/10771): Make server mode aware of `--cache-root` command line option. ([@koic][])
9 changes: 8 additions & 1 deletion lib/rubocop/server/cache.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 19 additions & 1 deletion lib/rubocop/server/cli.rb
Expand Up @@ -30,13 +30,15 @@ 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)

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
Expand All @@ -45,14 +47,15 @@ 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

run_command(server_command)

STATUS_SUCCESS
end
# rubocop:enable Metrics/MethodLength

def exit?
@exit
Expand Down Expand Up @@ -83,6 +86,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)
Expand All @@ -93,6 +107,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
Expand Down
31 changes: 31 additions & 0 deletions 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
18 changes: 18 additions & 0 deletions spec/rubocop/server/cli_spec.rb
Expand Up @@ -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
Expand Down