From 03823b5698dfdece1170e5b6cd631d303592d242 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Mon, 27 Feb 2023 17:50:13 -0500 Subject: [PATCH 1/2] Strategy#from_url: Amend conditions for Json When the `Json` strategy was introduced, I forgot to also ensure that it's only treated as usable (in `Strategy#from_url`) if a `livecheck` block uses `strategy :json`. As a result, `Json` is incorrectly treated as a usable strategy for all formulae/casks that contain a `strategy` block. Since all of these `livecheck` blocks specify a strategy, this bug doesn't meaningfully impact livecheck's behavior (i.e., these checks continue to use their explicitly-specified strategy). The only practical difference is that `Json` incorrectly appears in the list of usable strategies in livecheck's verbose JSON output. This commit modifies `Strategy#from_url` to address this issue. The easiest way to enforce this rule involved passing in the `@strategies` key (a symbol) into the `select` block, so we can compare it to `livecheck_strategy` (the strategy symbol specified in the `livecheck` block). --- Library/Homebrew/livecheck/strategy.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/livecheck/strategy.rb b/Library/Homebrew/livecheck/strategy.rb index 61e7e06ed3..0a4c9803e3 100644 --- a/Library/Homebrew/livecheck/strategy.rb +++ b/Library/Homebrew/livecheck/strategy.rb @@ -151,15 +151,15 @@ module Homebrew ).returns(T::Array[T.untyped]) } def from_url(url, livecheck_strategy: nil, url_provided: false, regex_provided: false, block_provided: false) - usable_strategies = strategies.values.select do |strategy| + usable_strategies = strategies.select do |strategy_symbol, strategy| if strategy == PageMatch # Only treat the strategy as usable if the `livecheck` block # contains a regex and/or `strategy` block next if !regex_provided && !block_provided elsif strategy == Json # Only treat the strategy as usable if the `livecheck` block - # contains a `strategy` block - next unless block_provided + # specifies the strategy and contains a `strategy` block + next if (livecheck_strategy != strategy_symbol) || !block_provided elsif strategy.const_defined?(:PRIORITY) && !strategy::PRIORITY.positive? && from_symbol(livecheck_strategy) != strategy @@ -169,7 +169,7 @@ module Homebrew end strategy.respond_to?(:match?) && strategy.match?(url) - end + end.values # Sort usable strategies in descending order by priority, using the # DEFAULT_PRIORITY when a strategy doesn't contain a PRIORITY constant From ec7fd75934cabc78b258dd5a4bcd5bfd1c79ff07 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Mon, 27 Feb 2023 18:45:49 -0500 Subject: [PATCH 2/2] Strategy#from_url: Replace #from_symbol call Passing the strategy symbol into the `#from_url` `select` block means that we can also get rid of a `#from_symbol` call in a different conditional branch, as we can directly compare the `livecheck_strategy` symbol to `strategy_symbol`. --- Library/Homebrew/livecheck/strategy.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/livecheck/strategy.rb b/Library/Homebrew/livecheck/strategy.rb index 0a4c9803e3..0d4c6c022c 100644 --- a/Library/Homebrew/livecheck/strategy.rb +++ b/Library/Homebrew/livecheck/strategy.rb @@ -162,7 +162,7 @@ module Homebrew next if (livecheck_strategy != strategy_symbol) || !block_provided elsif strategy.const_defined?(:PRIORITY) && !strategy::PRIORITY.positive? && - from_symbol(livecheck_strategy) != strategy + livecheck_strategy != strategy_symbol # Ignore strategies with a priority of 0 or lower, unless the # strategy is specified in the `livecheck` block next