Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Refactor of test helpers.
- Loading branch information
Showing
3 changed files
with
106 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters