Remove cask-specific checksum errors.

This commit is contained in:
Markus Reiter 2020-11-19 19:44:23 +01:00
parent 7a83f34dd1
commit c54a9937e7
8 changed files with 33 additions and 98 deletions

View File

@ -50,21 +50,20 @@ module Cask
def verify_download_integrity(fn) def verify_download_integrity(fn)
if @cask.sha256 == :no_check if @cask.sha256 == :no_check
opoo "No checksum defined for Cask '#{@cask}', skipping verification." opoo "No checksum defined for cask '#{@cask}', skipping verification."
return return
end end
ohai "Verifying checksum for Cask '#{@cask}'." if verbose?
expected = @cask.sha256
actual = fn.sha256
begin begin
fn.verify_checksum(expected) ohai "Verifying checksum for cask '#{@cask}'." if verbose?
fn.verify_checksum(@cask.sha256)
rescue ChecksumMissingError rescue ChecksumMissingError
raise CaskSha256MissingError.new(@cask.token, expected, actual) opoo <<~EOS
rescue ChecksumMismatchError Cannot verify integrity of '#{fn.basename}'.
raise CaskSha256MismatchError.new(@cask.token, expected, actual, fn) No checksum was provided for this cask.
For your reference, the checksum is:
sha256 "#{fn.sha256}"
EOS
end end
end end

View File

@ -207,76 +207,6 @@ module Cask
end end
end end
# Error with a cask's checksum.
#
# @api private
class CaskSha256Error < AbstractCaskErrorWithToken
attr_reader :expected, :actual
def initialize(token, expected = nil, actual = nil)
super(token)
@expected = expected
@actual = actual
end
end
# Error when a cask's checksum is missing.
#
# @api private
class CaskSha256MissingError < CaskSha256Error
extend T::Sig
sig { returns(String) }
def to_s
<<~EOS
Cask '#{token}' requires a checksum:
#{Formatter.identifier("sha256 \"#{actual}\"")}
EOS
end
end
# Error when a cask's checksum does not match.
#
# @api private
class CaskSha256MismatchError < CaskSha256Error
extend T::Sig
attr_reader :path
def initialize(token, expected, actual, path)
super(token, expected, actual)
@path = path
end
sig { returns(String) }
def to_s
<<~EOS
Checksum for Cask '#{token}' does not match.
Expected: #{Formatter.success(expected.to_s)}
Actual: #{Formatter.error(actual.to_s)}
File: #{path}
To retry an incomplete download, remove the file above.
If the issue persists, visit:
#{Formatter.url("https://github.com/Homebrew/homebrew-cask/blob/HEAD/doc/reporting_bugs/checksum_does_not_match_error.md")}
EOS
end
end
# Error when a cask has no checksum and the `--require-sha` flag is passed.
#
# @api private
class CaskNoShasumError < CaskSha256Error
extend T::Sig
sig { returns(String) }
def to_s
<<~EOS
Cask '#{token}' does not have a sha256 checksum defined and was not installed.
This means you have the #{Formatter.identifier("--require-sha")} option set, perhaps in your HOMEBREW_CASK_OPTS.
EOS
end
end
# Error during quarantining of a file. # Error during quarantining of a file.
# #
# @api private # @api private

View File

@ -163,7 +163,10 @@ module Cask
odebug "Checking cask has checksum" odebug "Checking cask has checksum"
return unless @cask.sha256 == :no_check return unless @cask.sha256 == :no_check
raise CaskNoShasumError, @cask.token raise CaskError, <<~EOS
Cask '#{@cask}' does not have a sha256 checksum defined and was not installed.
This means you have the #{Formatter.identifier("--require-sha")} option set, perhaps in your HOMEBREW_CASK_OPTS.
EOS
end end
def primary_container def primary_container

View File

@ -617,15 +617,15 @@ class ChecksumMissingError < ArgumentError; end
class ChecksumMismatchError < RuntimeError class ChecksumMismatchError < RuntimeError
attr_reader :expected, :hash_type attr_reader :expected, :hash_type
def initialize(fn, expected, actual) def initialize(path, expected, actual)
@expected = expected @expected = expected
@hash_type = expected.hash_type.to_s.upcase @hash_type = expected.hash_type.to_s.upcase
super <<~EOS super <<~EOS
#{@hash_type} mismatch #{@hash_type} mismatch
Expected: #{expected} Expected: #{Formatter.success(expected.to_s)}
Actual: #{actual} Actual: #{Formatter.error(actual.to_s)}
Archive: #{fn} File: #{path}
To retry an incomplete download, remove the file above. To retry an incomplete download, remove the file above.
EOS EOS
end end

