Skip to content

Commit

Permalink
Merge pull request #19084 from Homebrew/add-comments-to-rubocop-disables
Browse files Browse the repository at this point in the history
Add clarifying comments to `rubocop:disable`s
  • Loading branch information
issyl0 authored Jan 13, 2025
2 parents 730c93e + 9c8ff4c commit 4c9de7d
Show file tree
Hide file tree
Showing 27 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions Library/Homebrew/brew.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
end

exit 1
# Catch any other types of exceptions.
rescue Exception => e # rubocop:disable Lint/RescueException
onoe e

Expand Down
2 changes: 2 additions & 0 deletions Library/Homebrew/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ def fixopt(formula)
options = Options.create(args.flags_only)
build = Build.new(formula, options, args:)
build.install
# Any exception means the build did not complete.
# The `case` for what to do per-exception class is further down.
rescue Exception => e # rubocop:disable Lint/RescueException
error_hash = JSON.parse e.to_json

Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/cask/dsl/depends_on.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def macos=(*args)
MacOSRequirement.new([T.must(md[:version]).to_sym], comparator: md[:comparator])
elsif (md = /^\s*(?<comparator><|>|[=<>]=)\s*(?<version>\S+)\s*$/.match(first_arg))
MacOSRequirement.new([md[:version]], comparator: md[:comparator])
# This is not duplicate of the first case: see `args.first` and a different comparator.
else # rubocop:disable Lint/DuplicateBranch
MacOSRequirement.new([args.first], comparator: "==")
end
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/cmd/update-report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ def migrate_tap_migration
unless Formulary.factory(new_full_name).keg_only?
system HOMEBREW_BREW_FILE, "link", new_full_name, "--overwrite"
end
# Rescue any possible exception types.
rescue Exception => e # rubocop:disable Lint/RescueException
if Homebrew::EnvConfig.developer?
require "utils/backtrace"
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/dev-cmd/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def run
exec(*exec_args)
end
end
# Rescue any possible exception types.
rescue Exception => e # rubocop:disable Lint/RescueException
retry if retry_test?(f)

Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/download_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(size = 1)
sig { params(downloadable: Downloadable, force: T::Boolean).returns(Concurrent::Promises::Future) }
def enqueue(downloadable, force: false)
quiet = pool.max_length > 1
# Passing in arguments from outside into the future is a common `concurrent-ruby` pattern.
# Passing in arguments from outside into the future is a common `concurrent-ruby` pattern.
# rubocop:disable Lint/ShadowingOuterLocalVariable
Concurrent::Promises.future_on(pool, downloadable, force, quiet) do |downloadable, force, quiet|
downloadable.clear_cache if force
Expand Down
3 changes: 2 additions & 1 deletion Library/Homebrew/download_strategy.rbi
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# typed: strict

# This is a third-party implementation
# This is a third-party implementation.
# rubocop:disable Lint/StructNewOverride
class Mechanize::HTTP
ContentDisposition = Struct.new :type, :filename, :creation_date,
:modification_date, :read_date, :size, :parameters
end
# rubocop:enable Lint/StructNewOverride

