| 
									
										
										
										
											2021-03-17 01:58:31 +05:30
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 11:09:55 -04:00
										 |  |  | require "livecheck/strategy" | 
					
						
							| 
									
										
										
										
											2021-03-17 01:58:31 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-18 15:11:11 -08:00
										 |  |  | RSpec.describe Homebrew::Livecheck::Strategy::ElectronBuilder do | 
					
						
							| 
									
										
										
										
											2021-03-17 01:58:31 +05:30
										 |  |  |   subject(:electron_builder) { described_class } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |   let(:http_url) { "https://www.example.com/example/latest-mac.yml" } | 
					
						
							|  |  |  |   let(:non_http_url) { "ftp://brew.sh/" } | 
					
						
							| 
									
										
										
										
											2021-03-17 01:58:31 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |   let(:regex) { /Example[._-]v?(\d+(?:\.\d+)+)[._-]mac\.zip/i } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |   let(:content) do | 
					
						
							| 
									
										
										
										
											2021-03-17 01:58:31 +05:30
										 |  |  |     <<~EOS | 
					
						
							|  |  |  |       version: 1.2.3
 | 
					
						
							|  |  |  |       files: | 
					
						
							|  |  |  |         - url: Example-1.2.3-mac.zip | 
					
						
							|  |  |  |           sha512: MDXR0pxozBJjxxbtUQJOnhiaiiQkryLAwtcVjlnNiz30asm/PtSxlxWKFYN3kV/kl+jriInJrGypuzajTF6XIA== | 
					
						
							|  |  |  |           size: 92031237
 | 
					
						
							|  |  |  |           blockMapSize: 96080
 | 
					
						
							|  |  |  |         - url: Example-1.2.3.dmg | 
					
						
							|  |  |  |           sha512: k6WRDlZEfZGZHoOfUShpHxXZb5p44DRp+FAO2FXNx2kStZvyW9VuaoB7phPMfZpcMKrzfRfncpP8VEM8OB2y9g== | 
					
						
							|  |  |  |           size: 94972630
 | 
					
						
							|  |  |  |       path: Example-1.2.3-mac.zip | 
					
						
							|  |  |  |       sha512: MDXR0pxozBJjxxbtUQJOnhiaiiQkryLAwtcVjlnNiz30asm/PtSxlxWKFYN3kV/kl+jriInJrGypuzajTF6XIA== | 
					
						
							|  |  |  |       releaseDate: '2000-01-01T00:00:00.000Z' | 
					
						
							|  |  |  |     EOS | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2022-12-04 21:32:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |   let(:content_timestamp) do | 
					
						
							| 
									
										
										
										
											2022-12-04 21:32:31 -05:00
										 |  |  |     # An electron-builder YAML file may use a timestamp instead of an explicit | 
					
						
							|  |  |  |     # string value (with quotes) for `releaseDate`, so we need to make sure that | 
					
						
							|  |  |  |     # `ElectronBuilder#versions_from_content` won't encounter an error in this | 
					
						
							|  |  |  |     # scenario (e.g. `Tried to load unspecified class: Time`). | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |     content.sub(/releaseDate:\s*'([^']+)'/, 'releaseDate: \1') | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2022-12-04 21:32:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |   let(:content_matches) { ["1.2.3"] } | 
					
						
							| 
									
										
										
										
											2021-03-17 01:58:31 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |   let(:find_versions_return_hash) do | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |       matches: { | 
					
						
							|  |  |  |         "1.2.3" => Version.new("1.2.3"), | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       regex:   nil, | 
					
						
							|  |  |  |       url:     http_url, | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |   let(:find_versions_cached_return_hash) do | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |     find_versions_return_hash.merge({ cached: true }) | 
					
						
							| 
									
										
										
										
											2023-03-08 23:14:46 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2021-08-10 11:09:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 01:58:31 +05:30
										 |  |  |   describe "::match?" do | 
					
						
							| 
									
										
										
										
											2021-08-10 17:39:11 -04:00
										 |  |  |     it "returns true for a YAML file URL" do | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |       expect(electron_builder.match?(http_url)).to be true | 
					
						
							| 
									
										
										
										
											2021-03-17 01:58:31 +05:30
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 17:39:11 -04:00
										 |  |  |     it "returns false for non-YAML URL" do | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |       expect(electron_builder.match?(non_http_url)).to be false | 
					
						
							| 
									
										
										
										
											2021-03-17 01:58:31 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |   describe "::find_versions?" do | 
					
						
							|  |  |  |     it "finds versions in provided_content using a block" do | 
					
						
							|  |  |  |       expect(electron_builder.find_versions(url: http_url, provided_content: content)) | 
					
						
							|  |  |  |         .to eq(find_versions_cached_return_hash) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |       expect(electron_builder.find_versions(url: http_url, regex:, provided_content: content) do |yaml, regex| | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |         yaml["path"][regex, 1] | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |       end).to eq(find_versions_cached_return_hash.merge({ regex: })) | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(electron_builder.find_versions( | 
					
						
							|  |  |  |         url:              http_url, | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |         regex:, | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |         provided_content: content_timestamp, | 
					
						
							|  |  |  |       ) do |yaml, regex| | 
					
						
							|  |  |  |         yaml["path"][regex, 1] | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |       end).to eq(find_versions_cached_return_hash.merge({ regex: })) | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |       # NOTE: A regex should be provided using the `#regex` method in a | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |       #       `livecheck` block but we're using a regex literal in the `strategy` | 
					
						
							|  |  |  |       #       block here simply to ensure this method works as expected when a | 
					
						
							|  |  |  |       #       regex isn't provided. | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |       expect(electron_builder.find_versions(url: http_url, provided_content: content) do |yaml| | 
					
						
							| 
									
										
										
										
											2024-01-18 22:18:42 +00:00
										 |  |  |         regex = /^v?(\d+(?:\.\d+)+)$/i | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |         yaml["version"][regex, 1] | 
					
						
							|  |  |  |       end).to eq(find_versions_cached_return_hash) | 
					
						
							| 
									
										
										
										
											2021-03-17 02:29:53 +05:30
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2021-07-26 20:32:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |     it "errors if a block is not provided" do | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |       expect { electron_builder.find_versions(url: http_url, regex:, provided_content: content) } | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |         .to raise_error(ArgumentError, "ElectronBuilder only supports a regex when using a `strategy` block") | 
					
						
							| 
									
										
										
										
											2021-11-16 12:31:54 -05:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |     it "returns default match_data when url is blank" do | 
					
						
							|  |  |  |       expect(electron_builder.find_versions(url: "") { "1.2.3" }) | 
					
						
							|  |  |  |         .to eq({ matches: {}, regex: nil, url: "" }) | 
					
						
							| 
									
										
										
										
											2021-07-26 20:32:10 -04:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-02 15:20:33 -05:00
										 |  |  |     it "returns default match_data when content is blank" do | 
					
						
							|  |  |  |       expect(electron_builder.find_versions(url: http_url, provided_content: "") { "1.2.3" }) | 
					
						
							|  |  |  |         .to eq({ matches: {}, regex: nil, url: http_url, cached: true }) | 
					
						
							| 
									
										
										
										
											2021-07-26 20:32:10 -04:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2021-03-17 01:58:31 +05:30
										 |  |  |   end | 
					
						
							|  |  |  | end |