Added flag for cask_reason to output cask info or just message with cask name

This commit is contained in:
Zach Auten 2019-03-24 15:28:34 -04:00
parent 517083474a
commit 999c4743d0
3 changed files with 21 additions and 10 deletions

View File

@ -89,7 +89,7 @@ module Homebrew
end end
ofail e.message ofail e.message
# No formula with this name, try a missing formula lookup # No formula with this name, try a missing formula lookup
if (reason = MissingFormula.reason(f) || MissingFormula.cask_reason(f)) if (reason = MissingFormula.reason(f, show_info: true))
$stderr.puts reason $stderr.puts reason
end end
end end

View File

@ -6,8 +6,9 @@ require "cask/cask_loader"
module Homebrew module Homebrew
module MissingFormula module MissingFormula
class << self class << self
def reason(name, silent: false) def reason(name, silent: false, show_info: false)
blacklisted_reason(name) || tap_migration_reason(name) || deleted_reason(name, silent: silent) cask_reason(name, silent: silent, show_info: show_info) || blacklisted_reason(name) ||
tap_migration_reason(name) || deleted_reason(name, silent: silent)
end end
def blacklisted_reason(name) def blacklisted_reason(name)
@ -189,12 +190,13 @@ module Homebrew
end end
end end
def cask_reason(name, silent: false) def cask_reason(name, silent: false, show_info: false)
return if silent return if silent
cask = Cask::CaskLoader.load(name) cask = Cask::CaskLoader.load(name)
reason = "Found the following cask named \"#{name}\" instead:\n" reason = "Found a cask named \"#{name}\" instead.\n"
reason << Cask::Cmd::Info.get_info(cask) reason << Cask::Cmd::Info.get_info(cask) if show_info
reason
rescue Cask::CaskUnavailableError rescue Cask::CaskUnavailableError
nil nil
end end

View File

@ -138,16 +138,25 @@ describe Homebrew::MissingFormula do
end end
describe "::cask_reason", :cask do describe "::cask_reason", :cask do
subject { described_class.cask_reason(formula) } subject { described_class.cask_reason(formula, show_info: show_info) }
context "with a missing formula that exists as a cask" do context "with a formula name that is a cask and show_info: false" do
let(:formula) { "local-caffeine" } let(:formula) { "local-caffeine" }
let(:show_info) { false }
it { is_expected.to match(/Found the following cask named "local-caffeine" instead:\nlocal-caffeine: 1.2.3\n/) } it { is_expected.to match(/Found a cask named "local-caffeine" instead./) }
end end
context "with a missing formula that does not share a name with a cask" do context "with a formula name that is a cask and show_info: true" do
let(:formula) { "local-caffeine" }
let(:show_info) { true }
it { is_expected.to match(/Found a cask named "local-caffeine" instead.\nlocal-caffeine: 1.2.3\n/) }
end
context "with a formula name that is not a cask" do
let(:formula) { "missing-formula" } let(:formula) { "missing-formula" }
let(:show_info) { false }
it { is_expected.to be_nil } it { is_expected.to be_nil }
end end