-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
api_spec.rb
82 lines (68 loc) 路 2.51 KB
/
api_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) }
let(:json_invalid) { '{"foo":"bar"' }
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
def mock_curl_download(stdout:)
allow(Utils::Curl).to receive(:curl_download) do |*_args, **kwargs|
kwargs[:to].write stdout
end
end
describe "::fetch" do
it "fetches a JSON file" do
mock_curl_output stdout: json
fetched_json = described_class.fetch("foo.json")
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
expect { described_class.fetch("baz.txt") }.to raise_error(ArgumentError, /Invalid JSON file/)
end
end
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
fetched_json = described_class.fetch_json_api_file("foo.json", target: cache_dir/"foo.json")
expect(fetched_json).to eq json_hash
end
it "updates an existing JSON file" do
mock_curl_download stdout: json
fetched_json = described_class.fetch_json_api_file("bar.json", target: cache_dir/"bar.json")
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
describe "::fetch_file_source" do
it "fetches a file" do
mock_curl_output stdout: json
fetched_json = described_class.fetch_file_source("foo.json", repo: "Homebrew/homebrew-core", git_head: "master")
expect(fetched_json).to eq json
end
it "raises an error if the file does not exist" do
mock_curl_output success: false
expect {
described_class.fetch_file_source("bar.txt", repo: "Homebrew/homebrew-core", git_head: "master")
}.to raise_error(ArgumentError, /No file found/)
end
end
end