Expand testing of Homebrew::API
This commit is contained in:
parent
bab85d84e9
commit
08529c38a6
44
Library/Homebrew/test/api/cask_spec.rb
Normal file
44
Library/Homebrew/test/api/cask_spec.rb
Normal file
@ -0,0 +1,44 @@
|
||||
# typed: false
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "api"
|
||||
|
||||
describe Homebrew::API::Cask do
|
||||
let(:cache_dir) { mktmpdir }
|
||||
|
||||
before do
|
||||
stub_const("Homebrew::API::HOMEBREW_CACHE_API", cache_dir)
|
||||
end
|
||||
|
||||
def mock_curl_download(stdout:)
|
||||
allow(Utils::Curl).to receive(:curl_download) do |*_args, **kwargs|
|
||||
kwargs[:to].write stdout
|
||||
end
|
||||
end
|
||||
|
||||
describe "::all_casks" do
|
||||
let(:casks_json) {
|
||||
<<~EOS
|
||||
[{
|
||||
"token": "foo",
|
||||
"url": "https://brew.sh/foo"
|
||||
}, {
|
||||
"token": "bar",
|
||||
"url": "https://brew.sh/bar"
|
||||
}]
|
||||
EOS
|
||||
}
|
||||
let(:casks_hash) {
|
||||
{
|
||||
"foo" => { "url" => "https://brew.sh/foo" },
|
||||
"bar" => { "url" => "https://brew.sh/bar" },
|
||||
}
|
||||
}
|
||||
|
||||
it "returns the expected cask JSON list" do
|
||||
mock_curl_download stdout: casks_json
|
||||
casks_output = described_class.all_casks
|
||||
expect(casks_output).to eq casks_hash
|
||||
end
|
||||
end
|
||||
end
|
||||
64
Library/Homebrew/test/api/formula_spec.rb
Normal file
64
Library/Homebrew/test/api/formula_spec.rb
Normal file
@ -0,0 +1,64 @@
|
||||
# typed: false
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "api"
|
||||
|
||||
describe Homebrew::API::Formula do
|
||||
let(:cache_dir) { mktmpdir }
|
||||
|
||||
before do
|
||||
stub_const("Homebrew::API::HOMEBREW_CACHE_API", cache_dir)
|
||||
end
|
||||
|
||||
def mock_curl_download(stdout:)
|
||||
allow(Utils::Curl).to receive(:curl_download) do |*_args, **kwargs|
|
||||
kwargs[:to].write stdout
|
||||
end
|
||||
end
|
||||
|
||||
describe "::all_formulae" do
|
||||
let(:formulae_json) {
|
||||
<<~EOS
|
||||
[{
|
||||
"name": "foo",
|
||||
"url": "https://brew.sh/foo",
|
||||
"aliases": ["foo-alias1", "foo-alias2"]
|
||||
}, {
|
||||
"name": "bar",
|
||||
"url": "https://brew.sh/bar",
|
||||
"aliases": ["bar-alias"]
|
||||
}, {
|
||||
"name": "baz",
|
||||
"url": "https://brew.sh/baz",
|
||||
"aliases": []
|
||||
}]
|
||||
EOS
|
||||
}
|
||||
let(:formulae_hash) {
|
||||
{
|
||||
"foo" => { "url" => "https://brew.sh/foo", "aliases" => ["foo-alias1", "foo-alias2"] },
|
||||
"bar" => { "url" => "https://brew.sh/bar", "aliases" => ["bar-alias"] },
|
||||
"baz" => { "url" => "https://brew.sh/baz", "aliases" => [] },
|
||||
}
|
||||
}
|
||||
let(:formulae_aliases) {
|
||||
{
|
||||
"foo-alias1" => "foo",
|
||||
"foo-alias2" => "foo",
|
||||
"bar-alias" => "bar",
|
||||
}
|
||||
}
|
||||
|
||||
it "returns the expected formula JSON list" do
|
||||
mock_curl_download stdout: formulae_json
|
||||
formulae_output = described_class.all_formulae
|
||||
expect(formulae_output).to eq formulae_hash
|
||||
end
|
||||
|
||||
it "returns the expected formula alias list" do
|
||||
mock_curl_download stdout: formulae_json
|
||||
aliases_output = described_class.all_aliases
|
||||
expect(aliases_output).to eq formulae_aliases
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -7,12 +7,19 @@ 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 text file" do
|
||||
mock_curl_output stdout: text
|
||||
@ -36,4 +43,31 @@ describe Homebrew::API do
|
||||
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
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user