# This is a third-party implementation.
# rubocop:disable Style/OptionalBooleanParameter
class Mechanize::HTTP::ContentDispositionParser
sig {
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/extend/os/mac/readall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def valid_casks?(tap, os_name: nil, arch: ::Hardware::CPU.type)
raise "Missing URL" if cask.url.nil?
rescue Interrupt
raise
# Handle all possible exceptions reading Casks.
rescue Exception => e # rubocop:disable Lint/RescueException
os_and_arch = "macOS #{current_macos_version} on #{arch}"
onoe "Invalid cask (#{os_and_arch}): #{file}"
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2903,6 +2903,7 @@ def run_test(keep_tmp: false)
test
end
end
# Handle all possible exceptions running formula tests.
rescue Exception # rubocop:disable Lint/RescueException
staging.retain! if debug?
raise
Expand Down
9 changes: 8 additions & 1 deletion Library/Homebrew/formula_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,8 @@ def install
if pour_bottle?
begin
pour
# Catch any other types of exceptions as they leave us with nothing installed.
rescue Exception # rubocop:disable Lint/RescueException
# any exceptions must leave us with nothing installed
ignore_interrupts do
begin
FileUtils.rm_r(formula.prefix) if formula.prefix.directory?
Expand Down Expand Up @@ -825,6 +825,7 @@ def install_dependency(dep, inherited_options)
oh1 "Installing #{formula.full_name} dependency: #{Formatter.identifier(dep.name)}"
fi.install
fi.finish
# Handle all possible exceptions installing deps.
rescue Exception => e # rubocop:disable Lint/RescueException
ignore_interrupts do
tmp_keg.rename(installed_keg.to_path) if tmp_keg && !installed_keg.directory?
Expand Down Expand Up @@ -1022,6 +1023,7 @@ def build
formula.update_head_version

raise "Empty installation" if !formula.prefix.directory? || Keg.new(formula.prefix).empty_installation?
# Handle all possible exceptions when building.
rescue Exception => e # rubocop:disable Lint/RescueException
if e.is_a? BuildError
e.formula = formula
Expand Down Expand Up @@ -1099,6 +1101,7 @@ def link(keg)
puts "You can try again using:"
puts " brew link #{formula.name}"
@show_summary_heading = true
# Handle all other possible exceptions when linking.
rescue Exception => e # rubocop:disable Lint/RescueException
ofail "An unexpected error occurred during the `brew link` step"
puts "The formula built, but is not symlinked into #{HOMEBREW_PREFIX}"
Expand Down Expand Up @@ -1151,6 +1154,7 @@ def install_service
launchd_service_path.chmod 0644
log = formula.var/"log"
log.mkpath if service.include? log.to_s
# Handle all possible exceptions when installing service files.
rescue Exception => e # rubocop:disable Lint/RescueException
puts e
ofail "Failed to install service files"
Expand All @@ -1162,6 +1166,7 @@ def install_service
sig { params(keg: Keg).void }
def fix_dynamic_linkage(keg)
keg.fix_dynamic_linkage
# Rescue all possible exceptions when fixing linkage.
rescue Exception => e # rubocop:disable Lint/RescueException
ofail "Failed to fix install linkage"
puts "The formula built, but you may encounter issues using it or linking other"
Expand All @@ -1177,6 +1182,7 @@ def fix_dynamic_linkage(keg)
def clean
ohai "Cleaning" if verbose?
Cleaner.new(formula).clean
# Handle all possible exceptions when cleaning does not complete.
rescue Exception => e # rubocop:disable Lint/RescueException
opoo "The cleaning step did not complete successfully"
puts "Still, the installation was successful, so we will link it into your prefix."
Expand Down Expand Up @@ -1249,6 +1255,7 @@ def post_install
exec(*args)
end
end
# Handle all possible exceptions when postinstall does not complete.
rescue Exception => e # rubocop:disable Lint/RescueException
opoo "The post-install step did not complete successfully"
puts "You can try again using:"
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/ignorable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def self.hook_raise
def raise(*)
callcc do |continuation|
super
# Handle all possible exceptions.
rescue Exception => e # rubocop:disable Lint/RescueException
unless e.is_a?(ScriptError)
e.extend(ExceptionMixin)
Expand Down
2 changes: 2 additions & 0 deletions Library/Homebrew/migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ def migrate
EOS
rescue Interrupt
ignore_interrupts { backup_oldname }
# Any exception means the migration did not complete.
rescue Exception => e # rubocop:disable Lint/RescueException
onoe "The migration did not complete successfully."
puts e
Expand Down Expand Up @@ -357,6 +358,7 @@ def link_newname
puts
puts "You can try again using:"
puts " brew link #{formula.name}"
# Any exception means the `brew link` step did not complete.
rescue Exception => e # rubocop:disable Lint/RescueException
onoe "An unexpected error occurred during linking"
puts e
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/postinstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
formula.extend(Debrew::Formula)
end
formula.run_post_install
# Handle all possible exceptions.
rescue Exception => e # rubocop:disable Lint/RescueException
error_pipe.puts e.to_json
error_pipe.close
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/readall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def self.valid_formulae?(tap, bottle_tag: nil)
end
rescue Interrupt
raise
# Handle all possible exceptions reading formulae.
rescue Exception => e # rubocop:disable Lint/RescueException
onoe "Invalid formula (#{bottle_tag}): #{file}"
$stderr.puts e
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/reinstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def self.reinstall_formula(
fi.finish
rescue FormulaInstallationAlreadyAttemptedError
nil
# Any other exceptions we want to restore the previous keg and report the error.
rescue Exception # rubocop:disable Lint/RescueException
ignore_interrupts { restore_backup(keg, link_keg, verbose:) }
raise
Expand Down
3 changes: 2 additions & 1 deletion Library/Homebrew/resource_auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def audit_download_strategy

def audit_checksum
return if spec_name == :head
# rubocop:disable Style/InvertibleUnlessCondition (non-invertible)
# This condition is non-invertible.
# rubocop:disable Style/InvertibleUnlessCondition
return unless DownloadStrategyDetector.detect(url, using) <= CurlDownloadStrategy
# rubocop:enable Style/InvertibleUnlessCondition

Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/tap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,7 @@ def self.all
cache[:all] ||= begin
core_taps = [
CoreTap.instance,
# The conditional is valid here because we only want the cask tap on macOS.
(CoreCaskTap.instance if OS.mac?), # rubocop:disable Homebrew/MoveToExtendOS
].compact

Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
timeout = ENV["HOMEBREW_TEST_TIMEOUT_SECS"]&.to_i || DEFAULT_TEST_TIMEOUT_SECONDS
Timeout.timeout(timeout, &run_test)
end
# Any exceptions during the test run are reported.
rescue Exception => e # rubocop:disable Lint/RescueException
error_pipe.puts e.to_json
error_pipe.close
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/test/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "commands"

# These shared contexts starting with `when` don't make sense.
RSpec.shared_context "custom internal commands" do # rubocop:disable RSpec/ContextWording
let(:cmds) do
[
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/test/macos_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
specify "comparison with Symbol" do
expect(version).to be > :high_sierra
expect(version).to eq :mojave
# We're explicitly testing the `===` operator results here.
expect(version).to be === :mojave # rubocop:disable Style/CaseEquality
expect(version).to be < :catalina
end
Expand All @@ -34,13 +35,15 @@
specify "comparison with String" do
expect(version).to be > "10.3"
expect(version).to eq "10.14"
# We're explicitly testing the `===` operator results here.
expect(version).to be === "10.14" # rubocop:disable Style/CaseEquality
expect(version).to be < "10.15"
end

specify "comparison with Version" do
expect(version).to be > Version.new("10.3")
expect(version).to eq Version.new("10.14")
# We're explicitly testing the `===` operator results here.
expect(version).to be === Version.new("10.14") # rubocop:disable Style/CaseEquality
expect(version).to be < Version.new("10.15")
end
Expand Down
2 changes: 2 additions & 0 deletions Library/Homebrew/test/rubocops/patches_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def patches
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source:)
FormulaAudit/Patches: Patches from Debian should be https://, not http: #{patch_url}
EOS
# GitHub patch diff regexps can't be any shorter.
# rubocop:disable Layout/LineLength
elsif patch_url.match?(%r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)})
# rubocop:enable Layout/LineLength
Expand Down Expand Up @@ -232,6 +233,7 @@ class Foo < Formula
expect_offense_hash(message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source:)
FormulaAudit/Patches: GitLab patches should end with .diff, not .patch: #{patch_url}
EOS
# GitHub patch diff regexps can't be any shorter.
# rubocop:disable Layout/LineLength
elsif patch_url.match?(%r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)})
# rubocop:enable Layout/LineLength
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Config
end
end

