From 9d92ed868b74cfbacab3a1f082bd5c56b8b4802d Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Sat, 19 Dec 2020 11:53:19 -0500 Subject: [PATCH 1/3] Curl: Add debug parameter to curl_with_workarounds --- Library/Homebrew/utils/curl.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/utils/curl.rb b/Library/Homebrew/utils/curl.rb index e6d741561e..95bb32c40a 100644 --- a/Library/Homebrew/utils/curl.rb +++ b/Library/Homebrew/utils/curl.rb @@ -55,12 +55,13 @@ module Utils end def curl_with_workarounds( - *args, secrets: nil, print_stdout: nil, print_stderr: nil, verbose: nil, env: {}, **options + *args, secrets: nil, print_stdout: nil, print_stderr: nil, debug: nil, verbose: nil, env: {}, **options ) command_options = { secrets: secrets, print_stdout: print_stdout, print_stderr: print_stderr, + debug: debug, verbose: verbose, }.compact From e687774e8adf892b0a955c8bc1d3dfef18788579 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Sat, 19 Dec 2020 17:56:25 -0500 Subject: [PATCH 2/3] Curl: Allow option to omit `--retry` in curl_args --- Library/Homebrew/utils/curl.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/utils/curl.rb b/Library/Homebrew/utils/curl.rb index 95bb32c40a..9c35b36523 100644 --- a/Library/Homebrew/utils/curl.rb +++ b/Library/Homebrew/utils/curl.rb @@ -21,7 +21,7 @@ module Utils @curl end - def curl_args(*extra_args, show_output: false, user_agent: :default) + def curl_args(*extra_args, **options) args = [] # do not load .curlrc unless requested (must be the first argument) @@ -31,25 +31,25 @@ module Utils args << "--show-error" - args << "--user-agent" << case user_agent + args << "--user-agent" << case options[:user_agent] when :browser, :fake HOMEBREW_USER_AGENT_FAKE_SAFARI - when :default + when :default, nil HOMEBREW_USER_AGENT_CURL - else - user_agent + when String + options[:user_agent] end args << "--header" << "Accept-Language: en" - unless show_output + unless options[:show_output] == true args << "--fail" args << "--progress-bar" unless Context.current.verbose? args << "--verbose" if Homebrew::EnvConfig.curl_verbose? args << "--silent" unless $stdout.tty? end - args << "--retry" << Homebrew::EnvConfig.curl_retries + args << "--retry" << Homebrew::EnvConfig.curl_retries unless options[:retry] == false args + extra_args end From 27b7713f1ec1f02d184730c44f9289694bb01e9c Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Sat, 19 Dec 2020 18:02:39 -0500 Subject: [PATCH 3/3] Expand tests for Curl#curl_args --- Library/Homebrew/test/utils/curl_spec.rb | 48 +++++++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/test/utils/curl_spec.rb b/Library/Homebrew/test/utils/curl_spec.rb index af153426b5..2fe1397b10 100644 --- a/Library/Homebrew/test/utils/curl_spec.rb +++ b/Library/Homebrew/test/utils/curl_spec.rb @@ -3,25 +3,61 @@ require "utils/curl" -describe "curl" do +describe "Utils::Curl" do describe "curl_args" do + let(:args) { "foo" } + let(:user_agent_string) { "Lorem ipsum dolor sit amet" } + it "returns --disable as the first argument when HOMEBREW_CURLRC is not set" do # --disable must be the first argument according to "man curl" - expect(curl_args("foo").first).to eq("--disable") + expect(curl_args(*args).first).to eq("--disable") end - it "doesn't return --disable as the first argument when HOMEBREW_CURLRC is set" do + it "doesn't return `--disable` as the first argument when HOMEBREW_CURLRC is set" do ENV["HOMEBREW_CURLRC"] = "1" - expect(curl_args("foo").first).not_to eq("--disable") + expect(curl_args(*args).first).not_to eq("--disable") end it "uses `--retry 3` when HOMEBREW_CURL_RETRIES is unset" do - expect(curl_args("foo").join(" ")).to include("--retry 3") + expect(curl_args(*args).join(" ")).to include("--retry 3") end it "uses the given value for `--retry` when HOMEBREW_CURL_RETRIES is set" do ENV["HOMEBREW_CURL_RETRIES"] = "10" - expect(curl_args("foo").join(" ")).to include("--retry 10") + expect(curl_args(*args).join(" ")).to include("--retry 10") + end + + it "doesn't use `--retry` when `:retry` == `false`" do + expect(curl_args(*args, retry: false).join(" ")).not_to include("--retry") + end + + it "uses `--retry 3` when `:retry` == `true`" do + expect(curl_args(*args, retry: true).join(" ")).to include("--retry 3") + end + + it "uses HOMEBREW_USER_AGENT_FAKE_SAFARI when `:user_agent` is `:browser` or `:fake`" do + expect(curl_args(*args, user_agent: :browser).join(" ")) + .to include("--user-agent #{HOMEBREW_USER_AGENT_FAKE_SAFARI}") + expect(curl_args(*args, user_agent: :fake).join(" ")) + .to include("--user-agent #{HOMEBREW_USER_AGENT_FAKE_SAFARI}") + end + + it "uses HOMEBREW_USER_AGENT_CURL when `:user_agent` is `:default` or omitted" do + expect(curl_args(*args, user_agent: :default).join(" ")).to include("--user-agent #{HOMEBREW_USER_AGENT_CURL}") + expect(curl_args(*args, user_agent: nil).join(" ")).to include("--user-agent #{HOMEBREW_USER_AGENT_CURL}") + expect(curl_args(*args).join(" ")).to include("--user-agent #{HOMEBREW_USER_AGENT_CURL}") + end + + it "uses provided user agent string when `:user_agent` is a `String`" do + expect(curl_args(*args, user_agent: user_agent_string).join(" ")) + .to include("--user-agent #{user_agent_string}") + end + + it "uses `--fail` unless `:show_output` is `true`" do + expect(curl_args(*args, show_output: false).join(" ")).to include("--fail") + expect(curl_args(*args, show_output: nil).join(" ")).to include("--fail") + expect(curl_args(*args).join(" ")).to include("--fail") + expect(curl_args(*args, show_output: true).join(" ")).not_to include("--fail") end end end