From bc559b966c8e71c0b0582ccdf0f1c4d3d9d3fcfd Mon Sep 17 00:00:00 2001 From: Neal Chambers Date: Thu, 17 Aug 2023 13:55:58 +0900 Subject: [PATCH] Safely autocorrect Style/FetchEnvVar Inspecting 1483 files ..............................................................................................................................................................C.............................................................................................................................................................................CC.............................................................................................................................................................................C.................................................................................................................................................................................................................................................................................................................................................................C.................................................................................C..............................................................................................................................................................................................C.....................................................................................................................................................................................................................................C...................................................................................................................... Offenses: app/helpers/discourse_helper.rb:9:5: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch('DISCOURSE_URL') or ENV.fetch('DISCOURSE_URL', nil) instead of ENV['DISCOURSE_URL']. ENV['DISCOURSE_URL'] ^^^^^^^^^^^^^^^^^^^^ app/models/spree/preferences/configuration.rb:35:10: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch('RAILS_CACHE_ID') or ENV.fetch('RAILS_CACHE_ID', nil) instead of ENV['RAILS_CACHE_ID']. [ENV['RAILS_CACHE_ID'], self.class.name, name].flatten.join('::').underscore ^^^^^^^^^^^^^^^^^^^^^ app/models/spree/preferences/preferable.rb:84:10: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch("RAILS_CACHE_ID") or ENV.fetch("RAILS_CACHE_ID", nil) instead of ENV["RAILS_CACHE_ID"]. [ENV["RAILS_CACHE_ID"], self.class.name, name, id].join('::').underscore ^^^^^^^^^^^^^^^^^^^^^ app/services/default_country.rb:13:40: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch("DEFAULT_COUNTRY_CODE") or ENV.fetch("DEFAULT_COUNTRY_CODE", nil) instead of ENV["DEFAULT_COUNTRY_CODE"]. Spree::Country.cached_find_by(iso: ENV["DEFAULT_COUNTRY_CODE"]) || Spree::Country.first ^^^^^^^^^^^^^^^^^^^^^^^^^^^ app/services/default_country.rb:13:73: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected. Spree::Country.cached_find_by(iso: ENV.fetch("DEFAULT_COUNTRY_CODE", ^ app/services/default_country.rb:13:101: C: [Corrected] Layout/LineLength: Line is too long. [102/100] Spree::Country.cached_find_by(iso: ENV.fetch("DEFAULT_COUNTRY_CODE", nil)) || Spree::Country.first ^^ app/services/default_country.rb:14:1: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line. nil)) || Spree::Country.first ^^^ spec/base_spec_helper.rb:51:49: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch("SITE_URL") or ENV.fetch("SITE_URL", nil) instead of ENV["SITE_URL"]. ActionMailer::Base.default_url_options[:host] = ENV["SITE_URL"] ^^^^^^^^^^^^^^^ spec/controllers/spree/credit_cards_controller_spec.rb:8:20: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch('STRIPE_SECRET_TEST_API_KEY') or ENV.fetch('STRIPE_SECRET_TEST_API_KEY', nil) instead of ENV['STRIPE_SECRET_TEST_API_KEY']. let(:secret) { ENV['STRIPE_SECRET_TEST_API_KEY'] } ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ spec/models/order_balance_spec.rb:48:70: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected. expect(order_balance.display_amount).to eq(Spree::Money.new(20, ^ spec/models/order_balance_spec.rb:48:81: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch('currency') or ENV.fetch('currency', nil) instead of ENV['currency']. expect(order_balance.display_amount).to eq(Spree::Money.new(20, currency: ENV['currency'])) ^^^^^^^^^^^^^^^ spec/models/order_balance_spec.rb:48:101: C: [Corrected] Layout/LineLength: Line is too long. [108/100] expect(order_balance.display_amount).to eq(Spree::Money.new(20, currency: ENV.fetch('currency', nil))) ^^^^^^^^ spec/models/order_balance_spec.rb:49:1: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line. currency: ENV.fetch('currency', nil))) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ spec/models/order_balance_spec.rb:49:98: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected. currency: ENV.fetch('currency', ^ spec/models/order_balance_spec.rb:49:101: C: [Corrected] Layout/LineLength: Line is too long. [104/100] currency: ENV.fetch('currency', nil))) ^^^^ spec/models/order_balance_spec.rb:50:1: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line. nil))) ^^^ spec/support/vcr_setup.rb:10:50: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch('STRIPE_SECRET_TEST_API_KEY') or ENV.fetch('STRIPE_SECRET_TEST_API_KEY', nil) instead of ENV['STRIPE_SECRET_TEST_API_KEY']. config.filter_sensitive_data('') { ENV['STRIPE_SECRET_TEST_API_KEY'] } ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ spec/support/vcr_setup.rb:11:55: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch('STRIPE_CUSTOMER') or ENV.fetch('STRIPE_CUSTOMER', nil) instead of ENV['STRIPE_CUSTOMER']. config.filter_sensitive_data('') { ENV['STRIPE_CUSTOMER'] } ^^^^^^^^^^^^^^^^^^^^^^ 1483 files inspected, 18 offenses detected, 18 offenses corrected --- .rubocop_todo.yml | 22 +++++++------------ app/helpers/discourse_helper.rb | 2 +- app/models/spree/preferences/configuration.rb | 2 +- app/models/spree/preferences/preferable.rb | 2 +- app/services/default_country.rb | 3 ++- spec/base_spec_helper.rb | 2 +- .../spree/credit_cards_controller_spec.rb | 2 +- spec/models/order_balance_spec.rb | 4 +++- spec/support/vcr_setup.rb | 4 ++-- 9 files changed, 20 insertions(+), 23 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 608594cd53..bcf0ef608b 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,6 +6,14 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. +# URISchemes: http, https +Layout/LineLength: + Exclude: + - 'spec/system/admin/tag_rules_spec.rb' + # Offense count: 17 # Configuration parameters: AllowedMethods. # AllowedMethods: enums @@ -801,20 +809,6 @@ Style/ClassVars: Exclude: - 'lib/spree/core/delegate_belongs_to.rb' -# Offense count: 9 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowedVars. -Style/FetchEnvVar: - Exclude: - - 'app/helpers/discourse_helper.rb' - - 'app/models/spree/preferences/configuration.rb' - - 'app/models/spree/preferences/preferable.rb' - - 'app/services/default_country.rb' - - 'spec/base_spec_helper.rb' - - 'spec/controllers/spree/credit_cards_controller_spec.rb' - - 'spec/models/order_balance_spec.rb' - - 'spec/support/vcr_setup.rb' - # Offense count: 2 # This cop supports safe autocorrection (--autocorrect). Style/FileRead: diff --git a/app/helpers/discourse_helper.rb b/app/helpers/discourse_helper.rb index 551636fe39..28b45209a2 100644 --- a/app/helpers/discourse_helper.rb +++ b/app/helpers/discourse_helper.rb @@ -6,7 +6,7 @@ module DiscourseHelper end def discourse_url - ENV['DISCOURSE_URL'] + ENV.fetch('DISCOURSE_URL', nil) end def discourse_login_url diff --git a/app/models/spree/preferences/configuration.rb b/app/models/spree/preferences/configuration.rb index d1a514484d..a2997451a6 100644 --- a/app/models/spree/preferences/configuration.rb +++ b/app/models/spree/preferences/configuration.rb @@ -32,7 +32,7 @@ module Spree end def preference_cache_key(name) - [ENV['RAILS_CACHE_ID'], self.class.name, name].flatten.join('::').underscore + [ENV.fetch('RAILS_CACHE_ID', nil), self.class.name, name].flatten.join('::').underscore end def reset diff --git a/app/models/spree/preferences/preferable.rb b/app/models/spree/preferences/preferable.rb index af775ab37d..40131daef3 100644 --- a/app/models/spree/preferences/preferable.rb +++ b/app/models/spree/preferences/preferable.rb @@ -81,7 +81,7 @@ module Spree def preference_cache_key(name) return unless id - [ENV["RAILS_CACHE_ID"], self.class.name, name, id].join('::').underscore + [ENV.fetch("RAILS_CACHE_ID", nil), self.class.name, name, id].join('::').underscore end def save_pending_preferences diff --git a/app/services/default_country.rb b/app/services/default_country.rb index cb2aae92ec..609535a1fc 100644 --- a/app/services/default_country.rb +++ b/app/services/default_country.rb @@ -10,6 +10,7 @@ class DefaultCountry end def self.country - Spree::Country.cached_find_by(iso: ENV["DEFAULT_COUNTRY_CODE"]) || Spree::Country.first + Spree::Country.cached_find_by(iso: ENV.fetch("DEFAULT_COUNTRY_CODE", + nil)) || Spree::Country.first end end diff --git a/spec/base_spec_helper.rb b/spec/base_spec_helper.rb index ca66ef310e..7c7af2dc88 100644 --- a/spec/base_spec_helper.rb +++ b/spec/base_spec_helper.rb @@ -48,7 +48,7 @@ Capybara.configure do |config| end # Override setting in Spree engine: Spree::Core::MailSettings -ActionMailer::Base.default_url_options[:host] = ENV["SITE_URL"] +ActionMailer::Base.default_url_options[:host] = ENV.fetch("SITE_URL", nil) FactoryBot.use_parent_strategy = false FactoryBot::SyntaxRunner.include FileHelper diff --git a/spec/controllers/spree/credit_cards_controller_spec.rb b/spec/controllers/spree/credit_cards_controller_spec.rb index eb8432411b..a99cd850bd 100644 --- a/spec/controllers/spree/credit_cards_controller_spec.rb +++ b/spec/controllers/spree/credit_cards_controller_spec.rb @@ -5,7 +5,7 @@ require 'spec_helper' describe Spree::CreditCardsController, type: :controller do describe "using VCR", :vcr do let(:user) { create(:user) } - let(:secret) { ENV['STRIPE_SECRET_TEST_API_KEY'] } + let(:secret) { ENV.fetch('STRIPE_SECRET_TEST_API_KEY', nil) } before do Stripe.api_key = secret diff --git a/spec/models/order_balance_spec.rb b/spec/models/order_balance_spec.rb index 95eec3219b..a7f1aedc66 100644 --- a/spec/models/order_balance_spec.rb +++ b/spec/models/order_balance_spec.rb @@ -45,7 +45,9 @@ describe OrderBalance do end it 'returns the balance wraped in a Money object' do - expect(order_balance.display_amount).to eq(Spree::Money.new(20, currency: ENV['currency'])) + expect(order_balance.display_amount).to eq(Spree::Money.new(20, + currency: ENV.fetch('currency', + nil))) end end diff --git a/spec/support/vcr_setup.rb b/spec/support/vcr_setup.rb index 261563fff7..7cb9d1b63d 100644 --- a/spec/support/vcr_setup.rb +++ b/spec/support/vcr_setup.rb @@ -7,7 +7,7 @@ VCR.configure do |config| config.hook_into :webmock config.ignore_localhost = true config.configure_rspec_metadata! - config.filter_sensitive_data('') { ENV['STRIPE_SECRET_TEST_API_KEY'] } - config.filter_sensitive_data('') { ENV['STRIPE_CUSTOMER'] } + config.filter_sensitive_data('') { ENV.fetch('STRIPE_SECRET_TEST_API_KEY', nil) } + config.filter_sensitive_data('') { ENV.fetch('STRIPE_CUSTOMER', nil) } config.ignore_hosts('localhost', '127.0.0.1', '0.0.0.0', 'api.knapsackpro.com') end