Skip to content

Commit

Permalink
Merge pull request #16538 from dduugg/TopLevelMethodDefinition
Browse files Browse the repository at this point in the history
Enable Style/TopLevelMethodDefinition
  • Loading branch information
MikeMcQuaid committed Jan 28, 2024
2 parents 8392f70 + b6fef7b commit cfac516
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 106 deletions.
5 changes: 5 additions & 0 deletions Library/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/.yardopts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
--template-path yard/templates
--exclude test/
--exclude vendor/
--exclude yard/
extend/os/**/*.rb
**/*.rb
-
Expand Down
122 changes: 61 additions & 61 deletions Library/Homebrew/dev-cmd/extract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
22 changes: 11 additions & 11 deletions Library/Homebrew/test/cli/named_args_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
52 changes: 26 additions & 26 deletions Library/Homebrew/test/dev-cmd/bottle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
16 changes: 8 additions & 8 deletions Library/Homebrew/test/dev-cmd/determine-test-runners_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

0 comments on commit cfac516

Please sign in to comment.