Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Drastically improved Gems module and specs

  • Loading branch information...
commit 43db36d12d6ecec26fd8077858a881fc1d8dacf4 1 parent d096814
Winton Welsh authored December 29, 2010
46  lib/gem_template/gems.rb
@@ -6,28 +6,32 @@ module GemTemplate
6 6
     module Gems
7 7
       class <<self
8 8
         
9  
-        attr_accessor :configs, :gemset, :gemsets, :gemspec, :testing, :versions, :warn
  9
+        attr_accessor :config, :gemset, :gemsets, :versions
10 10
         
11  
-        Gems.configs = [ "#{File.expand_path('../../../', __FILE__)}/config/gemsets.yml" ]
12  
-        Gems.testing = false
13  
-        Gems.warn = true
14  
-        
15  
-        class Gemspec
  11
+        class SimpleStruct
16 12
           attr_accessor :hash
17 13
           
18 14
           def initialize(hash)
19 15
             @hash = hash
20 16
             @hash.each do |key, value|
21  
-              self.class.send(:define_method, key) { value }
  17
+              self.class.send(:define_method, key) { hash[key] }
  18
+              self.class.send(:define_method, "#{key}=") { |v| hash[key] = v }
22 19
             end
23 20
           end
24 21
         end
25 22
         
  23
+        Gems.config = SimpleStruct.new(
  24
+          :gemsets => [ "#{File.expand_path('../../../', __FILE__)}/config/gemsets.yml" ],
  25
+          :gemspec => "#{File.expand_path('../../../', __FILE__)}/config/gemspec.yml",
  26
+          :testing => false,
  27
+          :warn => true
  28
+        )
  29
+        
26 30
         def activate(*gems)
27 31
           begin
28  
-            require 'rubygems' if !defined?(::Gem) || @testing
  32
+            require 'rubygems' unless defined?(::Gem)
29 33
           rescue LoadError
30  
-            puts "rubygems library could not be required" if @warn
  34
+            puts "rubygems library could not be required" if @config.warn
31 35
           end
32 36
           
33 37
           self.gemset = :default unless defined?(@gemset) && @gemset
@@ -37,7 +41,7 @@ def activate(*gems)
37 41
             if defined?(gem)
38 42
               gem name.to_s, version
39 43
             else
