1require 'rubygems/test_case'
2require 'rubygems/ext'
3
4class TestGemExtRakeBuilder < Gem::TestCase
5  def setup
6    super
7
8    @ext = File.join @tempdir, 'ext'
9    @dest_path = File.join @tempdir, 'prefix'
10
11    FileUtils.mkdir_p @ext
12    FileUtils.mkdir_p @dest_path
13  end
14
15  def test_class_build
16    File.open File.join(@ext, 'mkrf_conf.rb'), 'w' do |mkrf_conf|
17      mkrf_conf.puts <<-EO_MKRF
18        File.open("Rakefile","w") do |f|
19          f.puts "task :default"
20        end
21      EO_MKRF
22    end
23
24    output = []
25    realdir = nil # HACK /tmp vs. /private/tmp
26
27    build_rake_in do |rake|
28      Dir.chdir @ext do
29        realdir = Dir.pwd
30        Gem::Ext::RakeBuilder.build 'mkrf_conf.rb', nil, @dest_path, output
31      end
32
33      output = output.join "\n"
34
35      refute_match %r%^rake failed:%, output
36      assert_match %r%^#{Regexp.escape @@ruby} mkrf_conf\.rb%, output
37      assert_match %r%^#{Regexp.escape rake} RUBYARCHDIR=#{Regexp.escape @dest_path} RUBYLIBDIR=#{Regexp.escape @dest_path}%, output
38    end
39  end
40
41  def test_class_build_fail
42    File.open File.join(@ext, 'mkrf_conf.rb'), 'w' do |mkrf_conf|
43      mkrf_conf.puts <<-EO_MKRF
44        File.open("Rakefile","w") do |f|
45          f.puts "task :default do abort 'fail' end"
46        end
47      EO_MKRF
48    end
49
50    output = []
51
52    build_rake_in(false) do |rake|
53      error = assert_raises Gem::InstallError do
54        Dir.chdir @ext do
55          Gem::Ext::RakeBuilder.build "mkrf_conf.rb", nil, @dest_path, output
56        end
57      end
58
59      assert_match %r%^rake failed:%, error.message
60      assert_match %r%^#{Regexp.escape @@ruby} mkrf_conf\.rb%, error.message
61      assert_match %r%^#{Regexp.escape rake} RUBYARCHDIR=#{Regexp.escape @dest_path} RUBYLIBDIR=#{Regexp.escape @dest_path}%, error.message
62    end
63  end
64
65end
66
67