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
A temporary directory where mirrors might put their data
An rcfile pointing at the source and mirror paths
Path to the mock, temporary mirrorrc
A list of gem names in the source_path
The path in which setup_gem_source
will place a RubyGems source index
An instance of TestSetup::UI
, a mock UI
for RubyGems.
Public Instance Methods
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.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.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 a new mirrorrc for Gem::Mirror
based on a setup from setup_gem_source.
# File lib/rubygems/mirror/test_setup.rb, line 138 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
Starts a server using Rack that will host the gem source
# File lib/rubygems/mirror/test_setup.rb, line 157 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 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
Cleanup temporary directories that are created by setup_gem_source.
# File lib/rubygems/mirror/test_setup.rb, line 130 def teardown_gem_source [@source_path, @source_working].each do |path| FileUtils.rm_rf path end end
Cleanup tempfiles created by setup_mirrorrc.
# File lib/rubygems/mirror/test_setup.rb, line 151 def teardown_mirrorrc FileUtils.rm_rf @mirrorrc_path if @mirrorrc_path FileUtils.rm_rf @mirror_path if @mirror_path end
Shutdown the local server hosting the source_path
# File lib/rubygems/mirror/test_setup.rb, line 170 def teardown_server @server && @server.shutdown @server_thread && @server_thread.join end
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
# File lib/rubygems/mirror/test_setup.rb, line 175 def with_server setup_ui setup_gem_source setup_mirrorrc setup_server yield ensure teardown_gem_source teardown_server teardown_mirrorrc teardown_ui end