diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh index 7feeac9f23..9c4326f309 100644 --- a/Library/Homebrew/brew.sh +++ b/Library/Homebrew/brew.sh @@ -576,7 +576,7 @@ fi if [[ -z "$HOMEBREW_RUBY_WARNINGS" ]] then - export HOMEBREW_RUBY_WARNINGS="-W0" + export HOMEBREW_RUBY_WARNINGS="-W1" fi if [[ -z "$HOMEBREW_BOTTLE_DOMAIN" ]] diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 80c31d5210..50db47d6e2 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -71,25 +71,29 @@ class AbstractDownloadStrategy # Unlike {Resource#stage}, this does not take a block. # # @api public - def stage + def stage(&block) UnpackStrategy.detect(cached_location, prioritise_extension: true, ref_type: @ref_type, ref: @ref) .extract_nestedly(basename: basename, prioritise_extension: true, verbose: verbose? && !quiet?) - chdir + chdir(&block) end - def chdir + def chdir(&block) entries = Dir["*"] raise "Empty archive" if entries.length.zero? - return if entries.length != 1 - begin - Dir.chdir entries.first - rescue - nil + if entries.length != 1 + yield if block + return + end + + if File.directory? entries.first + Dir.chdir(entries.first, &block) + elsif block + yield end end private :chdir diff --git a/Library/Homebrew/resource.rb b/Library/Homebrew/resource.rb index 47920e13c8..bcd4a97ef2 100644 --- a/Library/Homebrew/resource.rb +++ b/Library/Homebrew/resource.rb @@ -114,14 +114,15 @@ class Resource # A target or a block must be given, but not both. def unpack(target = nil) mktemp(download_name) do |staging| - downloader.stage - @source_modified_time = downloader.source_modified_time - apply_patches - if block_given? - yield ResourceStageContext.new(self, staging) - elsif target - target = Pathname(target) - target.install Pathname.pwd.children + downloader.stage do + @source_modified_time = downloader.source_modified_time + apply_patches + if block_given? + yield ResourceStageContext.new(self, staging) + elsif target + target = Pathname(target) + target.install Pathname.pwd.children + end end end end diff --git a/Library/Homebrew/style.rb b/Library/Homebrew/style.rb index 32a46a3471..7511754fdb 100644 --- a/Library/Homebrew/style.rb +++ b/Library/Homebrew/style.rb @@ -78,7 +78,13 @@ module Homebrew fix: false, except_cops: nil, only_cops: nil, display_cop_names: false, reset_cache: false, debug: false, verbose: false) Homebrew.install_bundler_gems! - require "rubocop" + + require "warnings" + + Warnings.ignore :parser_syntax do + require "rubocop" + end + require "rubocops" args = %w[ diff --git a/Library/Homebrew/test/spec_helper.rb b/Library/Homebrew/test/spec_helper.rb index 7b7b3bfe3c..7c2def7835 100644 --- a/Library/Homebrew/test/spec_helper.rb +++ b/Library/Homebrew/test/spec_helper.rb @@ -23,12 +23,17 @@ if ENV["HOMEBREW_TESTS_COVERAGE"] SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new(formatters) end +require_relative "../warnings" + +Warnings.ignore :parser_syntax do + require "rubocop" +end + require "rspec/its" require "rspec/github" require "rspec/wait" require "rspec/retry" require "rspec/sorbet" -require "rubocop" require "rubocop/rspec/support" require "find" require "byebug" diff --git a/Library/Homebrew/utils/rubocop.rb b/Library/Homebrew/utils/rubocop.rb index e1fb8fb857..2257c8bb31 100755 --- a/Library/Homebrew/utils/rubocop.rb +++ b/Library/Homebrew/utils/rubocop.rb @@ -2,18 +2,10 @@ # typed: false # frozen_string_literal: true -require "warning" +require_relative "../warnings" -warnings = [ - %r{warning: parser/current is loading parser/ruby\d+, which recognizes}, - /warning: \d+\.\d+\.\d+-compliant syntax, but you are running \d+\.\d+\.\d+\./, - %r{warning: please see https://github\.com/whitequark/parser#compatibility-with-ruby-mri\.}, -] - -warnings.each do |warning| - Warning.ignore warning +Warnings.ignore :parser_syntax do + require "rubocop" end -require "rubocop" - exit RuboCop::CLI.new.run diff --git a/Library/Homebrew/warnings.rb b/Library/Homebrew/warnings.rb new file mode 100644 index 0000000000..f0a3ed116f --- /dev/null +++ b/Library/Homebrew/warnings.rb @@ -0,0 +1,36 @@ +# typed: true +# frozen_string_literal: true + +require "warning" + +# Helper module for handling warnings. +# +# @api private +module Warnings + module_function + + COMMON_WARNINGS = { + parser_syntax: [ + %r{warning: parser/current is loading parser/ruby\d+, which recognizes}, + /warning: \d+\.\d+\.\d+-compliant syntax, but you are running \d+\.\d+\.\d+\./, + %r{warning: please see https://github\.com/whitequark/parser#compatibility-with-ruby-mri\.}, + ], + }.freeze + + def ignore(*warnings) + warnings.map! do |warning| + next warning if !warning.is_a?(Symbol) || !COMMON_WARNINGS.key?(warning) + + COMMON_WARNINGS[warning] + end + + warnings.flatten.each do |warning| + Warning.ignore warning + end + return unless block_given? + + result = yield + Warning.clear + result + end +end diff --git a/Library/Homebrew/warnings.rbi b/Library/Homebrew/warnings.rbi new file mode 100644 index 0000000000..5d83b4cc35 --- /dev/null +++ b/Library/Homebrew/warnings.rbi @@ -0,0 +1,5 @@ +# typed: strict + +module Warnings + include Kernel +end