Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactor Kumade::Packager

This moves the logic of each of the packagers into their own classes,
introduces a PackagerList class that maintains all the various
packagers, and improves tests around the packager.
  • Loading branch information...
commit d854184744a70d1786e65a3a4bbd6429988b7f00 1 parent fe4be8b
authored September 16, 2011 joshuaclayton committed September 16, 2011
9  features/kumade_executable.feature
@@ -21,7 +21,7 @@ Feature: Kumade executable
21 21
     And the output should contain:
22 22
       """
23 23
       ==> Git repo is clean
24  
-      ==> Packaged assets with Jammit
  24
+      ==> Packaged with Kumade::JammitPackager
25 25
                run  git push origin master
26 26
       ==> Pushed master -> origin
27 27
                run  git branch deploy
@@ -31,7 +31,7 @@ Feature: Kumade executable
31 31
                run  git checkout master && git branch -D deploy
32 32
       ==> Deployed to: pretend-staging
33 33
       """
34  
-    But the output should not contain "==> Packaged assets with More"
  34
+    But the output should not contain "==> Packaged with Kumade::MorePackager"
35 35
 
36 36
   Scenario: Default environment is staging
37 37
     When I run kumade with "-p"
@@ -52,7 +52,7 @@ Feature: Kumade executable
52 52
     Then the output should contain:
53 53
       """
54 54
       ==> Git repo is clean
55  
-      ==> Packaged assets with Jammit
  55
+      ==> Packaged with Kumade::JammitPackager
56 56
                run  git push origin new_branch
57 57
       ==> Pushed new_branch -> origin
58 58
                run  git branch deploy
@@ -95,4 +95,5 @@ Feature: Kumade executable
95 95
       end
96 96
       """
97 97
     When I run kumade with "pretend-staging -p"
98  
-    Then the output should match /kumade:before_asset_compilation.*Packaged assets with Jammit/
  98
+    Then the output should contain "kumade:before_asset_compilation"
  99
+    And the output should contain "==> Packaged with Kumade::JammitPackager"
4  features/kumade_without_jammit.feature
@@ -23,7 +23,7 @@ Feature: Kumade without jammit
23 23
       end
24 24
       """
25 25
     When I run kumade with "pretend-staging"
26  
-    Then the output should contain "Running kumade:before_asset_compilation task"
  26
+    Then the output should contain "Running rake task: kumade:before_asset_compilation"
27 27
     And the output should contain "Hi!"
28 28
 
29 29
   Scenario: Don't run rake task in pretend mode
@@ -36,5 +36,5 @@ Feature: Kumade without jammit
36 36
       end
37 37
       """
38 38
     When I run kumade with "pretend-staging -p"
39  
-    Then the output should contain "Running kumade:before_asset_compilation task"
  39
+    Then the output should contain "Running rake task: kumade:before_asset_compilation"
40 40
     And the output should not contain "Hi!"
1  kumade.gemspec
@@ -26,5 +26,6 @@ Gem::Specification.new do |s|
26 26
   s.add_development_dependency('cucumber', '~> 1.0.2')
27 27
   s.add_development_dependency('aruba', '~> 0.4.3')
28 28
   s.add_development_dependency('jammit', '~> 0.6.3')
  29
+  s.add_development_dependency('less', '~> 2.0')
29 30
   s.add_development_dependency('bourne')
30 31
 end
5  lib/kumade.rb
@@ -8,6 +8,11 @@ module Kumade
8 8
   autoload :Configuration,   "kumade/configuration"
9 9
   autoload :Heroku,          "kumade/heroku"
10 10
   autoload :Packager,        "kumade/packager"
  11
+  autoload :MorePackager,    "kumade/packagers/more_packager"
  12
+  autoload :JammitPackager,  "kumade/packagers/jammit_packager"
  13
+  autoload :NoopPackager,    "kumade/packagers/noop_packager"
  14
+  autoload :PackagerList,    "kumade/packager_list"
  15
+  autoload :RakeTaskRunner,  "kumade/rake_task_runner"
11 16
 
12 17
   def self.configuration
13 18
     @@configuration ||= Configuration.new
2  lib/kumade/deployer.rb
@@ -32,7 +32,7 @@ def pre_deploy
32 32
     end
33 33
 
34 34
     def package_assets
35  
-      packager.run
  35
+      @packager.run
36 36
     end
37 37
 
38 38
     def sync_github
92  lib/kumade/packager.rb
... ...
@@ -1,94 +1,46 @@
1 1
 module Kumade
2 2
   class Packager < Base
3  
-    attr_reader :git
4  
-
5  
-    def initialize(git)
  3
+    def initialize(git, packager = Packager.available_packager)
6 4
       super()
