aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/test_formula.rb
blob: 1591b425b7ab3e91c9ad9dce8eecbadb658a6ccd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
require 'testing_env'
require 'test/testball'

class FormulaTests < Test::Unit::TestCase
  include VersionAssertions

  def test_prefix
    f = TestBall.new
    assert_equal File.expand_path(f.prefix), (HOMEBREW_CELLAR+f.name+'0.1').to_s
    assert_kind_of Pathname, f.prefix
  end

  def test_installed?
    f = TestBall.new
    f.stubs(:installed_prefix).returns(stub(:directory? => false))
    assert !f.installed?

    f.stubs(:installed_prefix).returns(
      stub(:directory? => true, :children => [])
    )
    assert !f.installed?

    f.stubs(:installed_prefix).returns(
      stub(:directory? => true, :children => [stub])
    )
    assert f.installed?
  end

  def test_installed_prefix
    f = Class.new(TestBall).new
    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
    prefix.rmtree
  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
    prefix.rmtree
  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
    prefix.rmtree
  end

  def test_installed_prefix_head_active_spec
    ARGV.stubs(:build_head? => true)

    f = formula do
      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_active_spec
    ARGV.stubs(:build_devel? => true)

    f = formula do
      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
    assert x == y
    assert y == x
    assert x.eql?(y)
    assert y.eql?(x)
    assert x.hash == y.hash
  end

  def test_inequality
    x = TestBall.new("foo")
    y = TestBall.new("bar")
    assert x != y
    assert y != x
    assert x.hash != y.hash
    assert !x.eql?(y)
    assert !y.eql?(x)
  end

  def test_class_naming
    assert_equal 'ShellFm', Formula.class_s('shell.fm')
    assert_equal 'Fooxx', Formula.class_s('foo++')
    assert_equal 'SLang', Formula.class_s('s-lang')
    assert_equal 'PkgConfig', Formula.class_s('pkg-config')
    assert_equal 'FooBar', Formula.class_s('foo_bar')
  end

  def test_mirror_support
    f = Class.new(Formula) do
      url "file:///#{TEST_FOLDER}/bad_url/testball-0.1.tbz"
      mirror "file:///#{TEST_FOLDER}/tarballs/testball-0.1.tbz"
    end.new("test_mirror_support")

    shutup { f.fetch }

    assert_equal "file:///#{TEST_FOLDER}/bad_url/testball-0.1.tbz", f.url
    assert_equal "file:///#{TEST_FOLDER}/tarballs/testball-0.1.tbz",
      f.downloader.instance_variable_get(:@url)
  end

  def test_formula_spec_integration
    f = Class.new(Formula) do
      homepage 'http://example.com'
      url 'file:///foo.com/testball-0.1.tbz'
      mirror 'file:///foo.org/testball-0.1.tbz'
      sha1 '482e737739d946b7c8cbaf127d9ee9c148b999f5'

      head 'https://github.com/mxcl/homebrew.git', :tag => 'foo'

      devel do
        url 'file:///foo.com/testball-0.2.tbz'
        mirror 'file:///foo.org/testball-0.2.tbz'
        sha256 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'
      end

      bottle do
        sha1 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef' => :snow_leopard_32
        sha1 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef' => :snow_leopard
        sha1 'baadf00dbaadf00dbaadf00dbaadf00dbaadf00d' => :lion
        sha1 '8badf00d8badf00d8badf00d8badf00d8badf00d' => :mountain_lion
      end

      def initialize(name="spec_test_ball", path=nil)
        super
      end
    end.new

    assert_equal 'http://example.com', f.homepage
    assert_version_equal '0.1', f.version
    assert_equal f.stable, f.active_spec
    assert_instance_of CurlDownloadStrategy, f.downloader

    assert_instance_of SoftwareSpec, f.stable
    assert_instance_of Bottle, f.bottle
    assert_instance_of SoftwareSpec, f.devel
    assert_instance_of HeadSoftwareSpec, f.head
  end

  def test_path
    name = 'foo-bar'
    assert_equal Pathname.new("#{HOMEBREW_REPOSITORY}/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 << %{
        require 'formula'
        class #{Formula.class_s(name)} < Formula
          url 'foo-1.0'
          def initialize(*args)
            @homepage = 'http://example.com/'
            super
          end
        end
      }
    end
    assert_kind_of Formula, Formula.factory(name)
  ensure
    path.unlink
  end
end