From f95e1729a2d95d8dc80dadc8c05851d534a72f74 Mon Sep 17 00:00:00 2001 From: Tie Date: Sun, 1 Nov 2020 01:57:02 -0400 Subject: [PATCH] move logic from leaves to formula --- Library/Homebrew/cmd/leaves.rb | 5 +- Library/Homebrew/formula.rb | 12 ++++ Library/Homebrew/test/cmd/leaves_spec.rb | 44 +++++++++++--- Library/Homebrew/test/formula_spec.rb | 74 ++++++++++++++++++++++++ 4 files changed, 123 insertions(+), 12 deletions(-) diff --git a/Library/Homebrew/cmd/leaves.rb b/Library/Homebrew/cmd/leaves.rb index d84468f5fd..dceb3c0e42 100644 --- a/Library/Homebrew/cmd/leaves.rb +++ b/Library/Homebrew/cmd/leaves.rb @@ -2,7 +2,6 @@ # frozen_string_literal: true require "formula" -require "tab" require "cli/parser" module Homebrew @@ -23,9 +22,7 @@ module Homebrew def leaves leaves_args.parse - installed = Formula.installed.sort - deps_of_installed = installed.flat_map(&:runtime_formula_dependencies) - leaves = installed.map(&:full_name) - deps_of_installed.map(&:full_name) + leaves = Formula.installed_non_deps.map(&:full_name).sort leaves.each(&method(:puts)) end end diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 9fd75a3197..808c73b3f4 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1515,6 +1515,18 @@ class Formula end.uniq(&:name) end + # An array of installed {Formula} that are dependencies of other installed {Formula} + # @private + def self.installed_deps(formulae=installed) + formulae.flat_map(&:runtime_formula_dependencies).uniq(&:name) + end + + # An array of all installed {Formula} that are not dependencies of other installed {Formula} + # @private + def self.installed_non_deps(formulae=installed) + formulae - installed_deps(formulae) + end + def self.installed_with_alias_path(alias_path) return [] if alias_path.nil? diff --git a/Library/Homebrew/test/cmd/leaves_spec.rb b/Library/Homebrew/test/cmd/leaves_spec.rb index 4962a75603..16239d1cb3 100644 --- a/Library/Homebrew/test/cmd/leaves_spec.rb +++ b/Library/Homebrew/test/cmd/leaves_spec.rb @@ -8,14 +8,42 @@ describe "Homebrew.leaves_args" do end describe "brew leaves", :integration_test do - it "prints all Formulae that are not dependencies of other Formulae" do - setup_test_formula "foo" - setup_test_formula "bar" - (HOMEBREW_CELLAR/"foo/0.1/somedir").mkpath + context "when there are no installed Formulae" do + it "prints nothing" do + setup_test_formula "foo" + setup_test_formula "bar" + + expect { brew "leaves" } + .to not_to_output.to_stdout + .and not_to_output.to_stderr + .and be_a_success + end + end - expect { brew "leaves" } - .to output("foo\n").to_stdout - .and not_to_output.to_stderr - .and be_a_success + context "when there are only installed Formulae without dependencies" do + it "prints all installed Formulae" do + setup_test_formula "foo" + setup_test_formula "bar" + (HOMEBREW_CELLAR/"foo/0.1/somedir").mkpath + + expect { brew "leaves" } + .to output("foo\n").to_stdout + .and not_to_output.to_stderr + .and be_a_success + end + end + + context "when there are installed Formulae" do + it "prints all installed Formulae that are not dependencies of another installed Formula" do + setup_test_formula "foo" + setup_test_formula "bar" + (HOMEBREW_CELLAR/"foo/0.1/somedir").mkpath + (HOMEBREW_CELLAR/"bar/0.1/somedir").mkpath + + expect { brew "leaves" } + .to output("bar\n").to_stdout + .and not_to_output.to_stderr + .and be_a_success + end end end diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb index 9d9997dbd2..ad803f05f9 100644 --- a/Library/Homebrew/test/formula_spec.rb +++ b/Library/Homebrew/test/formula_spec.rb @@ -440,6 +440,80 @@ describe Formula do end end + describe "::installed_deps" do + let(:formula_is_dep) do + formula "foo" do + url "foo-1.1" + end + end + + let(:formula_with_deps) do + formula "bar" do + url "bar-1.0" + end + end + + let(:formulae) do + [ + formula_with_deps, + formula_is_dep + ] + end + + before do + allow(formula_with_deps).to receive(:runtime_formula_dependencies).and_return([ formula_is_dep ]) + end + + specify "without formulae parameter" do + allow(described_class).to receive(:installed).and_return(formulae) + + expect(described_class.installed_deps) + .to eq([formula_is_dep]) + end + + specify "with formulae parameter" do + expect(described_class.installed_deps(formulae)) + .to eq([formula_is_dep]) + end + end + + describe "::installed_non_deps" do + let(:formula_is_dep) do + formula "foo" do + url "foo-1.1" + end + end + + let(:formula_with_deps) do + formula "bar" do + url "bar-1.0" + end + end + + let(:formulae) do + [ + formula_with_deps, + formula_is_dep + ] + end + + before do + allow(formula_with_deps).to receive(:runtime_formula_dependencies).and_return([ formula_is_dep ]) + end + + specify "without formulae parameter" do + allow(described_class).to receive(:installed).and_return(formulae) + + expect(described_class.installed_non_deps) + .to eq([formula_with_deps]) + end + + specify "with formulae parameter" do + expect(described_class.installed_non_deps(formulae)) + .to eq([formula_with_deps]) + end + end + describe "::installed_with_alias_path" do specify "with alias path with nil" do expect(described_class.installed_with_alias_path(nil)).to be_empty