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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  | 
require "testing_env"
require "testball"
require "formula"
class FormulaTests < Homebrew::TestCase
  def test_formula_instantiation
    klass = Class.new(Formula) { url "http://example.com/foo-1.0.tar.gz" }
    name = "formula_name"
    path = Formulary.core_path(name)
    spec = :stable
    f = klass.new(name, path, spec)
    assert_equal name, f.name
    assert_equal path, f.path
    assert_raises(ArgumentError) { klass.new }
  end
  def test_prefix
    f = Testball.new
    assert_equal HOMEBREW_CELLAR/f.name/"0.1", f.prefix
    assert_kind_of Pathname, f.prefix
  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_any_version_installed?
    f = formula do
      url "foo"
      version "1.0"
    end
    refute_predicate f, :any_version_installed?
    prefix = HOMEBREW_CELLAR+f.name+"0.1"
    prefix.mkpath
    FileUtils.touch prefix+Tab::FILENAME
    assert_predicate f, :any_version_installed?
  ensure
    f.rack.rmtree
  end
  def test_installed?
    f = Testball.new
    f.stubs(:installed_prefix).returns(stub(:directory? => false))
    refute_predicate f, :installed?
    f.stubs(:installed_prefix).returns(
      stub(:directory? => true, :children => [])
    )
    refute_predicate f, :installed?
    f.stubs(:installed_prefix).returns(
      stub(:directory? => true, :children => [stub])
    )
    assert_predicate f, :installed?
  end
  def test_installed_prefix
    f = 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
    f.rack.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
    f.rack.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
    f.rack.rmtree
  end
  def test_installed_prefix_head
    f = formula("test", Pathname.new(__FILE__).expand_path, :head) 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
    f = formula("test", Pathname.new(__FILE__).expand_path, :devel) 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_equal x, y
    assert_eql x, y
    assert_equal x.hash, y.hash
  end
  def test_inequality
    x = Testball.new("foo")
    y = Testball.new("bar")
    refute_equal x, y
    refute_eql x, y
    refute_equal x.hash, y.hash
  end
  def test_comparison_with_non_formula_objects_does_not_raise
    refute_equal Testball.new, Object.new
  end
  def test_sort_operator
    assert_nil Testball.new <=> Object.new
  end
  def test_formula_spec_integration
    f = formula do
      homepage "http://example.com"
      url "http://example.com/test-0.1.tbz"
      mirror "http://example.org/test-0.1.tbz"
      sha1 TEST_SHA1
      head "http://example.com/test.git", :tag => "foo"
      devel do
        url "http://example.com/test-0.2.tbz"
        mirror "http://example.org/test-0.2.tbz"
        sha256 TEST_SHA256
      end
    end
    assert_equal "http://example.com", f.homepage
    assert_version_equal "0.1", f.version
    assert_predicate f, :stable?
    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_formula_set_active_spec
    f = formula do
      url "foo"
      version "1.0"
      revision 1
      devel do
        url "foo"
        version "1.0beta"
      end
    end
    assert_equal :stable, f.active_spec_sym
    assert_equal f.stable, f.send(:active_spec)
    assert_equal "1.0_1", f.pkg_version.to_s
    f.set_active_spec(:devel)
    assert_equal :devel, f.active_spec_sym
    assert_equal f.devel, f.send(:active_spec)
    assert_equal "1.0beta_1", f.pkg_version.to_s
    assert_raises(FormulaSpecificationError) { f.set_active_spec(:head) }
  end
  def test_path
    name = "foo-bar"
    assert_equal Pathname.new("#{HOMEBREW_LIBRARY}/Formula/#{name}.rb"), Formulary.core_path(name)
  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
    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_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
  def test_desc
    f = formula do
      desc "a formula"
      url "foo-1.0"
    end
    assert_equal "a formula", f.desc
  end
  def test_post_install_defined
    f1 = formula do
      url "foo-1.0"
      def post_install; end
    end
    f2 = formula do
      url "foo-1.0"
    end
    assert f1.post_install_defined?
    refute f2.post_install_defined?
  end
  def test_test_defined
    f1 = formula do
      url "foo-1.0"
      def test; end
    end
    f2 = formula do
      url "foo-1.0"
    end
    assert f1.test_defined?
    refute f2.test_defined?
  end
  def test_test_fixtures
    f1 = formula do
      url "foo-1.0"
    end
    assert_equal Pathname.new("#{HOMEBREW_LIBRARY}/Homebrew/test/fixtures/foo"),
      f1.test_fixtures("foo")
  end
  def test_to_hash
    f1 = formula("foo") do
      url "foo-1.0"
    end
    h = f1.to_hash
    assert h.is_a?(Hash), "Formula#to_hash should return a Hash"
    assert_equal "foo", h["name"]
    assert_equal "foo", h["full_name"]
    assert_equal "1.0", h["versions"]["stable"]
  end
  def test_to_hash_bottle
    MacOS.stubs(:version).returns(MacOS::Version.new("10.11"))
    f1 = formula("foo") do
      url "foo-1.0"
      bottle do
        cellar :any
        sha256 TEST_SHA256 => :el_capitan
      end
    end
    h = f1.to_hash
    assert h.is_a?(Hash), "Formula#to_hash should return a Hash"
    assert h["versions"]["bottle"], "The hash should say the formula is bottled"
  end
  def test_eligible_kegs_for_cleanup
    f1 = Class.new(Testball) { version "0.1" }.new
    f2 = Class.new(Testball) { version "0.2" }.new
    f3 = Class.new(Testball) { version "0.3" }.new
    shutup do
      f1.brew { f1.install }
      f2.brew { f2.install }
      f3.brew { f3.install }
    end
    assert_predicate f1, :installed?
    assert_predicate f2, :installed?
    assert_predicate f3, :installed?
    assert_equal f3.installed_kegs[0..1], f3.eligible_kegs_for_cleanup
  ensure
    [f1, f2, f3].each(&:clear_cache)
    f3.rack.rmtree
  end
end
  |