Verify more constants are not loaded at startup

Loading different constants can be tricky with all the requires in
Homebrew so we want to strengthen the existing checks to make
sure that things are not getting required when there is a performance
penalty. This expands the existing check to include more constants
beyond `Formula` that we don't expect to be defined and that pull
in a lot of other dependencies.
This commit is contained in:
apainintheneck 2024-08-10 14:27:29 -07:00
parent 3f1e90a3a5
commit f6a6979711
3 changed files with 42 additions and 25 deletions

View File

@ -1,8 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
RSpec.describe Homebrew, :integration_test do RSpec.describe Homebrew, :integration_test do
it "does not invoke `require \"formula\"` at startup" do it "does not require slow dependencies at startup" do
expect { brew "verify-formula-undefined" } expect { brew "verify-undefined" }
.to not_to_output.to_stdout .to not_to_output.to_stdout
.and not_to_output.to_stderr .and not_to_output.to_stderr
.and be_a_success .and be_a_success

View File

@ -1,23 +0,0 @@
# typed: strict
# frozen_string_literal: true
require "cli/parser"
module Homebrew
module Cmd
class VerifyFormulaUndefined < AbstractCommand
end
end
end
parser = Homebrew::CLI::Parser.new(Homebrew::Cmd::VerifyFormulaUndefined) do
usage_banner <<~EOS
`verify-formula-undefined`
Verifies that `require "formula"` has not been performed at startup.
EOS
end
parser.parse
Homebrew.failed = defined?(Formula) && Formula.respond_to?(:[])

View File

@ -0,0 +1,40 @@
# typed: strict
# frozen_string_literal: true
require "cli/parser"
UNDEFINED_CONSTANTS = %w[
Cask::Cask
Formula
Formulary
Homebrew::API
Tap
].freeze
module Homebrew
module Cmd
class VerifyUndefined < AbstractCommand
end
end
end
parser = Homebrew::CLI::Parser.new(Homebrew::Cmd::VerifyUndefined) do
usage_banner <<~EOS
`verify-undefined`
Verifies that the following constants have not been defined
at startup to make sure that startup times stay consistent.
Contants:
#{UNDEFINED_CONSTANTS.join("\n")}
EOS
end
parser.parse
UNDEFINED_CONSTANTS.each do |constant_name|
Object.const_get(constant_name)
ofail "#{constant_name} should not be defined at startup"
rescue NameError
# We expect this to error as it should not be defined.
end