| 
									
										
										
										
											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) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "::fetch" do | 
					
						
							| 
									
										
										
										
											2021-08-06 11:56:42 -04:00
										 |  |  |     it "fetches a text file" do | 
					
						
							|  |  |  |       mock_curl_output stdout: text | 
					
						
							|  |  |  |       fetched_text = described_class.fetch("foo.txt", json: false) | 
					
						
							|  |  |  |       expect(fetched_text).to eq text | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  |     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 | 
					
						
							|  |  |  | end |