Class: Gem::TestCase::SpecFetcherSetup

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems/test_utilities.rb

Overview

The SpecFetcherSetup allows easy setup of a remote source in RubyGems tests:

spec_fetcher do |f|
  f.gem  'a', 1
  f.spec 'a', 2
  f.gem  'b', 1' 'a' => '~> 1.0'
  f.clear
end

The above declaration creates two gems, a-1 and b-1, with a dependency from b to a. The declaration creates an additional spec a-2, but no gem for it (so it cannot be installed).

After the gems are created they are removed from Gem.dir.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test, repository) ⇒ SpecFetcherSetup

:nodoc:



210
211
212
213
214
215
216
217
# File 'lib/rubygems/test_utilities.rb', line 210

def initialize test, repository # :nodoc:
  @test       = test
  @repository = repository

  @gems       = {}
  @installed  = []
  @operations = []
end

Class Method Details

.declare(test, repository) {|setup| ... } ⇒ Object

Executes a SpecFetcher setup block. Yields an instance then creates the gems and specifications defined in the instance.

Yields:



202
203
204
205
206
207
208
# File 'lib/rubygems/test_utilities.rb', line 202

def self.declare test, repository
  setup = new test, repository

  yield setup

  setup.execute
end

Instance Method Details

#clearObject

Removes any created gems or specifications from Gem.dir (the default install location).



223
224
225
# File 'lib/rubygems/test_utilities.rb', line 223

def clear
  @operations << [:clear]
end

#created_specsObject

Returns a Hash of created Specification full names and the corresponding Specification.



231
232
233
234
235
236
237
238
239
# File 'lib/rubygems/test_utilities.rb', line 231

def created_specs
  created = {}

  @gems.keys.each do |spec|
    created[spec.full_name] = spec
  end

  created
end

#executeObject

Creates any defined gems or specifications



244
245
246
247
248
249
250
# File 'lib/rubygems/test_utilities.rb', line 244

def execute # :nodoc:
  execute_operations

  setup_fetcher

  created_specs
end

#execute_operationsObject

:nodoc:



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/rubygems/test_utilities.rb', line 252

def execute_operations # :nodoc:
  @operations.each do |operation, *arguments|
    case operation
    when :clear then
      @test.util_clear_gems
      @installed.clear
    when :gem then
      spec, gem = @test.util_gem(*arguments, &arguments.pop)

      write_spec spec

      @gems[spec] = gem
      @installed << spec
    when :spec then
      spec = @test.util_spec(*arguments, &arguments.pop)

      write_spec spec

      @gems[spec] = nil
      @installed << spec
    end
  end
end

#gem(name, version, dependencies = nil, &block) ⇒ Object

Creates a gem with name, version and deps. The created gem can be downloaded and installed.

The specification will be yielded before gem creation for customization, but only the block or the dependencies may be set, not both.



283
284
285
# File 'lib/rubygems/test_utilities.rb', line 283

def gem name, version, dependencies = nil, &block
  @operations << [:gem, name, version, dependencies, block]
end

#legacy_platformObject

Creates a legacy platform spec with the name ‘pl’ and version 1



290
291
292
293
294
295
# File 'lib/rubygems/test_utilities.rb', line 290

def legacy_platform
  spec 'pl', 1 do |s|
    s.platform = Gem::Platform.new 'i386-linux'
    s.instance_variable_set :@original_platform, 'i386-linux'
  end
end

#setup_fetcherObject

:nodoc:



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/rubygems/test_utilities.rb', line 297

def setup_fetcher # :nodoc:
  require 'zlib'
  require 'socket'
  require 'rubygems/remote_fetcher'

  unless @test.fetcher then
    @test.fetcher = Gem::FakeFetcher.new
    Gem::RemoteFetcher.fetcher = @test.fetcher
  end

  Gem::Specification.reset

  begin
    gem_repo, @test.gem_repo = @test.gem_repo, @repository
    @test.uri = URI @repository

    @test.util_setup_spec_fetcher(*@gems.keys)
  ensure
    @test.gem_repo = gem_repo
    @test.uri = URI gem_repo
  end

  # This works around util_setup_spec_fetcher adding all created gems to the
  # installed set.
  Gem::Specification.reset
  Gem::Specification.add_specs(*@installed)

  @gems.each do |spec, gem|
    next unless gem

    @test.fetcher.data["#{@repository}gems/#{spec.file_name}"] =
      Gem.read_binary(gem)

    FileUtils.cp gem, spec.cache_file
  end
end

#spec(name, version, dependencies = nil, &block) ⇒ Object

Creates a spec with name, version and deps. The created gem can be downloaded and installed.

The specification will be yielded before creation for customization, but only the block or the dependencies may be set, not both.



341
342
343
# File 'lib/rubygems/test_utilities.rb', line 341

def spec name, version, dependencies = nil, &block
  @operations << [:spec, name, version, dependencies, block]
end

#write_spec(spec) ⇒ Object

:nodoc:



345
346
347
348
349
# File 'lib/rubygems/test_utilities.rb', line 345

def write_spec spec # :nodoc:
  open spec.spec_file, 'w' do |io|
    io.write spec.to_ruby_for_cache
  end
end