Skip to content

Commit

Permalink
spec_helper: add check for unexpected network calls
Browse files Browse the repository at this point in the history
Any test that is not tagged as :needs_network and that makes
a call to an unapproved method in the `Utils::Curl` module
will raise an error unless that method gets mocked somehow.
  • Loading branch information
apainintheneck committed Mar 16, 2024
1 parent a3e5e3f commit 1ca9755
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Library/Homebrew/test/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
HOMEBREW_TEMP,
].freeze

# These are methods in `Utils::Curl` that we know won't make network
# requests and can be used even when :needs_network is not set.
ALLOWED_UTILS_CURL_METHODS = [:curl_args].freeze
DISALLOWED_UTILS_CURL_METHODS = (Utils::Curl.methods(false) - ALLOWED_UTILS_CURL_METHODS).freeze

# Make `instance_double` and `class_double`
# work when type-checking is active.
RSpec::Sorbet.allow_doubles!
Expand Down Expand Up @@ -166,6 +171,22 @@
skip "Requires network connection." unless ENV["HOMEBREW_TEST_ONLINE"]
end

config.before do |example|
next if example.metadata.key?(:needs_network)

DISALLOWED_UTILS_CURL_METHODS.each do |method|
allow(Utils::Curl).to receive(method).and_raise(<<~ERROR)
Unexpected call to Utils::Curl.#{method} without setting :needs_network.
If this curl method never makes network requests, add it to the
ALLOWED_UTILS_CURL_METHODS array in the spec_helper.rb file.
If this curl method won't make network requests this time in particular,
set :allow_utils_curl on the failing spec.
ERROR
end
end

config.before(:each, :needs_svn) do
svn_shim = HOMEBREW_SHIMS_PATH/"shared/svn"
skip "Subversion is not installed." unless quiet_system svn_shim, "--version"
Expand Down

0 comments on commit 1ca9755

Please sign in to comment.