summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_stub_specification.rb
blob: 91a46d7842ece2f8908302a8b3ac6fbc1c5e0385 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# frozen_string_literal: true
require "rubygems/test_case"
require "rubygems/stub_specification"

class TestStubSpecification < Gem::TestCase

  FOO = File.join SPECIFICATIONS, "foo-0.0.1-x86-mswin32.gemspec"
  BAR = File.join SPECIFICATIONS, "bar-0.0.2.gemspec"

  def setup
    super

    @base_dir = File.dirname(SPECIFICATIONS)
    @gems_dir = File.join File.dirname(SPECIFICATIONS), 'gem'
    @foo = Gem::StubSpecification.gemspec_stub FOO, @base_dir, @gems_dir
  end

  def test_initialize
    assert_equal "foo", @foo.name
    assert_equal Gem::Version.new("0.0.1"), @foo.version
    assert_equal Gem::Platform.new("mswin32"), @foo.platform
    assert_equal ["lib", "lib/f oo/ext"], @foo.require_paths
    assert @foo.stubbed?
  end

  def test_initialize_extension
    stub = stub_with_extension

    assert_equal 'stub_e',                    stub.name
    assert_equal v(2),                        stub.version
    assert_equal Gem::Platform::RUBY,         stub.platform
    assert_equal [stub.extension_dir, 'lib'], stub.require_paths
    assert_equal %w[ext/stub_e/extconf.rb],   stub.extensions
  end

  def test_initialize_version
    stub = stub_with_version

    assert_equal 'stub_v',                    stub.name
    assert_equal v(2),                        stub.version
  end

  def test_initialize_with_empty_version
    stub = stub_without_version

    assert_equal 'stub_v',                    stub.name
    assert_equal v(0),                        stub.version
  end

  def test_initialize_missing_stubline
    stub = Gem::StubSpecification.gemspec_stub(BAR, @base_dir, @gems_dir)
    assert_equal "bar", stub.name
    assert_equal Gem::Version.new("0.0.2"), stub.version
    assert_equal Gem::Platform.new("ruby"), stub.platform
    assert_equal ["lib"], stub.require_paths
    assert !stub.stubbed?
  end

  def test_contains_requirable_file_eh
    stub = stub_without_extension
    code_rb = File.join stub.gem_dir, 'lib', 'code.rb'
    FileUtils.mkdir_p File.dirname code_rb
    FileUtils.touch code_rb

    assert stub.contains_requirable_file? 'code'
  end

  def test_contains_requirable_file_eh_extension
    skip "I guess making the stub match the running platform should work" if Gem.java_platform?
    stub_with_extension do |stub|
      _, err = capture_io do
        refute stub.contains_requirable_file? 'nonexistent'
      end

      expected = "Ignoring stub_e-2 because its extensions are not built. " +
                 "Try: gem pristine stub_e --version 2\n"

      assert_equal expected, err
    end
  end

  def test_full_require_paths
    stub = stub_with_extension

    expected = [
      File.join(stub.full_gem_path, 'lib'),
      stub.extension_dir,
    ]

    assert_equal expected, stub.full_require_paths
  end

  def test_lib_dirs_glob
    stub = stub_without_extension

    assert_equal File.join(stub.full_gem_path, 'lib'), stub.lib_dirs_glob
  end

  def test_lib_dirs_glob_with_extension
    stub = stub_with_extension

    assert_equal File.join(stub.full_gem_path, 'lib'), stub.lib_dirs_glob
  end

  def test_matches_for_glob
    stub = stub_without_extension
    code_rb = File.join stub.gem_dir, 'lib', 'code.rb'
    FileUtils.mkdir_p File.dirname code_rb
    FileUtils.touch code_rb

    assert_equal code_rb, stub.matches_for_glob('code*').first
  end

  def test_matches_for_glob_with_bundler_inline
    stub = stub_with_extension
    code_rb = File.join stub.gem_dir, 'lib', 'code.rb'
    FileUtils.mkdir_p File.dirname code_rb
    FileUtils.touch code_rb

    stub.stub(:raw_require_paths, nil) do
      assert_equal code_rb, stub.matches_for_glob('code*').first
    end
  end

  def test_missing_extensions_eh
    skip "I guess making the stub match the running platform should work" if Gem.java_platform?
    stub = stub_with_extension do |s|
      extconf_rb = File.join s.gem_dir, s.extensions.first
      FileUtils.mkdir_p File.dirname extconf_rb

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

    assert stub.missing_extensions?

    stub.build_extensions

    refute stub.missing_extensions?
  end

  def test_missing_extensions_eh_default_gem
    spec = new_default_spec 'default', 1
    spec.extensions << 'extconf.rb'

    File.open spec.loaded_from, 'w' do |io|
      io.write spec.to_ruby_for_cache
    end

    default_spec = Gem::StubSpecification.gemspec_stub spec.loaded_from, spec.base_dir, spec.gems_dir

    refute default_spec.missing_extensions?
  end

  def test_missing_extensions_eh_none
    refute @foo.missing_extensions?
  end

  def test_to_spec
    real_foo = util_spec @foo.name, @foo.version
    real_foo.activate

    assert_equal @foo.version, Gem.loaded_specs[@foo.name].version,
                 'sanity check'

    assert_same real_foo, @foo.to_spec
  end

  def test_to_spec_with_other_specs_loaded_does_not_warn
    real_foo = util_spec @foo.name, @foo.version
    real_foo.activate
    bar = Gem::StubSpecification.gemspec_stub BAR, real_foo.base_dir, real_foo.gems_dir
    refute_predicate Gem.loaded_specs, :empty?
    assert bar.to_spec
  end

  def test_to_spec_activated
    assert @foo.to_spec.is_a?(Gem::Specification)
    assert_equal "foo", @foo.to_spec.name
    refute @foo.to_spec.instance_variable_get :@ignored
  end

  def test_to_spec_missing_extensions
    stub = stub_with_extension

    capture_io do
      stub.contains_requirable_file? 'nonexistent'
    end

    assert stub.to_spec.instance_variable_get :@ignored
  end

  def stub_with_version
    spec = File.join @gemhome, 'specifications', 'stub_e-2.gemspec'
    File.open spec, 'w' do |io|
      io.write <<-STUB
