Skip to content

Commit

Permalink
Replace ENV[] by ENV.fetch in rubocop project
Browse files Browse the repository at this point in the history
  • Loading branch information
j-miyake committed Apr 11, 2022
1 parent beabdf7 commit 1901117
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 21 deletions.
9 changes: 0 additions & 9 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ Style/IpAddresses:
Exclude:
- spec/rubocop/cop/style/ip_addresses_spec.rb

Style/FetchEnvVar:
AllowedVars:
- HOME
- RUBOCOP_CACHE_ROOT
- XDG_CACHE_HOME
- XDG_CONFIG_HOME
- CIRCLE_STAGE
- CI

Layout/EndOfLine:
EnforcedStyle: lf

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cli/command/suggest_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def skip?
# 1. On CI
# 2. When given RuboCop options that it doesn't make sense for
# 3. For all formatters except specified in `INCLUDED_FORMATTERS'`
ENV['CI'] ||
ENV.fetch('CI', nil) ||
@options[:only] || @options[:debug] || @options[:list_target_files] ||
@options[:out] || @options[:stdin] ||
!INCLUDED_FORMATTERS.include?(current_formatter)
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/result_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ def remove_files(files, dirs, remove_count)
end

def self.cache_root(config_store)
root = ENV['RUBOCOP_CACHE_ROOT']
root = ENV.fetch('RUBOCOP_CACHE_ROOT', nil)
root ||= config_store.for_pwd.for_all_cops['CacheRootDirectory']
root ||= if ENV.key?('XDG_CACHE_HOME')
# Include user ID in the path to make sure the user has write
# access.
File.join(ENV['XDG_CACHE_HOME'], Process.uid.to_s)
File.join(ENV.fetch('XDG_CACHE_HOME'), Process.uid.to_s)
else
File.join(ENV['HOME'], '.cache')
File.join(ENV.fetch('HOME'), '.cache')
end
File.join(root, 'rubocop_cache')
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/rspec/shared_contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
RSpec.shared_context 'isolated environment', :isolated_environment do
around do |example|
Dir.mktmpdir do |tmpdir|
original_home = ENV['HOME']
original_xdg_config_home = ENV['XDG_CONFIG_HOME']
original_home = ENV.fetch('HOME', nil)
original_xdg_config_home = ENV.fetch('XDG_CONFIG_HOME', nil)

# Make sure to expand all symlinks in the path first. Otherwise we may
# get mismatched pathnames when loading config files later on.
Expand Down
6 changes: 3 additions & 3 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1837,8 +1837,8 @@ def find_suggestions

# Ensure that these specs works in CI, since the feature is generally
# disabled in when ENV['CI'] is set.
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with('CI').and_return(false)
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:fetch).with('CI', nil).and_return(false)

# Mock the lockfile to be parsed by bundler
allow(Bundler).to receive(:default_lockfile)
Expand Down Expand Up @@ -2024,7 +2024,7 @@ def find_suggestions
end

context 'when in CI mode' do
before { allow(ENV).to receive(:[]).with('CI').and_return(true) }
before { allow(ENV).to receive(:fetch).with('CI', nil).and_return(true) }

it 'does not show the suggestion' do
expect { cli.run(['example.rb']) }.not_to suggest_extensions
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/result_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def abs(path)

it 'contains the given path and UID' do
cacheroot = described_class.cache_root(config_store)
expect(cacheroot).to eq(File.join(ENV['XDG_CACHE_HOME'], puid, 'rubocop_cache'))
expect(cacheroot).to eq(File.join(ENV.fetch('XDG_CACHE_HOME'), puid, 'rubocop_cache'))
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@

config.after(:suite) { RuboCop::Cop::Registry.reset! }

if %w[ruby-head-ascii_spec ruby-head-spec].include? ENV['CIRCLE_STAGE']
if %w[ruby-head-ascii_spec ruby-head-spec].include? ENV.fetch('CIRCLE_STAGE', nil)
config.filter_run_excluding broken_on: :ruby_head
end

if %w[jruby-9.3-ascii_spec jruby-9.3-spec].include? ENV['CIRCLE_STAGE']
if %w[jruby-9.3-ascii_spec jruby-9.3-spec].include? ENV.fetch('CIRCLE_STAGE', nil)
config.filter_run_excluding broken_on: :jruby
end
end
Expand Down

0 comments on commit 1901117

Please sign in to comment.