40  
-              puts "#{name} #{"(#{version})" if version} failed to activate" if @warn
  44
+              puts "#{name} #{"(#{version})" if version} failed to activate" if @config.warn
41 45
             end
42 46
           end
43 47
         end
@@ -46,7 +50,7 @@ def gemset=(gemset)
46 50
           if gemset
47 51
             @gemset = gemset.to_sym
48 52
         
49  
-            @gemsets = @configs.reverse.collect { |config|
  53
+            @gemsets = @config.gemsets.reverse.collect { |config|
50 54
               if config.is_a?(::String)
51 55
                 YAML::load(File.read(config)) rescue {}
52 56
               elsif config.is_a?(::Hash)
@@ -56,7 +60,7 @@ def gemset=(gemset)
56 60
               deep_merge(hash, symbolize_keys(config))
57 61
             end
58 62
         
59  
-            @versions = @gemsets[@gemspec.name.to_sym].inject({}) do |hash, (key, value)|
  63
+            @versions = @gemsets[gemspec.name.to_sym].inject({}) do |hash, (key, value)|
60 64
               if value.is_a?(::String)
61 65
                 hash[key] = value
62 66
               elsif value.is_a?(::Hash) && key == @gemset
@@ -71,18 +75,14 @@ def gemset=(gemset)
71 75
           end
72 76
         end
73 77
         
74  
-        def reload_gemspec
75  
-          data =
76  
-            YAML::load(
77  
-              File.read(
78  
-                "#{File.expand_path('../../../', __FILE__)}/config/gemspec.yml"
79  
-              )
80  
-            ) rescue {}
81  
-          
82  
-          @gemspec = Gemspec.new(data)
  78
+        def gemspec(reload=false)
  79
+          if @gemspec && !reload
  80
+            @gemspec
  81
+          else
  82
+            data = YAML::load(File.read(@config.gemspec)) rescue {}
  83
+            @gemspec = SimpleStruct.new(data)
  84
+          end
83 85
         end
84  
-        
85  
-        Gems.reload_gemspec
86 86
       
87 87
         private
88 88
       
2  spec/fixtures/gemsets.yml
... ...
@@ -1,4 +1,4 @@
1  
-gem_template:
  1
+name:
2 2
   rake: =0.8.7
3 3
   default:
4 4
     rspec: =1.3.1
61  spec/gem_template/gems_spec.rb
@@ -2,25 +2,25 @@
2 2
 
3 3
 describe GemTemplate::Gems do
4 4
   
5  
-  describe :activate do
6  
-    before(:each) do
7  
-      GemTemplate::Gems.configs = [
8  
-        "#{$root}/spec/fixtures/gemsets.yml"
9  
-      ]
10  
-      GemTemplate::Gems.gemset = nil
11  
-      GemTemplate::Gems.testing = true
12  
-    end
  5
+  before(:each) do
  6
+    @old_config = GemTemplate::Gems.config
13 7
     
14  
-    it "should warn if unable to require rubygems" do
15  
-      GemTemplate::Gems.stub!(:require)
16  
-      GemTemplate::Gems.should_receive(:require).with('rubygems').and_raise(LoadError)
17  
-      GemTemplate::Gems.stub!(:gem)
18  
-      out = capture_stdout do
19  
-        GemTemplate::Gems.activate :rspec
20  
-      end
21  
-      out.should =~ /rubygems library could not be required/
22  
-    end
  8
+    GemTemplate::Gems.config.gemspec = "#{$root}/spec/fixtures/gemspec.yml"
  9
+    GemTemplate::Gems.config.gemsets = [
  10
+      "#{$root}/spec/fixtures/gemsets.yml"
  11
+    ]
  12
+    GemTemplate::Gems.config.testing = true
  13
+    GemTemplate::Gems.config.warn = true
23 14
     
  15
+    GemTemplate::Gems.gemspec true
  16
+    GemTemplate::Gems.gemset = nil
  17
+  end
  18
+  
  19
+  after(:each) do
  20
+    GemTemplate::Gems.config = @old_config
  21
+  end
  22
+  
  23
+  describe :activate do
24 24
     it "should activate gems" do
25 25
       GemTemplate::Gems.stub!(:gem)
26 26
       GemTemplate::Gems.should_receive(:gem).with('rspec', '=1.3.1')
@@ -31,9 +31,9 @@
31 31
   
32 32
   describe :gemset= do
33 33
     before(:each) do
34  
-      GemTemplate::Gems.configs = [
  34
+      GemTemplate::Gems.config.gemsets = [
35 35
         {
36  
-          :gem_template => {
  36
+          :name => {
37 37
             :rake => '>0.8.6',
38 38
             :default => {
39 39
               :externals => '=1.0.2'
@@ -55,7 +55,7 @@
55 55
     
56 56
       it "should set @gemsets" do
57 57
         GemTemplate::Gems.gemsets.should == {
58  
-          :gem_template => {
  58
+          :name => {
59 59
             :rake => ">0.8.6",
60 60
             :default => {
61 61
               :externals => '=1.0.2',
@@ -93,7 +93,7 @@
93 93
     
94 94
       it "should set @gemsets" do
95 95
         GemTemplate::Gems.gemsets.should == {
96  
-          :gem_template => {
  96
+          :name => {
97 97
             :rake => ">0.8.6",
98 98
             :default => {
99 99
               :externals => '=1.0.2',
@@ -126,21 +126,6 @@
126 126
   end
127 127
   
128 128
   describe :reload_gemspec do
129  
-    before(:all) do
130  
-      GemTemplate::Gems.configs = [
131  
-        "#{$root}/spec/fixtures/gemsets.yml"
132  
-      ]
133  
-      GemTemplate::Gems.gemset = :default
134  
-    end
135  
-  
136  
-    before(:each) do
137  
-      @gemspec_path = "#{$root}/gem_template.gemspec"
138  
-      @gemspec = File.read(@gemspec_path)
139  
-      yml = File.read("#{$root}/spec/fixtures/gemspec.yml")
140  
-      File.stub!(:read).and_return yml
141  
-      GemTemplate::Gems.reload_gemspec
142  
-    end
143  
-  
144 129
     it "should populate @gemspec" do
145 130
       GemTemplate::Gems.gemspec.hash.should == {
146 131
         "name" => "name",
@@ -168,7 +153,9 @@
168 153
     end
169 154
   
170 155
     it "should produce a valid gemspec" do
171  
-      gemspec = eval(@gemspec, binding, @gemspec_path)
  156
+      GemTemplate::Gems.gemset = :default
  157
+      gemspec = File.expand_path("../../../gem_template.gemspec", __FILE__)
  158
+      gemspec = eval(File.read(gemspec), binding, gemspec)
172 159
       gemspec.validate.should == true
173 160
     end
174 161
   end
10  spec/spec_helper.rb
@@ -7,14 +7,4 @@
7 7
 require 'pp'
8 8
 
9 9
 Spec::Runner.configure do |config|
10  
-end
11  
-
12  
-def capture_stdout
13  
-  old = $stdout
14  
-  out = StringIO.new
15  
-  $stdout = out
16  
-  yield
17  
-  return out.string
18  
-ensure
19  
-  $stdout = old
20 10
 end

0 notes on commit 43db36d

Please sign in to comment.
Something went wrong with that request. Please try again.