This repository has been archived by the owner on Jun 8, 2019. It is now read-only.
/
deployer_spec.rb
456 lines (367 loc) · 11.6 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
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' }
before do
subject.stub(:say)
force_add_heroku_remote(remote_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
let(:git_mock) { mock() }
before { subject.stub(:git => git_mock) }
it "should call @git.push" do
git_mock.should_receive(:push).with("master")
subject.sync_github
end
end
describe Kumade::Deployer, "#sync_heroku" do
let(:environment) { 'my-env' }
subject { Kumade::Deployer.new(environment) }
let(:git_mock) { mock() }
before { subject.stub(:git => git_mock) }
it "should call git.create and git.push" do
git_mock.should_receive(:create).with("deploy")
git_mock.should_receive(:push).with("deploy:master", environment, true)
subject.sync_heroku
end
end
describe Kumade::Deployer, "#ensure_clean_git" do
let(:git_mock) { mock() }
before { subject.stub(:git => git_mock) }
it "should call git.ensure_clean_git" do
git_mock.should_receive(:ensure_clean_git)
subject.ensure_clean_git
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 => mock(: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 => mock(: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 => mock(: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 => mock(: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 => mock(: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
let(:git_mock) { mock() }
before { subject.stub(:git => git_mock) }
it "should call git.add_and_commit_all_in" do
git_mock.should_receive(:add_and_commit_all_in).with("dir", 'deploy', 'Compiled assets', "Added and committed all assets", "couldn't commit assets")
subject.git_add_and_commit_all_assets_in("dir")
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' }
before do
subject.stub(:say)
force_add_heroku_remote(environment)
end
it "runs db:migrate with the correct app" do
subject.stub(:run => true)
subject.should_receive(:heroku).
with("rake db:migrate")
subject.should_receive(:success).with("Migrated staging")
subject.heroku_migrate
end
end
describe Kumade::Deployer, "#ensure_heroku_remote_exists" do
let(:environment){ 'staging' }
let(:bad_environment){ 'bad' }
before do
subject.stub(:say)
force_add_heroku_remote(environment)
`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, "#heroku" do
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 --remote staging", //)
subject.heroku("rake")
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 --remote staging", //)
subject.heroku("rake")
end
end
end
describe Kumade::Deployer, "#post_deploy" do
let(:git_mock) { mock() }
before { subject.stub(:git => git_mock) }
it "should call git.delete" do
git_mock.should_receive(:delete).with('deploy', 'master')
subject.post_deploy
end
end