From c54a9937e7b3166865b78f219acb40d26903f100 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Thu, 19 Nov 2020 19:44:23 +0100 Subject: [PATCH] Remove cask-specific checksum errors. --- Library/Homebrew/cask/download.rb | 19 +++-- Library/Homebrew/cask/exceptions.rb | 70 ------------------- Library/Homebrew/cask/installer.rb | 5 +- Library/Homebrew/exceptions.rb | 8 +-- Library/Homebrew/resource.rb | 11 +-- .../Homebrew/test/cask/cmd/upgrade_spec.rb | 2 +- Library/Homebrew/test/cask/download_spec.rb | 10 +-- Library/Homebrew/test/cask/installer_spec.rb | 6 +- 8 files changed, 33 insertions(+), 98 deletions(-) diff --git a/Library/Homebrew/cask/download.rb b/Library/Homebrew/cask/download.rb index 33a643aae3..5d577eeb2e 100644 --- a/Library/Homebrew/cask/download.rb +++ b/Library/Homebrew/cask/download.rb @@ -50,21 +50,20 @@ module Cask def verify_download_integrity(fn) if @cask.sha256 == :no_check - opoo "No checksum defined for Cask '#{@cask}', skipping verification." + opoo "No checksum defined for cask '#{@cask}', skipping verification." return end - ohai "Verifying checksum for Cask '#{@cask}'." if verbose? - - expected = @cask.sha256 - actual = fn.sha256 - begin - fn.verify_checksum(expected) + ohai "Verifying checksum for cask '#{@cask}'." if verbose? + fn.verify_checksum(@cask.sha256) rescue ChecksumMissingError - raise CaskSha256MissingError.new(@cask.token, expected, actual) - rescue ChecksumMismatchError - raise CaskSha256MismatchError.new(@cask.token, expected, actual, fn) + opoo <<~EOS + Cannot verify integrity of '#{fn.basename}'. + No checksum was provided for this cask. + For your reference, the checksum is: + sha256 "#{fn.sha256}" + EOS end end diff --git a/Library/Homebrew/cask/exceptions.rb b/Library/Homebrew/cask/exceptions.rb index b5576e9dee..9778c29725 100644 --- a/Library/Homebrew/cask/exceptions.rb +++ b/Library/Homebrew/cask/exceptions.rb @@ -207,76 +207,6 @@ module Cask 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. # # @api private diff --git a/Library/Homebrew/cask/installer.rb b/Library/Homebrew/cask/installer.rb index 6da1fa5ce8..412abb3c61 100644 --- a/Library/Homebrew/cask/installer.rb +++ b/Library/Homebrew/cask/installer.rb @@ -163,7 +163,10 @@ module Cask odebug "Checking cask has checksum" 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 def primary_container diff --git a/Library/Homebrew/exceptions.rb b/Library/Homebrew/exceptions.rb index fadca48894..428140ac4d 100644 --- a/Library/Homebrew/exceptions.rb +++ b/Library/Homebrew/exceptions.rb @@ -617,15 +617,15 @@ class ChecksumMissingError < ArgumentError; end class ChecksumMismatchError < RuntimeError attr_reader :expected, :hash_type - def initialize(fn, expected, actual) + def initialize(path, expected, actual) @expected = expected @hash_type = expected.hash_type.to_s.upcase super <<~EOS #{@hash_type} mismatch - Expected: #{expected} - Actual: #{actual} - Archive: #{fn} + Expected: #{Formatter.success(expected.to_s)} + Actual: #{Formatter.error(actual.to_s)} + File: #{path} To retry an incomplete download, remove the file above. EOS end diff --git a/Library/Homebrew/resource.rb b/Library/Homebrew/resource.rb index 68c237471a..c2a2b749a8 100644 --- a/Library/Homebrew/resource.rb +++ b/Library/Homebrew/resource.rb @@ -146,13 +146,16 @@ class Resource def verify_download_integrity(fn) if fn.file? - ohai "Verifying #{fn.basename} checksum" if verbose? + ohai "Verifying checksum for '#{fn.basename}'." if verbose? fn.verify_checksum(checksum) end rescue ChecksumMissingError - opoo "Cannot verify integrity of #{fn.basename}" - puts "A checksum was not provided for this resource." - puts "For your reference the SHA-256 is: #{fn.sha256}" + opoo <<~EOS + Cannot verify integrity of '#{fn.basename}'. + No checksum was provided for this resource. + For your reference, the checksum is: + sha256 "#{fn.sha256}" + EOS end Checksum::TYPES.each do |type| diff --git a/Library/Homebrew/test/cask/cmd/upgrade_spec.rb b/Library/Homebrew/test/cask/cmd/upgrade_spec.rb index e930e00b7a..4c9f1a3197 100644 --- a/Library/Homebrew/test/cask/cmd/upgrade_spec.rb +++ b/Library/Homebrew/test/cask/cmd/upgrade_spec.rb @@ -344,7 +344,7 @@ describe Cask::Cmd::Upgrade, :cask do expect { 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_path).to be_a_directory diff --git a/Library/Homebrew/test/cask/download_spec.rb b/Library/Homebrew/test/cask/download_spec.rb index 65ba2b5ce1..c439c2200a 100644 --- a/Library/Homebrew/test/cask/download_spec.rb +++ b/Library/Homebrew/test/cask/download_spec.rb @@ -35,16 +35,16 @@ module Cask context "when the expected checksum is nil" do let(:expected_sha256) { nil } - it "raises an error" do - expect { verification }.to raise_error(CaskSha256MissingError, /sha256 "#{computed_sha256}"/) + it "outputs an error" do + expect { verification }.to output(/sha256 "#{computed_sha256}"/).to_stderr end end context "when the expected checksum is empty" do let(:expected_sha256) { Checksum.new(:sha256, "") } - it "raises an error" do - expect { verification }.to raise_error(CaskSha256MissingError, /sha256 "#{computed_sha256}"/) + it "outputs an error" do + expect { verification }.to output(/sha256 "#{computed_sha256}"/).to_stderr end end @@ -52,7 +52,7 @@ module Cask let(:expected_sha256) { Checksum.new(:sha256, deadbeef) } it "raises an error" do - expect { verification }.to raise_error CaskSha256MismatchError + expect { verification }.to raise_error ChecksumMismatchError end end end diff --git a/Library/Homebrew/test/cask/installer_spec.rb b/Library/Homebrew/test/cask/installer_spec.rb index 688312e91c..6a9a1bc67a 100644 --- a/Library/Homebrew/test/cask/installer_spec.rb +++ b/Library/Homebrew/test/cask/installer_spec.rb @@ -65,14 +65,14 @@ describe Cask::Installer, :cask do bad_checksum = Cask::CaskLoader.load(cask_path("bad-checksum")) expect { described_class.new(bad_checksum).install - }.to raise_error(Cask::CaskSha256MismatchError) + }.to raise_error(ChecksumMismatchError) end it "blows up on a missing checksum" do missing_checksum = Cask::CaskLoader.load(cask_path("missing-checksum")) expect { described_class.new(missing_checksum).install - }.to raise_error(Cask::CaskSha256MissingError) + }.to output(/Cannot verify integrity/).to_stderr end 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")) expect { described_class.new(no_checksum, require_sha: true).install - }.to raise_error(Cask::CaskNoShasumError) + }.to raise_error(/--require-sha/) end it "installs fine if sha256 :no_check is used with --require-sha and --force" do