kumade 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,21 +6,23 @@ module Kumade
6
6
  attr_reader :environment
7
7
  end
8
8
 
9
- def self.run(args=ARGV, out=$stdout)
9
+ def self.run(args=ARGV, out=StringIO.new)
10
10
  @out = out
11
11
  @options = parse_arguments!(args)
12
12
  @environment = args.shift || 'staging'
13
13
 
14
- deploy
14
+ swapping_stdout_for(@out) do
15
+ deploy
16
+ end
15
17
  end
16
18
 
17
19
  def self.deploy
18
20
  if pretending?
19
- @out.puts "==> In Pretend Mode"
21
+ puts "==> In Pretend Mode"
20
22
  end
21
- @out.puts "==> Deploying to: #{environment}"
22
- Deployer.new(environment, pretending?, @options[:cedar]).deploy
23
- @out.puts "==> Deployed to: #{environment}"
23
+ puts "==> Deploying to: #{environment}"
24
+ Deployer.new(environment, pretending?).deploy
25
+ puts "==> Deployed to: #{environment}"
24
26
  end
25
27
 
26
28
  def self.parse_arguments!(args)
@@ -32,17 +34,13 @@ module Kumade
32
34
  options[:pretend] = p
33
35
  end
34
36
 
35
- opts.on("-c", "--cedar", "Use this if your app is on cedar") do |cedar|
36
- options[:cedar] = cedar
37
- end
38
-
39
37
  opts.on_tail('-v', '--version', 'Show version') do
40
- @out.puts "kumade #{Kumade::VERSION}"
38
+ puts "kumade #{Kumade::VERSION}"
41
39
  exit
42
40
  end
43
41
 
44
42
  opts.on_tail('-h', '--help', 'Show this message') do
45
- @out.puts opts
43
+ puts opts
46
44
  exit
47
45
  end
48
46
  end.parse!(args)
@@ -50,6 +48,21 @@ module Kumade
50
48
  options
51
49
  end
52
50
 
51
+ def self.swapping_stdout_for(io)
52
+ begin
53
+ $real_stdout = $stdout
54
+ $stdout = io unless pretending?
55
+ yield
56
+ rescue Kumade::DeploymentError
57
+ unless pretending?
58
+ io.rewind
59
+ $real_stdout.print(io.read)
60
+ end
61
+ ensure
62
+ $stdout = $real_stdout
63
+ end
64
+ end
65
+
53
66
  def self.pretending?
54
67
  @options[:pretend]
55
68
  end
@@ -1,3 +1,3 @@
1
1
  module Kumade
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,5 +1,5 @@
1
1
  namespace :deploy do
2
- Kumade.environments.each do |environment|
2
+ Kumade::Git.environments.each do |environment|
3
3
  desc "Deploy to #{environment} environment"
4
4
  task environment do
5
5
  Kumade::Runner.run([environment])
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Kumade::Base, "#success" do
4
+ it "exists" do
5
+ subject.should respond_to(:success)
6
+ end
7
+ end
8
+
9
+ describe Kumade::Base, "#error" do
10
+ it "exists" do
11
+ subject.should respond_to(:error)
12
+ end
13
+
14
+ it "prints its message and raises its message" do
15
+ subject.should_receive(:say).with("==> ! I'm an error!", :red)
16
+ lambda{ subject.error("I'm an error!") }.should raise_error(Kumade::DeploymentError)
17
+ end
18
+ end
@@ -1,5 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
+ include Rake::DSL if defined?(Rake::DSL)
4
+
3
5
  describe Kumade::Deployer, "#pre_deploy" do
4
6
  before { subject.stub(:say) }
5
7
 
@@ -29,12 +31,11 @@ describe Kumade::Deployer, "#pre_deploy" do
29
31
  end
30
32
 
31
33
  describe Kumade::Deployer, "#deploy" do
32
- let(:remote_name){ 'staging' }
33
- let(:app_name){ 'kumade-staging' }
34
+ let(:remote_name) { 'staging' }
34
35
 
35
36
  before do
36
37
  subject.stub(:say)
37
- force_add_heroku_remote(remote_name, app_name)
38
+ force_add_heroku_remote(remote_name)
38
39
  end
39
40
 
40
41
  it "calls the correct methods in order" do
@@ -61,121 +62,39 @@ describe Kumade::Deployer, "#deploy" do
61
62
  end
62
63
 
63
64
  describe Kumade::Deployer, "#sync_github" do