7  
-      @git = git
  5
+      @packager = packager
  6
+      @git      = git
8 7
     end
9 8
 
10 9
     def run
11  
-      invoke_custom_task  if custom_task?
12  
-      package_with_jammit if jammit_installed?
13  
-      package_with_more   if more_installed?
  10
+      precompile_assets
  11
+      package
14 12
     end
15 13
 
16  
-    def invoke_custom_task
17  
-      success "Running kumade:before_asset_compilation task"
18  
-      Rake::Task["kumade:before_asset_compilation"].invoke unless Kumade.configuration.pretending?
  14
+    def self.available_packager
  15
+      Kumade::PackagerList.new.first
19 16
     end
20 17
 
21  
-    def custom_task?
22  
-      load("Rakefile") if File.exist?("Rakefile")
23  
-      Rake::Task.task_defined?("kumade:before_asset_compilation")
24  
-    end
  18
+    private
25 19
 
26  
-    def package_with_jammit
27  
-      begin
28  
-        success_message = "Packaged assets with Jammit"
  20
+    def precompile_assets
  21
+      RakeTaskRunner.new("kumade:before_asset_compilation", self).invoke
  22
+    end
29 23
 
30  
-        if Kumade.configuration.pretending?
31  
-          success(success_message)
32  
-        else
33  
-          Jammit.package!
  24
+    def package
  25
+      return success(success_message) if Kumade.configuration.pretending?
34 26
 
  27
+      begin
  28
+        @packager.package
  29
+        if @git.dirty?
  30
+          git_add_and_commit_all_assets_in(@packager.assets_path)
35 31
           success(success_message)
36  
-          git_add_and_commit_all_assets_in(jammit_assets_path)
37 32
         end
38  
-      rescue => jammit_error
39  
-        error("Error: #{jammit_error.class}: #{jammit_error.message}")
  33
+      rescue => packager_exception
  34
+        error("Error: #{packager_exception.class}: #{packager_exception.message}")
40 35
       end
41 36
     end
42 37
 
43  
-    def package_with_more
44  
-      success_message = "Packaged assets with More"
45  
-      if Kumade.configuration.pretending?
46  
-        success(success_message)
47  
-      else
48  
-        begin
49  
-          run "bundle exec rake more:generate"
50  
-          if git.dirty?
51  
-            success(success_message)
52  
-            git_add_and_commit_all_assets_in(more_assets_path)
53  
-          end
54  
-        rescue => more_error
55  
-          error("Error: #{more_error.class}: #{more_error.message}")
56  
-        end
57  
-      end
  38
+    def success_message
  39
+      "Packaged with #{@packager.name}"
58 40
     end
59 41
 
60 42
     def git_add_and_commit_all_assets_in(dir)
61  
-      git.add_and_commit_all_in(dir, Kumade::Heroku::DEPLOY_BRANCH, 'Compiled assets', "Added and committed all assets", "couldn't commit assets")
62  
-    end
63  
-
64  
-    def jammit_assets_path
65  
-      File.join(Jammit::PUBLIC_ROOT, Jammit.package_path)
66  
-    end
67  
-
68  
-    def more_assets_path
69  
-      File.join('public', ::Less::More.destination_path)
70  
-    end
71  
-
72  
-    def jammit_installed?
73  
-      @jammit_installed ||=
74  
-        (defined?(Jammit) ||
75  
-          begin
76  
-            require 'jammit'
77  
-            true
78  
-          rescue LoadError
79  
-            false
80  
-          end)
81  
-    end
82  
-
83  
-    def more_installed?
84  
-      @more_installed ||=
85  
-        (defined?(Less::More) ||
86  
-          begin
87  
-            require 'less/more'
88  
-            true
89  
-          rescue LoadError
90  
-            false
91  
-          end)
  43
+      @git.add_and_commit_all_in(dir, Kumade::Heroku::DEPLOY_BRANCH, 'Compiled assets', "Added and committed all assets", "couldn't commit assets")
92 44
     end
93 45
   end
94 46
 end
29  lib/kumade/packager_list.rb
... ...
@@ -0,0 +1,29 @@
  1
+module Kumade
  2
+  class PackagerList
  3
+    include Enumerable
  4
+
  5
+    PACKAGERS = [MorePackager, JammitPackager]
  6
+
  7
+    def initialize
  8
+      @packagers = build_packagers_list
  9
+    end
  10
+
  11
+    def each(&block)
  12
+      @packagers.each(&block)
  13
+    end
  14
+
  15
+    private
  16
+
  17
+    def build_packagers_list
  18
+      if installed_packagers.any?
  19
+        installed_packagers
  20
+      else
  21
