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.

tests: add exceptions for tests that use curl to download local files

These are acceptable ways to use curl in local, non-network tests.
For those edge cases we allow you to bypass the check with :needs_utils_curl.
  • Loading branch information
apainintheneck committed Mar 19, 2024
1 parent 8e67ee2 commit b66097f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Library/Homebrew/test/cask/cask_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@
expect(c.token).to eq("caffeine")
end

it "returns an instance of the Cask from a URL" do
it "returns an instance of the Cask from a URL", :needs_utils_curl do
expect(Homebrew::API).not_to receive(:fetch_json_api_file)
c = Cask::CaskLoader.load("file://#{tap_path}/Casks/local-caffeine.rb")
expect(c).to be_a(described_class)
expect(c.token).to eq("local-caffeine")
end

it "raises an error when failing to download a Cask from a URL" do
it "raises an error when failing to download a Cask from a URL", :needs_utils_curl do
expect(Homebrew::API).not_to receive(:fetch_json_api_file)
expect do
Cask::CaskLoader.load("file://#{tap_path}/Casks/notacask.rb")
end.to raise_error(Cask::CaskUnavailableError)
Expand Down
3 changes: 2 additions & 1 deletion Library/Homebrew/test/formulary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ class Wrong#{described_class.class_s(formula_name)} < Formula
expect(described_class.factory(formula_path)).to be_a(Formula)
end

it "returns a Formula when given a URL" do
it "returns a Formula when given a URL", :needs_utils_curl do
expect(Homebrew::API).not_to receive(:fetch_json_api_file)
formula = described_class.factory("file://#{formula_path}")
expect(formula).to be_a(Formula)
end
Expand Down
9 changes: 9 additions & 0 deletions Library/Homebrew/test/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@
skip "Requires network connection." unless ENV["HOMEBREW_TEST_ONLINE"]
end

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

allow(Utils::Curl).to receive(:curl_executable).and_raise(<<~ERROR)
Unexpected call to Utils::Curl.curl_executable without setting :needs_network or :needs_utils_curl.
ERROR
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 b66097f

Please sign in to comment.