| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 20:58:43 +01:00
										 |  |  | require "resource" | 
					
						
							| 
									
										
										
										
											2022-06-30 16:06:36 +02:00
										 |  |  | require "livecheck" | 
					
						
							| 
									
										
										
										
											2017-02-21 20:58:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-18 15:11:11 -08:00
										 |  |  | RSpec.describe Resource do | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |   subject(:resource) { described_class.new("test") } | 
					
						
							| 
									
										
										
										
											2022-07-02 10:09:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |   let(:livecheck_resource) do | 
					
						
							| 
									
										
										
										
											2022-07-02 13:01:07 +05:00
										 |  |  |     described_class.new do | 
					
						
							| 
									
										
										
										
											2022-07-04 12:41:58 -04:00
										 |  |  |       url "https://brew.sh/foo-1.0.tar.gz" | 
					
						
							| 
									
										
										
										
											2022-07-02 13:01:07 +05:00
										 |  |  |       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) | 
					
						
							| 
									
										
										
										
											2022-07-02 13:01:07 +05:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											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" } | 
					
						
							| 
									
										
										
										
											2022-10-18 01:32:55 +01:00
										 |  |  |       resource.url("foo", **specs) | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |       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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-30 17:06:32 +02:00
										 |  |  |   describe "#livecheck" do | 
					
						
							| 
									
										
										
										
											2022-06-30 17:27:48 +02:00
										 |  |  |     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) | 
					
						
							| 
									
										
										
										
											2022-06-30 16:06:36 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2022-06-30 17:06:32 +02:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2022-06-30 16:06:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-30 17:06:32 +02:00
										 |  |  |   describe "#livecheckable?" do | 
					
						
							|  |  |  |     it "returns false if livecheck block is not set in resource" do | 
					
						
							|  |  |  |       expect(resource.livecheckable?).to be false | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2022-06-30 16:06:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-30 17:06:32 +02:00
										 |  |  |     specify "livecheck block defined in resources" do | 
					
						
							| 
									
										
										
										
											2022-07-02 13:01:30 +05:00
										 |  |  |       expect(livecheck_resource.livecheckable?).to be true | 
					
						
							| 
									
										
										
										
											2022-06-30 16:06:36 +02:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 20:58:43 +01:00
										 |  |  |   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 | 
					
						
							| 
									
										
										
										
											2023-04-18 00:22:13 +01:00
										 |  |  |       strategy = Class.new(AbstractDownloadStrategy) | 
					
						
							| 
									
										
										
										
											2017-02-21 20:58:43 +01:00
										 |  |  |       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 | 
					
						
							| 
									
										
										
										
											2023-01-22 17:08:18 -08:00
										 |  |  |     fn = instance_double(Pathname, 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-03 22:13:41 +00: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)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |     expect do | 
					
						
							| 
									
										
										
										
											2021-01-31 13:14:23 -05:00
										 |  |  |       resource.verify_download_integrity(fn) | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |     end.to raise_error(ChecksumMismatchError) | 
					
						
							| 
									
										
										
										
											2017-02-21 20:58:43 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | end |