#!/usr/bin/ruby # This software is in the public domain, furnished "as is", without technical # support, and with no warranty, express or implied, as to its usefulness for # any purpose. ABS__FILE__=File.expand_path(__FILE__) $:.unshift File.dirname(ABS__FILE__) require 'pathname+yeast' require 'formula' require 'download_strategy' require 'keg' require 'utils' require 'brew.h' require 'hardware.rb' # these are defined in bin/brew, but we don't want to break our actual # homebrew tree, and we do want to test everything :) HOMEBREW_PREFIX=Pathname.new '/tmp/testbrew/prefix' HOMEBREW_CACHE=HOMEBREW_PREFIX.parent+"cache" HOMEBREW_CELLAR=HOMEBREW_PREFIX.parent+"cellar" HOMEBREW_USER_AGENT="Homebrew" (HOMEBREW_PREFIX+'Library'+'Formula').mkpath Dir.chdir HOMEBREW_PREFIX at_exit { HOMEBREW_PREFIX.parent.rmtree } require 'test/unit' # must be after at_exit require 'ARGV+yeast' # needs to be after test/unit to avoid conflict with OptionsParser class MockFormula #{path}` assert_not_nil Formula.factory(FOOBAR) end def test_cant_override_brew assert_raises(RuntimeError) { TestBallOverrideBrew.new } 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_zip nostdout { assert_nothing_raised { TestZip.new.brew {} } } end def test_no_ARGV_dupes ARGV.unshift'foo' ARGV.unshift'foo' n=0 ARGV.named.each{|arg| n+=1 if arg == 'foo'} assert_equal 1, n end def test_ARGV assert_raises(UsageError) { ARGV.named } assert_raises(UsageError) { ARGV.formulae } assert_raises(UsageError) { ARGV.kegs } assert ARGV.named_empty? (HOMEBREW_CELLAR+'foo'+'0.1').mkpath ARGV.stick_an_arg_in_thar assert_equal 1, ARGV.named.length assert_equal 1, ARGV.kegs.length assert_raises(FormulaUnavailableError) { ARGV.formulae } end def test_version_dir d=HOMEBREW_CELLAR+'foo-0.1.9' d.mkpath assert_equal '0.1.9', d.version end def test_lame_version_style f=MockFormula.new 'http://kent.dl.sourceforge.net/sourceforge/lame/lame-398-2.tar.gz' assert_equal '398-2', f.version end def test_ruby_version_style f=MockFormula.new 'ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p243.tar.gz' assert_equal '1.9.1-p243', f.version end # these will raise if we don't recognise your mac, but that prolly # indicates something went wrong rather than we don't know def test_hardware_cpu_type assert [:intel, :ppc].include?(Hardware.cpu_type) end def test_hardware_intel_family if Hardware.cpu_type == :intel assert [:core, :core2, :penryn, :nehalem].include?(Hardware.intel_family) end end def test_brew_h nostdout do assert_nothing_raised do f=TestBall.new make 'http://example.com/testball-0.1.tbz' info f.name clean f prune #TODO test diy function too end end end def test_my_float_assumptions # this may look ridiculous but honestly there's code in brewit that depends on # this behaviour so I wanted to be certain Ruby floating points are behaving f='10.6'.to_f assert_equal 10.6, f assert f >= 10.6 assert f <= 10.6 assert_equal 10.5, f-0.1 assert_equal 10.7, f+0.1 end def test_arch_for_command # NOTE only works on Snow Leopard I bet, pick a better file! arches=arch_for_command '/usr/bin/svn' assert_equal 3, arches.count assert arches.include?(:x86_64) assert arches.include?(:i386) assert arches.include?(:ppc7400) end def test_pathname_plus_yeast nostdout do assert_nothing_raised do assert !Pathname.getwd.rmdir_if_possible assert !Pathname.getwd.abv.empty? abcd=orig_abcd=HOMEBREW_CACHE+'abcd' FileUtils.cp ABS__FILE__, abcd abcd=HOMEBREW_PREFIX.install abcd assert (HOMEBREW_PREFIX+orig_abcd.basename).exist? assert abcd.exist? assert_equal HOMEBREW_PREFIX+'abcd', abcd assert_raises(RuntimeError) {abcd.write 'CONTENT'} abcd.unlink abcd.write 'HELLOWORLD' assert_equal 'HELLOWORLD', File.read(abcd) assert !orig_abcd.exist? rv=abcd.cp orig_abcd assert orig_abcd.exist? assert_equal rv, orig_abcd orig_abcd.unlink assert !orig_abcd.exist? abcd.cp HOMEBREW_CACHE assert orig_abcd.exist? foo1=HOMEBREW_CACHE+'foo-0.1.tar.gz' FileUtils.cp ABS__FILE__, foo1 assert foo1.file? assert_equal '.tar.gz', foo1.extname assert_equal 'foo-0.1', foo1.stem assert_equal '0.1', foo1.version HOMEBREW_CACHE.chmod_R 0777 end end assert_raises(RuntimeError) {Pathname.getwd.install 'non_existant_file'} end def test_formula_class_func assert_equal Formula.class_s('s-lang'), 'SLang' assert_equal Formula.class_s('pkg-config'), 'PkgConfig' assert_equal Formula.class_s('foo_bar'), 'FooBar' end end