aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/test_formula.rb
blob: 686d8b0f07008cb9af50322197d4af8ab959a719 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
require 'testing_env'
require 'test/testball'

class AbstractDownloadStrategy
  attr_reader :url
end

class MostlyAbstractFormula < Formula
  url ''
end

class FormulaTests < Test::Unit::TestCase

  def test_prefix
    nostdout do
      TestBall.new.brew do |f|
        assert_equal File.expand_path(f.prefix), (HOMEBREW_CELLAR+f.name+'0.1').to_s
        assert_kind_of Pathname, f.prefix
      end
    end
  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_cant_override_brew
    assert_raises(RuntimeError) do
      eval <<-EOS
      class TestBallOverrideBrew < Formula
        def initialize
          super "foo"
        end
        def brew
        end
      end
      EOS
    end
  end

  def test_abstract_formula
    f=MostlyAbstractFormula.new
    assert_equal '__UNKNOWN__', f.name
    assert_raises(RuntimeError) { f.prefix }
    nostdout { assert_raises(RuntimeError) { f.brew } }
  end

  def test_mirror_support
    HOMEBREW_CACHE.mkpath unless HOMEBREW_CACHE.exist?
    nostdout do
      f = TestBallWithMirror.new
      tarball, downloader = f.fetch
      assert_equal f.url, "file:///#{TEST_FOLDER}/bad_url/testball-0.1.tbz"
      assert_equal downloader.url, "file:///#{TEST_FOLDER}/tarballs/testball-0.1.tbz"
    end
  end

  def test_formula_specs
    f = SpecTestBall.new

    assert_equal 'http://example.com', f.homepage
    assert_equal 'file:///foo.com/testball-0.1.tbz', f.url
    assert_equal 1, f.mirrors.length
    assert_equal '0.1', f.version
    assert_equal f.stable, f.active_spec
    assert_equal CurlDownloadStrategy, f.download_strategy
    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

    assert_equal 'file:///foo.com/testball-0.1.tbz', f.stable.url
    assert_equal "https://downloads.sf.net/project/machomebrew/Bottles/spectestball-0.1.#{MacOS.cat}.bottle.tar.gz",
      f.bottle.url
    assert_equal 'file:///foo.com/testball-0.2.tbz', f.devel.url
    assert_equal 'https://github.com/mxcl/homebrew.git', f.head.url

    assert_nil f.stable.specs
    assert_nil f.bottle.specs
    assert_nil f.devel.specs
    assert_equal({ :tag => 'foo' }, f.head.specs)

    assert_equal CurlDownloadStrategy, f.stable.download_strategy
    assert_equal CurlBottleDownloadStrategy, f.bottle.download_strategy
    assert_equal CurlDownloadStrategy, f.devel.download_strategy
    assert_equal GitDownloadStrategy, f.head.download_strategy

    assert_instance_of Checksum, f.stable.checksum
    assert_instance_of Checksum, f.bottle.checksum
    assert_instance_of Checksum, f.devel.checksum
    assert !f.stable.checksum.empty?
    assert !f.bottle.checksum.empty?
    assert !f.devel.checksum.empty?
    assert_nil f.head.checksum
    assert_equal :sha1, f.stable.checksum.hash_type
    assert_equal :sha1, f.bottle.checksum.hash_type
    assert_equal :sha256, f.devel.checksum.hash_type
    assert_equal case MacOS.cat
      when :snowleopard then 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef'
      when :lion then 'baadf00dbaadf00dbaadf00dbaadf00dbaadf00d'
      when :mountainlion then '8badf00d8badf00d8badf00d8badf00d8badf00d'
      end, f.bottle.checksum.hexdigest
    assert_match /[0-9a-fA-F]{40}/, f.stable.checksum.hexdigest
    assert_match /[0-9a-fA-F]{64}/, f.devel.checksum.hexdigest

    assert_nil f.stable.md5
    assert_nil f.stable.sha256
    assert_nil f.bottle.md5
    assert_nil f.bottle.sha256
    assert_nil f.devel.md5
    assert_nil f.devel.sha1

    assert_equal 1, f.stable.mirrors.length
    assert f.bottle.mirrors.empty?
    assert_equal 1, f.devel.mirrors.length
    assert f.head.mirrors.empty?

    assert !f.stable.explicit_version?
    assert !f.bottle.explicit_version?
    assert !f.devel.explicit_version?
    assert_equal '0.1', f.stable.version
    assert_equal '0.1', f.bottle.version
    assert_equal '0.2', f.devel.version
    assert_equal 'HEAD', f.head.version
    assert_equal 0, f.bottle.revision
  end

  def test_devel_active_spec
    ARGV.push '--devel'
    f = SpecTestBall.new
    assert_equal f.devel, f.active_spec
    assert_equal '0.2', f.version
    assert_equal 'file:///foo.com/testball-0.2.tbz', f.url
    assert_equal CurlDownloadStrategy, f.download_strategy
    assert_instance_of CurlDownloadStrategy, f.downloader
    ARGV.delete '--devel'
  end

  def test_head_active_spec
    ARGV.push '--HEAD'
    f = SpecTestBall.new
    assert_equal f.head, f.active_spec
    assert_equal 'HEAD', f.version
    assert_equal 'https://github.com/mxcl/homebrew.git', f.url
    assert_equal GitDownloadStrategy, f.download_strategy
    assert_instance_of GitDownloadStrategy, f.downloader
    ARGV.delete '--HEAD'
  end

  def test_explicit_version_spec
    f = ExplicitVersionSpecTestBall.new
    assert_equal '0.3', f.version
    assert_equal '0.3', f.stable.version
    assert_equal '0.4', f.devel.version
    assert f.stable.explicit_version?
    assert f.devel.explicit_version?
  end

  def test_old_bottle_specs
    f = OldBottleSpecTestBall.new

    case MacOS.cat
    when :lion
      assert_instance_of Bottle, f.bottle
      assert_equal CurlBottleDownloadStrategy, f.bottle.download_strategy
      assert_nil f.bottle.specs
      assert_nil f.bottle.mirrors

      assert_equal 'file:///foo.com/testball-0.1-bottle.tar.gz', f.bottle.url

      assert_instance_of Checksum, f.bottle.checksum
      assert_equal :sha1, f.bottle.checksum.hash_type
      assert !f.bottle.checksum.empty?
      assert_equal 'baadf00dbaadf00dbaadf00dbaadf00dbaadf00d', f.bottle.sha1.hexdigest
      assert_nil f.bottle.md5
      assert_nil f.bottle.sha256

      assert !f.bottle.explicit_version?
      assert_equal 0, f.bottle.revision
      assert_equal '0.1', f.bottle.version
    else
      assert_nil f.bottle
    end
  end

  def test_ancient_bottle_specs
    f = AncientBottleSpecTestBall.new
    assert_nil f.bottle
  end

  def test_head_only_specs
    f = HeadOnlySpecTestBall.new

    assert_not_nil f.head
    assert_nil f.stable
    assert_nil f.bottle
    assert_nil f.devel

    assert_equal f.head, f.active_spec
    assert_equal 'HEAD', f.version
    assert_nil f.head.checksum
    assert_equal 'https://github.com/mxcl/homebrew.git', f.url
    assert_equal GitDownloadStrategy, f.download_strategy
    assert_instance_of GitDownloadStrategy, f.downloader
    assert_instance_of HeadSoftwareSpec, f.head
  end

  def test_incomplete_stable_specs
    f = IncompleteStableSpecTestBall.new

    assert_not_nil f.head
    assert_nil f.stable
    assert_nil f.bottle
    assert_nil f.devel

    assert_equal f.head, f.active_spec
    assert_equal 'HEAD', f.version
    assert_nil f.head.checksum
    assert_equal 'https://github.com/mxcl/homebrew.git', f.url
    assert_equal GitDownloadStrategy, f.download_strategy
    assert_instance_of GitDownloadStrategy, f.downloader
    assert_instance_of HeadSoftwareSpec, f.head
  end

  def test_head_only_with_version_specs
    f = IncompleteStableSpecTestBall.new

    assert_not_nil f.head
    assert_nil f.stable
    assert_nil f.bottle
    assert_nil f.devel

    assert_equal f.head, f.active_spec
    assert_equal 'HEAD', f.version
    assert_nil f.head.checksum
    assert_equal 'https://github.com/mxcl/homebrew.git', f.url
    assert_equal GitDownloadStrategy, f.download_strategy
    assert_instance_of GitDownloadStrategy, f.downloader
    assert_instance_of HeadSoftwareSpec, f.head
  end

  def test_explicit_strategy_specs
    f = ExplicitStrategySpecTestBall.new

    assert_not_nil f.stable
    assert_not_nil f.devel
    assert_not_nil f.head

    assert_equal f.stable, f.active_spec

    assert_nil f.stable.checksum
    assert_nil f.devel.checksum
    assert_nil f.head.checksum

    assert_equal MercurialDownloadStrategy, f.stable.download_strategy
    assert_equal BazaarDownloadStrategy, f.devel.download_strategy
    assert_equal SubversionDownloadStrategy, f.head.download_strategy

    assert_equal({ :tag => '0.2' }, f.stable.specs)
    assert_equal({ :tag => '0.3' }, f.devel.specs)
    assert f.head.specs.empty?
  end

  def test_revised_bottle_specs
    f = RevisedBottleSpecTestBall.new

    assert_equal 1, f.bottle.revision
    assert_equal case MacOS.cat
      when :snowleopard then 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef'
      when :lion then 'baadf00dbaadf00dbaadf00dbaadf00dbaadf00d'
      when :mountainlion then '8badf00d8badf00d8badf00d8badf00d8badf00d'
      end, f.bottle.checksum.hexdigest
  end
end