This repository has been archived by the owner on Jun 8, 2019. It is now read-only.
/
deployer_spec.rb
594 lines (471 loc) · 14.8 KB
/
deployer_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
require 'spec_helper'
describe Kumade::Deployer, "#pre_deploy" do
before { subject.stub(:say) }
it "calls the correct methods in order" do
%w(
ensure_clean_git
package_assets
sync_github
).each do |task|
subject.should_receive(task).ordered.and_return(true)
end
subject.pre_deploy
end
it "syncs to github" do
%w(
ensure_clean_git
package_assets
).each do |task|
subject.stub(task)
end
subject.should_receive(:sync_github)
subject.pre_deploy
end
end
describe Kumade::Deployer, "#deploy" do
let(:remote_name){ 'staging' }
let(:app_name){ 'kumade-staging' }
before do
subject.stub(:say)
force_add_heroku_remote(remote_name, app_name)
end
it "calls the correct methods in order" do
subject.stub(:run => true)
subject.should_receive(:ensure_heroku_remote_exists).
ordered
subject.should_receive(:pre_deploy).
ordered.
and_return(true)
subject.should_receive(:sync_heroku).
ordered.
and_return(true)
subject.should_receive(:heroku_migrate).
ordered
subject.should_receive(:post_deploy)
subject.deploy
end
end
describe Kumade::Deployer, "#sync_github" do
before { subject.stub(:say) }
it "calls `git push`" do
subject.should_receive(:run).
with("git push origin master").
and_return(true)
subject.sync_github
end
context "when `git push` fails" do
before { subject.stub(:run => false) }
it "prints an error message" do
subject.should_receive(:error).with("Failed to push master -> origin")
subject.sync_github
end
end
context "when syncing github succeeds" do
before { subject.stub(:run => true) }
it "does not raise an error" do
subject.should_not_receive(:error)
subject.sync_github
end
it "prints a success message" do
subject.should_receive(:success).with("Pushed master -> origin")
subject.sync_github
end
end
end
describe Kumade::Deployer, "#sync_heroku" do
let(:environment) { 'my-env' }
subject { Kumade::Deployer.new(environment) }
before { subject.stub(:say) }
context "when deploy branch exists" do
it "should calls `git push -f`" do
subject.stub(:branch_exist?).with("deploy").and_return(true)
subject.should_receive(:run).
with("git push -f #{environment} deploy:master").
and_return(true)
subject.sync_heroku
end
end
context "when deploy branch doesn't exists" do
it "should calls `git branch deploy` and `git push -f`" do
subject.stub(:branch_exist?).with("deploy").and_return(false)
subject.should_receive(:run).
with("git branch deploy").
and_return(true)
subject.should_receive(:run).
with("git push -f #{environment} deploy:master").
and_return(true)
subject.sync_heroku
end
end
context "when syncing to heroku fails" do
before do
subject.stub(:run => false)
end
it "prints an error" do
subject.should_receive(:error).twice
subject.sync_heroku
end
end
context "when syncing to heroku succeeds" do
before do
subject.stub(:run => true)
subject.stub(:say)
end
it "does not raise an error" do
subject.should_not_receive(:error)
subject.sync_heroku
end
it "prints a success message" do
subject.should_receive(:success).
with("Force pushed master -> #{environment}")
subject.sync_heroku
end
end
end
describe Kumade::Deployer, "#ensure_clean_git" do
before { subject.stub(:say) }
context "when git is dirty" do
before { subject.stub(:git_dirty? => true) }
it "prints an error" do
subject.should_receive(:error).with("Cannot deploy: repo is not clean.")
subject.ensure_clean_git
end
end
context "when git is clean" do
before { subject.stub(:git_dirty? => false) }
it "prints a success message" do
subject.should_not_receive(:error)
subject.should_receive(:success).with("Git repo is clean")
subject.ensure_clean_git
end
end
end
describe Kumade::Deployer, "#package_assets" do
context "with Jammit installed" do
it "calls package_with_jammit" do
subject.should_receive(:package_with_jammit)
subject.package_assets
end
end
context "with Jammit not installed" do
before { subject.stub(:jammit_installed? => false) }
it "does not call package_with_jammit" do
subject.should_not_receive(:package_with_jammit)
subject.package_assets
end
end
context "with More installed" do
before do
subject.stub(:jammit_installed? => false)
subject.stub(:more_installed? => true)
end
it "calls package_with_more" do
subject.should_receive(:package_with_more)
subject.package_assets
end
end
context "with More not installed" do
before do
subject.stub(:jammit_installed? => false)
subject.stub(:more_installed? => false)
end
it "does not call package_with_more" do
subject.should_not_receive(:package_with_more)
subject.package_assets
end
end
context "with custom rake task installed" do
before do
subject.stub(:jammit_installed? => false,
:more_installed? => false,
:invoke_custom_task => nil,
:custom_task? => true)
end
it "invokes custom task" do
subject.should_receive(:invoke_custom_task)
subject.package_assets
end
end
context "with custom rake task not installed" do
before do
subject.stub(:jammit_installed? => false,
:more_installed? => false,
:invoke_custom_task => nil,
:custom_task? => false)
end
it "does not invoke custom task" do
subject.should_not_receive(:invoke_custom_task)
subject.package_assets
end
end
end
describe Kumade::Deployer, "#package_with_jammit" do
before do
subject.stub(:git_add_and_commit_all_assets_in)
subject.stub(:say)
Jammit.stub(:package!)
end
it "calls Jammit.package!" do
Jammit.should_receive(:package!).once
subject.package_with_jammit
end
context "with updated assets" do
before { subject.stub(:git_dirty? => true) }
it "prints the correct message" do
subject.should_receive(:success).with("Packaged assets with Jammit")
subject.package_with_jammit
end
it "calls git_add_and_commit_all_assets_in" do
subject.stub(:jammit_assets_path => 'jammit-assets')
subject.should_receive(:git_add_and_commit_all_assets_in).
with('jammit-assets').
and_return(true)
subject.package_with_jammit
end
end
it "prints an error if packaging failed" do
Jammit.stub(:package!) do
raise Jammit::MissingConfiguration.new("random Jammit error")
end
subject.should_receive(:error).with("Error: Jammit::MissingConfiguration: random Jammit error")
subject.package_with_jammit
end
end
describe Kumade::Deployer, "#invoke_custom_task" do
before do
subject.stub(:say)
Rake::Task.stub(:[] => task)
end
let(:task) { stub('task', :invoke => nil) }
it "calls deploy task" do
Rake::Task.should_receive(:[]).with("kumade:before_asset_compilation")
task.should_receive(:invoke)
subject.invoke_custom_task
end
end
describe Kumade::Deployer, "#package_with_more" do
before do
subject.stub(:git_add_and_commit_all_assets_in => true,
:more_assets_path => 'assets')
subject.stub(:say)
end
it "calls the more:generate task" do
subject.should_receive(:run).with("bundle exec rake more:generate")
subject.package_with_more
end
context "with changed assets" do
it "prints a success message" do
subject.stub(:run).with("bundle exec rake more:generate")
subject.stub(:git_dirty? => true)
subject.should_receive(:success).with("Packaged assets with More")
subject.package_with_more
end
it "calls git_add_and_commit_all_assets_in if assets were added" do
subject.stub(:git_dirty? => true,
:more_assets_path => 'blerg')
subject.stub(:run).with("bundle exec rake more:generate")
subject.should_receive(:git_add_and_commit_all_assets_in).
with('blerg').
and_return(true)
subject.package_with_more
end
end
context "with no changed assets" do
it "prints no message" do
subject.stub(:run).with("bundle exec rake more:generate")
subject.stub(:git_dirty? => false)
subject.should_not_receive(:say)
subject.package_with_more
end
it "does not call git_add_and_commit_all_more_assets" do
subject.stub(:run).with("bundle exec rake more:generate")
subject.stub(:git_dirty? => false)
subject.should_not_receive(:git_add_and_commit_all_assets_in)
subject.package_with_more
end
end
it "prints an error if packaging failed" do
subject.stub(:run) do |arg|
if arg == "bundle exec rake more:generate"
raise "blerg"
end
end
subject.should_receive(:error).with("Error: RuntimeError: blerg")
subject.package_with_more
end
end
describe Kumade::Deployer, "#git_add_and_commit_all_assets_in" do
before do
subject.stub(:run => true)
subject.stub(:say)
end
it "prints a success message" do
subject.should_receive(:success).with("Added and committed all assets")
subject.git_add_and_commit_all_assets_in('blerg')
end
it "runs the correct commands" do
subject.should_receive(:run).
with("git checkout -b deploy && git add -f blerg && git commit -m 'Compiled assets'")
subject.git_add_and_commit_all_assets_in('blerg')
end
it "prints an error if it could not add and commit assets" do
subject.stub(:run => false)
subject.should_receive(:error).with("Cannot deploy: couldn't commit assets")
subject.git_add_and_commit_all_assets_in('blerg')
end
end
describe Kumade::Deployer, "#jammit_assets_path" do
it "returns the correct asset path" do
Jammit.stub(:package_path => 'blerg')
current_dir = File.expand_path(Dir.pwd)
subject.jammit_assets_path.should == File.join(current_dir, 'public', 'blerg')
end
end
describe Kumade::Deployer, "#more_assets_path" do
it "returns the correct asset path" do
module ::Less
class More
def self.destination_path
'blerg'
end
end
end
subject.more_assets_path.should == 'public/blerg'
end
end
describe Kumade::Deployer, "#jammit_installed?" do
it "returns true because it's loaded by the Gemfile" do
Kumade::Deployer.new.jammit_installed?.should be_true
end
it "returns false if jammit is not installed" do
Kumade::Deployer.new.jammit_installed?.should be_true
end
end
describe Kumade::Deployer, "#more_installed?" do
before do
if defined?(Less)
Object.send(:remove_const, :Less)
end
end
it "returns false if it does not find Less::More" do
Kumade::Deployer.new.more_installed?.should be_false
end
it "returns true if it finds Less::More" do
module Less
class More
end
end
Kumade::Deployer.new.more_installed?.should be_true
end
end
describe Kumade::Deployer, "#custom_task?" do
before do
Rake::Task.clear
end
it "returns true if it task found" do
namespace :kumade do
task :before_asset_compilation do
end
end
Kumade::Deployer.new.custom_task?.should be_true
end
it "returns false if task not found" do
Kumade::Deployer.new.custom_task?.should be_false
end
end
describe Kumade::Deployer, "#heroku_migrate" do
let(:environment){ 'staging' }
let(:app_name){ 'sushi' }
before do
subject.stub(:say)
force_add_heroku_remote(environment, app_name)
end
it "runs db:migrate with the correct app" do
subject.stub(:run => true)
subject.should_receive(:heroku).
with("rake db:migrate", app_name)
subject.should_receive(:success).with("Migrated #{app_name}")
subject.heroku_migrate
end
end
describe Kumade::Deployer, "#ensure_heroku_remote_exists" do
let(:environment){ 'staging' }
let(:bad_environment){ 'bad' }
let(:staging_app_name) { 'staging-sushi' }
before do
subject.stub(:say)
force_add_heroku_remote(environment, staging_app_name)
`git remote add #{bad_environment} blerg@example.com`
end
context "when the remote points to Heroku" do
subject { Kumade::Deployer.new(environment) }
it "does not print an error" do
subject.should_not_receive(:error)
subject.ensure_heroku_remote_exists
end
it "prints a success message" do
subject.should_receive(:success).with("#{environment} is a Heroku remote")
subject.ensure_heroku_remote_exists
end
end
context "when the remote does not exist" do
subject { Kumade::Deployer.new(environment) }
before { remove_remote(environment) }
it "prints an error" do
subject.should_receive(:error).with(%{Cannot deploy: "#{environment}" remote does not exist})
subject.ensure_heroku_remote_exists
end
end
context "when the remote does not point to Heroku" do
subject { Kumade::Deployer.new(bad_environment) }
it "prints an error" do
subject.should_receive(:error).with(%{Cannot deploy: "#{bad_environment}" remote does not point to Heroku})
subject.ensure_heroku_remote_exists
end
end
end
describe Kumade::Deployer, "#remote_exists?" do
let(:remote_name){ 'staging' }
before { force_add_heroku_remote(remote_name, 'i-am-a-heroku-app') }
it "returns true if the remote exists" do
subject.remote_exists?(remote_name).should be_true
end
it "returns false if the remote does not exist" do
remove_remote(remote_name)
subject.remote_exists?(remote_name).should be_false
end
end
describe Kumade::Deployer, "#heroku" do
let(:app_name){ 'sushi' }
context "when on Cedar" do
subject { Kumade::Deployer.new('staging', false, cedar = true) }
it "runs commands with `run`" do
subject.should_receive(:run_or_error).with("bundle exec heroku run rake --app #{app_name}", //)
subject.heroku("rake", app_name)
end
end
context "when not on Cedar" do
subject { Kumade::Deployer.new('staging', false, cedar = false) }
it "runs commands without `run`" do
subject.should_receive(:run_or_error).with("bundle exec heroku rake --app #{app_name}", //)
subject.heroku("rake", app_name)
end
end
end
describe Kumade::Deployer, "#success" do
it "exists" do
subject.should respond_to(:success)
end
end
describe Kumade::Deployer, "#error" do
it "exists" do
subject.should respond_to(:error)
end
end
describe Kumade::Deployer, "#post_deploy" do
before { subject.stub(:run => true, :say => true) }
it "cleans up the deploy branch" do
subject.should_receive(:run).with('git checkout master && git branch -D deploy')
subject.post_deploy
end
end