1require 'rubygems/test_case'
2require 'rubygems/ext'
3
4class TestGemExtConfigureBuilder < Gem::TestCase
5
6  def setup
7    super
8
9    @makefile_body =  "all:\n\t@echo ok\ninstall:\n\t@echo ok"
10
11    @ext = File.join @tempdir, 'ext'
12    @dest_path = File.join @tempdir, 'prefix'
13
14    FileUtils.mkdir_p @ext
15    FileUtils.mkdir_p @dest_path
16  end
17
18  def test_self_build
19    skip("test_self_build skipped on MS Windows (VC++)") if vc_windows?
20
21    File.open File.join(@ext, './configure'), 'w' do |configure|
22      configure.puts "#!/bin/sh\necho \"#{@makefile_body}\" > Makefile"
23    end
24
25    output = []
26
27    Dir.chdir @ext do
28      Gem::Ext::ConfigureBuilder.build nil, nil, @dest_path, output
29    end
30
31    assert_equal "sh ./configure --prefix=#{@dest_path}", output.shift
32    assert_equal "", output.shift
33    assert_contains_make_command '', output.shift
34    assert_match(/^ok$/m, output.shift)
35    assert_contains_make_command 'install', output.shift
36    assert_match(/^ok$/m, output.shift)
37  end
38
39  def test_self_build_fail
40    skip("test_self_build_fail skipped on MS Windows (VC++)") if vc_windows?
41    output = []
42
43    error = assert_raises Gem::InstallError do
44      Dir.chdir @ext do
45        Gem::Ext::ConfigureBuilder.build nil, nil, @dest_path, output
46      end
47    end
48
49    shell_error_msg = %r{(\./configure: .*)|((?:Can't|cannot) open \./configure(?:: No such file or directory)?)}
50    sh_prefix_configure = "sh ./configure --prefix="
51
52    expected = %r(configure failed:
53
54#{Regexp.escape sh_prefix_configure}#{Regexp.escape @dest_path}
55(?:.*?: )?#{shell_error_msg}
56)
57
58    assert_match expected, error.message
59
60    assert_equal "#{sh_prefix_configure}#{@dest_path}", output.shift
61    assert_match %r(#{shell_error_msg}), output.shift
62    assert_equal true, output.empty?
63  end
64
65  def test_self_build_has_makefile
66    if vc_windows? && !nmake_found?
67      skip("test_self_build_has_makefile skipped - nmake not found")
68    end
69
70    File.open File.join(@ext, 'Makefile'), 'w' do |makefile|
71      makefile.puts @makefile_body
72    end
73
74    output = []
75    Dir.chdir @ext do
76      Gem::Ext::ConfigureBuilder.build nil, nil, @dest_path, output
77    end
78
79    assert_contains_make_command '', output[0]
80    assert_contains_make_command 'install', output[2]
81  end
82
83end
84
85