+        [NoopPackager]
  22
+      end
  23
+    end
  24
+
  25
+    def installed_packagers
  26
+      PACKAGERS.select(&:installed?)
  27
+    end
  28
+  end
  29
+end
20  lib/kumade/packagers/jammit_packager.rb
... ...
@@ -0,0 +1,20 @@
  1
+begin
  2
+  require "jammit"
  3
+rescue LoadError
  4
+end
  5
+
  6
+module Kumade
  7
+  class JammitPackager
  8
+    def self.assets_path
  9
+      File.join(Jammit::PUBLIC_ROOT, Jammit.package_path)
  10
+    end
  11
+
  12
+    def self.installed?
  13
+      !!defined?(Jammit)
  14
+    end
  15
+
  16
+    def self.package
  17
+      Jammit.package!
  18
+    end
  19
+  end
  20
+end
20  lib/kumade/packagers/more_packager.rb
... ...
@@ -0,0 +1,20 @@
  1
+begin
  2
+  require "less/more"
  3
+rescue LoadError
  4
+end
  5
+
  6
+module Kumade
  7
+  class MorePackager
  8
+    def self.assets_path
  9
+      File.join("public", ::Less::More.destination_path)
  10
+    end
  11
+
  12
+    def self.installed?
  13
+      !!defined?(Less::More)
  14
+    end
  15
+
  16
+    def self.package
  17
+      ::Less::More.generate_all
  18
+    end
  19
+  end
  20
+end
14  lib/kumade/packagers/noop_packager.rb
... ...
@@ -0,0 +1,14 @@
  1
+module Kumade
  2
+  class NoopPackager
  3
+    def self.assets_path
  4
+      ""
  5
+    end
  6
+
  7
+    def self.package
  8
+    end
  9
+
  10
+    def self.installed?
  11
+      false
  12
+    end
  13
+  end
  14
+end
30  lib/kumade/rake_task_runner.rb
... ...
@@ -0,0 +1,30 @@
  1
+module Kumade
  2
+  class RakeTaskRunner
  3
+    def initialize(task_name, runner)
  4
+      @task_name = task_name
  5
+      @runner    = runner
  6
+    end
  7
+
  8
+    def invoke
  9
+      return unless task_defined?
  10
+
  11
+      @runner.success("Running rake task: #{@task_name}")
  12
+      Rake::Task[@task_name].invoke if task_should_be_run?
  13
+    end
  14
+
  15
+    private
  16
+
  17
+    def task_defined?
  18
+      load_rakefile
  19
+      Rake::Task.task_defined?(@task_name)
  20
+    end
  21
+
  22
+    def task_should_be_run?
  23
+      !Kumade.configuration.pretending?
  24
+    end
  25
+
  26
+    def load_rakefile
  27
+      load("Rakefile") if File.exist?("Rakefile")
  28
+    end
  29
+  end
  30
+end
17  spec/kumade/deployer_spec.rb
... ...
@@ -1,7 +1,5 @@
1 1
 require 'spec_helper'
2 2
 
3  
-require 'jammit'
4  
-
5 3
 describe Kumade::Deployer, "#pre_deploy" do
6 4
   let(:git) { subject.git }
7 5
 
@@ -114,3 +112,18 @@
114 112
     end
115 113
   end
116 114
 end
  115
+
  116
+describe Kumade::Deployer, "packaging" do
  117
+  let(:git)      { stub("git", :current_branch => "awesome", :delete => true) }
  118
+  let(:packager) { stub("packager", :run => true) }
  119
+
  120
+  before do
  121
+    Kumade::Git.stubs(:new => git)
  122
+    Kumade::Packager.stubs(:new => packager)
  123
+  end
  124
+
  125
+  it "builds the correct packager" do
  126
+    subject.deploy
  127
+    Kumade::Packager.should have_received(:new).with(git)
  128
+  end
  129
+end
31  spec/kumade/packager_list_spec.rb
... ...
@@ -0,0 +1,31 @@
  1
+require 'spec_helper'
  2
+
  3
+describe Kumade::PackagerList, "detecting packages" do
  4
+  it "returns an array containing the Jammit packager if Jammit is installed" do
  5
+    Kumade::JammitPackager.stubs(:installed? => true)
  6
+    Kumade::MorePackager.stubs(:installed? => false)
  7
+
  8
+    Kumade::PackagerList.new.to_a.should == [Kumade::JammitPackager]
  9
+  end
  10
+
  11
+  it "returns an array containing the More packager if More is installed" do
  12
+    Kumade::JammitPackager.stubs(:installed? => false)
  13
+    Kumade::MorePackager.stubs(:installed? => true)
  14
+
  15