# These shared contexts starting with `when` don't make sense.
RSpec.shared_context "Homebrew Cask", :needs_macos do # rubocop:disable RSpec/ContextWording
around do |example|
third_party_tap = Tap.fetch("third-party", "tap")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

RSpec::Matchers.define_negated_matcher :be_a_failure, :be_a_success

# These shared contexts starting with `when` don't make sense.
RSpec.shared_context "integration test" do # rubocop:disable RSpec/ContextWording
extend RSpec::Matchers::DSL

Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/test/utils/inreplace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
it "raises error if there is nothing to replace in block form" do
expect do
described_class.inreplace(file.path) do |s|
# Using `gsub!` here is what we want, and it's only a test.
s.gsub!("d", "f") # rubocop:disable Performance/StringReplacement
end
end.to raise_error(Utils::Inreplace::Error)
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/test/utils/spdx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
license_expression = { any_of: [
"MIT",
:public_domain,
# The final array item is legitimately a hash in the case of license expressions.
all_of: ["0BSD", "Zlib"], # rubocop:disable Style/HashAsLastArrayItem
"curl" => { with: "LLVM-exception" },
] }
Expand Down Expand Up @@ -195,6 +196,7 @@
license_expression = { any_of: [
"MIT",
:public_domain,
# The final array item is legitimately a hash in the case of license expressions.
all_of: ["0BSD", "Zlib"], # rubocop:disable Style/HashAsLastArrayItem
"curl" => { with: "LLVM-exception" },
] }
Expand Down Expand Up @@ -235,6 +237,7 @@
})
end

# The final array item is legitimately a hash in the case of license expressions.
# rubocop:disable Style/HashAsLastArrayItem
it "handles nested brackets" do
expect(described_class.string_to_license_expression("A AND (B OR (C AND D))")).to eq({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
let(:string) { "foo" }

it "replaces all occurrences" do
# Using `gsub!` here is what we want, and it's only a test.
string_extension.gsub!("o", "e") # rubocop:disable Performance/StringReplacement
expect(string_extension.inreplace_string).to eq("fee")
end
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/utils/fork.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def self.safe_fork(directory: nil, yield_parent: false)
Process::UID.change_privilege(Process.euid) if Process.euid != Process.uid

yield(error_pipe)
# This could be any type of exception, so rescue them all.
rescue Exception => e # rubocop:disable Lint/RescueException
error_hash = JSON.parse e.to_json

Expand Down

0 comments on commit 4c9de7d

Please sign in to comment.