From 35d4985c18b067b09665f57061f74a5b06e48621 Mon Sep 17 00:00:00 2001 From: Tom Lea Date: Sat, 17 Mar 2012 13:36:18 +0000 Subject: [PATCH] Refactor of test helpers. --- test/test_helper.rb | 53 +++++++++++++++- test/test_support/gem_factory.rb | 54 ++++++++++++++++ .../{ => test_support}/geminabox_test_case.rb | 62 +------------------ 3 files changed, 106 insertions(+), 63 deletions(-) create mode 100644 test/test_support/gem_factory.rb rename test/{ => test_support}/geminabox_test_case.rb (69%) diff --git a/test/test_helper.rb b/test/test_helper.rb index 22ce75b3..2ff49ef6 100644 --- a/test/test_helper.rb +++ b/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 + diff --git a/test/test_support/gem_factory.rb b/test/test_support/gem_factory.rb new file mode 100644 index 00000000..5f478a20 --- /dev/null +++ b/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 diff --git a/test/geminabox_test_case.rb b/test/test_support/geminabox_test_case.rb similarity index 69% rename from test/geminabox_test_case.rb rename to test/test_support/geminabox_test_case.rb index febb5f9f..87333f49 100644 --- a/test/geminabox_test_case.rb +++ b/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