From 5e57df7287c5c8f5fb4d24c66f1663ef2d5c4464 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Fri, 21 Feb 2025 21:54:29 -0500 Subject: [PATCH] livecheck: restrict POST hashes to symbol keys I initially set the type for livecheck's `post_form` and `post_json` hashes to allow either a string or symbol key. I used string keys in the documentation, as there will inevitably be some form field names that would pose a problem for symbols (e.g., `E-mail` uses a hyphen, `1twothree` starts with a digit, etc.). However, I remembered that we can simply use quote symbols like `:"E-mail"` to handle these situations, as they have the flexibility of a string while still being a symbol. With that in mind, this updates related type signatures to only allow symbol keys and updates documentation and tests accordingly. The documentation example contains a hyphenated form field, so it demonstrates how to handle names that don't work as a bare symbol. --- Library/Homebrew/livecheck.rb | 4 ++-- Library/Homebrew/livecheck/strategy.rb | 4 ++-- Library/Homebrew/test/livecheck/strategy_spec.rb | 10 ---------- Library/Homebrew/test/livecheck_spec.rb | 8 ++++---- docs/Brew-Livecheck.md | 4 ++-- 5 files changed, 10 insertions(+), 20 deletions(-) diff --git a/Library/Homebrew/livecheck.rb b/Library/Homebrew/livecheck.rb index ebfcc7ec52f26..efdf5339feba9 100644 --- a/Library/Homebrew/livecheck.rb +++ b/Library/Homebrew/livecheck.rb @@ -170,8 +170,8 @@ def throttle(rate = T.unsafe(nil)) params( # URL to check for version information. url: T.any(String, Symbol), - post_form: T.nilable(T::Hash[T.any(String, Symbol), String]), - post_json: T.nilable(T::Hash[T.any(String, Symbol), String]), + post_form: T.nilable(T::Hash[Symbol, String]), + post_json: T.nilable(T::Hash[Symbol, String]), ).returns(T.nilable(T.any(String, Symbol))) } def url(url = T.unsafe(nil), post_form: nil, post_json: nil) diff --git a/Library/Homebrew/livecheck/strategy.rb b/Library/Homebrew/livecheck/strategy.rb index ca67feef3609e..d591a7b7e9690 100644 --- a/Library/Homebrew/livecheck/strategy.rb +++ b/Library/Homebrew/livecheck/strategy.rb @@ -172,8 +172,8 @@ def self.from_url(url, livecheck_strategy: nil, regex_provided: false, block_pro # @return [Array] sig { params( - post_form: T.nilable(T::Hash[T.any(String, Symbol), String]), - post_json: T.nilable(T::Hash[T.any(String, Symbol), String]), + post_form: T.nilable(T::Hash[Symbol, String]), + post_json: T.nilable(T::Hash[Symbol, String]), ).returns(T::Array[String]) } def self.post_args(post_form: nil, post_json: nil) diff --git a/Library/Homebrew/test/livecheck/strategy_spec.rb b/Library/Homebrew/test/livecheck/strategy_spec.rb index ddb3bde2f5079..bb701e3b43783 100644 --- a/Library/Homebrew/test/livecheck/strategy_spec.rb +++ b/Library/Homebrew/test/livecheck/strategy_spec.rb @@ -9,14 +9,6 @@ let(:redirection_url) { "https://brew.sh/redirection" } let(:post_hash) do - { - "empty" => "", - "boolean" => "true", - "number" => "1", - "string" => "a + b = c", - } - end - let(:post_hash_symbol_keys) do { empty: "", boolean: "true", @@ -154,7 +146,6 @@ describe "::post_args" do it "returns an array including `--data` and an encoded form data string" do expect(strategy.post_args(post_form: post_hash)).to eq(["--data", form_string]) - expect(strategy.post_args(post_form: post_hash_symbol_keys)).to eq(["--data", form_string]) # If both `post_form` and `post_json` are present, only `post_form` will # be used. @@ -163,7 +154,6 @@ it "returns an array including `--json` and a JSON string" do expect(strategy.post_args(post_json: post_hash)).to eq(["--json", json_string]) - expect(strategy.post_args(post_json: post_hash_symbol_keys)).to eq(["--json", json_string]) end it "returns an empty array if `post_form` value is blank" do diff --git a/Library/Homebrew/test/livecheck_spec.rb b/Library/Homebrew/test/livecheck_spec.rb index d213d227d6700..e52fd76265020 100644 --- a/Library/Homebrew/test/livecheck_spec.rb +++ b/Library/Homebrew/test/livecheck_spec.rb @@ -29,10 +29,10 @@ let(:post_hash) do { - "empty" => "", - "boolean" => "true", - "number" => "1", - "string" => "a + b = c", + empty: "", + boolean: "true", + number: "1", + string: "a + b = c", } end diff --git a/docs/Brew-Livecheck.md b/docs/Brew-Livecheck.md index 65cd54624831e..90ffcbe58c9a5 100644 --- a/docs/Brew-Livecheck.md +++ b/docs/Brew-Livecheck.md @@ -119,8 +119,8 @@ Some checks require making a `POST` request and that can be accomplished by addi ```ruby livecheck do url "https://example.com/download.php", post_form: { - "Name" => "", - "E-mail" => "", + Name: "", + "E-mail": "", } regex(/href=.*?example[._-]v?(\d+(?:\.\d+)+)\.t/i) end