brew/Library/Homebrew/test/resource_spec.rb

202 lines
6.0 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-02-21 20:58:43 +01:00
require "resource"
require "livecheck"
2017-02-21 20:58:43 +01:00
RSpec.describe Resource do
subject(:resource) { described_class.new("test") }
2022-07-02 10:09:25 +02:00
let(:livecheck_resource) do
described_class.new do
2022-07-04 12:41:58 -04:00
url "https://brew.sh/foo-1.0.tar.gz"
sha256 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
livecheck do
2022-07-04 12:41:58 -04:00
url "https://brew.sh/test/releases"
regex(/foo[._-]v?(\d+(?:\.\d+)+)\.t/i)
end
end
end
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 "#livecheck" do
specify "when `livecheck` block is set" do
2022-07-04 12:41:58 -04:00
expect(livecheck_resource.livecheck.url).to eq("https://brew.sh/test/releases")
expect(livecheck_resource.livecheck.regex).to eq(/foo[._-]v?(\d+(?:\.\d+)+)\.t/i)
end
end
describe "#livecheck_defined?" do
it "returns false if `livecheck` block is not set in resource" do
expect(resource.livecheck_defined?).to be false
end
specify "`livecheck` block defined in resources" do
expect(livecheck_resource.livecheck_defined?).to be true
end
end
2017-02-21 20:58:43 +01:00
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 = Class.new(AbstractDownloadStrategy)
2017-02-21 20:58:43 +01:00
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
2023-01-22 17:08:18 -08:00
fn = instance_double(Pathname, 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)
2017-02-21 20:58:43 +01:00
.and_raise(ChecksumMismatchError.new(fn, checksum, Object.new))
expect do
resource.verify_download_integrity(fn)
end.to raise_error(ChecksumMismatchError)
2017-02-21 20:58:43 +01:00
end
end