diff --git a/Library/.rubocop.yml b/Library/.rubocop.yml index 1fc717c0b5e72..e9a242d734e8d 100644 --- a/Library/.rubocop.yml +++ b/Library/.rubocop.yml @@ -432,6 +432,11 @@ Style/SymbolArray: Style/TernaryParentheses: EnforcedStyle: require_parentheses_when_complex +Style/TopLevelMethodDefinition: + Enabled: true + Exclude: + - "Taps/**/*.rb" + # Trailing commas make diffs nicer. Style/TrailingCommaInArguments: EnforcedStyleForMultiline: comma diff --git a/Library/Homebrew/.yardopts b/Library/Homebrew/.yardopts index f9ca1591d5ae1..44655767cf664 100644 --- a/Library/Homebrew/.yardopts +++ b/Library/Homebrew/.yardopts @@ -7,6 +7,7 @@ --template-path yard/templates --exclude test/ --exclude vendor/ +--exclude yard/ extend/os/**/*.rb **/*.rb - diff --git a/Library/Homebrew/dev-cmd/extract.rb b/Library/Homebrew/dev-cmd/extract.rb index 1f3aea1c2946e..2f3291d5f3265 100644 --- a/Library/Homebrew/dev-cmd/extract.rb +++ b/Library/Homebrew/dev-cmd/extract.rb @@ -7,67 +7,6 @@ require "software_spec" require "tap" -def with_monkey_patch - # Since `method_defined?` is not a supported type guard, the use of `alias_method` below is not typesafe: - BottleSpecification.class_eval do - T.unsafe(self).alias_method :old_method_missing, :method_missing if method_defined?(:method_missing) - define_method(:method_missing) do |*| - # do nothing - end - end - - Module.class_eval do - T.unsafe(self).alias_method :old_method_missing, :method_missing if method_defined?(:method_missing) - define_method(:method_missing) do |*| - # do nothing - end - end - - Resource.class_eval do - T.unsafe(self).alias_method :old_method_missing, :method_missing if method_defined?(:method_missing) - define_method(:method_missing) do |*| - # do nothing - end - end - - DependencyCollector.class_eval do - T.unsafe(self).alias_method :old_parse_symbol_spec, :parse_symbol_spec if method_defined?(:parse_symbol_spec) - define_method(:parse_symbol_spec) do |*| - # do nothing - end - end - - yield -ensure - BottleSpecification.class_eval do - if method_defined?(:old_method_missing) - T.unsafe(self).alias_method :method_missing, :old_method_missing - undef :old_method_missing - end - end - - Module.class_eval do - if method_defined?(:old_method_missing) - T.unsafe(self).alias_method :method_missing, :old_method_missing - undef :old_method_missing - end - end - - Resource.class_eval do - if method_defined?(:old_method_missing) - T.unsafe(self).alias_method :method_missing, :old_method_missing - undef :old_method_missing - end - end - - DependencyCollector.class_eval do - if method_defined?(:old_parse_symbol_spec) - T.unsafe(self).alias_method :parse_symbol_spec, :old_parse_symbol_spec - undef :old_parse_symbol_spec - end - end -end - module Homebrew BOTTLE_BLOCK_REGEX = / bottle (?:do.+?end|:[a-z]+)\n\n/m @@ -222,4 +161,65 @@ def self.formula_at_revision(repo, name, file, rev) contents.sub!(BOTTLE_BLOCK_REGEX, "") with_monkey_patch { Formulary.from_contents(name, file, contents, ignore_errors: true) } end + + private_class_method def self.with_monkey_patch + # Since `method_defined?` is not a supported type guard, the use of `alias_method` below is not typesafe: + BottleSpecification.class_eval do + T.unsafe(self).alias_method :old_method_missing, :method_missing if method_defined?(:method_missing) + define_method(:method_missing) do |*| + # do nothing + end + end + + Module.class_eval do + T.unsafe(self).alias_method :old_method_missing, :method_missing if method_defined?(:method_missing) + define_method(:method_missing) do |*| + # do nothing + end + end + + Resource.class_eval do + T.unsafe(self).alias_method :old_method_missing, :method_missing if method_defined?(:method_missing) + define_method(:method_missing) do |*| + # do nothing + end + end + + DependencyCollector.class_eval do + T.unsafe(self).alias_method :old_parse_symbol_spec, :parse_symbol_spec if method_defined?(:parse_symbol_spec) + define_method(:parse_symbol_spec) do |*| + # do nothing + end + end + + yield + ensure + BottleSpecification.class_eval do + if method_defined?(:old_method_missing) + T.unsafe(self).alias_method :method_missing, :old_method_missing + undef :old_method_missing + end + end + + Module.class_eval do + if method_defined?(:old_method_missing) + T.unsafe(self).alias_method :method_missing, :old_method_missing + undef :old_method_missing + end + end + + Resource.class_eval do + if method_defined?(:old_method_missing) + T.unsafe(self).alias_method :method_missing, :old_method_missing + undef :old_method_missing + end + end + + DependencyCollector.class_eval do + if method_defined?(:old_parse_symbol_spec) + T.unsafe(self).alias_method :parse_symbol_spec, :old_parse_symbol_spec + undef :old_parse_symbol_spec + end + end + end end diff --git a/Library/Homebrew/test/cli/named_args_spec.rb b/Library/Homebrew/test/cli/named_args_spec.rb index ecbfbd34dbdb0..dc04310dffaec 100644 --- a/Library/Homebrew/test/cli/named_args_spec.rb +++ b/Library/Homebrew/test/cli/named_args_spec.rb @@ -2,20 +2,20 @@ require "cli/named_args" -def setup_unredable_formula(name) - error = FormulaUnreadableError.new(name, RuntimeError.new("testing")) - allow(Formulary).to receive(:factory).with(name, any_args).and_raise(error) -end +describe Homebrew::CLI::NamedArgs do + def setup_unredable_formula(name) + error = FormulaUnreadableError.new(name, RuntimeError.new("testing")) + allow(Formulary).to receive(:factory).with(name, any_args).and_raise(error) + end -def setup_unredable_cask(name) - error = Cask::CaskUnreadableError.new(name, "testing") - allow(Cask::CaskLoader).to receive(:load).with(name, any_args).and_raise(error) + def setup_unredable_cask(name) + error = Cask::CaskUnreadableError.new(name, "testing") + allow(Cask::CaskLoader).to receive(:load).with(name, any_args).and_raise(error) - config = instance_double(Cask::Config) - allow(Cask::Config).to receive(:from_args).and_return(config) -end + config = instance_double(Cask::Config) + allow(Cask::Config).to receive(:from_args).and_return(config) + end -describe Homebrew::CLI::NamedArgs do let(:foo) do formula "foo" do url "https://brew.sh" diff --git a/Library/Homebrew/test/dev-cmd/bottle_spec.rb b/Library/Homebrew/test/dev-cmd/bottle_spec.rb index 01f8e53d269bb..9f463a1508595 100644 --- a/Library/Homebrew/test/dev-cmd/bottle_spec.rb +++ b/Library/Homebrew/test/dev-cmd/bottle_spec.rb @@ -4,6 +4,32 @@ require "dev-cmd/bottle" describe "brew bottle" do + def stub_hash(parameters) + <<~EOS + { + "#{parameters[:name]}":{ + "formula":{ + "pkg_version":"#{parameters[:version]}", + "path":"#{parameters[:path]}" + }, + "bottle":{ + "root_url":"#{parameters[:root_url] || HOMEBREW_BOTTLE_DEFAULT_DOMAIN}", + "prefix":"/usr/local", + "cellar":"#{parameters[:cellar]}", + "rebuild":0, + "tags":{ + "#{parameters[:os]}":{ + "filename":"#{parameters[:filename]}", + "local_filename":"#{parameters[:local_filename]}", + "sha256":"#{parameters[:sha256]}" + } + } + } + } + } + EOS + end + it_behaves_like "parseable arguments" it "builds a bottle for the given Formula", :integration_test do @@ -538,29 +564,3 @@ def install end end end - -def stub_hash(parameters) - <<~EOS - { - "#{parameters[:name]}":{ - "formula":{ - "pkg_version":"#{parameters[:version]}", - "path":"#{parameters[:path]}" - }, - "bottle":{ - "root_url":"#{parameters[:root_url] || HOMEBREW_BOTTLE_DEFAULT_DOMAIN}", - "prefix":"/usr/local", - "cellar":"#{parameters[:cellar]}", - "rebuild":0, - "tags":{ - "#{parameters[:os]}":{ - "filename":"#{parameters[:filename]}", - "local_filename":"#{parameters[:local_filename]}", - "sha256":"#{parameters[:sha256]}" - } - } - } - } - } - EOS -end diff --git a/Library/Homebrew/test/dev-cmd/determine-test-runners_spec.rb b/Library/Homebrew/test/dev-cmd/determine-test-runners_spec.rb index 7b024052e4da8..9eebdbb66b0c9 100644 --- a/Library/Homebrew/test/dev-cmd/determine-test-runners_spec.rb +++ b/Library/Homebrew/test/dev-cmd/determine-test-runners_spec.rb @@ -4,6 +4,14 @@ require "cmd/shared_examples/args_parse" describe "brew determine-test-runners" do + def get_runners(file) + runner_line = File.open(file).first + json_text = runner_line[/runners=(.*)/, 1] + runner_hash = JSON.parse(json_text) + runner_hash.map { |item| item["runner"].delete_suffix(ephemeral_suffix) } + .sort + end + after do FileUtils.rm_f github_output end @@ -48,14 +56,6 @@ end end -def get_runners(file) - runner_line = File.open(file).first - json_text = runner_line[/runners=(.*)/, 1] - runner_hash = JSON.parse(json_text) - runner_hash.map { |item| item["runner"].delete_suffix(ephemeral_suffix) } - .sort -end - class DetermineRunnerTestHelper @instances = 0 diff --git a/Library/Homebrew/yard/templates/default/docstring/html/setup.rb b/Library/Homebrew/yard/templates/default/docstring/html/setup.rb index 860f921ef5b94..d3f34bb3e3685 100644 --- a/Library/Homebrew/yard/templates/default/docstring/html/setup.rb +++ b/Library/Homebrew/yard/templates/default/docstring/html/setup.rb @@ -1,6 +1,8 @@ # typed: true # frozen_string_literal: true +# This follows the docs at https://github.com/lsegal/yard/blob/main/docs/Templates.md#setuprb +# rubocop:disable Style/TopLevelMethodDefinition def init # `sorbet` is available transitively through the `yard-sorbet` plugin, but we're # outside of the standalone sorbet config, so `checked` is enabled by default @@ -16,3 +18,4 @@ def internal T.bind(self, YARD::Templates::Template, checked: false) erb(:internal) if object.has_tag?(:api) && object.tag(:api).text == "internal" end +# rubocop:enable Style/TopLevelMethodDefinition