Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

specs + make it work

  • Loading branch information...
commit ad5ee0830d4127fae1cd1ecc8e2066db86648acd 1 parent 469c252
@rkh authored
View
1  .gitignore
@@ -1,2 +1,3 @@
spec/.gems
spec/.gems.cached
+spec/.gem_tools
View
1  gem_tools.gemspec
@@ -7,7 +7,6 @@ Gem::Specification.new do |s|
s.description = "Extends Gem::Specification"
# Dependencies
- s.add_dependency "monkey-lib", "~> 0.4"
s.add_development_dependency "rspec", ">= 1.3.0"
# Those should be about the same in any BigBand extension.
View
40 lib/gem_tools.rb
@@ -42,21 +42,20 @@
# WITH THE SOFTWARE.
module GemTools
require 'rubygems/specification'
- require 'monkey-lib'
+ require 'fileutils'
VERSION = "0.1.0"
- # Generated file that will ship with your code if you use `run_code`.
- FILE = '.gem_tools.rb'
+ # Generated directory for extconf.rb that will ship with your code if you use `run_code`.
+ EXTDIR = '.gem_tools'
- # This code will be paced in GemTools::FILE and hooked into the gemspec
- # as additional extension (like extconf.rb). Will only be generated if you
- # use `run_code`.
- CODE = <<-RUBY.gsub /^\s+/, ''
+ # This code will be placed in GemTools::EXTDIR and hooked into the gemspec
+ # as additional extension. Will only be generated if you use `run_code`.
+ CODE = <<-RUBY.gsub /^\s+/, ''
require 'rubygems'
require 'gem_tools'
File.open('Makefile', 'w') { |f| f.puts 'all:', 'install:' }
- Gem::Specification.load("%s").run_code!
+ Gem::Specification.load("../%s").run_code!
RUBY
module ClassMethods
@@ -68,15 +67,17 @@ module ClassMethods
def new(name = nil, version = nil, &block)
file = caller.first[/^[^:]+/]
name ||= File.basename file, '.gemspec'
- super(file, version) do |s|
+ super(name, version) do |s|
s.extend GemTools::InstanceMethods
- s.instance_yield block if block
+ block.arity > 0 ? yield(s) : s.instance_eval(&block) if block
s.setup_hook file
end
end
end
module InstanceMethods
+ include FileUtils
+
##
# Sets up github url.
#
@@ -103,9 +104,11 @@ module InstanceMethods
# @param [#to_s] user Github user name.
# @param [#to_s] project Project name (defaults to gem name or 'xxx'-part of gem name if it matches user-xxx).
def github(user, project = nil)
- homepage "http://github.com/#{user}/#{project || name.sub(/^#{user}-/, '')}"
+ self.homepage = "http://github.com/#{user}/#{project || name.sub(/^#{user}-/, '')}"
end
+ alias github= github
+
##
# Allowes to execute some ruby code after installing gem.
#
@@ -128,9 +131,11 @@ def run_code
# @return [TrueClass, FalseClass] Does the gemspec ship with custom run_code blocks?
# @see run_code
def run_code?
- run_code.empty?
+ !run_code.empty?
end
+ alias running_code? run_code?
+
##
# Executes blocks passed to run_code
# @example
@@ -145,7 +150,7 @@ def run_code!
# @see setup_hook
# @api private
def setup_hook?
- @setup_hook or !run_code?
+ @setup_hook or !run_code? or name == 'gem_tools'
end
##
@@ -154,9 +159,12 @@ def setup_hook?
# @api private
def setup_hook!(file)
@setup_hook = true
- File.open(GemTools::FILE, 'w') { f << (CODE % file) }
- (files << file << GemTools::FILE).uniq!
- (extensions << GemTools::FILE).uniq!
+ rm_rf GemTools::EXTDIR
+ mkdir_p GemTools::EXTDIR
+ extfile = File.join(GemTools::EXTDIR, 'extconf.rb')
+ File.open(extfile, 'w') { |f| f << (CODE % file) }
+ (files << file << extfile).uniq!
+ (extensions << extfile).uniq!
add_dependency("gem_tools", "~> #{GemTools::VERSION}")
end
View
8 spec/gem-tools-example-run-code.gemspec
@@ -1,4 +1,8 @@
require 'gem_tools'
-Gem::Specification.new 'gem-tools-example-run-code', '1.0' do
-end
+Gem::Specification.new do |s|
+ s.name = 'gem-tools-example-run-code'
+ s.version = '1.0'
+ s.summary = 'foo'
+ s.run_code { puts 'w00t' }
+end
View
76 spec/gem_tools_spec.rb
@@ -5,8 +5,7 @@
describe GemTools do
include FileUtils
- GEM_DIR = '.gems'
- CACHED = "#{GEM_DIR}.cached"
+ GEM_DIR = File.expand_path '.gems'
class Proxy < Object
def initialize(&block) @block = block end
@@ -18,26 +17,71 @@ def gems(*args)
`ruby -rubygems -I../lib -S gem #{args.join ' '} 2>&1`
end
- def set_gem_dir(dir, *args)
- mkdir_p dir
- ENV['GEM_HOME'] = dir
- ENV['GEM_PATH'] = dir
- unless @fresh
- gems.build "../gem_tools*.gemspec"
- gems.install("*.gem", *args)
- @fresh = true
- end
+ def install(name)
+ gems.build "*-#{name}.gemspec"
+ gems.install "*-#{name}-*.gem", '--local'
+ end
+
+ def load_spec(name)
+ Gem::Specification.load("gem-tools-example-#{name}.gemspec")
end
before do
- set_gem_dir CACHED unless File.exists? CACHED
rm_rf GEM_DIR
- cp_r CACHED, GEM_DIR
- set_gem_dir GEM_DIR, "--local"
- Dir["*.gem"].each { |g| rm g }
+ mkdir_p GEM_DIR
+ ENV['GEM_HOME'] = GEM_DIR
+ ENV['GEM_PATH'] = GEM_DIR
+ chdir('..') do
+ gems.build "gem_tools*.gemspec"
+ gems.install("*.gem", '--local')
+ end
end
+ after { Dir["*.gem"].each { |g| rm g } }
+
it 'has installed gem_tools' do
gems.list.should include('gem_tools')
end
-end
+
+ before { @spec = Gem::Specification.new }
+
+ describe :github do
+ it "should set url correctly" do
+ @spec.name = 'bar'
+ @spec.github = 'foo'
+ @spec.homepage.should == 'http://github.com/foo/bar'
+ end
+
+ it "should allow setting the project name directly" do
+ @spec.name = 'notbar'
+ @spec.github 'foo', 'bar'
+ @spec.homepage.should == 'http://github.com/foo/bar'
+ end
+
+ it "should detect fork prefixes" do
+ @spec.name = 'foo-bar'
+ @spec.github 'foo'
+ @spec.homepage.should == 'http://github.com/foo/bar'
+ end
+ end
+
+ describe :run_code do
+ it 'should execute all blocks passed to run_code' do
+ x = 0
+ 5.times { @spec.run_code { x += 1 } }
+ x.should == 0
+ @spec.run_code!
+ x.should == 5
+ end
+
+ it 'should set run_code? for gemspecs' do
+ spec = load_spec('run-code')
+ spec.run_code.should_not be_empty
+ spec.should be_running_code
+ end
+
+ it 'should trigger code on gem install' do
+ install('run-code').should include('w00t')
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.