64
- before { subject.stub(:say) }
65
-
66
- it "calls `git push`" do
67
- subject.should_receive(:run).
68
- with("git push origin master").
69
- and_return(true)
70
- subject.sync_github
71
- end
72
-
73
- context "when `git push` fails" do
74
- before { subject.stub(:run => false) }
75
-
76
- it "prints an error message" do
77
- subject.should_receive(:error).with("Failed to push master -> origin")
78
-
79
- subject.sync_github
80
- end
81
- end
65
+ let(:git_mock) { mock() }
82
66
 
83
- context "when syncing github succeeds" do
84
- before { subject.stub(:run => true) }
67
+ before { subject.stub(:git => git_mock) }
85
68
 
86
- it "does not raise an error" do
87
- subject.should_not_receive(:error)
88
- subject.sync_github
89
- end
90
-
91
- it "prints a success message" do
92
- subject.should_receive(:success).with("Pushed master -> origin")
93
-
94
- subject.sync_github
95
- end
69
+ it "calls git.push" do
70
+ git_mock.should_receive(:push).with("master")
71
+ subject.sync_github
96
72
  end
97
73
  end
98
74
 
99
75
  describe Kumade::Deployer, "#sync_heroku" do
100
76
  let(:environment) { 'my-env' }
101
- subject { Kumade::Deployer.new(environment) }
102
- before { subject.stub(:say) }
103
-
104
- context "when deploy branch exists" do
105
- it "should calls `git push -f`" do
106
- subject.stub(:branch_exist?).with("deploy").and_return(true)
107
- subject.should_receive(:run).
108
- with("git push -f #{environment} deploy:master").
109
- and_return(true)
110
- subject.sync_heroku
111
- end
112
- end
113
-
114
- context "when deploy branch doesn't exists" do
115
- it "should calls `git branch deploy` and `git push -f`" do
116
- subject.stub(:branch_exist?).with("deploy").and_return(false)
117
- subject.should_receive(:run).
118
- with("git branch deploy").
119
- and_return(true)
120
- subject.should_receive(:run).
121
- with("git push -f #{environment} deploy:master").
122
- and_return(true)
123
- subject.sync_heroku
124
- end
125
- end
77
+ let(:git_mock) { mock() }
126
78
 
127
- context "when syncing to heroku fails" do
128
- before do
129
- subject.stub(:run => false)
130
- end
131
-
132
- it "prints an error" do
133
- subject.should_receive(:error).twice
134
- subject.sync_heroku
135
- end
136
- end
137
-
138
- context "when syncing to heroku succeeds" do
139
- before do
140
- subject.stub(:run => true)
141
- subject.stub(:say)
142
- end
143
-
144
- it "does not raise an error" do
145
- subject.should_not_receive(:error)
146
- subject.sync_heroku
147
- end
79
+ subject { Kumade::Deployer.new(environment) }
148
80
 
149
- it "prints a success message" do
150
- subject.should_receive(:success).
151
- with("Force pushed master -> #{environment}")
81
+ before { subject.stub(:git => git_mock) }
152
82
 
153
- subject.sync_heroku
154
- end
83
+ it "calls git.create and git.push" do
84
+ git_mock.should_receive(:create).with("deploy")
85
+ git_mock.should_receive(:push).with("deploy:master", environment, true)
86
+ subject.sync_heroku
155
87
  end
156
88
  end
157
89
 
158
90
  describe Kumade::Deployer, "#ensure_clean_git" do
159
- before { subject.stub(:say) }
91
+ let(:git_mock) { mock() }
160
92
 
161
- context "when git is dirty" do
162
- before { subject.stub(:git_dirty? => true) }
93
+ before { subject.stub(:git => git_mock) }
163
94
 
164
- it "prints an error" do
165
- subject.should_receive(:error).with("Cannot deploy: repo is not clean.")
166
- subject.ensure_clean_git
167
- end
168
- end
169
-
170
- context "when git is clean" do
171
- before { subject.stub(:git_dirty? => false) }
172
-
173
- it "prints a success message" do
174
- subject.should_not_receive(:error)
175
- subject.should_receive(:success).with("Git repo is clean")
176
-
177
- subject.ensure_clean_git
178
- end
95
+ it "calls git.ensure_clean_git" do
96
+ git_mock.should_receive(:ensure_clean_git)
97
+ subject.ensure_clean_git
179
98
  end
180
99
  end
181
100
 
@@ -261,7 +180,7 @@ describe Kumade::Deployer, "#package_with_jammit" do
261
180
  end
262
181
 
263
182
  context "with updated assets" do
264
- before { subject.stub(:git_dirty? => true) }
183
+ before { subject.stub(:git => mock(:dirty? => true)) }
265
184
 
266
185
  it "prints the correct message" do
267
186
  subject.should_receive(:success).with("Packaged assets with Jammit")
@@ -290,12 +209,13 @@ describe Kumade::Deployer, "#package_with_jammit" do
290
209
  end
