Skip to content

Commit

Permalink
Verify more constants are not loaded at startup
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apainintheneck committed Aug 11, 2024
1 parent 8e08a69 commit dd52b78
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Library/Homebrew/test/global_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

RSpec.describe Homebrew, :integration_test do
it "does not invoke `require \"formula\"` at startup" do
expect { brew "verify-formula-undefined" }
it "does not require slow dependencies at startup" do
expect { brew "verify-undefined" }
.to not_to_output.to_stdout
.and not_to_output.to_stderr
.and be_a_success
Expand Down

This file was deleted.

33 changes: 33 additions & 0 deletions Library/Homebrew/test/support/helper/cmd/brew-verify-undefined.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# typed: strict
# frozen_string_literal: true

require "cli/parser"

UNDEFINED_CONSTANTS = %w[
Cask::Cask
Formula
Formulary
Homebrew::API
Tap
].freeze

parser = Homebrew::CLI::Parser.new 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

0 comments on commit dd52b78

Please sign in to comment.