View File

@ -146,13 +146,16 @@ class Resource
def verify_download_integrity(fn) def verify_download_integrity(fn)
if fn.file? if fn.file?
ohai "Verifying #{fn.basename} checksum" if verbose? ohai "Verifying checksum for '#{fn.basename}'." if verbose?
fn.verify_checksum(checksum) fn.verify_checksum(checksum)
end end
rescue ChecksumMissingError rescue ChecksumMissingError
opoo "Cannot verify integrity of #{fn.basename}" opoo <<~EOS
puts "A checksum was not provided for this resource." Cannot verify integrity of '#{fn.basename}'.
puts "For your reference the SHA-256 is: #{fn.sha256}" No checksum was provided for this resource.
For your reference, the checksum is:
sha256 "#{fn.sha256}"
EOS
end end
Checksum::TYPES.each do |type| Checksum::TYPES.each do |type|

View File

@ -344,7 +344,7 @@ describe Cask::Cmd::Upgrade, :cask do
expect { expect {
described_class.run("bad-checksum") described_class.run("bad-checksum")
}.to raise_error(Cask::CaskSha256MismatchError).and(not_to_output(output_reverted).to_stderr) }.to raise_error(ChecksumMismatchError).and(not_to_output(output_reverted).to_stderr)
expect(bad_checksum).to be_installed expect(bad_checksum).to be_installed
expect(bad_checksum_path).to be_a_directory expect(bad_checksum_path).to be_a_directory

View File

@ -35,16 +35,16 @@ module Cask
context "when the expected checksum is nil" do context "when the expected checksum is nil" do
let(:expected_sha256) { nil } let(:expected_sha256) { nil }
it "raises an error" do it "outputs an error" do
expect { verification }.to raise_error(CaskSha256MissingError, /sha256 "#{computed_sha256}"/) expect { verification }.to output(/sha256 "#{computed_sha256}"/).to_stderr
end end
end end
context "when the expected checksum is empty" do context "when the expected checksum is empty" do
let(:expected_sha256) { Checksum.new(:sha256, "") } let(:expected_sha256) { Checksum.new(:sha256, "") }
it "raises an error" do it "outputs an error" do
expect { verification }.to raise_error(CaskSha256MissingError, /sha256 "#{computed_sha256}"/) expect { verification }.to output(/sha256 "#{computed_sha256}"/).to_stderr
end end
end end
@ -52,7 +52,7 @@ module Cask
let(:expected_sha256) { Checksum.new(:sha256, deadbeef) } let(:expected_sha256) { Checksum.new(:sha256, deadbeef) }
it "raises an error" do it "raises an error" do
expect { verification }.to raise_error CaskSha256MismatchError expect { verification }.to raise_error ChecksumMismatchError
end end
end end
end end

View File

@ -65,14 +65,14 @@ describe Cask::Installer, :cask do
bad_checksum = Cask::CaskLoader.load(cask_path("bad-checksum")) bad_checksum = Cask::CaskLoader.load(cask_path("bad-checksum"))
expect { expect {
described_class.new(bad_checksum).install described_class.new(bad_checksum).install
}.to raise_error(Cask::CaskSha256MismatchError) }.to raise_error(ChecksumMismatchError)
end end
it "blows up on a missing checksum" do it "blows up on a missing checksum" do
missing_checksum = Cask::CaskLoader.load(cask_path("missing-checksum")) missing_checksum = Cask::CaskLoader.load(cask_path("missing-checksum"))
expect { expect {
described_class.new(missing_checksum).install described_class.new(missing_checksum).install
}.to raise_error(Cask::CaskSha256MissingError) }.to output(/Cannot verify integrity/).to_stderr
end end
it "installs fine if sha256 :no_check is used" do it "installs fine if sha256 :no_check is used" do
@ -87,7 +87,7 @@ describe Cask::Installer, :cask do
no_checksum = Cask::CaskLoader.load(cask_path("no-checksum")) no_checksum = Cask::CaskLoader.load(cask_path("no-checksum"))
expect { expect {
described_class.new(no_checksum, require_sha: true).install described_class.new(no_checksum, require_sha: true).install
}.to raise_error(Cask::CaskNoShasumError) }.to raise_error(/--require-sha/)
end end
it "installs fine if sha256 :no_check is used with --require-sha and --force" do it "installs fine if sha256 :no_check is used with --require-sha and --force" do