diff --git a/Library/Homebrew/test/formulary_spec.rb b/Library/Homebrew/test/formulary_spec.rb new file mode 100644 index 0000000000..8e9d235896 --- /dev/null +++ b/Library/Homebrew/test/formulary_spec.rb @@ -0,0 +1,219 @@ +require "formula" +require "formula_installer" +require "utils/bottles" + +describe Formulary do + let(:formula_name) { "testball_bottle" } + let(:formula_path) { CoreTap.new.formula_dir/"#{formula_name}.rb" } + let(:formula_content) do + <<-EOS.undent + class #{subject.class_s(formula_name)} < Formula + url "file://#{TEST_FIXTURE_DIR}/tarballs/testball-0.1.tbz" + sha256 TESTBALL_SHA256 + + bottle do + cellar :any_skip_relocation + root_url "file://#{bottle_dir}" + sha256 "9abc8ce779067e26556002c4ca6b9427b9874d25f0cafa7028e05b5c5c410cb4" => :#{Utils::Bottles.tag} + end + + def install + prefix.install "bin" + prefix.install "libexec" + end + end + EOS + end + let(:bottle_dir) { Pathname.new("#{TEST_FIXTURE_DIR}/bottles") } + let(:bottle) { bottle_dir/"testball_bottle-0.1.#{Utils::Bottles.tag}.bottle.tar.gz" } + + describe "::class_s" do + it "replaces '+' with 'x'" do + expect(subject.class_s("foo++")).to eq("Fooxx") + end + + it "converts a string to PascalCase" do + expect(subject.class_s("shell.fm")).to eq("ShellFm") + expect(subject.class_s("s-lang")).to eq("SLang") + expect(subject.class_s("pkg-config")).to eq("PkgConfig") + expect(subject.class_s("foo_bar")).to eq("FooBar") + end + + it "replaces '@' with 'AT'" do + expect(subject.class_s("openssl@1.1")).to eq("OpensslAT11") + end + end + + describe "::factory" do + before(:each) do + formula_path.write formula_content + end + + it "returns a Formula" do + expect(subject.factory(formula_name)).to be_kind_of(Formula) + end + + it "returns a Formula when given a fully qualified name" do + expect(subject.factory("homebrew/core/#{formula_name}")).to be_kind_of(Formula) + end + + it "raises an error if the Formula cannot be found" do + expect { + subject.factory("not_existed_formula") + }.to raise_error(FormulaUnavailableError) + end + + context "if the Formula has the wrong class" do + let(:formula_name) { "giraffe" } + let(:formula_content) do + <<-EOS.undent + class Wrong#{subject.class_s(formula_name)} < Formula + end + EOS + end + + it "raises an error" do + expect { + subject.factory(formula_name) + }.to raise_error(FormulaClassUnavailableError) + end + end + + it "returns a Formula when given a path" do + expect(subject.factory(formula_path)).to be_kind_of(Formula) + end + + it "returns a Formula when given a URL" do + formula = shutup do + subject.factory("file://#{formula_path}") + end + + expect(formula).to be_kind_of(Formula) + end + + it "returns a Formula when given a bottle" do + formula = subject.factory(bottle) + expect(formula).to be_kind_of(Formula) + expect(formula.local_bottle_path).to eq(bottle.realpath) + end + + it "returns a Formula when given an alias" do + alias_dir = CoreTap.instance.alias_dir + alias_dir.mkpath + alias_path = alias_dir/"foo" + FileUtils.ln_s formula_path, alias_path + result = subject.factory("foo") + expect(result).to be_kind_of(Formula) + expect(result.alias_path).to eq(alias_path.to_s) + end + + context "with installed Formula" do + let(:formula) { subject.factory(formula_path) } + let(:installer) { FormulaInstaller.new(formula) } + + it "returns a Formula when given a rack" do + shutup do + installer.install + end + + f = subject.from_rack(formula.rack) + expect(f).to be_kind_of(Formula) + expect(f.build).to be_kind_of(Tab) + end + + it "returns a Formula when given a Keg" do + shutup do + installer.install + end + + keg = Keg.new(formula.prefix) + f = subject.from_keg(keg) + expect(f).to be_kind_of(Formula) + expect(f.build).to be_kind_of(Tab) + end + end + + context "from Tap" do + let(:tap) { Tap.new("homebrew", "foo") } + let(:formula_path) { tap.path/"#{formula_name}.rb" } + + it "returns a Formula when given a name" do + expect(subject.factory(formula_name)).to be_kind_of(Formula) + end + + it "returns a Formula from an Alias path" do + alias_dir = tap.path/"Aliases" + alias_dir.mkpath + FileUtils.ln_s formula_path, alias_dir/"bar" + expect(subject.factory("bar")).to be_kind_of(Formula) + end + + it "raises an error when the Formula cannot be found" do + expect { + subject.factory("#{tap}/not_existed_formula") + }.to raise_error(TapFormulaUnavailableError) + end + + it "returns a Formula when given a fully qualified name" do + expect(subject.factory("#{tap}/#{formula_name}")).to be_kind_of(Formula) + end + + it "raises an error if a Formula is in multiple Taps" do + begin + another_tap = Tap.new("homebrew", "bar") + (another_tap.path/"#{formula_name}.rb").write formula_content + expect { + subject.factory(formula_name) + }.to raise_error(TapFormulaAmbiguityError) + ensure + another_tap.path.rmtree + end + end + end + end + + specify "::from_contents" do + expect(subject.from_contents(formula_name, formula_path, formula_content)).to be_kind_of(Formula) + end + + specify "::to_rack" do + expect(subject.to_rack(formula_name)).to eq(HOMEBREW_CELLAR/formula_name) + + (HOMEBREW_CELLAR/formula_name).mkpath + expect(subject.to_rack(formula_name)).to eq(HOMEBREW_CELLAR/formula_name) + + expect { + subject.to_rack("a/b/#{formula_name}") + }.to raise_error(TapFormulaUnavailableError) + end + + describe "::find_with_priority" do + let(:core_path) { CoreTap.new.formula_dir/"#{formula_name}.rb" } + let(:tap) { Tap.new("homebrew", "foo") } + let(:tap_path) { tap.path/"#{formula_name}.rb" } + + before(:each) do + core_path.write formula_content + tap_path.write formula_content + end + + it "prioritizes core Formulae" do + formula = subject.find_with_priority(formula_name) + expect(formula).to be_kind_of(Formula) + expect(formula.path).to eq(core_path) + end + + it "prioritizes Formulae from pinned Taps" do + begin + tap.pin + formula = shutup do + subject.find_with_priority(formula_name) + end + expect(formula).to be_kind_of(Formula) + expect(formula.path).to eq(tap_path.realpath) + ensure + tap.pinned_symlink_path.parent.parent.rmtree + end + end + end +end diff --git a/Library/Homebrew/test/formulary_test.rb b/Library/Homebrew/test/formulary_test.rb deleted file mode 100644 index ea7ecf8d05..0000000000 --- a/Library/Homebrew/test/formulary_test.rb +++ /dev/null @@ -1,189 +0,0 @@ -require "testing_env" -require "formula" -require "formula_installer" -require "utils/bottles" - -class FormularyTest < Homebrew::TestCase - def test_class_naming - assert_equal "ShellFm", Formulary.class_s("shell.fm") - assert_equal "Fooxx", Formulary.class_s("foo++") - assert_equal "SLang", Formulary.class_s("s-lang") - assert_equal "PkgConfig", Formulary.class_s("pkg-config") - assert_equal "FooBar", Formulary.class_s("foo_bar") - assert_equal "OpensslAT11", Formulary.class_s("openssl@1.1") - end -end - -class FormularyFactoryTest < Homebrew::TestCase - def setup - super - @name = "testball_bottle" - @path = CoreTap.new.formula_dir/"#{@name}.rb" - @bottle_dir = Pathname.new("#{TEST_FIXTURE_DIR}/bottles") - @bottle = @bottle_dir/"testball_bottle-0.1.#{Utils::Bottles.tag}.bottle.tar.gz" - @path.write <<-EOS.undent - class #{Formulary.class_s(@name)} < Formula - url "file://#{TEST_FIXTURE_DIR}/tarballs/testball-0.1.tbz" - sha256 TESTBALL_SHA256 - - bottle do - cellar :any_skip_relocation - root_url "file://#{@bottle_dir}" - sha256 "9abc8ce779067e26556002c4ca6b9427b9874d25f0cafa7028e05b5c5c410cb4" => :#{Utils::Bottles.tag} - end - - def install - prefix.install "bin" - prefix.install "libexec" - end - end - EOS - end - - def test_factory - assert_kind_of Formula, Formulary.factory(@name) - end - - def test_factory_with_fully_qualified_name - assert_kind_of Formula, Formulary.factory("homebrew/core/#{@name}") - end - - def test_formula_unavailable_error - assert_raises(FormulaUnavailableError) { Formulary.factory("not_existed_formula") } - end - - def test_formula_class_unavailable_error - name = "giraffe" - path = CoreTap.new.formula_dir/"#{name}.rb" - path.write "class Wrong#{Formulary.class_s(name)} < Formula\nend\n" - - assert_raises(FormulaClassUnavailableError) { Formulary.factory(name) } - end - - def test_factory_from_path - assert_kind_of Formula, Formulary.factory(@path) - end - - def test_factory_from_url - formula = shutup { Formulary.factory("file://#{@path}") } - assert_kind_of Formula, formula - ensure - formula.path.unlink - end - - def test_factory_from_bottle - formula = Formulary.factory(@bottle) - assert_kind_of Formula, formula - assert_equal @bottle.realpath, formula.local_bottle_path - end - - def test_factory_from_alias - alias_dir = CoreTap.instance.alias_dir - alias_dir.mkpath - alias_path = alias_dir/"foo" - FileUtils.ln_s @path, alias_path - result = Formulary.factory("foo") - assert_kind_of Formula, result - assert_equal alias_path.to_s, result.alias_path - end - - def test_factory_from_rack_and_from_keg - formula = Formulary.factory(@path) - installer = FormulaInstaller.new(formula) - shutup { installer.install } - keg = Keg.new(formula.prefix) - f = Formulary.from_rack(formula.rack) - assert_kind_of Formula, f - assert_kind_of Tab, f.build - f = Formulary.from_keg(keg) - assert_kind_of Formula, f - assert_kind_of Tab, f.build - ensure - keg.unlink - end - - def test_load_from_contents - assert_kind_of Formula, Formulary.from_contents(@name, @path, @path.read) - end - - def test_to_rack - assert_equal HOMEBREW_CELLAR/@name, Formulary.to_rack(@name) - (HOMEBREW_CELLAR/@name).mkpath - assert_equal HOMEBREW_CELLAR/@name, Formulary.to_rack(@name) - assert_raises(TapFormulaUnavailableError) { Formulary.to_rack("a/b/#{@name}") } - end -end - -class FormularyTapFactoryTest < Homebrew::TestCase - def setup - super - @name = "foo" - @tap = Tap.new "homebrew", "foo" - @path = @tap.path/"#{@name}.rb" - @code = <<-EOS.undent - class #{Formulary.class_s(@name)} < Formula - url "foo-1.0" - end - EOS - @path.write @code - end - - def test_factory_tap_formula - assert_kind_of Formula, Formulary.factory(@name) - end - - def test_factory_tap_alias - alias_dir = @tap.path/"Aliases" - alias_dir.mkpath - FileUtils.ln_s @path, alias_dir/"bar" - assert_kind_of Formula, Formulary.factory("bar") - end - - def test_tap_formula_unavailable_error - assert_raises(TapFormulaUnavailableError) { Formulary.factory("#{@tap}/not_existed_formula") } - end - - def test_factory_tap_formula_with_fully_qualified_name - assert_kind_of Formula, Formulary.factory("#{@tap}/#{@name}") - end - - def test_factory_ambiguity_tap_formulae - another_tap = Tap.new "homebrew", "bar" - (another_tap.path/"#{@name}.rb").write @code - assert_raises(TapFormulaAmbiguityError) { Formulary.factory(@name) } - ensure - another_tap.path.rmtree - end -end - -class FormularyTapPriorityTest < Homebrew::TestCase - def setup - super - @name = "foo" - @core_path = CoreTap.new.formula_dir/"#{@name}.rb" - @tap = Tap.new "homebrew", "foo" - @tap_path = @tap.path/"#{@name}.rb" - code = <<-EOS.undent - class #{Formulary.class_s(@name)} < Formula - url "foo-1.0" - end - EOS - @core_path.write code - @tap_path.write code - end - - def test_find_with_priority_core_formula - formula = Formulary.find_with_priority(@name) - assert_kind_of Formula, formula - assert_equal @core_path, formula.path - end - - def test_find_with_priority_tap_formula - @tap.pin - formula = shutup { Formulary.find_with_priority(@name) } - assert_kind_of Formula, formula - assert_equal @tap_path.realpath, formula.path - ensure - @tap.pinned_symlink_path.parent.parent.rmtree - end -end