1# coding: UTF-8
2
3require 'rubygems/test_case'
4require 'rubygems/ext'
5
6class TestGemExtExtConfBuilder < Gem::TestCase
7
8  def setup
9    super
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_class_build
19    if vc_windows? && !nmake_found?
20      skip("test_class_build skipped - nmake not found")
21    end
22
23    File.open File.join(@ext, 'extconf.rb'), 'w' do |extconf|
24      extconf.puts "require 'mkmf'\ncreate_makefile 'foo'"
25    end
26
27    output = []
28
29    Dir.chdir @ext do
30      result =
31        Gem::Ext::ExtConfBuilder.build 'extconf.rb', nil, @dest_path, output
32
33      assert_same result, output
34    end
35
36    assert_match(/^#{Gem.ruby} extconf.rb/, output[0])
37    assert_equal "creating Makefile\n", output[1]
38    assert_contains_make_command '', output[2]
39    assert_contains_make_command 'install', output[4]
40    assert_empty Dir.glob(File.join(@ext, 'siteconf*.rb'))
41  end
42
43  def test_class_build_rbconfig_make_prog
44    configure_args = RbConfig::CONFIG['configure_args']
45
46    File.open File.join(@ext, 'extconf.rb'), 'w' do |extconf|
47      extconf.puts "require 'mkmf'\ncreate_makefile 'foo'"
48    end
49
50    output = []
51
52    Dir.chdir @ext do
53      Gem::Ext::ExtConfBuilder.build 'extconf.rb', nil, @dest_path, output
54    end
55
56    assert_equal "creating Makefile\n", output[1]
57    assert_contains_make_command '', output[2]
58    assert_contains_make_command 'install', output[4]
59  ensure
60    RbConfig::CONFIG['configure_args'] = configure_args
61  end
62
63  def test_class_build_env_make
64    configure_args, env_make = RbConfig::CONFIG['configure_args'], ENV.delete('make')
65    RbConfig::CONFIG['configure_args'] = ''
66    ENV['make'] = 'anothermake'
67
68    File.open File.join(@ext, 'extconf.rb'), 'w' do |extconf|
69      extconf.puts "require 'mkmf'\ncreate_makefile 'foo'"
70    end
71
72    output = []
73
74    assert_raises Gem::InstallError do
75      Dir.chdir @ext do
76        Gem::Ext::ExtConfBuilder.build 'extconf.rb', nil, @dest_path, output
77      end
78    end
79
80    assert_equal "creating Makefile\n", output[1]
81    assert_contains_make_command '', output[2]
82  ensure
83    RbConfig::CONFIG['configure_args'] = configure_args
84    ENV['make'] = env_make
85  end
86
87  def test_class_build_extconf_fail
88    if vc_windows? && !nmake_found?
89      skip("test_class_build_extconf_fail skipped - nmake not found")
90    end
91
92    File.open File.join(@ext, 'extconf.rb'), 'w' do |extconf|
93      extconf.puts "require 'mkmf'"
94      extconf.puts "have_library 'nonexistent' or abort 'need libnonexistent'"
95      extconf.puts "create_makefile 'foo'"
96    end
97
98    output = []
99
100    error = assert_raises Gem::InstallError do
101      Dir.chdir @ext do
102        Gem::Ext::ExtConfBuilder.build 'extconf.rb', nil, @dest_path, output
103      end
104    end
105
106    assert_match(/\Aextconf failed:
107
108#{Gem.ruby} extconf.rb.*
109checking for main\(\) in .*?nonexistent/m, error.message)
110
111    assert_equal("#{Gem.ruby} extconf.rb", output[0])
112  end
113
114  def test_class_build_unconventional
115    if vc_windows? && !nmake_found?
116      skip("test_class_build skipped - nmake not found")
117    end
118
119    File.open File.join(@ext, 'extconf.rb'), 'w' do |extconf|
120      extconf.puts <<-'EXTCONF'
121include RbConfig
122
123ruby_exe = "#{CONFIG['RUBY_INSTALL_NAME']}#{CONFIG['EXEEXT']}"
124ruby = File.join CONFIG['bindir'], ruby_exe
125
126open 'Makefile', 'w' do |io|
127  io.write <<-Makefile
128all: ruby
129install: ruby
130
131ruby:
132\t#{ruby} -e0
133
134  Makefile
135end
136      EXTCONF
137    end
138
139    output = []
140
141    Dir.chdir @ext do
142      Gem::Ext::ExtConfBuilder.build 'extconf.rb', nil, @dest_path, output
143    end
144
145    assert_contains_make_command '', output[2]
146    assert_contains_make_command 'install', output[4]
147    assert_empty Dir.glob(File.join(@ext, 'siteconf*.rb'))
148  end
149
150  def test_class_make
151    if vc_windows? && !nmake_found?
152      skip("test_class_make skipped - nmake not found")
153    end
154
155    output = []
156    makefile_path = File.join(@ext, 'Makefile')
157    File.open makefile_path, 'w' do |makefile|
158      makefile.puts "# ��"
159      makefile.puts "RUBYARCHDIR = $(foo)$(target_prefix)"
160      makefile.puts "RUBYLIBDIR = $(bar)$(target_prefix)"
161      makefile.puts "all:"
162      makefile.puts "install:"
163    end
164
165    Dir.chdir @ext do
166      Gem::Ext::ExtConfBuilder.make @ext, output
167    end
168
169    assert_contains_make_command '', output[0]
170    assert_contains_make_command 'install', output[2]
171  end
172
173  def test_class_make_no_Makefile
174    error = assert_raises Gem::InstallError do
175      Dir.chdir @ext do
176        Gem::Ext::ExtConfBuilder.make @ext, ['output']
177      end
178    end
179
180    expected = <<-EOF.strip
181Makefile not found:
182
183output
184    EOF
185
186    assert_equal expected, error.message
187  end
188
189end
190
191