+    Kumade::PackagerList.new.to_a.should == [Kumade::MorePackager]
  16
+  end
  17
+
  18
+  it "returns multiple packagers if they are installed" do
  19
+    Kumade::JammitPackager.stubs(:installed? => true)
  20
+    Kumade::MorePackager.stubs(:installed? => true)
  21
+
  22
+    Kumade::PackagerList.new.to_a.should =~ [Kumade::JammitPackager, Kumade::MorePackager]
  23
+  end
  24
+
  25
+  it "returns an array containing the no-op packager if no other packagers are found" do
  26
+    Kumade::JammitPackager.stubs(:installed? => false)
  27
+    Kumade::MorePackager.stubs(:installed? => false)
  28
+
  29
+    Kumade::PackagerList.new.to_a.should == [Kumade::NoopPackager]
  30
+  end
  31
+end
347  spec/kumade/packager_spec.rb
... ...
@@ -1,318 +1,115 @@
1 1
 require 'spec_helper'
2 2
 
3  
-require 'jammit'
  3
+describe Kumade::Packager, ".available_packager" do
  4
+  let(:packager_1) { "1st packager" }
  5
+  let(:packager_2) { "2nd packager" }
4 6
 
5  
-shared_context "with a fake Git" do
6  
-  let(:git) { stub() }
7  
-  subject   { Kumade::Packager.new(git) }
8  
-end
9  
-
10  
-describe Kumade::Packager, "#run" do
11  
-  include_context "with a fake Git"
12  
-
13  
-  before do
14  
-    subject.stubs(:package_with_jammit)
15  
-  end
16  
-
17  
-  context "with Jammit installed" do
18  
-    it "calls package_with_jammit" do
19  
-      subject.run
20  
-
21  
-      subject.should have_received(:package_with_jammit)
22  
-    end
23  
-  end
24  
-
25  
-  context "with Jammit not installed" do
26  
-    before { subject.stubs(:jammit_installed? => false) }
27  
-
28  
-    it "does not call package_with_jammit" do
29  
-      subject.run
30  
-
31  
-      subject.should_not have_received(:package_with_jammit)
32  
-    end
33  
-  end
34  
-
35  
-  context "with More installed" do
36  
-    before do
37  
-      subject.stubs(:jammit_installed? => false)
38  
-      subject.stubs(:more_installed? => true)
39  
-      subject.stubs(:package_with_more)
40  
-    end
41  
-
42  
-    it "calls package_with_more" do
43  
-      subject.run
44  
-
45  
-      subject.should have_received(:package_with_more)
46  
-    end
47  
-  end
48  
-
49  
-  context "with More not installed" do
50  
-    before do
51  
-      subject.stubs(:jammit_installed? => false)
52  
-      subject.stubs(:more_installed? => false)
53  
-    end
54  
-
55  
-    it "does not call package_with_more" do
56  
-      subject.run
57  
-
58  
-      subject.should_not have_received(:package_with_more)
59  
-    end
60  
-  end
61  
-
62  
-  context "with custom rake task installed" do
63  
-    before do
64  
-      subject.stubs(:jammit_installed?  => false,
65  
-                    :more_installed?    => false,
66  
-                    :invoke_custom_task => nil,
67  
-                    :custom_task?       => true)
68  
-    end
69  
-
70  
-    it "invokes custom task" do
71  
-      subject.run
72  
-
73  
-      subject.should have_received(:invoke_custom_task)
74  
-    end
  7
+  it "returns the first available packager" do
  8
+    Kumade::PackagerList.stubs(:new => [packager_1, packager_2])
  9
+    Kumade::Packager.available_packager.should == packager_1
75 10
   end
76 11
 
77  
-  context "with custom rake task not installed" do
78  
-    before do
79  
-      subject.stubs(:jammit_installed?  => false,
80  
-                    :more_installed?    => false,
81  
-                    :invoke_custom_task => nil,
82  
-                    :custom_task?       => false)
83  
-    end
84  
-
85  
-    it "does not invoke custom task" do
86  
-      subject.run
87  
-
88  
-      subject.should_not have_received(:invoke_custom_task)
89  
-    end
  12
+  it "returns nil if no packagers are availabke" do
  13
+    Kumade::PackagerList.stubs(:new => [])
  14
+    Kumade::Packager.available_packager.should be_nil
90 15
   end
91 16
 end
92 17
 
93  
-describe Kumade::Packager, "#invoke_custom_task" do
94  
-  include_context "with a fake Git"
95  
-
96  
-  let(:task) { stub('kumade:before_asset_compilation task', :invoke => nil) }
  18
+describe Kumade::Packager, "#run" do
  19
