From 4cbd4f296bd101ba17b94b1e9383fc5d7f5ba5fd Mon Sep 17 00:00:00 2001 From: Seeker Date: Thu, 31 Dec 2020 11:14:25 -0800 Subject: [PATCH] bottle: add tests for `merge_bottle_spec` --- Library/Homebrew/dev-cmd/bottle.rb | 12 ++-- Library/Homebrew/test/dev-cmd/bottle_spec.rb | 60 ++++++++++++++++++++ 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/dev-cmd/bottle.rb b/Library/Homebrew/dev-cmd/bottle.rb index 6297aea596..fd64520781 100644 --- a/Library/Homebrew/dev-cmd/bottle.rb +++ b/Library/Homebrew/dev-cmd/bottle.rb @@ -549,24 +549,26 @@ module Homebrew new_values = { root_url: new_bottle_hash["root_url"], prefix: new_bottle_hash["prefix"], - cellar: new_bottle_hash["cellar"].to_sym, + cellar: new_bottle_hash["cellar"], rebuild: new_bottle_hash["rebuild"], } old_keys.each do |key| next if key == :sha256 - old_value = old_bottle_spec.send(key) - new_value = new_values[key] - next if key == :cellar && old_value == :any && new_value == :any_skip_relocation + old_value = old_bottle_spec.send(key).to_s + new_value = new_values[key].to_s + next if key == :cellar && old_value == "any" && new_value == "any_skip_relocation" next if old_value.present? && new_value == old_value mismatches << "#{key}: old: #{old_value.inspect}, new: #{new_value.inspect}" end + return [mismatches, checksums] unless old_keys.include? :sha256 + old_bottle_spec.collector.each_key do |tag| old_value = old_bottle_spec.collector[tag].hexdigest - new_value = new_bottle_hash["tags"][tag.to_s] + new_value = new_bottle_hash.dig("tags", tag.to_s) if new_value.present? mismatches << "sha256 => #{tag}" else diff --git a/Library/Homebrew/test/dev-cmd/bottle_spec.rb b/Library/Homebrew/test/dev-cmd/bottle_spec.rb index 299a1f583c..baceed730a 100644 --- a/Library/Homebrew/test/dev-cmd/bottle_spec.rb +++ b/Library/Homebrew/test/dev-cmd/bottle_spec.rb @@ -173,6 +173,66 @@ describe Homebrew do "d9cc50eec8ac243148a121049c236cba06af4a0b1156ab397d0a2850aa79c137", ) end + + describe "#merge_bottle_spec" do + it "allows new bottle hash to be empty" do + valid_keys = [:root_url, :prefix, :cellar, :rebuild, :sha256] + old_spec = BottleSpecification.new + old_spec.sha256("f59bc65c91e4e698f6f050e1efea0040f57372d4dcf0996cbb8f97ced320403b" => :big_sur) + expect { homebrew.merge_bottle_spec(valid_keys, old_spec, {}) }.not_to raise_error + end + + it "checks for conflicting root URL" do + old_spec = BottleSpecification.new + old_spec.root_url("https://failbrew.bintray.com/bottles") + new_hash = { "root_url" => "https://testbrew.bintray.com/bottles" } + expect(homebrew.merge_bottle_spec([:root_url], old_spec, new_hash)).to eq [ + ['root_url: old: "https://failbrew.bintray.com/bottles", new: "https://testbrew.bintray.com/bottles"'], + [], + ] + end + + it "checks for conflicting prefix" do + old_spec = BottleSpecification.new + old_spec.prefix("/opt/failbrew") + new_hash = { "prefix" => "/opt/testbrew" } + expect(homebrew.merge_bottle_spec([:prefix], old_spec, new_hash)).to eq [ + ['prefix: old: "/opt/failbrew", new: "/opt/testbrew"'], + [], + ] + end + + it "checks for conflicting cellar" do + old_spec = BottleSpecification.new + old_spec.cellar("/opt/failbrew/Cellar") + new_hash = { "cellar" => "/opt/testbrew/Cellar" } + expect(homebrew.merge_bottle_spec([:cellar], old_spec, new_hash)).to eq [ + ['cellar: old: "/opt/failbrew/Cellar", new: "/opt/testbrew/Cellar"'], + [], + ] + end + + it "checks for conflicting rebuild number" do + old_spec = BottleSpecification.new + old_spec.rebuild(1) + new_hash = { "rebuild" => 2 } + expect(homebrew.merge_bottle_spec([:rebuild], old_spec, new_hash)).to eq [ + ['rebuild: old: "1", new: "2"'], + [], + ] + end + + it "checks for conflicting checksums" do + old_spec = BottleSpecification.new + old_spec.sha256("109c0cb581a7b5d84da36d84b221fb9dd0f8a927b3044d82611791c9907e202e" => :catalina) + old_spec.sha256("7571772bf7a0c9fe193e70e521318b53993bee6f351976c9b6e01e00d13d6c3f" => :mojave) + new_hash = { "tags" => { "catalina" => "ec6d7f08412468f28dee2be17ad8cd8b883b16b34329efcecce019b8c9736428" } } + expect(homebrew.merge_bottle_spec([:sha256], old_spec, new_hash)).to eq [ + ["sha256 => catalina"], + [{ "7571772bf7a0c9fe193e70e521318b53993bee6f351976c9b6e01e00d13d6c3f" => :mojave }], + ] + end + end end describe "brew bottle --merge", :integration_test, :needs_linux do