Permalink
Browse files

Merge branch 'master' into rails

  • Loading branch information...
2 parents 40dca3c + 43db36d commit 5f45d78e1fb8a5380c7d0b5e3b36b53ac2f4bab7 @winton committed Dec 29, 2010
Showing with 48 additions and 71 deletions.
  1. +23 −23 lib/gem_template/gems.rb
  2. +1 −1 spec/fixtures/gemsets.yml
  3. +24 −37 spec/gem_template/gems_spec.rb
  4. +0 −10 spec/spec_helper.rb
View
46 lib/gem_template/gems.rb
@@ -6,28 +6,32 @@ module GemTemplate
module Gems
class <<self
- attr_accessor :configs, :gemset, :gemsets, :gemspec, :testing, :versions, :warn
+ attr_accessor :config, :gemset, :gemsets, :versions
- Gems.configs = [ "#{File.expand_path('../../../', __FILE__)}/config/gemsets.yml" ]
- Gems.testing = false
- Gems.warn = true
-
- class Gemspec
+ class SimpleStruct
attr_accessor :hash
def initialize(hash)
@hash = hash
@hash.each do |key, value|
- self.class.send(:define_method, key) { value }
+ self.class.send(:define_method, key) { hash[key] }
+ self.class.send(:define_method, "#{key}=") { |v| hash[key] = v }
end
end
end
+ Gems.config = SimpleStruct.new(
+ :gemsets => [ "#{File.expand_path('../../../', __FILE__)}/config/gemsets.yml" ],
+ :gemspec => "#{File.expand_path('../../../', __FILE__)}/config/gemspec.yml",
+ :testing => false,
+ :warn => true
+ )
+
def activate(*gems)
begin
- require 'rubygems' if !defined?(::Gem) || @testing
+ require 'rubygems' unless defined?(::Gem)
rescue LoadError
- puts "rubygems library could not be required" if @warn
+ puts "rubygems library could not be required" if @config.warn
end
self.gemset = :default unless defined?(@gemset) && @gemset
@@ -37,7 +41,7 @@ def activate(*gems)
if defined?(gem)
gem name.to_s, version
else
- puts "#{name} #{"(#{version})" if version} failed to activate" if @warn
+ puts "#{name} #{"(#{version})" if version} failed to activate" if @config.warn
end
end
end
@@ -46,7 +50,7 @@ def gemset=(gemset)
if gemset
@gemset = gemset.to_sym
- @gemsets = @configs.reverse.collect { |config|
+ @gemsets = @config.gemsets.reverse.collect { |config|
if config.is_a?(::String)
YAML::load(File.read(config)) rescue {}
elsif config.is_a?(::Hash)
@@ -56,7 +60,7 @@ def gemset=(gemset)
deep_merge(hash, symbolize_keys(config))
end
- @versions = @gemsets[@gemspec.name.to_sym].inject({}) do |hash, (key, value)|
+ @versions = @gemsets[gemspec.name.to_sym].inject({}) do |hash, (key, value)|
if value.is_a?(::String)
hash[key] = value
elsif value.is_a?(::Hash) && key == @gemset
@@ -71,18 +75,14 @@ def gemset=(gemset)
end
end
- def reload_gemspec
- data =
- YAML::load(
- File.read(
- "#{File.expand_path('../../../', __FILE__)}/config/gemspec.yml"
- )
- ) rescue {}
-
- @gemspec = Gemspec.new(data)
+ def gemspec(reload=false)
+ if @gemspec && !reload
+ @gemspec
+ else
+ data = YAML::load(File.read(@config.gemspec)) rescue {}
+ @gemspec = SimpleStruct.new(data)
+ end
end
-
- Gems.reload_gemspec
private
View
2 spec/fixtures/gemsets.yml
@@ -1,4 +1,4 @@
-gem_template:
+name:
rake: =0.8.7
default:
rspec: =1.3.1
View
61 spec/gem_template/gems_spec.rb
@@ -2,25 +2,25 @@
describe GemTemplate::Gems do
- describe :activate do
- before(:each) do
- GemTemplate::Gems.configs = [
- "#{$root}/spec/fixtures/gemsets.yml"
- ]
- GemTemplate::Gems.gemset = nil
- GemTemplate::Gems.testing = true
- end
+ before(:each) do
+ @old_config = GemTemplate::Gems.config
- it "should warn if unable to require rubygems" do
- GemTemplate::Gems.stub!(:require)
- GemTemplate::Gems.should_receive(:require).with('rubygems').and_raise(LoadError)
- GemTemplate::Gems.stub!(:gem)
- out = capture_stdout do
- GemTemplate::Gems.activate :rspec
- end
- out.should =~ /rubygems library could not be required/
- end
+ GemTemplate::Gems.config.gemspec = "#{$root}/spec/fixtures/gemspec.yml"
+ GemTemplate::Gems.config.gemsets = [
+ "#{$root}/spec/fixtures/gemsets.yml"
+ ]
+ GemTemplate::Gems.config.testing = true
+ GemTemplate::Gems.config.warn = true
+ GemTemplate::Gems.gemspec true
+ GemTemplate::Gems.gemset = nil
+ end
+
+ after(:each) do
+ GemTemplate::Gems.config = @old_config
+ end
+
+ describe :activate do
it "should activate gems" do
GemTemplate::Gems.stub!(:gem)
GemTemplate::Gems.should_receive(:gem).with('rspec', '=1.3.1')
@@ -31,9 +31,9 @@
describe :gemset= do
before(:each) do
- GemTemplate::Gems.configs = [
+ GemTemplate::Gems.config.gemsets = [
{
- :gem_template => {
+ :name => {
:rake => '>0.8.6',
:default => {
:externals => '=1.0.2'
@@ -55,7 +55,7 @@
it "should set @gemsets" do
GemTemplate::Gems.gemsets.should == {
- :gem_template => {
+ :name => {
:rake => ">0.8.6",
:default => {
:externals => '=1.0.2',
@@ -93,7 +93,7 @@
it "should set @gemsets" do
GemTemplate::Gems.gemsets.should == {
- :gem_template => {
+ :name => {
:rake => ">0.8.6",
:default => {
:externals => '=1.0.2',
@@ -126,21 +126,6 @@
end
describe :reload_gemspec do
- before(:all) do
- GemTemplate::Gems.configs = [
- "#{$root}/spec/fixtures/gemsets.yml"
- ]
- GemTemplate::Gems.gemset = :default
- end
-
- before(:each) do
- @gemspec_path = "#{$root}/gem_template.gemspec"
- @gemspec = File.read(@gemspec_path)
- yml = File.read("#{$root}/spec/fixtures/gemspec.yml")
- File.stub!(:read).and_return yml
- GemTemplate::Gems.reload_gemspec
- end
-
it "should populate @gemspec" do
GemTemplate::Gems.gemspec.hash.should == {
"name" => "name",
@@ -168,7 +153,9 @@
end
it "should produce a valid gemspec" do
- gemspec = eval(@gemspec, binding, @gemspec_path)
+ GemTemplate::Gems.gemset = :default
+ gemspec = File.expand_path("../../../gem_template.gemspec", __FILE__)
+ gemspec = eval(File.read(gemspec), binding, gemspec)
gemspec.validate.should == true
end
end
View
10 spec/spec_helper.rb
@@ -7,14 +7,4 @@
require 'pp'
Spec::Runner.configure do |config|
-end
-
-def capture_stdout
- old = $stdout
- out = StringIO.new
- $stdout = out
- yield
- return out.string
-ensure
- $stdout = old
end

0 comments on commit 5f45d78

Please sign in to comment.