Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bottle: add tests for json merging #9365

Merged
merged 1 commit into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Library/Homebrew/dev-cmd/bottle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,15 @@ def bottle_formula(f, args:)
end
end

def merge(args:)
bottles_hash = args.named.reduce({}) do |hash, json_file|
hash.deep_merge(JSON.parse(IO.read(json_file))) do |key, first, second|
def parse_json_files(filenames)
filenames.map do |filename|
JSON.parse(IO.read(filename))
end
end

def merge_json_files(json_files)
json_files.reduce({}) do |hash, json_file|
hash.deep_merge(json_file) do |key, first, second|
if key == "cellar"
# Prioritize HOMEBREW_CELLAR over :any over :any_skip_relocation
cellars = [first, second]
Expand All @@ -452,6 +458,10 @@ def merge(args:)
second
end
end
end

def merge(args:)
bottles_hash = merge_json_files(parse_json_files(args.named))

any_cellars = ["any", "any_skip_relocation"]
bottles_hash.each do |formula_name, bottle_hash|
Expand Down
154 changes: 154 additions & 0 deletions Library/Homebrew/test/dev-cmd/bottle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# frozen_string_literal: true

require "cmd/shared_examples/args_parse"
require "dev-cmd/bottle"

describe "Homebrew.bottle_args" do
it_behaves_like "parseable arguments"
Expand Down Expand Up @@ -40,3 +41,156 @@
end
end
end

describe Homebrew do
subject(:homebrew) { described_class }

def stub_hash(parameters)
<<~EOS
{
"#{parameters[:name]}":{
"formula":{
"pkg_version":"#{parameters[:version]}",
"path":"#{parameters[:path]}"
},
"bottle":{
"root_url":"https://homebrew.bintray.com/bottles",
"prefix":"/usr/local",
"cellar":"#{parameters[:cellar]}",
"rebuild":0,
"tags":{
"#{parameters[:os]}":{
"filename":"#{parameters[:filename]}",
"local_filename":"#{parameters[:local_filename]}",
"sha256":"#{parameters[:sha256]}"
}
}
},
"bintray":{
"package":"#{parameters[:name]}",
"repository":"bottles"
}
}
}
EOS
end

let(:hello_hash_big_sur) {
JSON.parse(
stub_hash(
{
"name": "hello",
"version": "1.0",
"path": "/home/hello.rb",
"cellar": "any_skip_relocation",
"os": "big_sur",
"filename": "hello-1.0.big_sur.bottle.tar.gz",
"local_filename": "hello--1.0.big_sur.bottle.tar.gz",
"sha256": "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f",
},
),
)
}
let(:hello_hash_catalina) {
JSON.parse(
stub_hash(
{
"name": "hello",
"version": "1.0",
"path": "/home/hello.rb",
"cellar": "any_skip_relocation",
"os": "catalina",
"filename": "hello-1.0.catalina.bottle.tar.gz",
"local_filename": "hello--1.0.catalina.bottle.tar.gz",
"sha256": "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac",
},
),
)
}
let(:unzip_hash_big_sur) {
JSON.parse(
stub_hash(
{
"name": "unzip",
"version": "2.0",
"path": "/home/unzip.rb",
"cellar": "any_skip_relocation",
"os": "big_sur",
"filename": "unzip-2.0.big_sur.bottle.tar.gz",
"local_filename": "unzip--2.0.big_sur.bottle.tar.gz",
"sha256": "16cf230afdfcb6306c208d169549cf8773c831c8653d2c852315a048960d7e72",
},
),
)
}
let(:unzip_hash_catalina) {
JSON.parse(
stub_hash(
{
"name": "unzip",
"version": "2.0",
"path": "/home/unzip.rb",
"cellar": "any",
"os": "catalina",
"filename": "unzip-2.0.catalina.bottle.tar.gz",
"local_filename": "unzip--2.0.catalina.bottle.tar.gz",
"sha256": "d9cc50eec8ac243148a121049c236cba06af4a0b1156ab397d0a2850aa79c137",
},
),
)
}

specify "::parse_json_files" do
Tempfile.open("hello--1.0.big_sur.bottle.json") do |f|
f.write(
stub_hash(
{
"name": "hello",
"version": "1.0",
"path": "/home/hello.rb",
"cellar": "any_skip_relocation",
"os": "big_sur",
"filename": "hello-1.0.big_sur.bottle.tar.gz",
"local_filename": "hello--1.0.big_sur.bottle.tar.gz",
"sha256": "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f",
},
),
)
f.close
expect(
homebrew.parse_json_files([f.path]).first["hello"]["bottle"]["tags"]["big_sur"]["filename"],
).to eq("hello-1.0.big_sur.bottle.tar.gz")
end
end

specify "::merge_json_files" do
bottles_hash = homebrew.merge_json_files(
[hello_hash_big_sur, hello_hash_catalina, unzip_hash_big_sur, unzip_hash_catalina],
)

hello_hash = bottles_hash["hello"]
expect(hello_hash["bottle"]["cellar"]).to eq("any_skip_relocation")
expect(hello_hash["bottle"]["tags"]["big_sur"]["filename"]).to eq("hello-1.0.big_sur.bottle.tar.gz")
expect(hello_hash["bottle"]["tags"]["big_sur"]["local_filename"]).to eq("hello--1.0.big_sur.bottle.tar.gz")
expect(hello_hash["bottle"]["tags"]["big_sur"]["sha256"]).to eq(
"a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f",
)
expect(hello_hash["bottle"]["tags"]["catalina"]["filename"]).to eq("hello-1.0.catalina.bottle.tar.gz")
expect(hello_hash["bottle"]["tags"]["catalina"]["local_filename"]).to eq("hello--1.0.catalina.bottle.tar.gz")
expect(hello_hash["bottle"]["tags"]["catalina"]["sha256"]).to eq(
"5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac",
)
unzip_hash = bottles_hash["unzip"]
expect(unzip_hash["bottle"]["cellar"]).to eq("any")
expect(unzip_hash["bottle"]["tags"]["big_sur"]["filename"]).to eq("unzip-2.0.big_sur.bottle.tar.gz")
expect(unzip_hash["bottle"]["tags"]["big_sur"]["local_filename"]).to eq("unzip--2.0.big_sur.bottle.tar.gz")
expect(unzip_hash["bottle"]["tags"]["big_sur"]["sha256"]).to eq(
"16cf230afdfcb6306c208d169549cf8773c831c8653d2c852315a048960d7e72",
)
expect(unzip_hash["bottle"]["tags"]["catalina"]["filename"]).to eq("unzip-2.0.catalina.bottle.tar.gz")
expect(unzip_hash["bottle"]["tags"]["catalina"]["local_filename"]).to eq("unzip--2.0.catalina.bottle.tar.gz")
expect(unzip_hash["bottle"]["tags"]["catalina"]["sha256"]).to eq(
"d9cc50eec8ac243148a121049c236cba06af4a0b1156ab397d0a2850aa79c137",
)
end
end