# -*- encoding: utf-8 -*-
# stub: stub_v 2 ruby lib

Gem::Specification.new do |s|
  s.name = 'stub_v'
  s.version = Gem::Version.new '2'
end
      STUB

      io.flush

      stub = Gem::StubSpecification.gemspec_stub io.path, @gemhome, File.join(@gemhome, 'gems')

      yield stub if block_given?

      return stub
    end
  end

  def stub_without_version
    spec = File.join @gemhome, 'specifications', 'stub-2.gemspec'
    File.open spec, 'w' do |io|
      io.write <<-STUB
# -*- encoding: utf-8 -*-
# stub: stub_v ruby lib

Gem::Specification.new do |s|
  s.name = 'stub_v'
  s.version = ""
end
      STUB

      io.flush

      stub = Gem::StubSpecification.gemspec_stub io.path, @gemhome, File.join(@gemhome, 'gems')

      yield stub if block_given?

      return stub
    end
  end

  def stub_with_extension
    spec = File.join @gemhome, 'specifications', 'stub_e-2.gemspec'
    File.open spec, 'w' do |io|
      io.write <<-STUB
# -*- encoding: utf-8 -*-
# stub: stub_e 2 ruby lib
# stub: ext/stub_e/extconf.rb

Gem::Specification.new do |s|
  s.name = 'stub_e'
  s.version = Gem::Version.new '2'
  s.extensions = ['ext/stub_e/extconf.rb']
  s.installed_by_version = '2.2'
end
      STUB

      io.flush

      stub = Gem::StubSpecification.gemspec_stub io.path, @gemhome, File.join(@gemhome, 'gems')

      yield stub if block_given?

      return stub
    end
  end

  def stub_without_extension
    spec = File.join @gemhome, 'specifications', 'stub-2.gemspec'
    File.open spec, 'w' do |io|
      io.write <<-STUB
# -*- encoding: utf-8 -*-
# stub: stub 2 ruby lib

Gem::Specification.new do |s|
  s.name = 'stub'
  s.version = Gem::Version.new '2'
end
      STUB

      io.flush

      stub = Gem::StubSpecification.gemspec_stub io.path, @gemhome, File.join(@gemhome, 'gems')

      yield stub if block_given?

      return stub
    end
  end

end