+  let(:git)              { stub("git", :dirty? => true, :add_and_commit_all_in => true) }
  20
+  let(:packager)         { stub("packager", :name => "MyPackager", :package => true, :assets_path => 'fake_assets_path') }
  21
+  let(:rake_task_runner) { stub("RakeTaskRunner", :invoke => true) }
97 22
 
98 23
   before do
99  
-    subject.stubs(:say)
100  
-    Rake::Task.stubs(:[] => task)
  24
+    Kumade::RakeTaskRunner.stubs(:new => rake_task_runner)
  25
+    subject.stubs(:success => nil, :error => nil)
101 26
   end
102 27
 
103  
-  it "calls deploy task" do
104  
-    Rake::Task.expects(:[]).with("kumade:before_asset_compilation").returns(task)
  28
+  subject { Kumade::Packager.new(git, packager) }
105 29
 
106  
-    subject.invoke_custom_task
107  
-
108  
-    task.should have_received(:invoke)
  30
+  it "precompiles assets" do
  31
+    subject.run
  32
+    Kumade::RakeTaskRunner.should have_received(:new).with("kumade:before_asset_compilation", subject)
  33
+    rake_task_runner.should have_received(:invoke)
109 34
   end
110  
-end
111 35
 
112  
-describe Kumade::Packager, "#custom_task?" do
113  
-  include_context "with a fake Git"
  36
+  context "when packaging with a packager" do
  37
+    context "when pretending" do
  38
+      before do
  39
+        Kumade.configuration.pretending = true
  40
+      end
114 41
 
115  
-  before do
116  
-    Rake::Task.clear
117  
-  end
  42
+      it "prints a success message" do
  43
+        subject.run
  44
+        subject.should have_received(:success).with("Packaged with MyPackager")
  45
+      end
118 46
 
119  
-  it "returns true if it task found" do
120  
-    namespace :kumade do
121  
-      task :before_asset_compilation do
  47
+      it "does not package" do
  48
+        subject.run
  49
+        packager.should have_received(:package).never
122 50
       end
123 51
     end
124 52
 
125  
-    subject.custom_task?.should be_true
126  
-  end
127  
-
128  
-  it "returns false if task not found" do
129  
-    subject.custom_task?.should be_false
130  
-  end
131  
-end
132  
-
133  
-describe Kumade::Packager, "#package_with_jammit" do
134  
-  include_context "with a fake Git"
135  
-
136  
-  before do
137  
-    subject.stubs(:git_add_and_commit_all_assets_in)
138  
-    subject.stubs(:success)
139  
-    subject.stubs(:error)
140  
-    Jammit.stubs(:package!)
141  
-  end
142  
-
143  
-  it "calls Jammit.package!" do
144  
-    subject.package_with_jammit
145  
-
146  
-    Jammit.should have_received(:package!).once
147  
-  end
148  
-
149  
-  context "with updated assets" do
150  
-    before { subject.git.stubs(:dirty? => true) }
151  
-
152  
-    it "prints the correct message" do
153  
-      subject.package_with_jammit
  53
+    context "when not pretending" do
  54
+      before do
  55
+        Kumade.configuration.pretending = false
  56
+      end
154 57
 
155  
-      subject.should have_received(:success).with("Packaged assets with Jammit")
156  
-    end
  58
+      it "prints a success message" do
  59
+        subject.run
  60
+        subject.should have_received(:success).with("Packaged with MyPackager")
  61
+      end
157 62
 
158  
-    it "calls git_add_and_commit_all_assets_in" do
159  
-      subject.stubs(:jammit_assets_path => 'jammit-assets')
160  
-      subject.expects(:git_add_and_commit_all_assets_in).
161  
-        with('jammit-assets').
162  
-        returns(true)
  63
+      it "packages" do
  64
+        subject.run
  65
+        packager.should have_received(:package).once
  66
+      end
163 67
 
164  
-      subject.package_with_jammit
  68
+      it "prints an error if an exception is raised" do
  69
+        packager.stubs(:package).raises(RuntimeError.new("my specific error"))
  70
+        subject.run
  71
+        subject.should have_received(:error).with("Error: RuntimeError: my specific error")
  72
+      end
165 73
     end
166 74
   end
167 75
 
