From fcf8cb24fd34c416c710926e906664346954a583 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 6 Jul 2018 22:54:54 +0200 Subject: [PATCH] Remove unused `incremental_hash` method. --- .../Homebrew/cask/lib/hbc/verify/checksum.rb | 2 +- Library/Homebrew/extend/pathname.rb | 14 +----- .../test/cask/verify/checksum_spec.rb | 45 ++++++++++--------- 3 files changed, 25 insertions(+), 36 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/verify/checksum.rb b/Library/Homebrew/cask/lib/hbc/verify/checksum.rb index 620703d313..c3b82014bb 100644 --- a/Library/Homebrew/cask/lib/hbc/verify/checksum.rb +++ b/Library/Homebrew/cask/lib/hbc/verify/checksum.rb @@ -29,7 +29,7 @@ module Hbc end def computed - @computed ||= Digest::SHA2.file(downloaded_path).hexdigest + @computed ||= downloaded_path.sha256 end def verify_checksum diff --git a/Library/Homebrew/extend/pathname.rb b/Library/Homebrew/extend/pathname.rb index dfe24b7886..6e37f4ad9e 100644 --- a/Library/Homebrew/extend/pathname.rb +++ b/Library/Homebrew/extend/pathname.rb @@ -309,21 +309,9 @@ class Pathname /^#!\s*\S+/ =~ open("r") { |f| f.read(1024) } end - # @private - def incremental_hash(klass) - digest = klass.new - if digest.respond_to?(:file) - digest.file(self) - else - buf = "" - open("rb") { |f| digest << buf while f.read(16384, buf) } - end - digest.hexdigest - end - def sha256 require "digest/sha2" - incremental_hash(Digest::SHA256) + Digest::SHA256.file(self).hexdigest end def verify_checksum(expected) diff --git a/Library/Homebrew/test/cask/verify/checksum_spec.rb b/Library/Homebrew/test/cask/verify/checksum_spec.rb index 117b9089df..52c86ffc08 100644 --- a/Library/Homebrew/test/cask/verify/checksum_spec.rb +++ b/Library/Homebrew/test/cask/verify/checksum_spec.rb @@ -1,35 +1,35 @@ describe Hbc::Verify::Checksum, :cask do let(:cask) { double("cask", token: "cask") } - let(:downloaded_path) { double("downloaded_path") } + let(:downloaded_path) { instance_double("Pathname") } let(:verification) { described_class.new(cask, downloaded_path) } before do - allow(cask).to receive(:sha256).and_return(sha256) + allow(cask).to receive(:sha256).and_return(expected_sha256) end describe ".me?" do subject { described_class.me?(cask) } - context "sha256 is :no_check" do - let(:sha256) { :no_check } + context "when expected sha256 is :no_check" do + let(:expected_sha256) { :no_check } it { is_expected.to be false } end - context "sha256 is nil" do - let(:sha256) { nil } + context "when expected sha256 is nil" do + let(:expected_sha256) { nil } it { is_expected.to be true } end context "sha256 is empty" do - let(:sha256) { "" } + let(:expected_sha256) { "" } it { is_expected.to be true } end - context "sha256 is a valid shasum" do - let(:sha256) { "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef" } + context "when expected sha256 is a valid shasum" do + let(:expected_sha256) { "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef" } it { is_expected.to be true } end @@ -38,46 +38,47 @@ describe Hbc::Verify::Checksum, :cask do describe "#verify" do subject { verification.verify } - let(:computed) { "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef" } - before do - allow(verification).to receive(:computed).and_return(computed) + allow(cask).to receive(:sha256).and_return(expected_sha256) + allow(downloaded_path).to receive(:sha256).and_return(actual_sha256) end - context "sha256 matches computed" do - let(:sha256) { computed } + let(:actual_sha256) { "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef" } + + context "when expected matches actual sha256" do + let(:expected_sha256) { actual_sha256 } it "does not raise an error" do expect { subject }.not_to raise_error end end - context "sha256 is :no_check" do - let(:sha256) { :no_check } + context "when expected sha256 is :no_check" do + let(:expected_sha256) { :no_check } it "does not raise an error" do expect { subject }.not_to raise_error end end - context "sha256 does not match computed" do - let(:sha256) { "d3adb33fd3adb33fd3adb33fd3adb33fd3adb33fd3adb33fd3adb33fd3adb33f" } + context "when expected does not match sha256" do + let(:expected_sha256) { "d3adb33fd3adb33fd3adb33fd3adb33fd3adb33fd3adb33fd3adb33fd3adb33f" } it "raises an error" do expect { subject }.to raise_error(Hbc::CaskSha256MismatchError) end end - context "sha256 is nil" do - let(:sha256) { nil } + context "when expected sha256 is nil" do + let(:expected_sha256) { nil } it "raises an error" do expect { subject }.to raise_error(Hbc::CaskSha256MissingError) end end - context "sha256 is empty" do - let(:sha256) { "" } + context "when expected sha256 is empty" do + let(:expected_sha256) { "" } it "raises an error" do expect { subject }.to raise_error(Hbc::CaskSha256MissingError)