Convert dependency_expansion
test to spec.
This commit is contained in:
parent
4e34f7b60e
commit
7393485b7b
136
Library/Homebrew/test/dependency_expansion_spec.rb
Normal file
136
Library/Homebrew/test/dependency_expansion_spec.rb
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
require "dependency"
|
||||||
|
|
||||||
|
describe Dependency do
|
||||||
|
def build_dep(name, tags = [], deps = [])
|
||||||
|
dep = described_class.new(name.to_s, tags)
|
||||||
|
allow(dep).to receive(:to_formula).and_return(double(deps: deps, name: name))
|
||||||
|
dep
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:foo) { build_dep(:foo) }
|
||||||
|
let(:bar) { build_dep(:bar) }
|
||||||
|
let(:baz) { build_dep(:baz) }
|
||||||
|
let(:qux) { build_dep(:qux) }
|
||||||
|
let(:deps) { [foo, bar, baz, qux] }
|
||||||
|
let(:formula) { double(deps: deps, name: "f") }
|
||||||
|
|
||||||
|
describe "::expand" do
|
||||||
|
it "yields dependent and dependency pairs" do
|
||||||
|
i = 0
|
||||||
|
described_class.expand(formula) do |dependent, dep|
|
||||||
|
expect(dependent).to eq(formula)
|
||||||
|
expect(deps[i]).to eq(dep)
|
||||||
|
i += 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns the dependencies" do
|
||||||
|
expect(described_class.expand(formula)).to eq(deps)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "prunes all when given a block with ::prune" do
|
||||||
|
expect(described_class.expand(formula) { described_class.prune }).to be_empty
|
||||||
|
end
|
||||||
|
|
||||||
|
it "can prune selectively" do
|
||||||
|
deps = described_class.expand(formula) do |_, dep|
|
||||||
|
described_class.prune if dep.name == "foo"
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(deps).to eq([bar, baz, qux])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "preserves dependency order" do
|
||||||
|
allow(foo).to receive(:to_formula).and_return(double(name: "f", deps: [qux, baz]))
|
||||||
|
expect(described_class.expand(formula)).to eq([qux, baz, foo, bar])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "skips optionals by default" do
|
||||||
|
deps = [build_dep(:foo, [:optional]), bar, baz, qux]
|
||||||
|
f = double(deps: deps, build: double(with?: false), name: "f")
|
||||||
|
expect(described_class.expand(f)).to eq([bar, baz, qux])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "keeps recommended dependencies by default" do
|
||||||
|
deps = [build_dep(:foo, [:recommended]), bar, baz, qux]
|
||||||
|
f = double(deps: deps, build: double(with?: true), name: "f")
|
||||||
|
expect(described_class.expand(f)).to eq(deps)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "merges repeated dependencies with differing options" do
|
||||||
|
foo2 = build_dep(:foo, ["option"])
|
||||||
|
baz2 = build_dep(:baz, ["option"])
|
||||||
|
deps << foo2 << baz2
|
||||||
|
deps = [foo2, bar, baz2, qux]
|
||||||
|
deps.zip(described_class.expand(formula)) do |expected, actual|
|
||||||
|
expect(expected.tags).to eq(actual.tags)
|
||||||
|
expect(expected).to eq(actual)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "merges dependencies and perserves env_proc" do
|
||||||
|
env_proc = double
|
||||||
|
dep = described_class.new("foo", [], env_proc)
|
||||||
|
allow(dep).to receive(:to_formula).and_return(double(deps: [], name: "foo"))
|
||||||
|
deps.replace([dep])
|
||||||
|
expect(described_class.expand(formula).first.env_proc).to eq(env_proc)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "merges tags without duplicating them" do
|
||||||
|
foo2 = build_dep(:foo, ["option"])
|
||||||
|
foo3 = build_dep(:foo, ["option"])
|
||||||
|
deps << foo2 << foo3
|
||||||
|
|
||||||
|
expect(described_class.expand(formula).first.tags).to eq(%w[option])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "skips parent but yields children with ::skip" do
|
||||||
|
f = double(
|
||||||
|
name: "f",
|
||||||
|
deps: [
|
||||||
|
build_dep(:foo, [], [bar, baz]),
|
||||||
|
build_dep(:foo, [], [baz]),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
deps = described_class.expand(f) do |_dependent, dep|
|
||||||
|
described_class.skip if %w[foo qux].include? dep.name
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(deps).to eq([bar, baz])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "keeps dependency but prunes recursive dependencies with ::keep_but_prune_recursive_deps" do
|
||||||
|
foo = build_dep(:foo, [:build], bar)
|
||||||
|
baz = build_dep(:baz, [:build])
|
||||||
|
f = double(name: "f", deps: [foo, baz])
|
||||||
|
|
||||||
|
deps = described_class.expand(f) do |_dependent, dep|
|
||||||
|
described_class.keep_but_prune_recursive_deps if dep.build?
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(deps).to eq([foo, baz])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns only the dependencies given as a collection as second argument" do
|
||||||
|
expect(formula.deps).to eq([foo, bar, baz, qux])
|
||||||
|
expect(described_class.expand(formula, [bar, baz])).to eq([bar, baz])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "doesn't raise an error when a dependency is cyclic" do
|
||||||
|
foo = build_dep(:foo)
|
||||||
|
bar = build_dep(:bar, [], [foo])
|
||||||
|
allow(foo).to receive(:to_formula).and_return(double(deps: [bar], name: foo.name))
|
||||||
|
f = double(name: "f", deps: [foo, bar])
|
||||||
|
expect { described_class.expand(f) }.not_to raise_error
|
||||||
|
end
|
||||||
|
|
||||||
|
it "cleans the expand stack" do
|
||||||
|
foo = build_dep(:foo)
|
||||||
|
allow(foo).to receive(:to_formula).and_raise(FormulaUnavailableError, foo.name)
|
||||||
|
f = double(name: "f", deps: [foo])
|
||||||
|
expect { described_class.expand(f) }.to raise_error(FormulaUnavailableError)
|
||||||
|
expect(described_class.instance_variable_get(:@expand_stack)).to be_empty
|
||||||
|
end
|
||||||
|
end
|
@ -1,138 +0,0 @@
|
|||||||
require "testing_env"
|
|
||||||
require "dependency"
|
|
||||||
|
|
||||||
class DependencyExpansionTests < Homebrew::TestCase
|
|
||||||
def build_dep(name, tags = [], deps = [])
|
|
||||||
dep = Dependency.new(name.to_s, tags)
|
|
||||||
dep.stubs(:to_formula).returns(stub(deps: deps, name: name))
|
|
||||||
dep
|
|
||||||
end
|
|
||||||
|
|
||||||
def setup
|
|
||||||
super
|
|
||||||
@foo = build_dep(:foo)
|
|
||||||
@bar = build_dep(:bar)
|
|
||||||
@baz = build_dep(:baz)
|
|
||||||
@qux = build_dep(:qux)
|
|
||||||
@deps = [@foo, @bar, @baz, @qux]
|
|
||||||
@f = stub(deps: @deps, name: "f")
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_expand_yields_dependent_and_dep_pairs
|
|
||||||
i = 0
|
|
||||||
Dependency.expand(@f) do |dependent, dep|
|
|
||||||
assert_equal @f, dependent
|
|
||||||
assert_equal dep, @deps[i]
|
|
||||||
i += 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_expand_no_block
|
|
||||||
assert_equal @deps, Dependency.expand(@f)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_expand_prune_all
|
|
||||||
assert_empty Dependency.expand(@f) { Dependency.prune }
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_expand_selective_pruning
|
|
||||||
deps = Dependency.expand(@f) do |_, dep|
|
|
||||||
Dependency.prune if dep.name == "foo"
|
|
||||||
end
|
|
||||||
|
|
||||||
assert_equal [@bar, @baz, @qux], deps
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_expand_preserves_dependency_order
|
|
||||||
@foo.stubs(:to_formula).returns(stub(name: "f", deps: [@qux, @baz]))
|
|
||||||
assert_equal [@qux, @baz, @foo, @bar], Dependency.expand(@f)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_expand_skips_optionals_by_default
|
|
||||||
deps = [build_dep(:foo, [:optional]), @bar, @baz, @qux]
|
|
||||||
f = stub(deps: deps, build: stub(with?: false), name: "f")
|
|
||||||
assert_equal [@bar, @baz, @qux], Dependency.expand(f)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_expand_keeps_recommendeds_by_default
|
|
||||||
deps = [build_dep(:foo, [:recommended]), @bar, @baz, @qux]
|
|
||||||
f = stub(deps: deps, build: stub(with?: true), name: "f")
|
|
||||||
assert_equal deps, Dependency.expand(f)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_merges_repeated_deps_with_differing_options
|
|
||||||
@foo2 = build_dep(:foo, ["option"])
|
|
||||||
@baz2 = build_dep(:baz, ["option"])
|
|
||||||
@deps << @foo2 << @baz2
|
|
||||||
deps = [@foo2, @bar, @baz2, @qux]
|
|
||||||
deps.zip(Dependency.expand(@f)) do |expected, actual|
|
|
||||||
assert_equal expected.tags, actual.tags
|
|
||||||
assert_equal expected, actual
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_merger_preserves_env_proc
|
|
||||||
env_proc = stub
|
|
||||||
dep = Dependency.new("foo", [], env_proc)
|
|
||||||
dep.stubs(:to_formula).returns(stub(deps: [], name: "foo"))
|
|
||||||
@deps.replace [dep]
|
|
||||||
assert_equal env_proc, Dependency.expand(@f).first.env_proc
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_merged_tags_no_dupes
|
|
||||||
@foo2 = build_dep(:foo, ["option"])
|
|
||||||
@foo3 = build_dep(:foo, ["option"])
|
|
||||||
@deps << @foo2 << @foo3
|
|
||||||
|
|
||||||
assert_equal %w[option], Dependency.expand(@f).first.tags
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_skip_skips_parent_but_yields_children
|
|
||||||
f = stub(
|
|
||||||
name: "f",
|
|
||||||
deps: [
|
|
||||||
build_dep(:foo, [], [@bar, @baz]),
|
|
||||||
build_dep(:foo, [], [@baz]),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
deps = Dependency.expand(f) do |_dependent, dep|
|
|
||||||
Dependency.skip if %w[foo qux].include? dep.name
|
|
||||||
end
|
|
||||||
|
|
||||||
assert_equal [@bar, @baz], deps
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_keep_dep_but_prune_recursive_deps
|
|
||||||
foo = build_dep(:foo, [:build], @bar)
|
|
||||||
baz = build_dep(:baz, [:build])
|
|
||||||
f = stub(name: "f", deps: [foo, baz])
|
|
||||||
|
|
||||||
deps = Dependency.expand(f) do |_dependent, dep|
|
|
||||||
Dependency.keep_but_prune_recursive_deps if dep.build?
|
|
||||||
end
|
|
||||||
|
|
||||||
assert_equal [foo, baz], deps
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_deps_with_collection_argument
|
|
||||||
assert_equal [@foo, @bar, @baz, @qux], @f.deps
|
|
||||||
assert_equal [@bar, @baz], Dependency.expand(@f, [@bar, @baz])
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_cyclic_dependency
|
|
||||||
foo = build_dep(:foo)
|
|
||||||
bar = build_dep(:bar, [], [foo])
|
|
||||||
foo.stubs(:to_formula).returns(stub(deps: [bar], name: "foo"))
|
|
||||||
f = stub(name: "f", deps: [foo, bar])
|
|
||||||
assert_nothing_raised { Dependency.expand(f) }
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_clean_expand_stack
|
|
||||||
foo = build_dep(:foo)
|
|
||||||
foo.stubs(:to_formula).raises(FormulaUnavailableError, "foo")
|
|
||||||
f = stub(name: "f", deps: [foo])
|
|
||||||
assert_raises(FormulaUnavailableError) { Dependency.expand(f) }
|
|
||||||
assert_empty Dependency.instance_variable_get(:@expand_stack)
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
x
Reference in New Issue
Block a user