module Gem::Mirror::TestSetup

Provide assistance for authors of code that utilises Gem::Mirror. The module defines several setup and teardown methods that can be used to provide a new gem source on disk.

Attributes

mirror_path[R]

A temporary directory where mirrors might put their data

mirrorrc[R]

An rcfile pointing at the source and mirror paths

mirrorrc_path[R]

Path to the mock, temporary mirrorrc

source_gems[R]

A list of gem names in the source_path

source_path[R]

The path in which setup_gem_source will place a RubyGems source index

ui[R]

An instance of TestSetup::UI, a mock UI for RubyGems.

Public Instance Methods

setup_gem_source() click to toggle source

Setup a new gem source directory containing a few gems suitable for testing mirrors, and place the path to that in source_path.

# File lib/rubygems/mirror/test_setup.rb, line 64
def setup_gem_source
  @source_path = Dir.mktmpdir("test_gem_source_path_#{$$}")

  Dir.mkdir gemdir = File.join(@source_path, 'gems')

  @source_working = working = Dir.mktmpdir("test_gem_source_#{$$}")
  FileUtils.mkdir_p rzspecdir = File.join(@source_path, "quick/Marshal.#{Gem.marshal_version}")

  Dir.mkdir File.join(working, 'lib')

  gemspecs = %w[a b c].map do |name|
    FileUtils.touch File.join(working, 'lib', "#{name}.rb")
    FileUtils.touch File.join(rzspecdir, "#{name}spec.rz")
    Gem::Specification.new do |s|
      s.platform = Gem::Platform::RUBY
      s.name = name
      s.version = 1.0
      s.author = 'rubygems'
      s.email = 'example@example.com'
      s.homepage = 'http://example.com'
      s.has_rdoc = false
      s.description = 'desc'
      s.summary = "summ"
      s.require_paths = %w[lib]
      s.files = %W[lib/#{name}.rb]
      s.rubyforge_project = 'rubygems'
    end
  end

  # add prerelease gem versions
  prerelease_gemspecs = %w[x y].map do |name|
    FileUtils.touch File.join(working, 'lib', "#{name}.rb")
    FileUtils.touch File.join(rzspecdir, "#{name}spec.rz")
    Gem::Specification.new do |s|
      s.platform = Gem::Platform::RUBY
      s.name = name
      s.version = "0.1.b"
      s.author = 'rubygems'
      s.email = 'example@example.com'
      s.homepage = 'http://example.com'
      s.has_rdoc = false
      s.description = 'desc'
      s.summary = "summ"
      s.require_paths = %w[lib]
      s.files = %W[lib/#{name}.rb]
      s.rubyforge_project = 'rubygems'
    end
  end

  gemspecs.concat(prerelease_gemspecs)

  gemspecs.each do |spec|
    path = File.join(working, "#{spec.name}.gemspec")
    open(path, 'w') do |io|
      io.write(spec.to_ruby)
    end
    Dir.chdir(working) do
      gem_file = Gem::Package.build(spec)
      FileUtils.mv(gem_file, File.join(@source_path, 'gems', gem_file))
    end
  end

  @source_gems = Dir[File.join(gemdir, '*.gem')].map {|p|File.basename(p)}

  Gem::Indexer.new(@source_path).generate_index
end
setup_mirrorrc() click to toggle source

Setup a new mirrorrc for Gem::Mirror based on a setup from setup_gem_source.

# File lib/rubygems/mirror/test_setup.rb, line 140
def setup_mirrorrc
  @mirror_path = Dir.mktmpdir("test_gem_mirror_path_#{$$}")
  @mirrorrc = Tempfile.new('testgemmirrorrc')
  opts = {
    'mirrors' => {
      'from' => "http://127.0.0.1:8808/", 'to' => @mirror_path
    }
  }
  @mirrorrc.write YAML.dump(opts)
  @mirrorrc_path = @mirrorrc.path
end
setup_server() click to toggle source

Starts a server using Rack that will host the gem source

# File lib/rubygems/mirror/test_setup.rb, line 159
def setup_server
  opts = { :Port => 8808, :DocumentRoot => @source_path }
  unless $DEBUG
    require 'logger'
    opts[:Logger] = Logger.new('/dev/null')
    opts[:AccessLog] = Logger.new('/dev/null')
  end
  @server = WEBrick::HTTPServer.new opts
  @server_thread = Thread.new { @server.start }
  @server_thread.join(0.1) # pickup early errors and give it time to start
end
setup_ui() click to toggle source

Setup a new mock UI using TestSetup::UI

# File lib/rubygems/mirror/test_setup.rb, line 52
def setup_ui
  @old_ui = Gem::DefaultUserInteraction.ui
  @ui = Gem::DefaultUserInteraction.ui = UI.new
end
teardown_gem_source() click to toggle source

Cleanup temporary directories that are created by setup_gem_source.

# File lib/rubygems/mirror/test_setup.rb, line 132
def teardown_gem_source
  [@source_path, @source_working].each do |path|
    FileUtils.rm_rf path
  end
end
teardown_mirrorrc() click to toggle source

Cleanup tempfiles created by setup_mirrorrc.

# File lib/rubygems/mirror/test_setup.rb, line 153
def teardown_mirrorrc
  FileUtils.rm_rf @mirrorrc_path if @mirrorrc_path
  FileUtils.rm_rf @mirror_path if @mirror_path
end
teardown_server() click to toggle source

Shutdown the local server hosting the source_path

# File lib/rubygems/mirror/test_setup.rb, line 172
def teardown_server
  @server && @server.shutdown
  @server_thread && @server_thread.join
end
teardown_ui() click to toggle source

Restore RubyGems default UI

# File lib/rubygems/mirror/test_setup.rb, line 58
def teardown_ui
  Gem::DefaultUserInteraction.ui = @old_ui if @old_ui
end
with_server() { || ... } click to toggle source
# File lib/rubygems/mirror/test_setup.rb, line 177
def with_server
  setup_ui
  setup_gem_source
  setup_mirrorrc
  setup_server
  yield
ensure
  teardown_gem_source
  teardown_server
  teardown_mirrorrc
  teardown_ui
end