291
210
 
292
211
  describe Kumade::Deployer, "#invoke_custom_task" do
212
+ let(:task) { stub('task', :invoke => nil) }
213
+
293
214
  before do
215
+ subject.stub(:say)
294
216
  Rake::Task.stub(:[] => task)
295
217
  end
296
218
 
297
- let(:task) { stub('task', :invoke => nil) }
298
-
299
219
  it "calls deploy task" do
300
220
  Rake::Task.should_receive(:[]).with("kumade:before_asset_compilation")
301
221
  task.should_receive(:invoke)
@@ -318,14 +238,14 @@ describe Kumade::Deployer, "#package_with_more" do
318
238
  context "with changed assets" do
319
239
  it "prints a success message" do
320
240
  subject.stub(:run).with("bundle exec rake more:generate")
321
- subject.stub(:git_dirty? => true)
241
+ subject.stub(:git => mock(:dirty? => true))
322
242
  subject.should_receive(:success).with("Packaged assets with More")
323
243
 
324
244
  subject.package_with_more
325
245
  end
326
246
 
327
247
  it "calls git_add_and_commit_all_assets_in if assets were added" do
328
- subject.stub(:git_dirty? => true,
248
+ subject.stub(:git => mock(:dirty? => true),
329
249
  :more_assets_path => 'blerg')
330
250
  subject.stub(:run).with("bundle exec rake more:generate")
331
251
  subject.should_receive(:git_add_and_commit_all_assets_in).
@@ -339,7 +259,7 @@ describe Kumade::Deployer, "#package_with_more" do
339
259
  context "with no changed assets" do
340
260
  it "prints no message" do
341
261
  subject.stub(:run).with("bundle exec rake more:generate")
342
- subject.stub(:git_dirty? => false)
262
+ subject.stub(:git => mock(:dirty? => false))
343
263
  subject.should_not_receive(:say)
344
264
 
345
265
  subject.package_with_more
@@ -347,7 +267,7 @@ describe Kumade::Deployer, "#package_with_more" do
347
267
 
348
268
  it "does not call git_add_and_commit_all_more_assets" do
349
269
  subject.stub(:run).with("bundle exec rake more:generate")
350
- subject.stub(:git_dirty? => false)
270
+ subject.stub(:git => mock(:dirty? => false))
351
271
  subject.should_not_receive(:git_add_and_commit_all_assets_in)
352
272
 
353
273
  subject.package_with_more
@@ -368,29 +288,13 @@ describe Kumade::Deployer, "#package_with_more" do
368
288
  end
369
289
 
370
290
  describe Kumade::Deployer, "#git_add_and_commit_all_assets_in" do
371
- before do
372
- subject.stub(:run => true)
373
- subject.stub(:say)
374
- end
375
-
376
- it "prints a success message" do
377
- subject.should_receive(:success).with("Added and committed all assets")
378
-
379
- subject.git_add_and_commit_all_assets_in('blerg')
380
- end
381
-
382
- it "runs the correct commands" do
383
- subject.should_receive(:run).
384
- with("git checkout -b deploy && git add -f blerg && git commit -m 'Compiled assets'")
385
-
386
- subject.git_add_and_commit_all_assets_in('blerg')
387
- end
291
+ let(:git_mock) { mock() }
388
292
 
389
- it "prints an error if it could not add and commit assets" do
390
- subject.stub(:run => false)
391
- subject.should_receive(:error).with("Cannot deploy: couldn't commit assets")
293
+ before { subject.stub(:git => git_mock) }
392
294
 
393
- subject.git_add_and_commit_all_assets_in('blerg')
295
+ it "calls git.add_and_commit_all_in" do
296
+ git_mock.should_receive(:add_and_commit_all_in).with("dir", 'deploy', 'Compiled assets', "Added and committed all assets", "couldn't commit assets")
297
+ subject.git_add_and_commit_all_assets_in("dir")
394
298
  end
395
299
  end
396
300
 
@@ -465,32 +369,30 @@ describe Kumade::Deployer, "#custom_task?" do
465
369
  end
466
370
 
467
371
  describe Kumade::Deployer, "#heroku_migrate" do
468
- let(:environment){ 'staging' }
469
- let(:app_name){ 'sushi' }
372
+ let(:environment) { 'staging' }
470
373
 
471
374
  before do
472
375
  subject.stub(:say)
473
- force_add_heroku_remote(environment, app_name)
376
+ force_add_heroku_remote(environment)
474
377
  end
475
378
 
476
379
  it "runs db:migrate with the correct app" do
477
380
  subject.stub(:run => true)
478
381
  subject.should_receive(:heroku).
479
- with("rake db:migrate", app_name)
480
- subject.should_receive(:success).with("Migrated #{app_name}")
382
+ with("rake db:migrate")
383
+ subject.should_receive(:success).with("Migrated staging")
481
384
 
482
385
  subject.heroku_migrate
483
386
  end
484
387
  end
485
388
 
486
389
  describe Kumade::Deployer, "#ensure_heroku_remote_exists" do
487
- let(:environment){ 'staging' }
488
- let(:bad_environment){ 'bad' }
489
- let(:staging_app_name) { 'staging-sushi' }
390
+ let(:environment) { 'staging' }
391
+ let(:bad_environment) { 'bad' }
490
392
 
491
393
  before do
492
394
  subject.stub(:say)
493
- force_add_heroku_remote(environment, staging_app_name)
395
+ force_add_heroku_remote(environment)
494
396
  `git remote add #{bad_environment} blerg@example.com`
495
397
  end
496
398
 
@@ -533,61 +435,52 @@ describe Kumade::Deployer, "#ensure_heroku_remote_exists" do
533
435
  end
534
436
  end
535
437
 
536
- describe Kumade::Deployer, "#remote_exists?" do
537
- let(:remote_name){ 'staging' }
538
-
539
- before { force_add_heroku_remote(remote_name, 'i-am-a-heroku-app') }
540
-
541
- it "returns true if the remote exists" do
542
- subject.remote_exists?(remote_name).should be_true
438
+ describe Kumade::Deployer, "#cedar?" do
439
+ context "when on Cedar" do
440
+ subject { Kumade::Deployer.new('staging', false) }
441
+ before { subject.stub(:heroku).and_return(" bamboo\n* cedar\n") }
442
+ its(:cedar?) { should be_true }
543
443
  end
544
444
 
545
- it "returns false if the remote does not exist" do
546
- remove_remote(remote_name)
547
-
548
- subject.remote_exists?(remote_name).should be_false
445
+ context "when not on Cedar" do
446
+ subject { Kumade::Deployer.new('staging', false) }
447
+ before { subject.stub(:heroku).and_return("* bamboo\n cedar\n") }
448
+ its(:cedar?) { should be_false }
549
449
  end
550
450
  end
551
451
 
552
452
  describe Kumade::Deployer, "#heroku" do
553
- let(:app_name){ 'sushi' }
554
-
555
453
  context "when on Cedar" do
556
- subject { Kumade::Deployer.new('staging', false, cedar = true) }
557
-
454
+ subject { Kumade::Deployer.new('staging', false) }
455
+ before { subject.stub(:cedar?).and_return(true) }
558
456
  it "runs commands with `run`" do
559
- subject.should_receive(:run_or_error).with("bundle exec heroku run rake --app #{app_name}", //)
560
- subject.heroku("rake", app_name)
457
+ subject.should_receive(:run_or_error).with("bundle exec heroku run rake --remote staging", //)
458
+ subject.heroku("rake")
561
459
  end
562
460
  end
563
461
 
564
462
  context "when not on Cedar" do
565
- subject { Kumade::Deployer.new('staging', false, cedar = false) }
566
-
463
+ subject { Kumade::Deployer.new('staging', false) }
464
+ before { subject.stub(:cedar?).and_return(false) }
567
465
  it "runs commands without `run`" do
568
- subject.should_receive(:run_or_error).with("bundle exec heroku rake --app #{app_name}", //)
569
- subject.heroku("rake", app_name)
466
+ subject.should_receive(:run_or_error).with("bundle exec heroku rake --remote staging", //)
467
+ subject.heroku("rake")
570
468
  end
571
469
  end
572
470
  end
573
471
 
574
- describe Kumade::Deployer, "#success" do
575
- it "exists" do
576
- subject.should respond_to(:success)
577
- end
578
- end
579
-
580
- describe Kumade::Deployer, "#error" do
581
- it "exists" do
582
- subject.should respond_to(:error)
583
- end
584
- end
585
-
586
472
  describe Kumade::Deployer, "#post_deploy" do
587
- before { subject.stub(:run => true, :say => true) }
473
+ let(:git_mock) { mock() }
474
+
475
+ before { subject.stub(:git => git_mock) }
588
476
 
589
- it "cleans up the deploy branch" do
590
- subject.should_receive(:run).with('git checkout master && git branch -D deploy')
477
+ it "calls git.delete" do
478
+ git_mock.should_receive(:delete).with('deploy', 'master')
591
479
  subject.post_deploy
592
480
  end
481
+
482
+ it "prints its message and raises its message" do
483
+ subject.should_receive(:say).with("==> ! I'm an error!", :red)
484
+ lambda { subject.error("I'm an error!") }.should raise_error(Kumade::DeploymentError)
485
+ end
593
486
  end