2020-10-10 14:16:11 +02:00
|
|
|
# typed: false
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-21 20:58:43 +01:00
|
|
|
require "resource"
|
|
|
|
|
|
|
|
describe Resource do
|
2021-01-31 13:14:23 -05:00
|
|
|
subject(:resource) { described_class.new("test") }
|
2017-02-21 20:58:43 +01:00
|
|
|
|
|
|
|
describe "#url" do
|
|
|
|
it "sets the URL" do
|
2021-01-31 13:14:23 -05:00
|
|
|
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
|
2021-01-31 13:14:23 -05:00
|
|
|
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)
|
2021-01-31 13:14:23 -05:00
|
|
|
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)
|
2021-01-31 13:14:23 -05:00
|
|
|
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
|
2021-01-31 13:14:23 -05:00
|
|
|
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
|
2021-01-31 13:14:23 -05:00
|
|
|
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" }
|
2021-01-31 13:14:23 -05:00
|
|
|
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
|
2021-01-31 13:14:23 -05:00
|
|
|
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
|
2021-01-31 13:14:23 -05:00
|
|
|
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)
|
2021-01-31 13:14:23 -05:00
|
|
|
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
|
2021-01-31 13:14:23 -05:00
|
|
|
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
|
2021-01-31 13:14:23 -05:00
|
|
|
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
|
2022-03-01 00:01:13 +00:00
|
|
|
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
|
2021-01-31 13:14:23 -05:00
|
|
|
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
|
2021-01-31 13:14:23 -05:00
|
|
|
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
|
2022-03-01 00:01:13 +00:00
|
|
|
expect(resource.checksum).to be_nil
|
2017-02-21 20:58:43 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the checksum set with #sha256" do
|
2021-01-31 13:14:23 -05:00
|
|
|
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)
|
2021-01-31 13:14:23 -05:00
|
|
|
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
|
2021-01-31 13:14:23 -05:00
|
|
|
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
|
2021-01-31 13:14:23 -05:00
|
|
|
resource.patch(:p1) { url "file:///my.patch" }
|
2018-01-21 08:29:38 -08:00
|
|
|
owner = Object.new
|
2021-01-31 13:14:23 -05:00
|
|
|
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
|
2021-01-31 13:14:23 -05:00
|
|
|
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)
|
|
|
|
|
2021-01-31 13:14:23 -05:00
|
|
|
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")
|
2021-01-31 13:14:23 -05:00
|
|
|
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 {
|
2021-01-31 13:14:23 -05:00
|
|
|
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
|