livecheck: changes made and tests added

This commit is contained in:
nandahkrishna 2020-08-27 22:46:06 +05:30
parent c9f0642d45
commit 4e0cc48b65
No known key found for this signature in database
GPG Key ID: 067E5FCD58ADF3AA
2 changed files with 39 additions and 5 deletions

View File

@ -1,6 +1,13 @@
# frozen_string_literal: true # frozen_string_literal: true
require "livecheck/strategy"
module Homebrew module Homebrew
# The `Livecheck` module consists of methods used by the `brew livecheck`
# command. These methods receive print the requested livecheck information
# for formulae.
#
# @api private
module Livecheck module Livecheck
module_function module_function
@ -29,6 +36,9 @@ module Homebrew
rc rc
].freeze ].freeze
# Executes the livecheck logic for each formula in the `formulae_to_check` array
# and prints the results.
# @return [nil]
def livecheck_formulae(formulae_to_check, args) def livecheck_formulae(formulae_to_check, args)
# Identify any non-homebrew/core taps in use for current formulae # Identify any non-homebrew/core taps in use for current formulae
non_core_taps = {} non_core_taps = {}
@ -145,6 +155,9 @@ module Homebrew
puts JSON.generate(formulae_checked.compact) if args.json? puts JSON.generate(formulae_checked.compact) if args.json?
end end
# Returns the fully-qualified name of a formula if the full_name argument is
# provided, returns the name otherwise.
# @return [String]
def formula_name(formula, args:) def formula_name(formula, args:)
args.full_name? ? formula.full_name : formula.name args.full_name? ? formula.full_name : formula.name
end end
@ -166,6 +179,9 @@ module Homebrew
status_hash status_hash
end end
# If a formula has to be skipped, it prints or returns a Hash contaning the reason
# for doing so, else it returns false.
# @return [Hash, nil, Boolean]
def skip_conditions(formula, args:) def skip_conditions(formula, args:)
if formula.deprecated? && !formula.livecheckable? if formula.deprecated? && !formula.livecheckable?
return status_hash(formula, "deprecated", args: args) if args.json? return status_hash(formula, "deprecated", args: args) if args.json?
@ -218,6 +234,8 @@ module Homebrew
false false
end end
# Formats and prints the livecheck result for a formula.
# @return [nil]
def print_latest_version(info, args:) def print_latest_version(info, args:)
formula_s = "#{Tty.blue}#{info[:formula]}#{Tty.reset}" formula_s = "#{Tty.blue}#{info[:formula]}#{Tty.reset}"
formula_s += " (guessed)" if !info[:meta][:livecheckable] && args.verbose? formula_s += " (guessed)" if !info[:meta][:livecheckable] && args.verbose?
@ -237,6 +255,8 @@ module Homebrew
puts "#{formula_s} : #{current_s} ==> #{latest_s}" puts "#{formula_s} : #{current_s} ==> #{latest_s}"
end end
# Returns an Array containing the formula URLs that can be used by livecheck.
# @return [Array]
def checkable_urls(formula) def checkable_urls(formula)
urls = [] urls = []
urls << formula.head.url if formula.head urls << formula.head.url if formula.head
@ -249,6 +269,8 @@ module Homebrew
urls.compact urls.compact
end end
# Preprocesses and returns the URL used by livecheck.
# @return [String]
def preprocess_url(url) def preprocess_url(url)
# Check for GitHub repos on github.com, not AWS # Check for GitHub repos on github.com, not AWS
url.sub!("github.s3.amazonaws.com", "github.com") if url.include?("github") url.sub!("github.s3.amazonaws.com", "github.com") if url.include?("github")
@ -260,7 +282,7 @@ module Homebrew
elsif url.include? "releases" elsif url.include? "releases"
url = url.sub(%r{/releases/.*}, ".git") url = url.sub(%r{/releases/.*}, ".git")
elsif url.include? "downloads" elsif url.include? "downloads"
url = Pathname.new(url.sub(%r{/downloads(.*)}, "\\1")).dirname.to_s+".git" url = "#{Pathname.new(url.sub(%r{/downloads(.*)}, "\\1")).dirname}.git"
elsif !url.end_with?(".git") elsif !url.end_with?(".git")
# Truncate the URL at the user/repo part, if possible # Truncate the URL at the user/repo part, if possible
%r{(?<github_repo_url>(?:[a-z]+://)?github.com/[^/]+/[^/#]+)} =~ url %r{(?<github_repo_url>(?:[a-z]+://)?github.com/[^/]+/[^/#]+)} =~ url
@ -276,6 +298,9 @@ module Homebrew
url url
end end
# Identifies the latest version of the formula and returns a Hash containing
# the version information. Returns nil if a latest version couldn't be found.
# @return [Hash, nil]
def latest_version(formula, args:) def latest_version(formula, args:)
has_livecheckable = formula.livecheckable? has_livecheckable = formula.livecheckable?
livecheck = formula.livecheck livecheck = formula.livecheck

View File

@ -13,7 +13,7 @@ describe Homebrew::Livecheck do
head "https://github.com/Homebrew/brew.git" head "https://github.com/Homebrew/brew.git"
livecheck do livecheck do
url "https://github.s3.amazonaws.com/Homebrew/brew/releases/latest" url(+"https://github.s3.amazonaws.com/Homebrew/brew/releases/latest")
regex(%r{href=.*?/tag/v?(\d+(?:\.\d+)+)["' >]}i) regex(%r{href=.*?/tag/v?(\d+(?:\.\d+)+)["' >]}i)
end end
end end
@ -137,12 +137,21 @@ describe Homebrew::Livecheck do
end end
end end
describe "::preprocess_url" do
it "returns the preprocessed URL for livecheck to use" do
expect(livecheck.preprocess_url(f.livecheck.url))
.to eq("https://github.com/Homebrew/brew/releases/latest")
end
end
describe "::livecheck_formulae", :needs_network do describe "::livecheck_formulae", :needs_network do
it "checks for the latest versions of the formulae" do it "checks for the latest versions of the formulae" do
allow(args).to receive(:debug?).and_return(true) allow(args).to receive(:debug?).and_return(false)
allow(args).to receive(:newer_only?).and_return(false)
expect(livecheck.livecheck_formulae([f], args)) expect { livecheck.livecheck_formulae([f], args) }
.to eq(/Formula/) .to output(/test : 0\.0\.1 ==> (\d+(?:\.\d+)+)/im).to_stdout
.and not_to_output.to_stderr
end end
end end
end end