Merge pull request #16538 from dduugg/TopLevelMethodDefinition

Enable Style/TopLevelMethodDefinition
This commit is contained in:
Mike McQuaid 2024-01-28 16:06:34 +00:00 committed by GitHub
commit cfac516ee0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 117 additions and 108 deletions

View File

@ -432,6 +432,11 @@ Style/SymbolArray:
Style/TernaryParentheses: Style/TernaryParentheses:
EnforcedStyle: require_parentheses_when_complex EnforcedStyle: require_parentheses_when_complex
Style/TopLevelMethodDefinition:
Enabled: true
Exclude:
- "Taps/**/*.rb"
# Trailing commas make diffs nicer. # Trailing commas make diffs nicer.
Style/TrailingCommaInArguments: Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: comma EnforcedStyleForMultiline: comma

View File

@ -7,6 +7,7 @@
--template-path yard/templates --template-path yard/templates
--exclude test/ --exclude test/
--exclude vendor/ --exclude vendor/
--exclude yard/
extend/os/**/*.rb extend/os/**/*.rb
**/*.rb **/*.rb
- -

View File

@ -7,67 +7,6 @@ require "formulary"
require "software_spec" require "software_spec"
require "tap" 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 module Homebrew
BOTTLE_BLOCK_REGEX = / bottle (?:do.+?end|:[a-z]+)\n\n/m BOTTLE_BLOCK_REGEX = / bottle (?:do.+?end|:[a-z]+)\n\n/m
@ -222,4 +161,65 @@ module Homebrew
contents.sub!(BOTTLE_BLOCK_REGEX, "") contents.sub!(BOTTLE_BLOCK_REGEX, "")
with_monkey_patch { Formulary.from_contents(name, file, contents, ignore_errors: true) } with_monkey_patch { Formulary.from_contents(name, file, contents, ignore_errors: true) }
end 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 end

View File

@ -2,6 +2,7 @@
require "cli/named_args" require "cli/named_args"
describe Homebrew::CLI::NamedArgs do
def setup_unredable_formula(name) def setup_unredable_formula(name)
error = FormulaUnreadableError.new(name, RuntimeError.new("testing")) error = FormulaUnreadableError.new(name, RuntimeError.new("testing"))
allow(Formulary).to receive(:factory).with(name, any_args).and_raise(error) allow(Formulary).to receive(:factory).with(name, any_args).and_raise(error)
@ -15,7 +16,6 @@ def setup_unredable_cask(name)
allow(Cask::Config).to receive(:from_args).and_return(config) allow(Cask::Config).to receive(:from_args).and_return(config)
end end
describe Homebrew::CLI::NamedArgs do
let(:foo) do let(:foo) do
formula "foo" do formula "foo" do
url "https://brew.sh" url "https://brew.sh"

View File

@ -4,6 +4,32 @@ require "cmd/shared_examples/args_parse"
require "dev-cmd/bottle" require "dev-cmd/bottle"
describe "brew bottle" do 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_behaves_like "parseable arguments"
it "builds a bottle for the given Formula", :integration_test do it "builds a bottle for the given Formula", :integration_test do
@ -538,29 +564,3 @@ describe "brew bottle" do
end end
end 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

View File

@ -4,6 +4,14 @@ require "dev-cmd/determine-test-runners"
require "cmd/shared_examples/args_parse" require "cmd/shared_examples/args_parse"
describe "brew determine-test-runners" do 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 after do
FileUtils.rm_f github_output FileUtils.rm_f github_output
end end
@ -48,14 +56,6 @@ describe "brew determine-test-runners" do
end end
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 class DetermineRunnerTestHelper
@instances = 0 @instances = 0

View File

@ -1,6 +1,8 @@
# typed: true # typed: true
# frozen_string_literal: 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 def init
# `sorbet` is available transitively through the `yard-sorbet` plugin, but we're # `sorbet` is available transitively through the `yard-sorbet` plugin, but we're
# outside of the standalone sorbet config, so `checked` is enabled by default # 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) T.bind(self, YARD::Templates::Template, checked: false)
erb(:internal) if object.has_tag?(:api) && object.tag(:api).text == "internal" erb(:internal) if object.has_tag?(:api) && object.tag(:api).text == "internal"
end end
# rubocop:enable Style/TopLevelMethodDefinition