move logic from leaves to formula

This commit is contained in:
Tie 2020-11-01 01:57:02 -04:00
parent cbb0b87e75
commit f95e1729a2
4 changed files with 123 additions and 12 deletions

View File

@ -2,7 +2,6 @@
# frozen_string_literal: true # frozen_string_literal: true
require "formula" require "formula"
require "tab"
require "cli/parser" require "cli/parser"
module Homebrew module Homebrew
@ -23,9 +22,7 @@ module Homebrew
def leaves def leaves
leaves_args.parse leaves_args.parse
installed = Formula.installed.sort leaves = Formula.installed_non_deps.map(&:full_name).sort
deps_of_installed = installed.flat_map(&:runtime_formula_dependencies)
leaves = installed.map(&:full_name) - deps_of_installed.map(&:full_name)
leaves.each(&method(:puts)) leaves.each(&method(:puts))
end end
end end

View File

@ -1515,6 +1515,18 @@ class Formula
end.uniq(&:name) end.uniq(&:name)
end 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) def self.installed_with_alias_path(alias_path)
return [] if alias_path.nil? return [] if alias_path.nil?

View File

@ -8,7 +8,20 @@ describe "Homebrew.leaves_args" do
end end
describe "brew leaves", :integration_test do describe "brew leaves", :integration_test do
it "prints all Formulae that are not dependencies of other Formulae" do 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
context "when there are only installed Formulae without dependencies" do
it "prints all installed Formulae" do
setup_test_formula "foo" setup_test_formula "foo"
setup_test_formula "bar" setup_test_formula "bar"
(HOMEBREW_CELLAR/"foo/0.1/somedir").mkpath (HOMEBREW_CELLAR/"foo/0.1/somedir").mkpath
@ -18,4 +31,19 @@ describe "brew leaves", :integration_test do
.and not_to_output.to_stderr .and not_to_output.to_stderr
.and be_a_success .and be_a_success
end 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 end

View File

@ -440,6 +440,80 @@ describe Formula do
end end
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 describe "::installed_with_alias_path" do
specify "with alias path with nil" do specify "with alias path with nil" do
expect(described_class.installed_with_alias_path(nil)).to be_empty expect(described_class.installed_with_alias_path(nil)).to be_empty