brew/Library/Homebrew/test/test_formula.rb

271 lines
6.3 KiB
Ruby
Raw Normal View History

2010-03-16 22:33:13 -07:00
require 'testing_env'
2014-11-07 17:03:51 -06:00
require 'testball'
class FormulaTests < Homebrew::TestCase
def test_formula_instantiation
2014-02-21 20:07:41 -05:00
klass = Class.new(Formula) { url "http://example.com/foo-1.0.tar.gz" }
name = "formula_name"
path = Formula.path(name)
spec = :stable
2014-02-21 20:07:41 -05:00
f = klass.new(name, path, spec)
assert_equal name, f.name
2014-02-21 20:07:41 -05:00
assert_equal path, f.path
assert_raises(ArgumentError) { klass.new }
2014-02-21 20:07:41 -05:00
end
2010-03-16 22:33:13 -07:00
def test_prefix
2013-04-08 17:43:01 -05:00
f = TestBall.new
2013-12-11 19:18:23 -06:00
assert_equal HOMEBREW_CELLAR/f.name/'0.1', f.prefix
2013-04-08 17:43:01 -05:00
assert_kind_of Pathname, f.prefix
2010-03-16 22:33:13 -07:00
end
def test_revised_prefix
f = Class.new(TestBall) { revision 1 }.new
assert_equal HOMEBREW_CELLAR/f.name/'0.1_1', f.prefix
end
def test_installed?
f = TestBall.new
f.stubs(:installed_prefix).returns(stub(:directory? => false))
2014-06-11 12:21:03 -05:00
refute_predicate f, :installed?
f.stubs(:installed_prefix).returns(
stub(:directory? => true, :children => [])
)
2014-06-11 12:21:03 -05:00
refute_predicate f, :installed?
f.stubs(:installed_prefix).returns(
stub(:directory? => true, :children => [stub])
)
2014-06-11 12:21:03 -05:00
assert_predicate f, :installed?
end
2013-05-25 17:19:19 -05:00
def test_installed_prefix
2014-12-28 00:03:16 -05:00
f = TestBall.new
2013-05-25 17:19:19 -05:00
assert_equal f.prefix, f.installed_prefix
end
def test_installed_prefix_head_installed
f = formula do
head 'foo'
devel do
url 'foo'
version '1.0'
end
end
prefix = HOMEBREW_CELLAR+f.name+f.head.version
prefix.mkpath
assert_equal prefix, f.installed_prefix
ensure
f.rack.rmtree
2013-05-25 17:19:19 -05:00
end
def test_installed_prefix_devel_installed
f = formula do
head 'foo'
devel do
url 'foo'
version '1.0'
end
end
prefix = HOMEBREW_CELLAR+f.name+f.devel.version
prefix.mkpath
assert_equal prefix, f.installed_prefix
ensure
f.rack.rmtree
2013-05-25 17:19:19 -05:00
end
def test_installed_prefix_stable_installed
f = formula do
head 'foo'
devel do
url 'foo'
version '1.0-devel'
end
end
prefix = HOMEBREW_CELLAR+f.name+f.version
prefix.mkpath
assert_equal prefix, f.installed_prefix
ensure
f.rack.rmtree
2013-05-25 17:19:19 -05:00
end
def test_installed_prefix_head
2014-06-19 21:35:47 -05:00
f = formula("test", Pathname.new(__FILE__).expand_path, :head) do
2013-05-25 17:19:19 -05:00
head 'foo'
devel do
url 'foo'
version '1.0-devel'
end
end
prefix = HOMEBREW_CELLAR+f.name+f.head.version
assert_equal prefix, f.installed_prefix
end
def test_installed_prefix_devel
2014-06-19 21:35:47 -05:00
f = formula("test", Pathname.new(__FILE__).expand_path, :devel) do
2013-05-25 17:19:19 -05:00
head 'foo'
devel do
url 'foo'
version '1.0-devel'
end
end
prefix = HOMEBREW_CELLAR+f.name+f.devel.version
assert_equal prefix, f.installed_prefix
end
def test_equality
x = TestBall.new
y = TestBall.new
2014-06-04 16:31:57 -05:00
assert_equal x, y
assert_eql x, y
2014-06-04 16:31:57 -05:00
assert_equal x.hash, y.hash
end
def test_inequality
x = TestBall.new("foo")
y = TestBall.new("bar")
2014-06-10 22:43:47 -05:00
refute_equal x, y
refute_eql x, y
2014-06-10 22:43:47 -05:00
refute_equal x.hash, y.hash
end
def test_comparison_with_non_formula_objects_does_not_raise
2014-06-10 22:43:47 -05:00
refute_equal TestBall.new, Object.new
end
def test_sort_operator
assert_nil TestBall.new <=> Object.new
end
2010-03-16 22:33:13 -07:00
def test_class_naming
2014-02-21 00:43:58 -05:00
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')
2010-03-16 22:33:13 -07:00
end
def test_formula_spec_integration
f = Class.new(Formula) do
homepage 'http://example.com'
2014-06-12 17:58:12 -05:00
url 'http://example.com/test-0.1.tbz'
mirror 'http://example.org/test-0.1.tbz'
sha1 TEST_SHA1
2014-06-12 17:58:12 -05:00
head 'http://example.com/test.git', :tag => 'foo'
devel do
2014-06-12 17:58:12 -05:00
url 'http://example.com/test-0.2.tbz'
mirror 'http://example.org/test-0.2.tbz'
sha256 TEST_SHA256
end
end.new("test", Pathname.new(__FILE__).expand_path, :stable)
2012-04-05 21:09:24 -05:00
assert_equal 'http://example.com', f.homepage
assert_version_equal '0.1', f.version
assert_predicate f, :stable?
2012-04-05 21:09:24 -05:00
2014-12-27 16:31:38 -05:00
assert_version_equal "0.1", f.stable.version
assert_version_equal "0.2", f.devel.version
assert_version_equal "HEAD", f.head.version
end
def test_path
name = 'foo-bar'
2013-10-30 13:20:48 -07:00
assert_equal Pathname.new("#{HOMEBREW_LIBRARY}/Formula/#{name}.rb"), Formula.path(name)
end
def test_factory
name = 'foo-bar'
path = HOMEBREW_PREFIX+"Library/Formula/#{name}.rb"
path.dirname.mkpath
File.open(path, 'w') do |f|
f << %{
2014-02-21 00:43:58 -05:00
class #{Formulary.class_s(name)} < Formula
url 'foo-1.0'
end
}
end
assert_kind_of Formula, Formulary.factory(name)
ensure
path.unlink
end
def test_class_specs_are_always_initialized
f = formula { url 'foo-1.0' }
%w{stable devel head}.each do |spec|
assert_kind_of SoftwareSpec, f.class.send(spec)
end
end
def test_incomplete_instance_specs_are_not_accessible
f = formula { url 'foo-1.0' }
%w{devel head}.each { |spec| assert_nil f.send(spec) }
end
def test_honors_attributes_declared_before_specs
f = formula do
url 'foo-1.0'
depends_on 'foo'
devel { url 'foo-1.1' }
end
%w{stable devel head}.each do |spec|
assert_equal 'foo', f.class.send(spec).deps.first.name
end
end
def test_simple_version
assert_equal PkgVersion.parse('1.0'), formula { url 'foo-1.0.bar' }.pkg_version
end
def test_version_with_revision
f = formula do
url 'foo-1.0.bar'
revision 1
end
assert_equal PkgVersion.parse('1.0_1'), f.pkg_version
end
def test_head_ignores_revisions
2014-06-19 21:35:47 -05:00
f = formula("test", Pathname.new(__FILE__).expand_path, :head) do
url 'foo-1.0.bar'
revision 1
head 'foo'
end
assert_equal PkgVersion.parse('HEAD'), f.pkg_version
end
def test_raises_when_non_formula_constant_exists
const = :SomeConst
Object.const_set(const, Module.new)
begin
assert_raises(FormulaUnavailableError) { Formulary.factory("some_const") }
ensure
Object.send(:remove_const, const)
end
end
2014-08-10 16:47:34 -05:00
def test_legacy_options
f = formula do
url "foo-1.0"
def options
[["--foo", "desc"], ["--bar", "desc"]]
end
option "baz"
end
assert f.option_defined?("foo")
assert f.option_defined?("bar")
assert f.option_defined?("baz")
end
2010-03-16 22:33:13 -07:00
end