spec_helper: add check for unexpected network calls

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.
This commit is contained in:
apainintheneck 2024-03-16 13:14:12 -07:00
parent 8e67ee26d7
commit b66097fa3d
3 changed files with 15 additions and 3 deletions

View File

@ -46,13 +46,15 @@ RSpec.describe Cask::Cask, :cask do
expect(c.token).to eq("caffeine") expect(c.token).to eq("caffeine")
end 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") c = Cask::CaskLoader.load("file://#{tap_path}/Casks/local-caffeine.rb")
expect(c).to be_a(described_class) expect(c).to be_a(described_class)
expect(c.token).to eq("local-caffeine") expect(c.token).to eq("local-caffeine")
end 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 expect do
Cask::CaskLoader.load("file://#{tap_path}/Casks/notacask.rb") Cask::CaskLoader.load("file://#{tap_path}/Casks/notacask.rb")
end.to raise_error(Cask::CaskUnavailableError) end.to raise_error(Cask::CaskUnavailableError)

View File

@ -119,7 +119,8 @@ RSpec.describe Formulary do
expect(described_class.factory(formula_path)).to be_a(Formula) expect(described_class.factory(formula_path)).to be_a(Formula)
end 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}") formula = described_class.factory("file://#{formula_path}")
expect(formula).to be_a(Formula) expect(formula).to be_a(Formula)
end end

View File

@ -165,6 +165,15 @@ RSpec.configure do |config|
skip "Requires network connection." unless ENV["HOMEBREW_TEST_ONLINE"] skip "Requires network connection." unless ENV["HOMEBREW_TEST_ONLINE"]
end 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 config.before(:each, :needs_svn) do
svn_shim = HOMEBREW_SHIMS_PATH/"shared/svn" svn_shim = HOMEBREW_SHIMS_PATH/"shared/svn"
skip "Subversion is not installed." unless quiet_system svn_shim, "--version" skip "Subversion is not installed." unless quiet_system svn_shim, "--version"