168  
-  it "prints an error if packaging failed" do
169  
-    Jammit.expects(:package!).raises(Jammit::MissingConfiguration.new("random Jammit error"))
170  
-
171  
-    subject.package_with_jammit
172  
-
173  
-    subject.should have_received(:error).with("Error: Jammit::MissingConfiguration: random Jammit error")
174  
-  end
175  
-end
176  
-
177  
-describe Kumade::Packager, "#package_with_more" do
178  
-  include_context "with a fake Git"
179  
-
180  
-  before do
181  
-    subject.stubs(:git_add_and_commit_all_assets_in => true,
182  
-                 :more_assets_path                 => 'assets')
183  
-    subject.stubs(:say)
184  
-  end
185  
-
186  
-  it "calls the more:generate task" do
187  
-    git.expects(:dirty?).returns(true)
188  
-    subject.expects(:run).with("bundle exec rake more:generate")
189  
-    subject.package_with_more
190  
-  end
191  
-
192  
-  context "with changed assets" do
  76
+  context "when packaging and the repository becomes dirty" do
193 77
     before do
  78
+      Kumade.configuration.pretending = false
194 79
       git.stubs(:dirty? => true)
195 80
     end
196 81
 
197  
-    it "prints a success message" do
198  
-      subject.stubs(:run).with("bundle exec rake more:generate")
199  
-      subject.expects(:success).with("Packaged assets with More")
200  
-
201  
-      subject.package_with_more
202  
-    end
203  
-
204  
-    it "calls git_add_and_commit_all_assets_in if assets were added" do
205  
-      subject.stubs(:more_assets_path => 'blerg')
206  
-      subject.stubs(:run).with("bundle exec rake more:generate")
207  
-      subject.expects(:git_add_and_commit_all_assets_in).
208  
-        with('blerg').
209  
-        returns(true)
210  
-
211  
-      subject.package_with_more
212  
-    end
213  
-  end
214  
-
215  
-  context "with no changed assets" do
216  
-    it "prints no message" do
217  
-      subject.stubs(:run).with("bundle exec rake more:generate")
218  
-      subject.stubs(:git => mock(:dirty? => false))
219  
-      subject.expects(:say).never
220  
-
221  
-      subject.package_with_more
222  
-    end
223  
-
224  
-    it "does not call git_add_and_commit_all_more_assets" do
225  
-      subject.stubs(:run).with("bundle exec rake more:generate")
226  
-      subject.stubs(:git => mock(:dirty? => false))
227  
-      subject.expects(:git_add_and_commit_all_assets_in).never
228  
-
229  
-      subject.package_with_more
  82
+    it "performs a commit" do
  83
+      subject.run
  84
+      git.should have_received(:add_and_commit_all_in).
  85
+        with(packager.assets_path,
  86
+             Kumade::Heroku::DEPLOY_BRANCH,
  87
+             'Compiled assets',
  88
+             "Added and committed all assets",
  89
+             "couldn't commit assets")
230 90
     end
231  
-  end
232  
-
233  
-  it "prints an error if packaging failed" do
234  
-    subject.stubs(:run).with("bundle exec rake more:generate").raises("blerg")
235 91
 
236  
-    subject.expects(:error).with("Error: RuntimeError: blerg")
237  
-
238  
-    subject.package_with_more
239  
-  end
240  
-end
241  
-
242  
-describe Kumade::Packager, "#git_add_and_commit_all_assets_in" do
243  
-  include_context "with a fake Git"
244  
-
245  
-  it "should call git.add_and_commit_all_in" do
246  
-    git.expects(:add_and_commit_all_in).with("dir", 'deploy', 'Compiled assets', "Added and committed all assets", "couldn't commit assets")
247  
-    subject.git_add_and_commit_all_assets_in("dir")
248  
-  end
249  
-end
250  
-
251  
-describe Kumade::Packager, "#jammit_assets_path" do
252  
-  let(:git)      { stub() }
253  
-  let(:packager) { Kumade::Packager.new(git) }
254  
-
255  
-  before do
256  
-    Jammit.stubs(:package_path).returns('blerg')
257  
-  end
258  
-
259  
-  subject { packager.jammit_assets_path }
260  
-
261  
-  it { should == File.join(Jammit::PUBLIC_ROOT, 'blerg') }
262  
-end
263  
-
264  
-describe Kumade::Packager, "#more_assets_path" do
265  
-  include_context "with a fake Git"
266  
-
267  
-  it "returns the correct asset path" do
268  
-    module ::Less
269  
-      class More
270  
-        def self.destination_path
271  
-          'blerg'
272  
-        end
273  
-      end
  92
+    it "prints the success message after committing" do
  93
+      git.stubs(:add_and_commit_all_in).raises(RuntimeError.new("something broke"))
  94
+      subject.run
  95
+      subject.should have_received(:success).never
274 96
     end
275  
-    subject.more_assets_path.should == 'public/blerg'
276 97
   end
