From 40bbdc659e366a7c79635ab652f02a8b9737c8fe Mon Sep 17 00:00:00 2001 From: Bo Anderson Date: Mon, 30 May 2022 04:37:09 +0100 Subject: [PATCH] Fix some Style/FetchEnvVar offences --- Library/Homebrew/dev-cmd/tests.rb | 10 +++++----- Library/Homebrew/diagnostic.rb | 8 ++++---- Library/Homebrew/language/python.rb | 2 +- Library/Homebrew/software_spec.rb | 4 ++-- Library/Homebrew/startup/bootsnap.rb | 6 +++--- Library/Homebrew/startup/config.rb | 2 +- Library/Homebrew/test/cask/artifact/pkg_spec.rb | 12 ++++++------ Library/Homebrew/test/cmd/--caskroom_spec.rb | 2 +- Library/Homebrew/test/cmd/--cellar_spec.rb | 2 +- Library/Homebrew/test/cmd/--prefix_spec.rb | 4 ++-- Library/Homebrew/test/cmd/--repository_spec.rb | 2 +- Library/Homebrew/test/spec_helper.rb | 2 +- .../spec/shared_context/integration_test.rb | 5 ++--- .../Homebrew/test/support/lib/startup/config.rb | 6 +++--- Library/Homebrew/test/utils/user_spec.rb | 10 +++++----- Library/Homebrew/test/utils_spec.rb | 4 ++-- Library/Homebrew/utils.rb | 2 +- Library/Homebrew/utils/analytics.rb | 2 +- Library/Homebrew/utils/gems.rb | 16 ++++++++-------- 19 files changed, 50 insertions(+), 51 deletions(-) diff --git a/Library/Homebrew/dev-cmd/tests.rb b/Library/Homebrew/dev-cmd/tests.rb index caaeb5eea5..b54af9e64a 100644 --- a/Library/Homebrew/dev-cmd/tests.rb +++ b/Library/Homebrew/dev-cmd/tests.rb @@ -57,15 +57,15 @@ module Homebrew reason: "reporting test flakiness") end - ENV["BUILDPULSE_ACCESS_KEY_ID"] = ENV["HOMEBREW_BUILDPULSE_ACCESS_KEY_ID"] - ENV["BUILDPULSE_SECRET_ACCESS_KEY"] = ENV["HOMEBREW_BUILDPULSE_SECRET_ACCESS_KEY"] + ENV["BUILDPULSE_ACCESS_KEY_ID"] = ENV.fetch("HOMEBREW_BUILDPULSE_ACCESS_KEY_ID") + ENV["BUILDPULSE_SECRET_ACCESS_KEY"] = ENV.fetch("HOMEBREW_BUILDPULSE_SECRET_ACCESS_KEY") ohai "Sending test results to BuildPulse" safe_system Formula["buildpulse-test-reporter"].opt_bin/"buildpulse-test-reporter", "submit", "#{HOMEBREW_LIBRARY_PATH}/test/junit", - "--account-id", ENV["HOMEBREW_BUILDPULSE_ACCOUNT_ID"], - "--repository-id", ENV["HOMEBREW_BUILDPULSE_REPOSITORY_ID"] + "--account-id", ENV.fetch("HOMEBREW_BUILDPULSE_ACCOUNT_ID"), + "--repository-id", ENV.fetch("HOMEBREW_BUILDPULSE_REPOSITORY_ID") end def changed_test_files @@ -212,7 +212,7 @@ module Homebrew ENV["HOMEBREW_TESTS_GEM_USER_DIR"] = gem_user_dir # Let `bundle` in PATH find its gem. - ENV["GEM_PATH"] = "#{ENV["GEM_PATH"]}:#{gem_user_dir}" + ENV["GEM_PATH"] = "#{ENV.fetch("GEM_PATH")}:#{gem_user_dir}" # Submit test flakiness information using BuildPulse # BUILDPULSE used in spec_helper.rb diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb index 6d1eee6e41..dedbc71e85 100644 --- a/Library/Homebrew/diagnostic.rb +++ b/Library/Homebrew/diagnostic.rb @@ -541,7 +541,7 @@ module Homebrew end def check_git_version - minimum_version = ENV["HOMEBREW_MINIMUM_GIT_VERSION"] + minimum_version = ENV.fetch("HOMEBREW_MINIMUM_GIT_VERSION") return unless Utils::Git.available? return if Version.create(Utils::Git.version) >= Version.create(minimum_version) @@ -668,7 +668,7 @@ module Homebrew end def check_tmpdir - tmpdir = ENV["TMPDIR"] + tmpdir = ENV.fetch("TMPDIR", nil) return if tmpdir.nil? || File.directory?(tmpdir) <<~EOS @@ -828,7 +828,7 @@ module Homebrew cmd_map.reject! { |_cmd_name, cmd_paths| cmd_paths.size == 1 } return if cmd_map.empty? - if ENV["CI"] && cmd_map.keys.length == 1 && + if ENV["CI"].present? && cmd_map.keys.length == 1 && cmd_map.keys.first == "brew-test-bot" return end @@ -1007,7 +1007,7 @@ module Homebrew add_info "Cask Environment Variables:", ((locale_variables + environment_variables).sort.each do |var| next unless ENV.key?(var) - var = %Q(#{var}="#{ENV[var]}") + var = %Q(#{var}="#{ENV.fetch(var)}") user_tilde(var) end) end diff --git a/Library/Homebrew/language/python.rb b/Library/Homebrew/language/python.rb index e2dbd22d15..8348523abc 100644 --- a/Library/Homebrew/language/python.rb +++ b/Library/Homebrew/language/python.rb @@ -26,7 +26,7 @@ module Language end def self.each_python(build, &block) - original_pythonpath = ENV["PYTHONPATH"] + original_pythonpath = ENV.fetch("PYTHONPATH", nil) pythons = { "python@3" => "python3", "pypy" => "pypy", "pypy3" => "pypy3" } diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb index eb649ce55e..437af9f0a8 100644 --- a/Library/Homebrew/software_spec.rb +++ b/Library/Homebrew/software_spec.rb @@ -508,8 +508,8 @@ class BottleSpecification prefix = Pathname(cellar).parent.to_s - cellar_relocatable = cellar.size >= HOMEBREW_CELLAR.to_s.size && ENV["HOMEBREW_RELOCATE_BUILD_PREFIX"] - prefix_relocatable = prefix.size >= HOMEBREW_PREFIX.to_s.size && ENV["HOMEBREW_RELOCATE_BUILD_PREFIX"] + cellar_relocatable = cellar.size >= HOMEBREW_CELLAR.to_s.size && ENV["HOMEBREW_RELOCATE_BUILD_PREFIX"].present? + prefix_relocatable = prefix.size >= HOMEBREW_PREFIX.to_s.size && ENV["HOMEBREW_RELOCATE_BUILD_PREFIX"].present? compatible_cellar = cellar == HOMEBREW_CELLAR.to_s || cellar_relocatable compatible_prefix = prefix == HOMEBREW_PREFIX.to_s || prefix_relocatable diff --git a/Library/Homebrew/startup/bootsnap.rb b/Library/Homebrew/startup/bootsnap.rb index a3ebe359c5..ddecf639ef 100644 --- a/Library/Homebrew/startup/bootsnap.rb +++ b/Library/Homebrew/startup/bootsnap.rb @@ -1,7 +1,7 @@ # typed: false # frozen_string_literal: true -homebrew_bootsnap_enabled = !ENV["HOMEBREW_NO_BOOTSNAP"] && ENV["HOMEBREW_BOOTSNAP"] +homebrew_bootsnap_enabled = ENV["HOMEBREW_NO_BOOTSNAP"].nil? && !ENV["HOMEBREW_BOOTSNAP"].nil? # portable ruby doesn't play nice with bootsnap # Can't use .exclude? here because we haven't required active_support yet. @@ -24,14 +24,14 @@ if homebrew_bootsnap_enabled Homebrew.install_bundler_gems!(only_warn_on_failure: true) ENV["HOMEBREW_BOOTSNAP_RETRY"] = "1" - exec ENV["HOMEBREW_BREW_FILE"], *ARGV + exec ENV.fetch("HOMEBREW_BREW_FILE"), *ARGV end end ENV.delete("HOMEBREW_BOOTSNAP_RETRY") if defined?(Bootsnap) - cache = ENV["HOMEBREW_CACHE"] || ENV["HOMEBREW_DEFAULT_CACHE"] + cache = ENV.fetch("HOMEBREW_CACHE", nil) || ENV.fetch("HOMEBREW_DEFAULT_CACHE", nil) # Can't use .blank? here because we haven't required active_support yet. raise "Needs HOMEBREW_CACHE or HOMEBREW_DEFAULT_CACHE!" if cache.nil? || cache.empty? # rubocop:disable Rails/Blank diff --git a/Library/Homebrew/startup/config.rb b/Library/Homebrew/startup/config.rb index 0439e8ab03..52c6836acc 100644 --- a/Library/Homebrew/startup/config.rb +++ b/Library/Homebrew/startup/config.rb @@ -51,5 +51,5 @@ end.freeze # The Ruby path and args to use for forked Ruby calls HOMEBREW_RUBY_EXEC_ARGS = [ RUBY_PATH, - ENV["HOMEBREW_RUBY_WARNINGS"], + ENV.fetch("HOMEBREW_RUBY_WARNINGS"), ].freeze diff --git a/Library/Homebrew/test/cask/artifact/pkg_spec.rb b/Library/Homebrew/test/cask/artifact/pkg_spec.rb index 49ae668d9c..85234faf67 100644 --- a/Library/Homebrew/test/cask/artifact/pkg_spec.rb +++ b/Library/Homebrew/test/cask/artifact/pkg_spec.rb @@ -19,9 +19,9 @@ describe Cask::Artifact::Pkg, :cask do sudo: true, print_stdout: true, env: { - "LOGNAME" => ENV["USER"], - "USER" => ENV["USER"], - "USERNAME" => ENV["USER"], + "LOGNAME" => ENV.fetch("USER"), + "USER" => ENV.fetch("USER"), + "USERNAME" => ENV.fetch("USER"), }, ) @@ -68,9 +68,9 @@ describe Cask::Artifact::Pkg, :cask do sudo: true, print_stdout: true, env: { - "LOGNAME" => ENV["USER"], - "USER" => ENV["USER"], - "USERNAME" => ENV["USER"], + "LOGNAME" => ENV.fetch("USER"), + "USER" => ENV.fetch("USER"), + "USERNAME" => ENV.fetch("USER"), }, ) diff --git a/Library/Homebrew/test/cmd/--caskroom_spec.rb b/Library/Homebrew/test/cmd/--caskroom_spec.rb index 6277d5c855..c69005b758 100644 --- a/Library/Homebrew/test/cmd/--caskroom_spec.rb +++ b/Library/Homebrew/test/cmd/--caskroom_spec.rb @@ -8,7 +8,7 @@ describe "brew --caskroom" do it "prints Homebrew's Caskroom", :integration_test do expect { brew_sh "--caskroom" } - .to output("#{ENV["HOMEBREW_PREFIX"]}/Caskroom\n").to_stdout + .to output("#{ENV.fetch("HOMEBREW_PREFIX")}/Caskroom\n").to_stdout .and not_to_output.to_stderr .and be_a_success end diff --git a/Library/Homebrew/test/cmd/--cellar_spec.rb b/Library/Homebrew/test/cmd/--cellar_spec.rb index 22eee9e244..63c78854a0 100644 --- a/Library/Homebrew/test/cmd/--cellar_spec.rb +++ b/Library/Homebrew/test/cmd/--cellar_spec.rb @@ -8,7 +8,7 @@ describe "brew --cellar" do it "prints Homebrew's Cellar", :integration_test do expect { brew_sh "--cellar" } - .to output("#{ENV["HOMEBREW_CELLAR"]}\n").to_stdout + .to output("#{ENV.fetch("HOMEBREW_CELLAR")}\n").to_stdout .and not_to_output.to_stderr .and be_a_success end diff --git a/Library/Homebrew/test/cmd/--prefix_spec.rb b/Library/Homebrew/test/cmd/--prefix_spec.rb index 44a4244667..d16163b132 100644 --- a/Library/Homebrew/test/cmd/--prefix_spec.rb +++ b/Library/Homebrew/test/cmd/--prefix_spec.rb @@ -8,14 +8,14 @@ describe "brew --prefix" do it "prints Homebrew's prefix", :integration_test do expect { brew_sh "--prefix" } - .to output("#{ENV["HOMEBREW_PREFIX"]}\n").to_stdout + .to output("#{ENV.fetch("HOMEBREW_PREFIX")}\n").to_stdout .and not_to_output.to_stderr .and be_a_success end it "prints the prefix for a Formula", :integration_test do expect { brew_sh "--prefix", "wget" } - .to output("#{ENV["HOMEBREW_PREFIX"]}/opt/wget\n").to_stdout + .to output("#{ENV.fetch("HOMEBREW_PREFIX")}/opt/wget\n").to_stdout .and not_to_output.to_stderr .and be_a_success end diff --git a/Library/Homebrew/test/cmd/--repository_spec.rb b/Library/Homebrew/test/cmd/--repository_spec.rb index d85df8f3a8..33547f7575 100644 --- a/Library/Homebrew/test/cmd/--repository_spec.rb +++ b/Library/Homebrew/test/cmd/--repository_spec.rb @@ -8,7 +8,7 @@ describe "brew --repository" do it "prints Homebrew's repository", :integration_test do expect { brew_sh "--repository" } - .to output("#{ENV["HOMEBREW_REPOSITORY"]}\n").to_stdout + .to output("#{ENV.fetch("HOMEBREW_REPOSITORY")}\n").to_stdout .and not_to_output.to_stderr .and be_a_success end diff --git a/Library/Homebrew/test/spec_helper.rb b/Library/Homebrew/test/spec_helper.rb index 5426ab469b..b58a6ab87c 100644 --- a/Library/Homebrew/test/spec_helper.rb +++ b/Library/Homebrew/test/spec_helper.rb @@ -35,7 +35,7 @@ require "find" require "byebug" require "timeout" -$LOAD_PATH.push(File.expand_path("#{ENV["HOMEBREW_LIBRARY"]}/Homebrew/test/support/lib")) +$LOAD_PATH.push(File.expand_path("#{ENV.fetch("HOMEBREW_LIBRARY")}/Homebrew/test/support/lib")) require_relative "../global" diff --git a/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb b/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb index c282e20b4c..939ff4f086 100644 --- a/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb +++ b/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb @@ -82,7 +82,6 @@ RSpec.shared_context "integration test" do # rubocop:disable RSpec/ContextWordin "HOMEBREW_BREW_FILE" => HOMEBREW_PREFIX/"bin/brew", "HOMEBREW_INTEGRATION_TEST" => command_id_from_args(args), "HOMEBREW_TEST_TMPDIR" => TEST_TMPDIR, - "HOMEBREW_DEVELOPER" => ENV["HOMEBREW_DEVELOPER"], "HOMEBREW_DEV_CMD_RUN" => "true", "GEM_HOME" => nil, ) @@ -127,7 +126,7 @@ RSpec.shared_context "integration test" do # rubocop:disable RSpec/ContextWordin def brew_sh(*args) Bundler.with_clean_env do - stdout, stderr, status = Open3.capture3("#{ENV["HOMEBREW_PREFIX"]}/bin/brew", *args) + stdout, stderr, status = Open3.capture3("#{ENV.fetch("HOMEBREW_PREFIX")}/bin/brew", *args) $stdout.print stdout $stderr.print stderr status @@ -216,7 +215,7 @@ RSpec.shared_context "integration test" do # rubocop:disable RSpec/ContextWordin full_name = Tap.fetch(name).full_name # Check to see if the original Homebrew process has taps we can use. - system_tap_path = Pathname("#{ENV["HOMEBREW_LIBRARY"]}/Taps/#{full_name}") + system_tap_path = Pathname("#{ENV.fetch("HOMEBREW_LIBRARY")}/Taps/#{full_name}") if system_tap_path.exist? system "git", "clone", "--shared", system_tap_path, tap.path system "git", "-C", tap.path, "checkout", "master" diff --git a/Library/Homebrew/test/support/lib/startup/config.rb b/Library/Homebrew/test/support/lib/startup/config.rb index ea9aad2897..b748525955 100644 --- a/Library/Homebrew/test/support/lib/startup/config.rb +++ b/Library/Homebrew/test/support/lib/startup/config.rb @@ -3,10 +3,10 @@ raise "HOMEBREW_BREW_FILE was not exported! Please call bin/brew directly!" unless ENV["HOMEBREW_BREW_FILE"] -HOMEBREW_BREW_FILE = Pathname.new(ENV["HOMEBREW_BREW_FILE"]).freeze +HOMEBREW_BREW_FILE = Pathname.new(ENV.fetch("HOMEBREW_BREW_FILE")).freeze TEST_TMPDIR = ENV.fetch("HOMEBREW_TEST_TMPDIR") do |k| - dir = Dir.mktmpdir("homebrew-tests-", ENV["HOMEBREW_TEMP"] || "/tmp") + dir = Dir.mktmpdir("homebrew-tests-", ENV.fetch("HOMEBREW_TEMP")) at_exit do # Child processes inherit this at_exit handler, but we don't want them # to clean TEST_TMPDIR up prematurely (i.e., when they exit early for a test). @@ -35,7 +35,7 @@ HOMEBREW_LOGS = (HOMEBREW_PREFIX.parent/"logs").freeze HOMEBREW_TEMP = (HOMEBREW_PREFIX.parent/"temp").freeze HOMEBREW_RUBY_EXEC_ARGS = [ RUBY_PATH, - ENV["HOMEBREW_RUBY_WARNINGS"], + ENV.fetch("HOMEBREW_RUBY_WARNINGS"), "-I", HOMEBREW_LIBRARY_PATH/"test/support/lib" ].freeze diff --git a/Library/Homebrew/test/utils/user_spec.rb b/Library/Homebrew/test/utils/user_spec.rb index 53c7089252..b223b30ec6 100644 --- a/Library/Homebrew/test/utils/user_spec.rb +++ b/Library/Homebrew/test/utils/user_spec.rb @@ -6,7 +6,7 @@ require "utils/user" describe User do subject { described_class.current } - it { is_expected.to eq ENV["USER"] } + it { is_expected.to eq ENV.fetch("USER") } describe "#gui?" do before do @@ -17,8 +17,8 @@ describe User do context "when the current user is in a console session" do let(:who_output) { <<~EOS - #{ENV["USER"]} console Oct 1 11:23 - #{ENV["USER"]} ttys001 Oct 1 11:25 + #{ENV.fetch("USER")} console Oct 1 11:23 + #{ENV.fetch("USER")} ttys001 Oct 1 11:25 EOS } @@ -28,8 +28,8 @@ describe User do context "when the current user is not in a console session" do let(:who_output) { <<~EOS - #{ENV["USER"]} ttys001 Oct 1 11:25 - fake_user ttys002 Oct 1 11:27 + #{ENV.fetch("USER")} ttys001 Oct 1 11:25 + fake_user ttys002 Oct 1 11:27 EOS } diff --git a/Library/Homebrew/test/utils_spec.rb b/Library/Homebrew/test/utils_spec.rb index a729d9988d..64c068c02a 100644 --- a/Library/Homebrew/test/utils_spec.rb +++ b/Library/Homebrew/test/utils_spec.rb @@ -114,10 +114,10 @@ describe "globally-scoped helper methods" do ENV["LC_ALL"] = "en_US.UTF-8" with_custom_locale("C") do - expect(ENV["LC_ALL"]).to eq("C") + expect(ENV.fetch("LC_ALL")).to eq("C") end - expect(ENV["LC_ALL"]).to eq("en_US.UTF-8") + expect(ENV.fetch("LC_ALL")).to eq("en_US.UTF-8") end end diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 2eef3d1cf7..eac31a7a5c 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -387,7 +387,7 @@ module Kernel ENV["DISPLAY"] = Homebrew::EnvConfig.display - with_env(DBUS_SESSION_BUS_ADDRESS: ENV["HOMEBREW_DBUS_SESSION_BUS_ADDRESS"]) do + with_env(DBUS_SESSION_BUS_ADDRESS: ENV.fetch("HOMEBREW_DBUS_SESSION_BUS_ADDRESS", nil)) do safe_system(browser, *args) end end diff --git a/Library/Homebrew/utils/analytics.rb b/Library/Homebrew/utils/analytics.rb index 865570f3aa..5996793d2a 100644 --- a/Library/Homebrew/utils/analytics.rb +++ b/Library/Homebrew/utils/analytics.rb @@ -34,7 +34,7 @@ module Utils --data aip=1 --data t=#{type} --data tid=#{analytics_id} - --data cid=#{ENV["HOMEBREW_ANALYTICS_USER_UUID"]} + --data cid=#{ENV.fetch("HOMEBREW_ANALYTICS_USER_UUID")} --data an=#{HOMEBREW_PRODUCT} --data av=#{HOMEBREW_VERSION} ] diff --git a/Library/Homebrew/utils/gems.rb b/Library/Homebrew/utils/gems.rb index 0eaada3c8e..24b7c5bd90 100644 --- a/Library/Homebrew/utils/gems.rb +++ b/Library/Homebrew/utils/gems.rb @@ -18,7 +18,7 @@ module Homebrew end def gem_user_dir - ENV["HOMEBREW_TESTS_GEM_USER_DIR"] || Gem.user_dir + ENV.fetch("HOMEBREW_TESTS_GEM_USER_DIR", nil) || Gem.user_dir end def gem_user_bindir @@ -60,7 +60,7 @@ module Homebrew # Set TMPDIR so Xcode's `make` doesn't fall back to `/var/tmp/`, # which may be not user-writable. - ENV["TMPDIR"] = ENV["HOMEBREW_TEMP"] + ENV["TMPDIR"] = ENV.fetch("HOMEBREW_TEMP", nil) return unless setup_path @@ -108,7 +108,7 @@ module Homebrew odie_if_defined <<~EOS the '#{name}' gem is installed but couldn't find '#{executable}' in the PATH: - #{ENV["PATH"]} + #{ENV.fetch("PATH")} EOS end @@ -129,11 +129,11 @@ module Homebrew end def install_bundler_gems!(only_warn_on_failure: false, setup_path: true, groups: []) - old_path = ENV["PATH"] - old_gem_path = ENV["GEM_PATH"] - old_gem_home = ENV["GEM_HOME"] - old_bundle_gemfile = ENV["BUNDLE_GEMFILE"] - old_bundle_with = ENV["BUNDLE_WITH"] + old_path = ENV.fetch("PATH", nil) + old_gem_path = ENV.fetch("GEM_PATH", nil) + old_gem_home = ENV.fetch("GEM_HOME", nil) + old_bundle_gemfile = ENV.fetch("BUNDLE_GEMFILE", nil) + old_bundle_with = ENV.fetch("BUNDLE_WITH", nil) install_bundler!