Merge pull request #4434 from reitermarkus/simplify-sha256

Remove unused `incremental_hash` method.
This commit is contained in:
Mike McQuaid 2018-07-07 13:38:11 +01:00 committed by GitHub
commit bef2765dd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 36 deletions

View File

@ -29,7 +29,7 @@ module Hbc
end
def computed
@computed ||= Digest::SHA2.file(downloaded_path).hexdigest
@computed ||= downloaded_path.sha256
end
def verify_checksum

View File

@ -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)

View File

@ -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)