277  
-end
278  
-
279  
-describe Kumade::Packager, "#jammit_installed?" do
280  
-  include_context "with a fake Git"
281  
-
282  
-  it "returns true because it's loaded by the Gemfile" do
283  
-    subject.jammit_installed?.should be_true
284  
-  end
285  
-
286  
-  it "returns false if jammit is not installed" do
287  
-    subject.jammit_installed?.should be_true
288  
-  end
289  
-end
290  
-
291  
-describe Kumade::Packager, "#more_installed?" do
292  
-  include_context "with a fake Git"
293 98
 
294  
-  context "when More is not installed" do
  99
+  context "when packaging and the repository is not dirty" do
295 100
     before do
296  
-      if defined?(Less)
297  
-        Object.send(:remove_const, :Less)
298  
-      end
299  
-    end
300  
-
301  
-    it "returns false" do
302  
-      subject.more_installed?.should be_false
  101
+      Kumade.configuration.pretending = false
  102
+      git.stubs(:dirty? => false)
303 103
     end
304  
-  end
305 104
 
306  
-  context "when More is installed" do
307  
-    before do
308  
-      module Less
309  
-        class More
310  
-        end
311  
-      end
  105
+    it "does not print a success message" do
  106
+      subject.run
  107
+      subject.should have_received(:success).never
312 108
     end
313 109
 
314  
-    it "returns true" do
315  
-      subject.more_installed?.should be_true
  110
+    it "doesn't perform a commit" do
  111
+      subject.run
  112
+      git.should have_received(:add_and_commit_all_in).never
316 113
     end
317 114
   end
318 115
 end
27  spec/kumade/packagers/jammit_packager_spec.rb
... ...
@@ -0,0 +1,27 @@
  1
+require "spec_helper"
  2
+
  3
+require "jammit"
  4
+
  5
+describe Kumade::JammitPackager do
  6
+  subject { Kumade::JammitPackager }
  7
+
  8
+  it_should_behave_like "packager"
  9
+
  10
+  its(:assets_path) { should == File.join(Jammit::PUBLIC_ROOT, Jammit.package_path) }
  11
+
  12
+  it "knows how to package itself" do
  13
+    ::Jammit.stubs(:package!)
  14
+    subject.package
  15
+    ::Jammit.should have_received(:package!).once
  16
+  end
  17
+
  18
+  context "when Jammit is defined" do
  19
+    before { Jammit }
  20
+    it     { should be_installed }
  21
+  end
  22
+
  23
+  context "when Jammit is not defined" do
  24
+    before { Object.send(:remove_const, :Jammit) }
  25
+    it     { should_not be_installed }
  26
+  end
  27
+end
37  spec/kumade/packagers/more_packager_spec.rb
... ...
@@ -0,0 +1,37 @@
  1
+require "spec_helper"
  2
+
  3
+require "less"
  4
+
  5
+describe Kumade::MorePackager do
  6
+  subject { Kumade::MorePackager }
  7
+
  8
+  before do
  9
+    define_constant "Less::More" do
  10
+      def self.destination_path
  11
+        "awesome_destination"
  12
+      end
  13
+    end
  14
+  end
  15
+
  16
+  it_should_behave_like "packager"
  17
+
  18
+  its(:assets_path) { should == File.join('public', ::Less::More.destination_path) }
  19
+
  20
+  it "knows how to package itself" do
  21
+    Less::More.stubs(:generate_all)
  22
+
  23
+    subject.package
  24
+
  25
+    Less::More.should have_received(:generate_all).once
  26
+  end
  27
+
  28
+  context "when More is defined" do
  29
+    before { Less::More }
  30
+    it     { should be_installed }
  31
+  end
  32
+
  33
+  context "when Less::More is not defined" do
  34
+    before { Less.send(:remove_const, :More) }
  35
+    it     { should_not be_installed }
  36
+  end
  37
+end
9  spec/kumade/packagers/noop_packager_spec.rb
... ...
@@ -0,0 +1,9 @@
  1
+require "spec_helper"
  2
+
  3
+describe Kumade::NoopPackager do
  4
+  subject { Kumade::NoopPackager }
  5
+
  6
+  it_should_behave_like "packager"
  7
+
  8
+  its(:assets_path) { should == "" }
  9
+end
77  spec/kumade/rake_task_runner_spec.rb
... ...
@@ -0,0 +1,77 @@
  1
+require "spec_helper"
  2
+
  3
+describe Kumade::RakeTaskRunner do
  4
+  let(:runner) { stub("runner", :success => true) }
  5
+
  6
+  context "when the task doesn't exist" do
  7
+    subject { Kumade::RakeTaskRunner.new("bogus:task", runner) }
  8
+
  9
+    it "does not notify the user that the task was run successfully" do
  10
+      subject.invoke
  11
+      runner.should have_received(:success).never
  12
+    end
  13
+  end
  14
+
  15
