From 7392f9811e6c5a03038b336d9560c6fa1d76682d Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Sun, 24 Jul 2022 22:59:42 +0200 Subject: [PATCH 1/6] `Formulary`: use variations hash when installing from API --- Library/Homebrew/formulary.rb | 5 +++++ Library/Homebrew/test/formulary_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index df7bfff951..0ca6a8033e 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -136,6 +136,11 @@ module Formulary class_s = Formulary.class_s(name) json_formula = Homebrew::API::Formula.all_formulae[name] + bottle_tag = Utils::Bottles.tag.to_s + if json_formula.key?("variations") && json_formula["variations"].key?(bottle_tag) + json_formula = json_formula.merge(json_formula["variations"][bottle_tag]) + end + klass = Class.new(::Formula) do desc json_formula["desc"] homepage json_formula["homepage"] diff --git a/Library/Homebrew/test/formulary_spec.rb b/Library/Homebrew/test/formulary_spec.rb index 6276ecf876..68eb82733e 100644 --- a/Library/Homebrew/test/formulary_spec.rb +++ b/Library/Homebrew/test/formulary_spec.rb @@ -260,6 +260,16 @@ describe Formulary do } end + let(:variations_json) do + { + "variations" => { + Utils::Bottles.tag.to_s => { + "dependencies" => ["dep", "variations_dep"], + }, + }, + } + end + before do allow(described_class).to receive(:loader_for).and_return(described_class::FormulaAPILoader.new(formula_name)) end @@ -303,6 +313,16 @@ describe Formulary do formula.install }.to raise_error("Cannot build from source from abstract formula.") end + + it "returns a Formula with variations when given a name", :needs_macos do + allow(Homebrew::API::Formula).to receive(:all_formulae).and_return formula_json_contents(variations_json) + allow(Utils::Bottles).to receive(:tag).and_return(:arm64_monterey) + + formula = described_class.factory(formula_name) + expect(formula).to be_kind_of(Formula) + expect(formula.deps.count).to eq 5 + expect(formula.deps.map(&:name).include?("variations_dep")).to be true + end end end From bae5abda82002a88de44d854213a7ea4e308b3a2 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Mon, 25 Jul 2022 08:49:19 +0200 Subject: [PATCH 2/6] Remove `uses_from_macos` dep duplication in `FormulaAPILoader` --- Library/Homebrew/formulary.rb | 22 +++++++++++++--------- Library/Homebrew/test/formulary_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 0ca6a8033e..1e2aca4d43 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -141,6 +141,12 @@ module Formulary json_formula = json_formula.merge(json_formula["variations"][bottle_tag]) end + uses_from_macos_names = json_formula["uses_from_macos"].map do |dep| + next dep unless dep.is_a? Hash + + dep.keys.first + end + klass = Class.new(::Formula) do desc json_formula["desc"] homepage json_formula["homepage"] @@ -181,20 +187,18 @@ module Formulary disable! date: disable_date, because: reason end - json_formula["build_dependencies"].each do |dep| - depends_on dep => :build - end - json_formula["dependencies"].each do |dep| + next if uses_from_macos_names.include? dep + depends_on dep end - json_formula["recommended_dependencies"].each do |dep| - depends_on dep => :recommended - end + [:build, :recommended, :optional].each do |type| + json_formula["#{type}_dependencies"].each do |dep| + next if uses_from_macos_names.include? dep - json_formula["optional_dependencies"].each do |dep| - depends_on dep => :optional + depends_on dep => type + end end json_formula["uses_from_macos"].each do |dep| diff --git a/Library/Homebrew/test/formulary_spec.rb b/Library/Homebrew/test/formulary_spec.rb index 68eb82733e..b303cb3ec0 100644 --- a/Library/Homebrew/test/formulary_spec.rb +++ b/Library/Homebrew/test/formulary_spec.rb @@ -270,6 +270,16 @@ describe Formulary do } end + let(:linux_variations_json) do + { + "variations" => { + "x86_64_linux" => { + "dependencies" => ["dep", "uses_from_macos_dep"], + }, + }, + } + end + before do allow(described_class).to receive(:loader_for).and_return(described_class::FormulaAPILoader.new(formula_name)) end @@ -323,6 +333,16 @@ describe Formulary do expect(formula.deps.count).to eq 5 expect(formula.deps.map(&:name).include?("variations_dep")).to be true end + + it "returns a Formula without duplicated deps and uses_from_macos with variations on Linux", :needs_linux do + allow(Homebrew::API::Formula).to receive(:all_formulae).and_return formula_json_contents(linux_variations_json) + allow(Utils::Bottles).to receive(:tag).and_return(:arm64_monterey) + + formula = described_class.factory(formula_name) + expect(formula).to be_kind_of(Formula) + expect(formula.deps.count).to eq 5 + expect(formula.deps.map(&:name).include?("variations_dep")).to be true + end end end From edc14c37866ce1d50f0e9f15e7b4d93923505bdd Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Mon, 25 Jul 2022 08:56:10 +0200 Subject: [PATCH 3/6] Fix style --- Library/Homebrew/test/formulary_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/test/formulary_spec.rb b/Library/Homebrew/test/formulary_spec.rb index b303cb3ec0..8e0708d692 100644 --- a/Library/Homebrew/test/formulary_spec.rb +++ b/Library/Homebrew/test/formulary_spec.rb @@ -335,7 +335,8 @@ describe Formulary do end it "returns a Formula without duplicated deps and uses_from_macos with variations on Linux", :needs_linux do - allow(Homebrew::API::Formula).to receive(:all_formulae).and_return formula_json_contents(linux_variations_json) + allow(Homebrew::API::Formula) + .to receive(:all_formulae).and_return formula_json_contents(linux_variations_json) allow(Utils::Bottles).to receive(:tag).and_return(:arm64_monterey) formula = described_class.factory(formula_name) From d4e5886571b4f64080dddf86c12b60ab47e484b7 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Mon, 25 Jul 2022 17:43:54 +0200 Subject: [PATCH 4/6] Simplify checking for variations hash availability Co-authored-by: Mike McQuaid --- Library/Homebrew/formulary.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 1e2aca4d43..b00c0a91f0 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -136,9 +136,10 @@ module Formulary class_s = Formulary.class_s(name) json_formula = Homebrew::API::Formula.all_formulae[name] - bottle_tag = Utils::Bottles.tag.to_s - if json_formula.key?("variations") && json_formula["variations"].key?(bottle_tag) - json_formula = json_formula.merge(json_formula["variations"][bottle_tag]) + if (bottle_tag = Utils::Bottles.tag.to_s.presence) && + (variations = json_formula["variations"].presence) && + (variation = variations[bottle_tag].presence) + json_formula = json_formula.merge(variation) end uses_from_macos_names = json_formula["uses_from_macos"].map do |dep| From 233cef08cf9f93ae2deeafdc710ca1b7ad520e72 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Mon, 25 Jul 2022 18:31:35 +0200 Subject: [PATCH 5/6] Fix style --- Library/Homebrew/formulary.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index b00c0a91f0..658b211c62 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -136,8 +136,8 @@ module Formulary class_s = Formulary.class_s(name) json_formula = Homebrew::API::Formula.all_formulae[name] - if (bottle_tag = Utils::Bottles.tag.to_s.presence) && - (variations = json_formula["variations"].presence) && + if (bottle_tag = Utils::Bottles.tag.to_s.presence) && + (variations = json_formula["variations"].presence) && (variation = variations[bottle_tag].presence) json_formula = json_formula.merge(variation) end From ebf109ade5e050e0b12a8c56d3e1282223d2d3d9 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Mon, 25 Jul 2022 19:22:15 +0200 Subject: [PATCH 6/6] Fix tests --- Library/Homebrew/test/formulary_spec.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Library/Homebrew/test/formulary_spec.rb b/Library/Homebrew/test/formulary_spec.rb index 8e0708d692..0fc043fe6b 100644 --- a/Library/Homebrew/test/formulary_spec.rb +++ b/Library/Homebrew/test/formulary_spec.rb @@ -326,7 +326,6 @@ describe Formulary do it "returns a Formula with variations when given a name", :needs_macos do allow(Homebrew::API::Formula).to receive(:all_formulae).and_return formula_json_contents(variations_json) - allow(Utils::Bottles).to receive(:tag).and_return(:arm64_monterey) formula = described_class.factory(formula_name) expect(formula).to be_kind_of(Formula) @@ -337,12 +336,11 @@ describe Formulary do it "returns a Formula without duplicated deps and uses_from_macos with variations on Linux", :needs_linux do allow(Homebrew::API::Formula) .to receive(:all_formulae).and_return formula_json_contents(linux_variations_json) - allow(Utils::Bottles).to receive(:tag).and_return(:arm64_monterey) formula = described_class.factory(formula_name) expect(formula).to be_kind_of(Formula) expect(formula.deps.count).to eq 5 - expect(formula.deps.map(&:name).include?("variations_dep")).to be true + expect(formula.deps.map(&:name).include?("uses_from_macos_dep")).to be true end end end