named_args: print error if formula and cask found but one is unreadable

This commit is contained in:
Seeker 2021-01-27 10:03:44 -08:00
parent 89930157b5
commit 1ce8bd00fb
4 changed files with 41 additions and 7 deletions

View File

@ -110,7 +110,17 @@ module Homebrew
if only != :formula if only != :formula
begin begin
return Cask::CaskLoader.load(name, config: Cask::Config.from_args(@parent)) cask = Cask::CaskLoader.load(name, config: Cask::Config.from_args(@parent))
if unreadable_error.present?
onoe <<~EOS
Failed to load formula: #{name}
#{unreadable_error}
EOS
opoo "Treating #{name} as a cask."
end
return cask
rescue Cask::CaskUnreadableError => e rescue Cask::CaskUnreadableError => e
# Need to rescue before `CaskUnavailableError` (superclass of this) # Need to rescue before `CaskUnavailableError` (superclass of this)
# The cask was found, but there's a problem with its implementation # The cask was found, but there's a problem with its implementation
@ -287,12 +297,22 @@ module Homebrew
end end
def warn_if_cask_conflicts(ref, loaded_type) def warn_if_cask_conflicts(ref, loaded_type)
cask = Cask::CaskLoader.load ref
message = "Treating #{ref} as a #{loaded_type}." message = "Treating #{ref} as a #{loaded_type}."
message += " For the cask, use #{cask.tap.name}/#{cask.token}" if cask.tap.present? begin
cask = Cask::CaskLoader.load ref
message += " For the cask, use #{cask.tap.name}/#{cask.token}" if cask.tap.present?
rescue Cask::CaskUnreadableError => e
# Need to rescue before `CaskUnavailableError` (superclass of this)
# The cask was found, but there's a problem with its implementation
onoe <<~EOS
Failed to load cask: #{ref}
#{e}
EOS
rescue Cask::CaskUnavailableError
# No ref conflict with a cask, do nothing
return
end
opoo message.freeze opoo message.freeze
rescue Cask::CaskUnavailableError
# No ref conflict with a cask, do nothing
end end
end end
end end

View File

@ -122,6 +122,7 @@ describe Homebrew::CLI::NamedArgs do
setup_unredable_cask "foo" setup_unredable_cask "foo"
expect(described_class.new("foo").to_formulae_and_casks).to eq [foo] expect(described_class.new("foo").to_formulae_and_casks).to eq [foo]
expect { described_class.new("foo").to_formulae_and_casks }.to output(/Failed to load cask: foo/).to_stderr
end end
it "returns cask when formula is unreadable and cask is present" do it "returns cask when formula is unreadable and cask is present" do
@ -129,6 +130,7 @@ describe Homebrew::CLI::NamedArgs do
stub_cask_loader foo_cask stub_cask_loader foo_cask
expect(described_class.new("foo").to_formulae_and_casks).to eq [foo_cask] expect(described_class.new("foo").to_formulae_and_casks).to eq [foo_cask]
expect { described_class.new("foo").to_formulae_and_casks }.to output(/Failed to load formula: foo/).to_stderr
end end
it "raises an error when formula is absent and cask is unreadable" do it "raises an error when formula is absent and cask is unreadable" do

View File

@ -10,6 +10,10 @@ end
describe "brew --cache", :integration_test do describe "brew --cache", :integration_test do
it "prints all cache files for a given Formula" do it "prints all cache files for a given Formula" do
expect { brew "--cache", testball } expect { brew "--cache", testball }
.to output(%r{#{HOMEBREW_CACHE}/downloads/[\da-f]{64}--testball-}o).to_stdout
.and output(/Treating #{Regexp.escape(testball)} as a formula/).to_stderr
.and be_a_success
expect { brew "--cache", "--formula", testball }
.to output(%r{#{HOMEBREW_CACHE}/downloads/[\da-f]{64}--testball-}o).to_stdout .to output(%r{#{HOMEBREW_CACHE}/downloads/[\da-f]{64}--testball-}o).to_stdout
.and not_to_output.to_stderr .and not_to_output.to_stderr
.and be_a_success .and be_a_success
@ -17,6 +21,10 @@ describe "brew --cache", :integration_test do
it "prints the cache files for a given Cask" do it "prints the cache files for a given Cask" do
expect { brew "--cache", cask_path("local-caffeine") } expect { brew "--cache", cask_path("local-caffeine") }
.to output(%r{#{HOMEBREW_CACHE}/downloads/[\da-f]{64}--caffeine\.zip}o).to_stdout
.and output(/Treating #{Regexp.escape(cask_path("local-caffeine"))} as a cask/).to_stderr
.and be_a_success
expect { brew "--cache", "--cask", cask_path("local-caffeine") }
.to output(%r{#{HOMEBREW_CACHE}/downloads/[\da-f]{64}--caffeine\.zip}o).to_stdout .to output(%r{#{HOMEBREW_CACHE}/downloads/[\da-f]{64}--caffeine\.zip}o).to_stdout
.and not_to_output.to_stderr .and not_to_output.to_stderr
.and be_a_success .and be_a_success
@ -30,7 +38,7 @@ describe "brew --cache", :integration_test do
#{HOMEBREW_CACHE}/downloads/[\da-f]{64}--caffeine\.zip #{HOMEBREW_CACHE}/downloads/[\da-f]{64}--caffeine\.zip
}xo, }xo,
).to_stdout ).to_stdout
.and not_to_output.to_stderr .and output(/(Treating .* as a formula).*(Treating .* as a cask)/m).to_stderr
.and be_a_success .and be_a_success
end end
end end

View File

@ -39,6 +39,10 @@ describe "brew home", :integration_test do
it "opens the homepage for a given Cask" do it "opens the homepage for a given Cask" do
expect { brew "home", local_caffeine_path, "HOMEBREW_BROWSER" => "echo" } expect { brew "home", local_caffeine_path, "HOMEBREW_BROWSER" => "echo" }
.to output(/#{local_caffeine_homepage}/).to_stdout
.and output(/Treating #{Regexp.escape(local_caffeine_path)} as a cask/).to_stderr
.and be_a_success
expect { brew "home", "--cask", local_caffeine_path, "HOMEBREW_BROWSER" => "echo" }
.to output(/#{local_caffeine_homepage}/).to_stdout .to output(/#{local_caffeine_homepage}/).to_stdout
.and not_to_output.to_stderr .and not_to_output.to_stderr
.and be_a_success .and be_a_success
@ -49,7 +53,7 @@ describe "brew home", :integration_test do
expect { brew "home", "testballhome", local_caffeine_path, "HOMEBREW_BROWSER" => "echo" } expect { brew "home", "testballhome", local_caffeine_path, "HOMEBREW_BROWSER" => "echo" }
.to output(/#{testballhome_homepage} #{local_caffeine_homepage}/).to_stdout .to output(/#{testballhome_homepage} #{local_caffeine_homepage}/).to_stdout
.and not_to_output.to_stderr .and output(/Treating #{Regexp.escape(local_caffeine_path)} as a cask/).to_stderr
.and be_a_success .and be_a_success
end end
end end