From e604cf742bf158b9f133c14ac438822326fbbd2e Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Fri, 18 Dec 2020 10:47:09 -0500 Subject: [PATCH 1/4] dev-cmd/livecheck: use safe navigation on #sort_by --- Library/Homebrew/dev-cmd/livecheck.rb | 73 ++++++++++++++------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/Library/Homebrew/dev-cmd/livecheck.rb b/Library/Homebrew/dev-cmd/livecheck.rb index ae616463fc..ff95169b37 100644 --- a/Library/Homebrew/dev-cmd/livecheck.rb +++ b/Library/Homebrew/dev-cmd/livecheck.rb @@ -60,44 +60,45 @@ module Homebrew puts ENV["HOMEBREW_LIVECHECK_WATCHLIST"] if ENV["HOMEBREW_LIVECHECK_WATCHLIST"].present? end - formulae_and_casks_to_check = if args.tap - tap = Tap.fetch(args.tap) - formulae = args.cask? ? [] : tap.formula_files.map { |path| Formulary.factory(path) } - casks = args.formula? ? [] : tap.cask_files.map { |path| Cask::CaskLoader.load(path) } - formulae + casks - elsif args.installed? - formulae = args.cask? ? [] : Formula.installed - casks = args.formula? ? [] : Cask::Caskroom.casks - formulae + casks - elsif args.all? - formulae = args.cask? ? [] : Formula.to_a - casks = args.formula? ? [] : Cask::Cask.to_a - formulae + casks - elsif args.named.present? - if args.formula? - args.named.to_formulae - elsif args.cask? - args.named.to_casks - else - args.named.to_formulae_and_casks - end - elsif File.exist?(WATCHLIST_PATH) - begin - names = Pathname.new(WATCHLIST_PATH).read.lines - .reject { |line| line.start_with?("#") || line.blank? } - .map(&:strip) - - named_args = T.unsafe(CLI::NamedArgs).new(*names) - named_args.to_formulae_and_casks.reject do |formula_or_cask| - (args.formula? && !formula_or_cask.is_a?(Formula)) || - (args.cask? && !formula_or_cask.is_a?(Cask::Cask)) + formulae_and_casks_to_check = + if args.tap + tap = Tap.fetch(args.tap) + formulae = args.cask? ? [] : tap.formula_files.map { |path| Formulary.factory(path) } + casks = args.formula? ? [] : tap.cask_files.map { |path| Cask::CaskLoader.load(path) } + formulae + casks + elsif args.installed? + formulae = args.cask? ? [] : Formula.installed + casks = args.formula? ? [] : Cask::Caskroom.casks + formulae + casks + elsif args.all? + formulae = args.cask? ? [] : Formula.to_a + casks = args.formula? ? [] : Cask::Cask.to_a + formulae + casks + elsif args.named.present? + if args.formula? + args.named.to_formulae + elsif args.cask? + args.named.to_casks + else + args.named.to_formulae_and_casks end - rescue Errno::ENOENT => e - onoe e + elsif File.exist?(WATCHLIST_PATH) + begin + names = Pathname.new(WATCHLIST_PATH).read.lines + .reject { |line| line.start_with?("#") || line.blank? } + .map(&:strip) + + named_args = T.unsafe(CLI::NamedArgs).new(*names) + named_args.to_formulae_and_casks.reject do |formula_or_cask| + (args.formula? && !formula_or_cask.is_a?(Formula)) || + (args.cask? && !formula_or_cask.is_a?(Cask::Cask)) + end + rescue Errno::ENOENT => e + onoe e + end + end&.sort_by do |formula_or_cask| + formula_or_cask.respond_to?(:token) ? formula_or_cask.token : formula_or_cask.name end - end.sort_by do |formula_or_cask| - formula_or_cask.respond_to?(:token) ? formula_or_cask.token : formula_or_cask.name - end raise UsageError, "No formulae or casks to check." if formulae_and_casks_to_check.blank? From 9f7e471382d61b5a6ea61c48767271edcedcebe2 Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Fri, 18 Dec 2020 21:59:31 +0900 Subject: [PATCH 2/4] livecheck: raise error if no watchlist --- Library/Homebrew/dev-cmd/livecheck.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Library/Homebrew/dev-cmd/livecheck.rb b/Library/Homebrew/dev-cmd/livecheck.rb index ff95169b37..3c08adf53b 100644 --- a/Library/Homebrew/dev-cmd/livecheck.rb +++ b/Library/Homebrew/dev-cmd/livecheck.rb @@ -96,6 +96,9 @@ module Homebrew rescue Errno::ENOENT => e onoe e end + else + raise UsageError, "ENV['HOMEBREW_LIVECHECK_WATCHLIST'] or ~/.brew_livecheck_watchlist is required " \ + "if no formula or cask argument is passed" end&.sort_by do |formula_or_cask| formula_or_cask.respond_to?(:token) ? formula_or_cask.token : formula_or_cask.name end From ffc5c3206e8a9ec980e9dd659a5eca381cf1bd0a Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Sun, 20 Dec 2020 11:34:07 +0900 Subject: [PATCH 3/4] repair message --- Library/Homebrew/dev-cmd/livecheck.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Library/Homebrew/dev-cmd/livecheck.rb b/Library/Homebrew/dev-cmd/livecheck.rb index 3c08adf53b..a074f51933 100644 --- a/Library/Homebrew/dev-cmd/livecheck.rb +++ b/Library/Homebrew/dev-cmd/livecheck.rb @@ -97,8 +97,7 @@ module Homebrew onoe e end else - raise UsageError, "ENV['HOMEBREW_LIVECHECK_WATCHLIST'] or ~/.brew_livecheck_watchlist is required " \ - "if no formula or cask argument is passed" + raise UsageError, "A watchlist file is required when no arguments are given." end&.sort_by do |formula_or_cask| formula_or_cask.respond_to?(:token) ? formula_or_cask.token : formula_or_cask.name end From c7080ba812c707aafdd23f79211f7d11f484e015 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Sat, 19 Dec 2020 23:02:42 -0500 Subject: [PATCH 4/4] Expand dev-cmd/livecheck tests --- Library/Homebrew/test/dev-cmd/livecheck_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Library/Homebrew/test/dev-cmd/livecheck_spec.rb b/Library/Homebrew/test/dev-cmd/livecheck_spec.rb index 9bd0dfee50..56a9509f0a 100644 --- a/Library/Homebrew/test/dev-cmd/livecheck_spec.rb +++ b/Library/Homebrew/test/dev-cmd/livecheck_spec.rb @@ -21,4 +21,11 @@ describe "brew livecheck", :integration_test do .and not_to_output.to_stderr .and be_a_success end + + it "gives an error when no arguments are given and there's no watchlist" do + expect { brew "livecheck" } + .to output(/Invalid usage: A watchlist file is required when no arguments are given\./).to_stderr + .and not_to_output.to_stdout + .and be_a_failure + end end