summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_ext_builder.rb
blob: 3da9bc9e60e76b768e155340e6f69a6cb81c19b2 (plain)
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
require 'rubygems/test_case'
require 'rubygems/ext'
require 'rubygems/installer'

class TestGemExtBuilder < Gem::TestCase

  def setup
    super

    @ext = File.join @tempdir, 'ext'
    @dest_path = File.join @tempdir, 'prefix'

    FileUtils.mkdir_p @ext
    FileUtils.mkdir_p @dest_path

    @orig_DESTDIR = ENV['DESTDIR']

    @spec = util_spec 'a'

    @builder = Gem::Ext::Builder.new @spec, ''
  end

  def teardown
    ENV['DESTDIR'] = @orig_DESTDIR

    super
  end

  def test_class_make
    ENV['DESTDIR'] = 'destination'
    results = []

    Dir.chdir @ext do
      open 'Makefile', 'w' do |io|
        io.puts <<-MAKEFILE
all:
\t@#{Gem.ruby} -e "puts %Q{all: \#{ENV['DESTDIR']}}"

clean:
\t@#{Gem.ruby} -e "puts %Q{clean: \#{ENV['DESTDIR']}}"

install:
\t@#{Gem.ruby} -e "puts %Q{install: \#{ENV['DESTDIR']}}"
        MAKEFILE
      end

      Gem::Ext::Builder.make @dest_path, results
    end

    results = results.join "\n"

    if RUBY_VERSION > '2.0' then
      assert_match %r%"DESTDIR=#{ENV['DESTDIR']}" clean$%,   results
      assert_match %r%"DESTDIR=#{ENV['DESTDIR']}"$%,         results
      assert_match %r%"DESTDIR=#{ENV['DESTDIR']}" install$%, results
    else
      refute_match %r%"DESTDIR=#{ENV['DESTDIR']}" clean$%,   results
      refute_match %r%"DESTDIR=#{ENV['DESTDIR']}"$%,         results
      refute_match %r%"DESTDIR=#{ENV['DESTDIR']}" install$%, results
    end

    if /nmake/ !~ results
      assert_match %r%^clean: destination$%,   results
      assert_match %r%^all: destination$%,     results
      assert_match %r%^install: destination$%, results
    end
  end

  def test_class_make_no_clean
    ENV['DESTDIR'] = 'destination'
    results = []

    Dir.chdir @ext do
      open 'Makefile', 'w' do |io|
        io.puts <<-MAKEFILE
all:
\t@#{Gem.ruby} -e "puts %Q{all: \#{ENV['DESTDIR']}}"

