brew/Library/Homebrew/test/resource_spec.rb

172 lines
5.1 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
2017-02-21 20:58:43 +01:00
require "resource"
describe Resource do
subject(:resource) { described_class.new("test") }
2017-02-21 20:58:43 +01:00
describe "#url" do
it "sets the URL" do
resource.url("foo")
expect(resource.url).to eq("foo")
2017-02-21 20:58:43 +01:00
end
it "can set the URL with specifications" do
resource.url("foo", branch: "master")
expect(resource.url).to eq("foo")
expect(resource.specs).to eq(branch: "master")
2017-02-21 20:58:43 +01:00
end
it "can set the URL with a custom download strategy class" do
strategy = Class.new(AbstractDownloadStrategy)
resource.url("foo", using: strategy)
expect(resource.url).to eq("foo")
expect(resource.download_strategy).to eq(strategy)
2017-02-21 20:58:43 +01:00
end
it "can set the URL with specifications and a custom download strategy class" do
strategy = Class.new(AbstractDownloadStrategy)
resource.url("foo", using: strategy, branch: "master")
expect(resource.url).to eq("foo")
expect(resource.specs).to eq(branch: "master")
expect(resource.download_strategy).to eq(strategy)
2017-02-21 20:58:43 +01:00
end
it "can set the URL with a custom download strategy symbol" do
resource.url("foo", using: :git)
expect(resource.url).to eq("foo")
expect(resource.download_strategy).to eq(GitDownloadStrategy)
2017-02-21 20:58:43 +01:00
end
2019-08-19 14:27:29 +10:00
it "raises an error if the download strategy class is unknown" do
expect { resource.url("foo", using: Class.new) }.to raise_error(TypeError)
2017-02-21 20:58:43 +01:00
end
it "does not mutate the specifications hash" do
specs = { using: :git, branch: "master" }
resource.url("foo", specs)
expect(resource.specs).to eq(branch: "master")
expect(resource.using).to eq(:git)
2017-02-21 20:58:43 +01:00
expect(specs).to eq(using: :git, branch: "master")
end
end
describe "#version" do
it "sets the version" do
resource.version("1.0")
expect(resource.version).to eq(Version.parse("1.0"))
expect(resource.version).not_to be_detected_from_url
2017-02-21 20:58:43 +01:00
end
it "can detect the version from a URL" do
resource.url("https://brew.sh/foo-1.0.tar.gz")
expect(resource.version).to eq(Version.parse("1.0"))
expect(resource.version).to be_detected_from_url
2017-02-21 20:58:43 +01:00
end
it "can set the version with a scheme" do
klass = Class.new(Version)
resource.version klass.new("1.0")
expect(resource.version).to eq(Version.parse("1.0"))
expect(resource.version).to be_a(klass)
2017-02-21 20:58:43 +01:00
end
it "can set the version from a tag" do
resource.url("https://brew.sh/foo-1.0.tar.gz", tag: "v1.0.2")
expect(resource.version).to eq(Version.parse("1.0.2"))
expect(resource.version).to be_detected_from_url
2017-02-21 20:58:43 +01:00
end
it "rejects non-string versions" do
expect { resource.version(1) }.to raise_error(TypeError)
expect { resource.version(2.0) }.to raise_error(TypeError)
expect { resource.version(Object.new) }.to raise_error(TypeError)
2017-02-21 20:58:43 +01:00
end
it "returns nil if unset" do
expect(resource.version).to be_nil
2017-02-21 20:58:43 +01:00
end
end
describe "#mirrors" do
it "is empty by defaults" do
expect(resource.mirrors).to be_empty
2017-02-21 20:58:43 +01:00
end
it "returns an array of mirrors added with #mirror" do
resource.mirror("foo")
resource.mirror("bar")
expect(resource.mirrors).to eq(%w[foo bar])
2017-02-21 20:58:43 +01:00
end
end
describe "#checksum" do
it "returns nil if unset" do
expect(resource.checksum).to be_nil
2017-02-21 20:58:43 +01:00
end
it "returns the checksum set with #sha256" do
resource.sha256(TEST_SHA256)
expect(resource.checksum).to eq(Checksum.new(TEST_SHA256))
2017-02-21 20:58:43 +01:00
end
end
describe "#download_strategy" do
it "returns the download strategy" do
strategy = Object.new
expect(DownloadStrategyDetector)
.to receive(:detect).with("foo", nil).and_return(strategy)
resource.url("foo")
expect(resource.download_strategy).to eq(strategy)
2017-02-21 20:58:43 +01:00
end
end
2018-01-21 08:29:38 -08:00
describe "#owner" do
it "sets the owner" do
owner = Object.new
resource.owner = owner
expect(resource.owner).to eq(owner)
2018-01-21 08:29:38 -08:00
end
it "sets its owner to be the patches' owner" do
resource.patch(:p1) { url "file:///my.patch" }
2018-01-21 08:29:38 -08:00
owner = Object.new
resource.owner = owner
resource.patches.each do |p|
2018-01-21 08:29:38 -08:00
expect(p.resource.owner).to eq(owner)
end
end
end
describe "#patch" do
it "adds a patch" do
resource.patch(:p1, :DATA)
expect(resource.patches.count).to eq(1)
expect(resource.patches.first.strip).to eq(:p1)
2018-01-21 08:29:38 -08:00
end
end
2017-02-21 20:58:43 +01:00
specify "#verify_download_integrity_missing" do
fn = Pathname.new("test")
allow(fn).to receive(:file?).and_return(true)
expect(fn).to receive(:verify_checksum).and_raise(ChecksumMissingError)
expect(fn).to receive(:sha256)
resource.verify_download_integrity(fn)
2017-02-21 20:58:43 +01:00
end
specify "#verify_download_integrity_mismatch" do
2020-03-28 00:10:42 +00:00
fn = double(file?: true, basename: "foo")
checksum = resource.sha256(TEST_SHA256)
2017-02-21 20:58:43 +01:00
expect(fn).to receive(:verify_checksum).with(checksum)
.and_raise(ChecksumMismatchError.new(fn, checksum, Object.new))
2017-07-29 19:55:05 +02:00
expect {
resource.verify_download_integrity(fn)
2017-07-29 19:55:05 +02:00
}.to raise_error(ChecksumMismatchError)
2017-02-21 20:58:43 +01:00
end
end