Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor of test helpers.
  • Loading branch information
tomlea committed Mar 17, 2012
1 parent 3db163b commit 35d4985
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 63 deletions.
53 changes: 50 additions & 3 deletions test/test_helper.rb
@@ -1,14 +1,61 @@
require "rubygems"
gem "bundler"
require "bundler/setup"

require 'geminabox'
require 'minitest/autorun'
require "geminabox"
require "geminabox_test_case"
require 'fileutils'
require 'test_support/gem_factory'
require 'test_support/geminabox_test_case'

module TestMethodMagic
def test(test_name, &block)
define_method "test: #{test_name} ", &block
end
end

MiniTest::Unit::TestCase.extend TestMethodMagic
class MiniTest::Unit::TestCase
extend TestMethodMagic

TEST_DATA_DIR="/tmp/geminabox-test-data"
def clean_data_dir
FileUtils.rm_rf(TEST_DATA_DIR)
FileUtils.mkdir(TEST_DATA_DIR)
Geminabox.data = TEST_DATA_DIR
end

def self.fixture(path)
File.join(File.expand_path("../fixtures", __FILE__), path)
end

def fixture(*args)
self.class.fixture(*args)
end


def silence_stream(stream)
old_stream = stream.dup
stream.reopen('/dev/null')
stream.sync = true
yield
ensure
stream.reopen(old_stream)
end

def silence
silence_stream(STDERR) do
silence_stream(STDOUT) do
yield
end
end
end

def inject_gems(&block)
silence do
yield GemFactory.new(File.join(Geminabox.data, "gems"))
Gem::Indexer.new(Geminabox.data).generate_index
end
end

end

54 changes: 54 additions & 0 deletions test/test_support/gem_factory.rb
@@ -0,0 +1,54 @@
class GemFactory
def self.gem_file(*args)
new("/tmp/geminabox-fixtures").gem(*args)
end

def initialize(path)
@path = Pathname.new(File.expand_path(path))
end

def gem(name, options = {})
version = options[:version] || "1.0.0"

dependincies = options.fetch(:deps, {}).collect do |dep, requirement|
dep = [*dep]
gem(*dep)
if requirement
"s.add_dependency(#{dep.first.to_s.inspect}, #{requirement.inspect})"
else
"s.add_dependency(#{dep.first.to_s.inspect})"
end
end.join("\n")

name = name.to_s
path = @path.join("#{name}-#{version}.gem")
FileUtils.mkdir_p File.dirname(path)

unless File.exists? path
spec = %{
Gem::Specification.new do |s|
s.name = #{name.inspect}
s.version = #{version.inspect}
s.summary = #{name.inspect}
s.description = s.summary + " description"
s.author = 'Test'
s.files = []
#{dependincies}
end
}

spec_file = Tempfile.open("spec") do |tmpfile|
tmpfile << spec
tmpfile.close

Dir.chdir File.dirname(path) do
system "gem build #{tmpfile.path}"
end
end

raise "Failed to build gem #{name}" unless File.exists? path
end
path
end

end
Expand Up @@ -103,27 +103,17 @@ def assert_can_push(gemname = :example, *args)
assert_match( /Gem .* received and indexed./, geminabox_push(gem_file(gemname, *args)))
end

def self.fixture(path)
File.join(File.expand_path("../fixtures", __FILE__), path)
end

def fixture(*args)
self.class.fixture(*args)
end

def find_free_port
server = TCPServer.new('127.0.0.1', 0)
port = server.addr[1]
server.close
port
end


def start_app!
clean_data_dir
@test_server_port = find_free_port

FileUtils.rm_rf("/tmp/geminabox-test-data")
FileUtils.mkdir("/tmp/geminabox-test-data")

server_options = {
:app => config.to_app,
:Port => @test_server_port,
Expand Down Expand Up @@ -168,54 +158,6 @@ def stop_app!
Process.kill(9, @app_server) if @app_server
end

module GemFactory
def gem_file(name, options = {})
version = options[:version] || "1.0.0"

dependincies = options.fetch(:deps, {}).collect do |dep, requirement|
dep = [*dep]
gem_file(*dep)
if requirement
"s.add_dependency(#{dep.first.to_s.inspect}, #{requirement.inspect})"
else
"s.add_dependency(#{dep.first.to_s.inspect})"
end
end.join("\n")

name = name.to_s
path = "/tmp/geminabox-fixtures/#{name}-#{version}.gem"
FileUtils.mkdir_p File.dirname(path)

unless File.exists? path
spec = %{
Gem::Specification.new do |s|
s.name = #{name.inspect}
s.version = #{version.inspect}
s.summary = #{name.inspect}
s.description = s.summary + " description"
s.author = 'Test'
s.files = []
#{dependincies}
end
}

spec_file = Tempfile.open("spec") do |tmpfile|
tmpfile << spec
tmpfile.close

Dir.chdir File.dirname(path) do
system "gem build #{tmpfile.path}"
end
end

raise "Failed to build gem #{name}" unless File.exists? path
end
path
end

extend self
end

def gem_file(*args)
GemFactory.gem_file(*args)
end
Expand Down

0 comments on commit 35d4985

Please sign in to comment.