install:
\t@#{Gem.ruby} -e "puts %Q{install: \#{ENV['DESTDIR']}}"
        MAKEFILE
      end

      Gem::Ext::Builder.make @dest_path, results
    end

    results = results.join "\n"

    if RUBY_VERSION > '2.0' then
      assert_match %r%"DESTDIR=#{ENV['DESTDIR']}" clean$%,   results
      assert_match %r%"DESTDIR=#{ENV['DESTDIR']}"$%,         results
      assert_match %r%"DESTDIR=#{ENV['DESTDIR']}" install$%, results
    else
      refute_match %r%"DESTDIR=#{ENV['DESTDIR']}" clean$%,   results
      refute_match %r%"DESTDIR=#{ENV['DESTDIR']}"$%,         results
      refute_match %r%"DESTDIR=#{ENV['DESTDIR']}" install$%, results
    end
  end

  def test_build_extensions
    @spec.extensions << 'ext/extconf.rb'

    ext_dir = File.join @spec.gem_dir, 'ext'

    FileUtils.mkdir_p ext_dir

    extconf_rb = File.join ext_dir, 'extconf.rb'

    open extconf_rb, 'w' do |f|
      f.write <<-'RUBY'
        require 'mkmf'

        create_makefile 'a'
      RUBY
    end

    ext_lib_dir = File.join ext_dir, 'lib'
    FileUtils.mkdir ext_lib_dir
    FileUtils.touch File.join ext_lib_dir, 'a.rb'

    use_ui @ui do
      @builder.build_extensions
    end

    assert_path_exists @spec.extension_install_dir
    assert_path_exists @spec.gem_build_complete_path
    assert_path_exists File.join @spec.extension_install_dir, 'gem_make.out'
    assert_path_exists File.join @spec.extension_install_dir, 'a.rb'
    assert_path_exists File.join @spec.gem_dir, 'lib', 'a.rb'
  end

  def test_build_extensions_none
    use_ui @ui do
      @builder.build_extensions
    end

    assert_equal '', @ui.output
    assert_equal '', @ui.error

    refute_path_exists File.join @spec.extension_install_dir, 'gem_make.out'
  end

  def test_build_extensions_rebuild_failure
    FileUtils.mkdir_p @spec.extension_install_dir
    FileUtils.touch @spec.gem_build_complete_path

    @spec.extensions << nil

    assert_raises Gem::Ext::BuildError do
      use_ui @ui do
        @builder.build_extensions
      end
    end

    refute_path_exists @spec.gem_build_complete_path
  end

  def test_build_extensions_extconf_bad
    @spec.extensions << 'extconf.rb'

    FileUtils.mkdir_p @spec.gem_dir

    e = assert_raises Gem::Ext::BuildError do
      use_ui @ui do
        @builder.build_extensions
      end
    end

    assert_match(/\AERROR: Failed to build gem native extension.$/, e.message)

    assert_equal "Building native extensions.  This could take a while...\n",
                 @ui.output
    assert_equal '', @ui.error

    gem_make_out = File.join @spec.extension_install_dir, 'gem_make.out'

    assert_match %r%#{Regexp.escape Gem.ruby} extconf\.rb%,
                 File.read(gem_make_out)
    assert_match %r%#{Regexp.escape Gem.ruby}: No such file%,
                 File.read(gem_make_out)

    refute_path_exists @spec.gem_build_complete_path
  end

  def test_build_extensions_unsupported
    FileUtils.mkdir_p @spec.gem_dir
    gem_make_out = File.join @spec.extension_install_dir, 'gem_make.out'
    @spec.extensions << nil

    e = assert_raises Gem::Ext::BuildError do
      use_ui @ui do
        @builder.build_extensions
      end
    end

    assert_match(/^\s*No builder for extension ''$/, e.message)

    assert_equal "Building native extensions.  This could take a while...\n",
                 @ui.output
    assert_equal '', @ui.error

    assert_equal "No builder for extension ''\n", File.read(gem_make_out)

    refute_path_exists @spec.gem_build_complete_path
  ensure
    FileUtils.rm_f gem_make_out
  end

  def test_build_extensions_with_build_args
    args = ["--aa", "--bb"]
    @builder.build_args = args
    @spec.extensions << 'extconf.rb'

    FileUtils.mkdir_p @spec.gem_dir

    open File.join(@spec.gem_dir, "extconf.rb"), "w" do |f|
      f.write <<-'RUBY'
        puts "IN EXTCONF"
        extconf_args = File.join File.dirname(__FILE__), 'extconf_args'
        File.open extconf_args, 'w' do |f|
          f.puts ARGV.inspect
        end

        File.open 'Makefile', 'w' do |f|
          f.puts "clean:\n\techo cleaned"
          f.puts "default:\n\techo built"
          f.puts "install:\n\techo installed"
        end
      RUBY
    end

    use_ui @ui do
      @builder.build_extensions
    end

    path = File.join @spec.gem_dir, "extconf_args"

    assert_equal args.inspect, File.read(path).strip
    assert_path_exists @spec.extension_install_dir
  end

  def test_initialize
    build_info_dir = File.join @gemhome, 'build_info'

    FileUtils.mkdir_p build_info_dir

    build_info_file = File.join build_info_dir, "#{@spec.full_name}.info"

    open build_info_file, 'w' do |io|
      io.puts '--with-foo-dir=/nonexistent'
    end

    builder = Gem::Ext::Builder.new @spec

    assert_equal %w[--with-foo-dir=/nonexistent], builder.build_args
  end

  def test_initialize_build_args
    builder = Gem::Ext::Builder.new @spec, %w[--with-foo-dir=/nonexistent]

    assert_equal %w[--with-foo-dir=/nonexistent], builder.build_args
  end

end