+  context "when Rakefile exists" do
  16
+    subject { Kumade::RakeTaskRunner.new("bogus:task", runner) }
  17
+
  18
+    before do
  19
+      File.stubs(:exist?).with("Rakefile").returns(true)
  20
+    end
  21
+
  22
+    it "loads the Rakefile" do
  23
+      subject.stubs(:load).with("Rakefile")
  24
+      subject.invoke
  25
+      subject.should have_received(:load).with("Rakefile")
  26
+    end
  27
+  end
  28
+
  29
+  context "when the task exists" do
  30
+    let(:task_name)    { "kumade:test:custom_task_name" }
  31
+    let(:invoked_task) { stub("invoked", :invoke! => false) }
  32
+
  33
+    before do
  34
+      Rake::Task.define_task task_name do
  35
+        invoked_task.invoke!
  36
+      end
  37
+    end
  38
+
  39
+    after do
  40
+      Rake::Task[task_name].reenable
  41
+    end
  42
+
  43
+    subject { Kumade::RakeTaskRunner.new(task_name, runner) }
  44
+
  45
+    context "when pretending" do
  46
+      before do
  47
+        Kumade.configuration.pretending = true
  48
+      end
  49
+
  50
+      it "notifies the user that the task was run successfully" do
  51
+        subject.invoke
  52
+        runner.should have_received(:success).with("Running rake task: #{task_name}")
  53
+      end
  54
+
  55
+      it "does not invoke the task" do
  56
+        subject.invoke
  57
+        invoked_task.should have_received(:invoke!).never
  58
+      end
  59
+    end
  60
+
  61
+    context "when not pretending" do
  62
+      before do
  63
+        Kumade.configuration.pretending = false
  64
+      end
  65
+
  66
+      it "notifies the user that the task was run successfully" do
  67
+        subject.invoke
  68
+        runner.should have_received(:success).with("Running rake task: #{task_name}")
  69
+      end
  70
+
  71
+      it "invokes the task" do
  72
+        subject.invoke
  73
+        invoked_task.should have_received(:invoke!).once
  74
+      end
  75
+    end
  76
+  end
  77
+end
2  spec/spec_helper.rb
@@ -21,7 +21,7 @@ def remove_remote(remote_name)
21 21
 
22 22
 
23 23
 spec_dir = Pathname.new(File.expand_path(File.dirname(__FILE__)))
24  
-Dir[spec_dir.join('support', '**')].each {|f| require File.expand_path(f) }
  24
+Dir[spec_dir.join('support', '**', "*.rb")].each {|f| require File.expand_path(f) }
25 25
 
26 26
 RSpec.configure do |config|
27 27
   config.mock_with :mocha
41  spec/support/define_constant.rb
... ...
@@ -0,0 +1,41 @@
  1
+module DefineConstant
  2
+  def define_constant(path, base = Object, &block)
  3
+    namespace, class_name = *constant_path(path)
  4
+    klass = Class.new(base)
  5
+    namespace.const_set(class_name, klass)
  6
+    klass.class_eval(&block) if block_given?
  7
+    @defined_constants << path
  8
+    klass
  9
+  end
  10
+
  11
+  def clear_generated_constants
  12
+    @defined_constants.reverse.each do |path|
  13
+      namespace, class_name = *constant_path(path)
  14
+      if namespace.const_defined?(class_name)
  15
+        namespace.send(:remove_const, class_name)
  16
+      end
  17
+    end
  18
+
  19
+    @defined_constants.clear
  20
+  end
  21
+
  22
+  private
  23
+
  24
+  def constant_path(constant_name)
  25
+    names = constant_name.split('::')
  26
+    class_name = names.pop
  27
+    namespace = names.inject(Object) { |result, name| result.const_get(name) }
  28
+    [namespace, class_name]
  29
+  end
  30
+end
  31
+
  32
+RSpec.configure do |config|
  33
+  config.include DefineConstant
  34
+  config.before do
  35
+    @defined_constants = []
  36
+  end
  37
+
  38
+  config.after do
  39
+    clear_generated_constants
  40
+  end
  41
+end
6  spec/support/shared_examples/packager.rb
... ...
@@ -0,0 +1,6 @@
  1
+share_examples_for "packager" do
  2
+  its(:assets_path) { should_not be_nil }
  3
+
  4
+  it { should respond_to(:installed?) }
  5
+  it { should respond_to(:package) }
  6
+end

1 note on commit d854184

Marcos Tapajós

Great!

Mike Burns

Shouldn't the module code go inside the begin, since the module code references Jammit?

Gabe Berke-Williams

Packagers only run if packager.installed? is true, so that's effectively a guard over the entire class.

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