git-topic 0.1.6.4 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,502 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'fileutils'
4
-
5
-
6
- describe GitTopic do
7
-
8
- before( :all ) do
9
- @starting_dir = Dir.pwd
10
- @user = ENV['USER'] || `whoami`
11
- end
12
-
13
- before( :each ) do
14
- # setup the directories
15
- FileUtils.rm_rf './tmp'
16
- FileUtils.mkdir './tmp'
17
-
18
- # Copy our repos into tmp
19
- %w(fresh in-progress).each do |d|
20
- FileUtils.mkdir "./tmp/#{d}"
21
- FileUtils.cp_r "spec/template/#{d}", "./tmp/#{d}/.git"
22
- end
23
- FileUtils.cp_r "spec/template/origin", './tmp'
24
- FileUtils.cp_r "spec/template/origin-fresh", './tmp'
25
-
26
- %w(origin origin-fresh fresh in-progress).each do |repo|
27
- # set template branches to their proper name (i.e. matching @user)
28
- Dir.chdir "./tmp/#{repo}"
29
- git_branches.each do |orig_name|
30
- new_name = orig_name.gsub( 'USER', @user )
31
- system(
32
- "git branch -m #{orig_name} #{new_name}"
33
- ) unless orig_name == new_name
34
- system "git fetch --prune > /dev/null 2> /dev/null"
35
- end
36
- Dir.chdir @starting_dir
37
- end
38
- Dir.chdir './tmp'
39
-
40
- # capture output
41
- @output = ''
42
- @err = ''
43
- $stdout.stub!( :write ) { |*args| @output.<<( *args )}
44
- $stderr.stub!( :write ) { |*args| @err.<<( *args )}
45
- end
46
-
47
- after( :each ) { Dir.chdir @starting_dir }
48
-
49
-
50
- # helpers # {{{
51
-
52
- def use_repo( repo )
53
- Dir.chdir( repo )
54
- # Exit if e.g. GIT_DIR is set
55
- raise "Spec error" unless `git rev-parse --git-dir`.chomp == '.git'
56
- end
57
-
58
-
59
- def git_branch
60
- all_branches = `git branch --no-color`.split( "\n" )
61
- current_branch = all_branches.find{|b| b =~ /^\*/}
62
-
63
- current_branch[ 2..-1 ] unless current_branch.nil?
64
- end
65
-
66
- def git_head
67
- `git rev-parse HEAD`.chomp
68
- end
69
-
70
- def git_origin_master
71
- `git rev-parse origin/master`.chomp
72
- end
73
-
74
- def git_config( key )
75
- `git config #{key}`.chomp
76
- end
77
-
78
- def git_branch_merge
79
- git_config "branch.#{git_branch}.merge"
80
- end
81
-
82
- def git_branch_remote
83
- git_config "branch.#{git_branch}.remote"
84
- end
85
-
86
- def git_branches
87
- `git branch --no-color`.split( "\n" ).map do |bn|
88
- bn.gsub /^\*?\s*/, ''
89
- end
90
- end
91
-
92
- def git_remote_branches
93
- `git branch -r --no-color`.split( "\n" ).map do |bn|
94
- bn.gsub! %r{^\s*origin/}, ''
95
- bn.gsub! %r{ ->.*$}, ''
96
- bn
97
- end
98
- end
99
-
100
- # }}}
101
-
102
- describe "#work_on" do
103
-
104
- share_examples_for "#work_on general cases" do
105
-
106
- it "
107
- should trim namespaces from args and output a warning
108
- ".oneline do
109
-
110
- git_branch.should_not == "wip/davidjh/topic"
111
- GitTopic.work_on "wip/#{@user}/topic"
112
- git_branch.should == "wip/davidjh/topic"
113
- end
114
-
115
- end
116
-
117
- describe "in fresh" do
118
- before( :each ) { use_repo( 'fresh' )}
119
- after( :each ) { Dir.chdir( '..' )}
120
-
121
- it_should_behave_like "#work_on general cases"
122
-
123
- it "
124
- should create (and switch to) a new branch with a name that matches the
125
- given topic, in the wip namespace. A remote tracking branch should also
126
- be set up.
127
- ".oneline do
128
-
129
- GitTopic.work_on( 'topic' )
130
- git_branch.should == "wip/#{@user}/topic"
131
- git_branch_remote.should == 'origin'
132
- git_branch_merge.should == "refs/heads/wip/#{@user}/topic"
133
- end
134
-
135
- it "should fail if no topic is given" do
136
- lambda { GitTopic.work_on( nil )}.should raise_error
137
- end
138
-
139
- end
140
-
141
- describe "in in-progress" do
142
-
143
- before( :each ) { use_repo( 'in-progress' )}
144
- after( :each ) { Dir.chdir( '..' )}
145
-
146
- it_should_behave_like "#work_on general cases"
147
-
148
- it "should switch to (rather than create) an existing topic branch" do
149
- git_branches.should include( "wip/#{@user}/zombie-basic" )
150
- lambda{ GitTopic.work_on 'zombie-basic' }.should_not raise_error
151
-
152
- git_branch.should == "wip/#{@user}/zombie-basic"
153
- end
154
-
155
- it "
156
- should use (and then destroy) the rejected branch for the topic, if one
157
- exists
158
- ".oneline do
159
-
160
- git_remote_branches.should include( "rejected/#{@user}/krakens" )
161
- GitTopic.work_on 'krakens'
162
- git_branch.should == "wip/#{@user}/krakens"
163
- git_remote_branches.should_not include( "rejected/#{@user}/krakens" )
164
- git_remote_branches.should include( "wip/#{@user}/krakens" )
165
- git_head.should == '44ffd9c9c8b52b201659e3ad318cdad6ec836b46'
166
- end
167
-
168
- it "
169
- should use (and then destroy) the review branch for the topic, if one
170
- exists
171
- ".oneline do
172
-
173
- git_remote_branches.should include( "rejected/#{@user}/krakens" )
174
- GitTopic.work_on 'krakens'
175
- git_branch.should == "wip/#{@user}/krakens"
176
- git_remote_branches.should_not include( "rejected/#{@user}/krakens" )
177
- git_remote_branches.should include( "wip/#{@user}/krakens" )
178
- git_head.should == '44ffd9c9c8b52b201659e3ad318cdad6ec836b46'
179
- end
180
-
181
- end
182
-
183
- pending "with no origin"
184
-
185
- end
186
-
187
- describe "#done" do
188
-
189
- describe "in in-progress" do
190
-
191
- before( :each ) { use_repo 'in-progress' }
192
- after( :each ) { Dir.chdir '..' }
193
-
194
- describe "without an argument" do
195
-
196
- it "should fail if the working tree is dirty" do
197
- GitTopic.work_on 'zombie-basic'
198
- File.open( 'foo', 'w' ){|f| f.puts "some content" }
199
- system "git add -N foo"
200
-
201
- lambda{ GitTopic.done }.should raise_error
202
- end
203
-
204
- it "should fail if not on a wip branch" do
205
- `git checkout master > /dev/null 2> /dev/null`
206
- lambda{ GitTopic.done }.should raise_error
207
- end
208
-
209
- it "
210
- should push the wip branch to origin in the review namespace, delete the
211
- local branch, and leave the user on master
212
- ".oneline do
213
-
214
- git_branches.should include( "wip/#{@user}/zombie-basic" )
215
- git_remote_branches.should_not include( "review/#{@user}/zombie-basic" )
216
- GitTopic.work_on 'zombie-basic'
217
- GitTopic.done
218
-
219
- git_branches.should_not include( "wip/#{@user}/zombie-basic" )
220
- git_remote_branches.should include( "review/#{@user}/zombie-basic" )
221
- git_remote_branches.should_not include( "wip/#{@user}/zombie-basic" )
222
- git_branch.should == 'master'
223
- end
224
- end
225
-
226
- describe "with an argument" do
227
-
228
- it "should fail for non-wip branch arguments" do
229
- git_branches.should_not include( "wip/#{@user}/invalid-branch" )
230
- lambda{ GitTopic.done( 'invalid-branch' )}.should raise_error
231
- end
232
-
233
- it "should succeed for superfluous wip-branch arguments" do
234
- git_branches.should include( "wip/#{@user}/zombie-basic" )
235
- git_remote_branches.should_not include( "review/#{@user}/zombie-basic" )
236
- GitTopic.work_on 'zombie-basic'
237
- GitTopic.done( 'zombie-basic' )
238
-
239
- git_branches.should_not include( "wip/#{@user}/zombie-basic" )
240
- git_remote_branches.should include( "review/#{@user}/zombie-basic" )
241
- git_remote_branches.should_not include( "wip/#{@user}/zombie-basic" )
242
- git_branch.should == 'master'
243
- end
244
-
245
- it "
246
- should succeed for wip-branch arguments, and leave the user on the
247
- same branch
248
- ".oneline do
249
- git_branches.should include( "wip/#{@user}/pirates-advanced" )
250
- git_branches.should include( "wip/#{@user}/zombie-basic" )
251
- git_remote_branches.should_not include( "review/#{@user}/zombie-basic" )
252
-
253
- GitTopic.work_on 'pirates-advanced'
254
- GitTopic.done 'zombie-basic'
255
-
256
- git_branch.should == "wip/#{@user}/pirates-advanced"
257
- git_branches.should_not include( "wip/#{@user}/zombie-basic" )
258
- git_remote_branches.should include( "review/#{@user}/zombie-basic" )
259
- git_remote_branches.should_not include( "wip/#{@user}/zombie-basic" )
260
- end
261
-
262
- it "should succeed for fully-qualified wip-branch arguments" do
263
- git_branches.should include( "wip/#{@user}/zombie-basic" )
264
- git_remote_branches.should_not include( "review/#{@user}/zombie-basic" )
265
- GitTopic.done( "wip/#{@user}/zombie-basic" )
266
-
267
- git_branches.should_not include( "wip/#{@user}/zombie-basic" )
268
- git_remote_branches.should include( "review/#{@user}/zombie-basic" )
269
- git_remote_branches.should_not include( "wip/#{@user}/zombie-basic" )
270
- git_branch.should == 'master'
271
- end
272
- end
273
- end
274
- end
275
-
276
- describe "#status" do
277
-
278
- describe "with pending review branches" do
279
-
280
- before( :each ) { use_repo 'in-progress' }
281
- after( :each ) { Dir.chdir '..' }
282
-
283
-
284
- it "should not show my review branches, but it should show others'" do
285
- git_remote_branches.should include 'review/davidjh/pirates'
286
-
287
- GitTopic.status
288
- @output.should_not be_nil
289
-
290
- @output.should_not =~ /^#\s*pirates\s*$/m
291
- @output.should =~ /^#\s*ninja-basic\s*$/m
292
- @output.should =~ /^#\s*zombie-basic\s*$/m
293
- end
294
-
295
- it "should show my rejected topics" do
296
- git_remote_branches.should include 'rejected/davidjh/krakens'
297
- GitTopic.status
298
- @output.should_not be_nil
299
-
300
- @output.should =~ /^#\s*krakens\s*$/m
301
- end
302
-
303
- end
304
-
305
- describe "passed the --prepended flag" do
306
- before( :each ) { use_repo 'in-progress' }
307
- after( :each ) { Dir.chdir '..' }
308
-
309
- it "should invoke git status before producing its output" do
310
- GitTopic.status( :prepended => true )
311
- @output.should_not be_nil
312
- @output.should =~ /# On branch master/
313
- end
314
- end
315
-
316
- pending "with useless args"
317
-
318
- end
319
-
320
- describe "#review" do
321
-
322
- describe "with no review branches" do
323
- before( :each ) { use_repo 'fresh' }
324
- after( :each ) { Dir.chdir '..' }
325
-
326
- it "should report that there is nothing to do" do
327
- git_remote_branches.each do |b|
328
- b.should_not =~ /review/
329
- end
330
-
331
- GitTopic.review
332
- @output.should =~ /nothing to review/
333
- end
334
- end
335
-
336
- describe "with some review branches" do
337
- before( :each ) { use_repo 'in-progress' }
338
- after( :each ) { Dir.chdir '..' }
339
-
340
- it "
341
- should create a local tracking branch for the oldest remote review
342
- branch if none was specified
343
- " do
344
-
345
- git_remote_branches.should include 'review/user24601/zombie-basic'
346
- GitTopic.review
347
- git_branch.should == 'review/user24601/zombie-basic'
348
- git_branch_remote.should == 'origin'
349
- git_branch_merge.should == 'refs/heads/review/user24601/zombie-basic'
350
- end
351
-
352
- it "should create a local tracking branch for the specified topic" do
353
- git_remote_branches.should include 'review/user24601/ninja-basic'
354
- GitTopic.review( 'user24601/ninja-basic' )
355
- git_branch.should == 'review/user24601/ninja-basic'
356
- git_branch_remote.should == 'origin'
357
- git_branch_merge.should == 'refs/heads/review/user24601/ninja-basic'
358
- end
359
-
360
- it "
361
- should accept only a topic arg (vice <user>/<topic>) when the topic is
362
- unambiguous.
363
- ".oneline do
364
- git_remote_branches.should include 'review/user24601/ninja-basic'
365
- GitTopic.review( 'ninja-basic' )
366
- git_branch.should == 'review/user24601/ninja-basic'
367
- git_branch_remote.should == 'origin'
368
- git_branch_merge.should == 'refs/heads/review/user24601/ninja-basic'
369
- end
370
-
371
- it "should error if an illegal topic is specified" do
372
- lambda{ GitTopic.review( 'fakeuser/faketopic' )}.should raise_error
373
- end
374
- end
375
-
376
- pending "with non-branch arg"
377
- pending "with non-review-branch arg"
378
- pending "with my-review-branch arg"
379
-
380
- end
381
-
382
- describe "#accept" do
383
-
384
- describe "while on a review branch" do
385
- before( :each ) do
386
- use_repo 'in-progress'
387
- GitTopic.review 'user24601/zombie-basic'
388
- end
389
- after( :each ) { Dir.chdir '..' }
390
-
391
- describe "with no specified argument" do
392
- it "
393
- should merge to master, push master and destroy the local and remote
394
- branches when the merge is a fast-forward merge
395
- ".oneline do
396
-
397
- git_branch.should == 'review/user24601/zombie-basic'
398
- GitTopic.accept
399
- git_branch.should == 'master'
400
- git_branches.should_not include( 'review/user24601/zombie-basic' )
401
- git_remote_branches.should_not include( 'review/user24601/zombie-basic' )
402
-
403
- git_head.should == '0ce06c616769768f09f5e629cfcc68eabe3dee81'
404
- git_origin_master.should == '0ce06c616769768f09f5e629cfcc68eabe3dee81'
405
- end
406
- end
407
- end
408
-
409
- describe "while on a review branch that does not FF" do
410
- before( :each ) do
411
- use_repo 'in-progress'
412
- system "
413
- git checkout master > /dev/null 2> /dev/null &&
414
- git merge origin/wip/prevent-ff > /dev/null 2> /dev/null
415
- "
416
- @original_git_Head = git_head
417
- GitTopic.review 'user24601/zombie-basic'
418
- end
419
- after( :each ) { Dir.chdir '..' }
420
-
421
- it "should refuse to accept the review branch" do
422
- git_branch.should == 'review/user24601/zombie-basic'
423
- lambda{ GitTopic.accept }.should raise_error
424
- git_branch.should == 'review/user24601/zombie-basic'
425
- git_remote_branches.should include( 'review/user24601/zombie-basic' )
426
-
427
- system "git checkout master > /dev/null 2> /dev/null"
428
- git_head.should == @original_git_Head
429
- end
430
- end
431
-
432
- describe "while not on a review branch" do
433
- before( :each ) { use_repo 'in-progress' }
434
- after( :each ) { Dir.chdir '..' }
435
-
436
- it "should fail" do
437
- lambda{ GitTopic.accept }.should raise_error
438
- end
439
- end
440
-
441
- pending "with an arg"
442
-
443
- end
444
-
445
- describe "#reject" do
446
-
447
- describe "while on a review branch" do
448
- before( :each ) do
449
- use_repo 'in-progress'
450
- GitTopic.review 'user24601/zombie-basic'
451
- end
452
- after( :each ) { Dir.chdir '..' }
453
-
454
- describe "with no specified argument" do
455
- it "
456
- should move branch to the rejected namespace and destroy the local and
457
- remote review branches
458
- ".oneline do
459
-
460
- git_branch.should == 'review/user24601/zombie-basic'
461
- GitTopic.reject
462
- git_branch.should == 'master'
463
- git_branches.should_not include( 'review/user24601/zombie-basic' )
464
- git_remote_branches.should_not include( 'review/user24601/zombie-basic' )
465
- git_remote_branches.should include( 'rejected/user24601/zombie-basic' )
466
- end
467
- end
468
- end
469
-
470
- describe "while not on a review branch" do
471
- before( :each ) { use_repo 'in-progress' }
472
- after( :each ) { Dir.chdir '..' }
473
-
474
- it "should fail" do
475
- lambda{ GitTopic.reject }.should raise_error
476
- end
477
- end
478
-
479
- pending "with an arg"
480
-
481
- end
482
-
483
-
484
- describe "#install_aliases" do
485
- it "should install aliases" do
486
- GitTopic.install_aliases :local => true
487
- git_config( 'alias.work-on' ).should == 'topic work-on'
488
- git_config( 'alias.done' ).should == 'topic done'
489
- git_config( 'alias.review' ).should == 'topic review'
490
- git_config( 'alias.accept' ).should == 'topic accept'
491
- git_config( 'alias.reject' ).should == 'topic reject'
492
-
493
- git_config( 'alias.w' ).should == 'topic work-on'
494
- git_config( 'alias.r' ).should == 'topic review'
495
- git_config( 'alias.st' ).should == 'topic status --prepended'
496
- end
497
-
498
- pending "with an arg"
499
-
500
- end
501
- end
502
-