Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent unexpected network calls in tests #16903

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Library/Homebrew/cli/named_args.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,15 @@ def to_paths(only: parent&.only_formula_or_cask, recurse_tap: false)
paths = []

if formula_path.exist? ||
(!CoreTap.instance.installed? && Homebrew::API::Formula.all_formulae.key?(path.basename.to_s))
(!Homebrew::EnvConfig.no_install_from_api? &&
!CoreTap.instance.installed? &&
Homebrew::API::Formula.all_formulae.key?(path.basename.to_s))
paths << formula_path
end
if cask_path.exist? ||
(!CoreCaskTap.instance.installed? && Homebrew::API::Cask.all_casks.key?(path.basename.to_s))
(!Homebrew::EnvConfig.no_install_from_api? &&
!CoreCaskTap.instance.installed? &&
Homebrew::API::Cask.all_casks.key?(path.basename.to_s))
paths << cask_path
end

Expand Down
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)
reitermarkus marked this conversation as resolved.
Show resolved Hide resolved
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
5 changes: 5 additions & 0 deletions Library/Homebrew/test/cask/info_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
require "utils"

RSpec.describe Cask::Info, :cask do
before do
# Prevent unnecessary network requests in `Utils::Analytics.cask_output`
ENV["HOMEBREW_NO_ANALYTICS"] = "1"
end

it "displays some nice info about the specified Cask" do
expect do
described_class.info(Cask::CaskLoader.load("local-transmission"))
Expand Down
7 changes: 6 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 Expand Up @@ -383,6 +384,10 @@ def formula_json_contents(extra_items = {})
before do
ENV.delete("HOMEBREW_NO_INSTALL_FROM_API")

# avoid unnecessary network calls
allow(Homebrew::API::Formula).to receive_messages(all_aliases: {}, all_renames: {})
allow(CoreTap.instance).to receive(:tap_migrations).and_return({})
Comment on lines +387 to +388
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nicer to ensure some correct files are written on disk instead of mocking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of laziness, I'm going to skip that for now.


# don't try to load/fetch gcc/glibc
allow(DevelopmentTools).to receive_messages(needs_libc_formula?: false, needs_compiler_formula?: false)
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
2 changes: 1 addition & 1 deletion Library/Homebrew/test/tap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def setup_completion(link:)
end

describe "#remote" do
it "returns the remote URL" do
it "returns the remote URL", :needs_network do
reitermarkus marked this conversation as resolved.
Show resolved Hide resolved
setup_git_repo

expect(homebrew_foo_tap.remote).to eq("https://github.com/Homebrew/homebrew-foo")
Expand Down