| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  | # typed: false | 
					
						
							|  |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "api" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe Homebrew::API do | 
					
						
							|  |  |  |   let(:text) { "foo" } | 
					
						
							|  |  |  |   let(:json) { '{"foo":"bar"}' } | 
					
						
							|  |  |  |   let(:json_hash) { JSON.parse(json) } | 
					
						
							| 
									
										
										
										
											2022-12-30 01:54:32 -05:00
										 |  |  |   let(:json_invalid) { '{"foo":"bar"' } | 
					
						
							| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-16 12:05:38 +00:00
										 |  |  |   before do | 
					
						
							|  |  |  |     described_class.clear_cache | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  |   def mock_curl_output(stdout: "", success: true) | 
					
						
							|  |  |  |     curl_output = OpenStruct.new(stdout: stdout, success?: success) | 
					
						
							|  |  |  |     allow(Utils::Curl).to receive(:curl_output).and_return curl_output | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-30 01:54:32 -05:00
										 |  |  |   def mock_curl_download(stdout:) | 
					
						
							|  |  |  |     allow(Utils::Curl).to receive(:curl_download) do |*_args, **kwargs| | 
					
						
							|  |  |  |       kwargs[:to].write stdout | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  |   describe "::fetch" do | 
					
						
							|  |  |  |     it "fetches a JSON file" do | 
					
						
							|  |  |  |       mock_curl_output stdout: json | 
					
						
							| 
									
										
										
										
											2021-08-09 11:00:00 -04:00
										 |  |  |       fetched_json = described_class.fetch("foo.json") | 
					
						
							| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  |       expect(fetched_json).to eq json_hash | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises an error if the file does not exist" do | 
					
						
							|  |  |  |       mock_curl_output success: false | 
					
						
							|  |  |  |       expect { described_class.fetch("bar.txt") }.to raise_error(ArgumentError, /No file found/) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises an error if the JSON file is invalid" do | 
					
						
							|  |  |  |       mock_curl_output stdout: text | 
					
						
							| 
									
										
										
										
											2021-08-09 11:00:00 -04:00
										 |  |  |       expect { described_class.fetch("baz.txt") }.to raise_error(ArgumentError, /Invalid JSON file/) | 
					
						
							| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2022-12-30 01:54:32 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe "::fetch_json_api_file" do | 
					
						
							|  |  |  |     let!(:cache_dir) { mktmpdir } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     before do | 
					
						
							|  |  |  |       (cache_dir/"bar.json").write "tmp" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "fetches a JSON file" do | 
					
						
							|  |  |  |       mock_curl_download stdout: json | 
					
						
							| 
									
										
										
										
											2023-02-16 21:49:03 +00:00
										 |  |  |       fetched_json, = described_class.fetch_json_api_file("foo.json", target: cache_dir/"foo.json") | 
					
						
							| 
									
										
										
										
											2022-12-30 01:54:32 -05:00
										 |  |  |       expect(fetched_json).to eq json_hash | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "updates an existing JSON file" do | 
					
						
							|  |  |  |       mock_curl_download stdout: json | 
					
						
							| 
									
										
										
										
											2023-02-16 21:49:03 +00:00
										 |  |  |       fetched_json, = described_class.fetch_json_api_file("bar.json", target: cache_dir/"bar.json") | 
					
						
							| 
									
										
										
										
											2022-12-30 01:54:32 -05:00
										 |  |  |       expect(fetched_json).to eq json_hash | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises an error if the JSON file is invalid" do | 
					
						
							|  |  |  |       mock_curl_download stdout: json_invalid | 
					
						
							|  |  |  |       expect { | 
					
						
							|  |  |  |         described_class.fetch_json_api_file("baz.json", target: cache_dir/"baz.json") | 
					
						
							|  |  |  |       }.to raise_error(SystemExit) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2023-01-28 02:15:05 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe "::fetch_file_source" do | 
					
						
							|  |  |  |     it "fetches a file" do | 
					
						
							|  |  |  |       mock_curl_output stdout: json | 
					
						
							| 
									
										
										
										
											2023-02-16 12:05:38 +00:00
										 |  |  |       fetched_json = described_class.fetch_homebrew_cask_source("foo", git_head: "master") | 
					
						
							| 
									
										
										
										
											2023-01-28 02:15:05 -06:00
										 |  |  |       expect(fetched_json).to eq json | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "raises an error if the file does not exist" do | 
					
						
							|  |  |  |       mock_curl_output success: false | 
					
						
							|  |  |  |       expect { | 
					
						
							| 
									
										
										
										
											2023-02-16 12:05:38 +00:00
										 |  |  |         described_class.fetch_homebrew_cask_source("bar", git_head: "master") | 
					
						
							|  |  |  |       }.to raise_error(ArgumentError, /No valid file found/) | 
					
						
							| 
									
										
										
										
											2023-01-28 02:15:05 -06:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  | end |