Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor of test helpers.

  • Loading branch information...
commit 35d4985c18b067b09665f57061f74a5b06e48621 1 parent 3db163b
@cwninja cwninja authored
View
53 test/test_helper.rb
@@ -1,9 +1,12 @@
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)
@@ -11,4 +14,48 @@ def 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
+
View
54 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
View
62 test/geminabox_test_case.rb → test/test_support/geminabox_test_case.rb
@@ -103,14 +103,6 @@ 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]
@@ -118,12 +110,10 @@ def find_free_port
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,
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.