drnic-github 0.3.9 → 0.3.10

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.
@@ -1,700 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
-
3
- describe "github" do
4
- # -- home --
5
- specify "home should open the project home page" do
6
- running :home do
7
- setup_url_for
8
- @helper.should_receive(:open).once.with("https://github.com/user/project/tree/master")
9
- end
10
- end
11
-
12
- specify "home defunkt should open the home page of defunkt's fork" do
13
- running :home, "defunkt" do
14
- setup_url_for
15
- @helper.should_receive(:open).once.with("https://github.com/defunkt/project/tree/master")
16
- end
17
- end
18
-
19
- # -- browse --
20
- specify "browse should open the project home page with the current branch" do
21
- running :browse do
22
- setup_url_for
23
- setup_user_and_branch("user", "test-branch")
24
- @helper.should_receive(:open).once.with("https://github.com/user/project/tree/test-branch")
25
- end
26
- end
27
-
28
- specify "browse pending should open the project home page with the 'pending' branch" do
29
- running :browse, "pending" do
30
- setup_url_for
31
- setup_user_and_branch("user", "test-branch")
32
- @helper.should_receive(:open).once.with("https://github.com/user/project/tree/pending")
33
- end
34
- end
35
-
36
- specify "browse defunkt pending should open the home page of defunkt's fork with the 'pending' branch" do
37
- running :browse, "defunkt", "pending" do
38
- setup_url_for
39
- @helper.should_receive(:open).once.with("https://github.com/defunkt/project/tree/pending")
40
- end
41
- end
42
-
43
- specify "browse defunkt/pending should open the home page of defunkt's fork with the 'pending' branch" do
44
- running :browse, "defunkt/pending" do
45
- setup_url_for
46
- @helper.should_receive(:open).once.with("https://github.com/defunkt/project/tree/pending")
47
- end
48
- end
49
-
50
- # -- network --
51
- specify "network should open the network page for this repo" do
52
- running :network, 'web' do
53
- setup_url_for
54
- @helper.should_receive(:open).once.with("https://github.com/user/project/network")
55
- end
56
- end
57
-
58
- specify "network defunkt should open the network page for defunkt's fork" do
59
- running :network, 'web', "defunkt" do
60
- setup_url_for
61
- @helper.should_receive(:open).once.with("https://github.com/defunkt/project/network")
62
- end
63
- end
64
-
65
- # -- info --
66
- specify "info should show info for this project" do
67
- running :info do
68
- setup_url_for
69
- setup_remote(:origin, :user => "user", :ssh => true)
70
- setup_remote(:defunkt)
71
- setup_remote(:external, :url => "home:/path/to/project.git")
72
- stdout.should == <<-EOF
73
- == Info for project
74
- You are user
75
- Currently tracking:
76
- - defunkt (as defunkt)
77
- - home:/path/to/project.git (as external)
78
- - user (as origin)
79
- EOF
80
- end
81
- end
82
-
83
- # -- track --
84
- specify "track defunkt should track a new remote for defunkt" do
85
- running :track, "defunkt" do
86
- setup_url_for
87
- @helper.should_receive(:tracking?).with("defunkt").once.and_return(false)
88
- @command.should_receive(:git).with("remote add defunkt git://github.com/defunkt/project.git").once
89
- end
90
- end
91
-
92
- specify "track --private defunkt should track a new remote for defunkt using ssh" do
93
- running :track, "--private", "defunkt" do
94
- setup_url_for
95
- @helper.should_receive(:tracking?).with("defunkt").and_return(false)
96
- @command.should_receive(:git).with("remote add defunkt git@github.com:defunkt/project.git")
97
- end
98
- end
99
-
100
- specify "track --ssh defunkt should be equivalent to track --private defunkt" do
101
- running :track, "--ssh", "defunkt" do
102
- setup_url_for
103
- @helper.should_receive(:tracking?).with("defunkt").and_return(false)
104
- @command.should_receive(:git).with("remote add defunkt git@github.com:defunkt/project.git")
105
- end
106
- end
107
-
108
- specify "track defunkt should die if the defunkt remote exists" do
109
- running :track, "defunkt" do
110
- setup_url_for
111
- @helper.should_receive(:tracking?).with("defunkt").once.and_return(true)
112
- @command.should_receive(:die).with("Already tracking defunkt").and_return { raise "Died" }
113
- self.should raise_error(RuntimeError)
114
- end
115
- end
116
-
117
- specify "track should die with no args" do
118
- running :track do
119
- @command.should_receive(:die).with("Specify a user to track").and_return { raise "Died" }
120
- self.should raise_error(RuntimeError)
121
- end
122
- end
123
-
124
- specify "track should accept user/project syntax" do
125
- running :track, "defunkt/github-gem.git" do
126
- setup_url_for
127
- @helper.should_receive(:tracking?).with("defunkt").and_return false
128
- @command.should_receive(:git).with("remote add defunkt git://github.com/defunkt/github-gem.git")
129
- end
130
- end
131
-
132
- specify "track defunkt/github-gem.git should function with no origin remote" do
133
- running :track, "defunkt/github-gem.git" do
134
- @helper.stub!(:url_for).with("origin").and_return ""
135
- @helper.stub!(:tracking?).and_return false
136
- @command.should_receive(:git).with("remote add defunkt git://github.com/defunkt/github-gem.git")
137
- self.should_not raise_error(SystemExit)
138
- stderr.should_not =~ /^Error/
139
- end
140
- end
141
-
142
- specify "track origin defunkt/github-gem should track defunkt/github-gem as the origin remote" do
143
- running :track, "origin", "defunkt/github-gem" do
144
- @helper.stub!(:url_for).with("origin").and_return ""
145
- @helper.stub!(:tracking?).and_return false
146
- @command.should_receive(:git).with("remote add origin git://github.com/defunkt/github-gem.git")
147
- stderr.should_not =~ /^Error/
148
- end
149
- end
150
-
151
- specify "track --private origin defunkt/github-gem should track defunkt/github-gem as the origin remote using ssh" do
152
- running :track, "--private", "origin", "defunkt/github-gem" do
153
- @helper.stub!(:url_for).with("origin").and_return ""
154
- @helper.stub!(:tracking?).and_return false
155
- @command.should_receive(:git).with("remote add origin git@github.com:defunkt/github-gem.git")
156
- stderr.should_not =~ /^Error/
157
- end
158
- end
159
-
160
- # -- fetch --
161
- specify "fetch should die with no args" do
162
- running :fetch do
163
- @command.should_receive(:die).with("Specify a user to pull from").and_return { raise "Died "}
164
- self.should raise_error(RuntimeError)
165
- end
166
- end
167
-
168
- specify "pull defunkt should start tracking defunkt if they're not already tracked" do
169
- running :pull, "defunkt" do
170
- mock_members 'defunkt'
171
- setup_remote(:origin, :user => "user", :ssh => true)
172
- setup_remote(:external, :url => "home:/path/to/project.git")
173
- GitHub.should_receive(:invoke).with(:track, "defunkt").and_return { raise "Tracked" }
174
- self.should raise_error("Tracked")
175
- end
176
- end
177
-
178
- specify "pull defunkt should create defunkt/master and pull from the defunkt remote" do
179
- running :pull, "defunkt" do
180
- mock_members 'defunkt'
181
- setup_remote(:defunkt)
182
- @helper.should_receive(:branch_dirty?).and_return false
183
- @command.should_receive(:git).with("fetch defunkt").ordered
184
- @command.should_receive(:git_exec).with("checkout -b defunkt/master defunkt/master").ordered
185
- stdout.should == "Switching to defunkt-master\n"
186
- end
187
- end
188
-
189
- specify "pull defunkt should switch to pre-existing defunkt/master and pull from the defunkt remote" do
190
- running :pull, "defunkt" do
191
- mock_members 'defunkt'
192
- setup_remote(:defunkt)
193
- @helper.should_receive(:branch_dirty?).and_return true
194
- @command.should_receive(:die).with("Unable to switch branches, your current branch has uncommitted changes").and_return { raise "Died" }
195
- self.should raise_error(RuntimeError)
196
- end
197
- end
198
-
199
- specify "fetch defunkt/wip should create defunkt/wip and fetch from wip branch on defunkt remote" do
200
- running :fetch, "defunkt/wip" do
201
- setup_remote(:defunkt, :remote_branches => ["master", "wip"])
202
- @helper.should_receive(:branch_dirty?).and_return false
203
- @command.should_receive(:git).with("fetch defunkt wip:refs/remotes/defunkt/wip").ordered
204
- @command.should_receive(:git).with("update-ref refs/heads/defunkt/wip refs/remotes/defunkt/wip").ordered
205
- @command.should_receive(:git_exec).with("checkout defunkt/wip").ordered
206
- stdout.should == "Fetching defunkt/wip\n"
207
- end
208
- end
209
-
210
- specify "fetch --merge defunkt should fetch from defunkt remote into current branch" do
211
- running :fetch, "--merge", "defunkt" do
212
- setup_remote(:defunkt)
213
- @helper.should_receive(:branch_dirty?).and_return false
214
- @command.should_receive(:git).with("fetch defunkt master:refs/remotes/defunkt/master").ordered
215
- @command.should_receive(:git).with("update-ref refs/heads/defunkt/master refs/remotes/defunkt/master").ordered
216
- @command.should_receive(:git_exec).with("checkout defunkt/master").ordered
217
- stdout.should == "Fetching defunkt/master\n"
218
- end
219
- end
220
-
221
- # -- fetch --
222
- specify "fetch should die with no args" do
223
- running :fetch do
224
- @command.should_receive(:die).with("Specify a user to pull from").and_return { raise "Died" }
225
- self.should raise_error(RuntimeError)
226
- end
227
- end
228
-
229
- specify "fetch defunkt should start tracking defunkt if they're not already tracked" do
230
- running :fetch, "defunkt" do
231
- setup_remote(:origin, :user => "user", :ssh => true)
232
- setup_remote(:external, :url => "home:/path/to/project.git")
233
- GitHub.should_receive(:invoke).with(:track, "defunkt").and_return { raise "Tracked" }
234
- self.should raise_error("Tracked")
235
- end
236
- end
237
-
238
- specify "fetch defunkt should create defunkt/master and fetch from the defunkt remote" do
239
- running :fetch, "defunkt" do
240
- setup_remote(:defunkt)
241
- @helper.should_receive(:branch_dirty?).and_return false
242
- @command.should_receive(:git).with("fetch defunkt master:refs/remotes/defunkt/master").ordered
243
- @command.should_receive(:git).with("update-ref refs/heads/defunkt/master refs/remotes/defunkt/master").ordered
244
- @command.should_receive(:git_exec).with("checkout defunkt/master").ordered
245
- stdout.should == "Fetching defunkt/master\n"
246
- end
247
- end
248
-
249
- specify "pull defunkt wip should create defunkt/wip and pull from wip branch on defunkt remote" do
250
- running :pull, "defunkt", "wip" do
251
- mock_members 'defunkt'
252
- setup_remote(:defunkt)
253
- @helper.should_receive(:branch_dirty?).and_return true
254
- @command.should_receive(:die).with("Unable to switch branches, your current branch has uncommitted changes").and_return { raise "Died" }
255
- self.should raise_error(RuntimeError)
256
- end
257
- end
258
-
259
- specify "pull defunkt/wip should switch to pre-existing defunkt/wip and pull from wip branch on defunkt remote" do
260
- running :pull, "defunkt/wip" do
261
- mock_members 'defunkt'
262
- setup_remote(:defunkt)
263
- @helper.should_receive(:branch_dirty?).and_return false
264
- @command.should_receive(:git).with("fetch defunkt").ordered
265
- @command.should_receive(:git_exec).with("checkout -b defunkt/wip defunkt/wip").ordered
266
- stdout.should == "Switching to defunkt-wip\n"
267
- end
268
- end
269
-
270
- specify "pull --merge defunkt should pull from defunkt remote into current branch" do
271
- running :pull, "--merge", "defunkt" do
272
- mock_members 'defunkt'
273
- setup_remote(:defunkt)
274
- @helper.should_receive(:branch_dirty?).and_return false
275
- @command.should_receive(:git_exec).with("pull defunkt master")
276
- end
277
- end
278
-
279
- specify "pull falls through for non-recognized commands" do
280
- running :pull, 'remote' do
281
- mock_members 'defunkt'
282
- setup_remote(:defunkt)
283
- @command.should_receive(:git_exec).with("pull remote")
284
- end
285
- end
286
-
287
- specify "pull passes along args when falling through" do
288
- running :pull, 'remote', '--stat' do
289
- mock_members 'defunkt'
290
- @command.should_receive(:git_exec).with("pull remote --stat")
291
- end
292
- end
293
-
294
- # -- clone --
295
- specify "clone should die with no args" do
296
- running :clone do
297
- @command.should_receive(:die).with("Specify a user to pull from").and_return { raise "Died" }
298
- self.should raise_error(RuntimeError)
299
- end
300
- end
301
-
302
- specify "clone should fall through with just one arg" do
303
- running :clone, "git://git.kernel.org/linux.git" do
304
- @command.should_receive(:git_exec).with("clone git://git.kernel.org/linux.git")
305
- end
306
- end
307
-
308
- specify "clone defunkt github-gem should clone the repo" do
309
- running :clone, "defunkt", "github-gem" do
310
- @command.should_receive(:current_user?).and_return(nil)
311
- @command.should_receive(:git_exec).with("clone git://github.com/defunkt/github-gem.git")
312
- end
313
- end
314
-
315
- specify "clone defunkt/github-gem should clone the repo" do
316
- running :clone, "defunkt/github-gem" do
317
- @command.should_receive(:current_user?).and_return(nil)
318
- @command.should_receive(:git_exec).with("clone git://github.com/defunkt/github-gem.git")
319
- end
320
- end
321
-
322
- specify "clone --ssh defunkt github-gem should clone the repo using the private URL" do
323
- running :clone, "--ssh", "defunkt", "github-gem" do
324
- @command.should_receive(:git_exec).with("clone git@github.com:defunkt/github-gem.git")
325
- end
326
- end
327
-
328
- specify "clone defunkt github-gem repo should clone the repo into the dir 'repo'" do
329
- running :clone, "defunkt", "github-gem", "repo" do
330
- @command.should_receive(:current_user?).and_return(nil)
331
- @command.should_receive(:git_exec).with("clone git://github.com/defunkt/github-gem.git repo")
332
- end
333
- end
334
-
335
- specify "clone defunkt/github-gem repo should clone the repo into the dir 'repo'" do
336
- running :clone, "defunkt/github-gem", "repo" do
337
- @command.should_receive(:current_user?).and_return(nil)
338
- @command.should_receive(:git_exec).with("clone git://github.com/defunkt/github-gem.git repo")
339
- end
340
- end
341
-
342
- specify "clone --ssh defunkt github-gem repo should clone the repo using the private URL into the dir 'repo'" do
343
- running :clone, "--ssh", "defunkt", "github-gem", "repo" do
344
- @command.should_receive(:git_exec).with("clone git@github.com:defunkt/github-gem.git repo")
345
- end
346
- end
347
-
348
- specify "clone defunkt/github-gem repo should clone the repo into the dir 'repo'" do
349
- running :clone, "defunkt/github-gem", "repo" do
350
- @command.should_receive(:current_user?).and_return(nil)
351
- @command.should_receive(:git_exec).with("clone git://github.com/defunkt/github-gem.git repo")
352
- end
353
- end
354
-
355
- specify "clone a selected repo after showing search results" do
356
- running :clone, "--search", "github-gem" do
357
- json = StringIO.new '{"repositories":[' +
358
- '{"name":"github-gem","size":300,"followers":499,"username":"defunkt","language":"Ruby","fork":false,"id":"repo-1653","type":"repo","pushed":"2008-12-04T03:14:00Z","forks":59,"description":"The official `github` command line helper for simplifying your GitHub experience.","score":3.4152448,"created":"2008-02-28T09:35:34Z"},' +
359
- '{"name":"github-gem-builder","size":76,"followers":26,"username":"pjhyett","language":"Ruby","fork":false,"id":"repo-67489","type":"repo","pushed":"2008-11-04T04:54:57Z","forks":3,"description":"The scripts used to build RubyGems on GitHub","score":3.4152448,"created":"2008-10-24T22:29:32Z"}' +
360
- ']}'
361
- json.rewind
362
- @command.should_receive(:open).with("http://github.com/api/v1/json/search/github-gem").and_return(json)
363
- GitHub::UI.should_receive(:display_select_list).with(%w[defunkt/github-gem pjhyett/github-gem-builder]).
364
- and_return("defunkt/github-gem")
365
- @command.should_receive(:current_user?).and_return(nil)
366
- @command.should_receive(:git_exec).with("clone git://github.com/defunkt/github-gem.git")
367
- end
368
- end
369
-
370
- # -- fork --
371
- specify "fork should print out help" do
372
- running :fork do
373
- @helper.should_receive(:remotes).and_return({})
374
- @command.should_receive(:die).with("Specify a user/project to fork, or run from within a repo").and_return { raise "Died" }
375
- self.should raise_error(RuntimeError)
376
- end
377
- end
378
-
379
- specify "fork this repo should create github fork and replace origin remote" do
380
- running :fork do
381
- setup_github_token
382
- setup_url_for "origin", "defunkt", "github-gem"
383
- setup_remote "origin", :user => "defunkt", :project => "github-gem"
384
- setup_user_and_branch
385
- @command.should_receive(:sh).with("curl -F 'login=drnic' -F 'token=MY_GITHUB_TOKEN' http://github.com/defunkt/github-gem/fork")
386
- @command.should_receive(:git, "config remote.origin.url git@github.com/drnic/github-gem.git")
387
- stdout.should == "defunkt/github-gem forked\n"
388
- end
389
- end
390
-
391
- specify "fork a user/project repo" do
392
- running :fork, "defunkt/github-gem" do
393
- setup_github_token
394
- @command.should_receive(:sh).with("curl -F 'login=drnic' -F 'token=MY_GITHUB_TOKEN' http://github.com/defunkt/github-gem/fork")
395
- @command.should_receive(:git_exec, "clone git://github.com/defunkt/github-gem.git")
396
- stdout.should == "Giving GitHub a moment to create the fork...\n"
397
- end
398
- end
399
-
400
- specify "fork a user project repo" do
401
- running :fork, "defunkt", "github-gem" do
402
- setup_github_token
403
- @command.should_receive("sh").with("curl -F 'login=drnic' -F 'token=MY_GITHUB_TOKEN' http://github.com/defunkt/github-gem/fork")
404
- @command.should_receive(:git_exec, "clone git://github.com/defunkt/github-gem.git")
405
- stdout.should == "Giving GitHub a moment to create the fork...\n"
406
- end
407
- end
408
-
409
- # -- create-from-local --
410
-
411
-
412
- # -- search --
413
- specify "search finds multiple results" do
414
- running :search, "github-gem" do
415
- json = StringIO.new '{"repositories":[' +
416
- '{"name":"github-gem","size":300,"followers":499,"username":"defunkt","language":"Ruby","fork":false,"id":"repo-1653","type":"repo","pushed":"2008-12-04T03:14:00Z","forks":59,"description":"The official `github` command line helper for simplifying your GitHub experience.","score":3.4152448,"created":"2008-02-28T09:35:34Z"},' +
417
- '{"name":"github-gem-builder","size":76,"followers":26,"username":"pjhyett","language":"Ruby","fork":false,"id":"repo-67489","type":"repo","pushed":"2008-11-04T04:54:57Z","forks":3,"description":"The scripts used to build RubyGems on GitHub","score":3.4152448,"created":"2008-10-24T22:29:32Z"}' +
418
- ']}'
419
- json.rewind
420
- @command.should_receive(:open).with("http://github.com/api/v1/json/search/github-gem").and_return(json)
421
- stdout.should == "defunkt/github-gem\npjhyett/github-gem-builder\n"
422
- end
423
- end
424
-
425
- specify "search finds no results" do
426
- running :search, "xxxxxxxxxx" do
427
- json = StringIO.new '{"repositories":[]}'
428
- json.rewind
429
- @command.should_receive(:open).with("http://github.com/api/v1/json/search/xxxxxxxxxx").and_return(json)
430
- stdout.should == "No results found\n"
431
- end
432
- end
433
-
434
- specify "search shows usage if no arguments given" do
435
- running :search do
436
- @command.should_receive(:die).with("Usage: github search [query]").and_return { raise "Died" }
437
- self.should raise_error(RuntimeError)
438
- end
439
- end
440
-
441
-
442
- # -- pull-request --
443
- specify "pull-request should die with no args" do
444
- running :'pull-request' do
445
- setup_url_for
446
- @command.should_receive(:die).with("Specify a user for the pull request").and_return { raise "Died" }
447
- self.should raise_error(RuntimeError)
448
- end
449
- end
450
-
451
- specify "pull-request user should track user if untracked" do
452
- running :'pull-request', "user" do
453
- setup_url_for
454
- setup_remote :origin, :user => "kballard"
455
- setup_remote :defunkt
456
- GitHub.should_receive(:invoke).with(:track, "user").and_return { raise "Tracked" }
457
- self.should raise_error("Tracked")
458
- end
459
- end
460
-
461
- specify "pull-request user/branch should generate a pull request" do
462
- running :'pull-request', "user/branch" do
463
- setup_url_for
464
- setup_remote :origin, :user => "kballard"
465
- setup_remote :user
466
- @command.should_receive(:git_exec).with("request-pull user/branch origin")
467
- end
468
- end
469
-
470
- specify "pull-request user should generate a pull request with branch master" do
471
- running :'pull-request', "user" do
472
- setup_url_for
473
- setup_remote :origin, :user => "kballard"
474
- setup_remote :user
475
- @command.should_receive(:git_exec).with("request-pull user/master origin")
476
- end
477
- end
478
-
479
- specify "pull-request user branch should generate a pull request" do
480
- running:'pull-request', "user", "branch" do
481
- setup_url_for
482
- setup_remote :origin, :user => "kballard"
483
- setup_remote :user
484
- @command.should_receive(:git_exec).with("request-pull user/branch origin")
485
- end
486
- end
487
-
488
- # -- fallthrough --
489
- specify "should fall through to actual git commands" do
490
- running :commit do
491
- @command.should_receive(:git_exec).with(["commit", []])
492
- end
493
- end
494
-
495
- specify "should pass along arguments when falling through" do
496
- running :commit, '-a', '-m', 'yo mama' do
497
- @command.should_receive(:git_exec).with(["commit", ["-a", "-m", 'yo mama']])
498
- end
499
- end
500
-
501
- # -- default --
502
- specify "should print the default message" do
503
- running :default do
504
- GitHub.should_receive(:descriptions).any_number_of_times.and_return({
505
- "home" => "Open the home page",
506
- "browsing" => "Browse the github page for this branch",
507
- "commands" => "description",
508
- "tracking" => "Track a new repo"
509
- })
510
- GitHub.should_receive(:flag_descriptions).any_number_of_times.and_return({
511
- "home" => {:flag => "Flag description"},
512
- "browsing" => {},
513
- "commands" => {},
514
- "tracking" => {:flag1 => "Flag one", :flag2 => "Flag two"}
515
- })
516
- @command.should_receive(:puts).with(<<-EOS.gsub(/^ /, ''))
517
- Usage: github command <space separated arguments>
518
- Available commands:
519
- browsing => Browse the github page for this branch
520
- commands => description
521
- home => Open the home page
522
- --flag: Flag description
523
- tracking => Track a new repo
524
- --flag1: Flag one
525
- --flag2: Flag two
526
- EOS
527
- end
528
- end
529
-
530
- # -----------------
531
-
532
- def running(cmd, *args, &block)
533
- Runner.new(self, cmd, *args, &block).run
534
- end
535
-
536
- class Runner
537
- include SetupMethods
538
-
539
- def initialize(parent, cmd, *args, &block)
540
- @cmd_name = cmd.to_s
541
- @command = GitHub.find_command(cmd)
542
- @helper = @command.helper
543
- @args = args
544
- @block = block
545
- @parent = parent
546
- end
547
-
548
- def run
549
- self.instance_eval &@block
550
- mock_remotes unless @remotes.nil?
551
- GitHub.should_receive(:load).with(GitHub::BasePath + "/commands/commands.rb")
552
- GitHub.should_receive(:load).with(GitHub::BasePath + "/commands/helpers.rb")
553
- GitHub.should_receive(:load).with(GitHub::BasePath + "/commands/network.rb")
554
- GitHub.should_receive(:load).with(GitHub::BasePath + "/commands/issues.rb")
555
- args = @args.clone
556
- GitHub.parse_options(args) # strip out the flags
557
- GitHub.should_receive(:invoke).with(@cmd_name, *args).and_return do
558
- GitHub.send(GitHub.send(:__mock_proxy).send(:munge, :invoke), @cmd_name, *args)
559
- end
560
- invoke = lambda { GitHub.activate([@cmd_name, *@args]) }
561
- if @expected_result
562
- expectation, result = @expected_result
563
- case result
564
- when Spec::Matchers::RaiseError, Spec::Matchers::Change, Spec::Matchers::ThrowSymbol
565
- invoke.send expectation, result
566
- else
567
- invoke.call.send expectation, result
568
- end
569
- else
570
- invoke.call
571
- end
572
- @stdout_mock.invoke unless @stdout_mock.nil?
573
- @stderr_mock.invoke unless @stderr_mock.nil?
574
- end
575
-
576
- def setup_remote(remote, options = {:user => nil, :project => "project", :remote_branches => nil})
577
- @remotes ||= {}
578
- @remote_branches ||= {}
579
- user = options[:user] || remote
580
- project = options[:project]
581
- ssh = options[:ssh]
582
- url = options[:url]
583
- remote_branches = options[:remote_branches] || ["master"]
584
- if url
585
- @remotes[remote] = url
586
- elsif ssh
587
- @remotes[remote] = "git@github.com:#{user}/#{project}.git"
588
- else
589
- @remotes[remote] = "git://github.com/#{user}/#{project}.git"
590
- end
591
-
592
- @remote_branches[remote] = (@remote_branches[remote] || Array.new) | remote_branches
593
- @helper.should_receive(:remote_branch?).any_number_of_times.and_return do |remote, branch|
594
- @remote_branches.fetch(remote.to_sym,[]).include?(branch)
595
- end
596
- end
597
-
598
- def mock_remotes()
599
- @helper.should_receive(:remotes).any_number_of_times.and_return(@remotes)
600
- end
601
-
602
- def mock_members(members)
603
- @helper.should_receive(:network_members).any_number_of_times.and_return(members)
604
- end
605
-
606
- def should(result)
607
- @expected_result = [:should, result]
608
- end
609
-
610
- def should_not(result)
611
- @expected_result = [:should_not, result]
612
- end
613
-
614
- def stdout
615
- if @stdout_mock.nil?
616
- output = ""
617
- @stdout_mock = DeferredMock.new(output)
618
- $stdout.should_receive(:write).any_number_of_times do |str|
619
- output << str
620
- end
621
- end
622
- @stdout_mock
623
- end
624
-
625
- def stderr
626
- if @stderr_mock.nil?
627
- output = ""
628
- @stderr_mock = DeferredMock.new(output)
629
- $stderr.should_receive(:write).any_number_of_times do |str|
630
- output << str
631
- end
632
- end
633
- @stderr_mock
634
- end
635
-
636
- class DeferredMock
637
- def initialize(obj = nil)
638
- @obj = obj
639
- @calls = []
640
- @expectations = []
641
- end
642
-
643
- attr_reader :obj
644
-
645
- def invoke(obj = nil)
646
- obj ||= @obj
647
- @calls.each do |sym, args|
648
- obj.send sym, *args
649
- end
650
- @expectations.each do |exp|
651
- exp.invoke
652
- end
653
- end
654
-
655
- def should(*args)
656
- if args.empty?
657
- exp = Expectation.new(self, :should)
658
- @expectations << exp
659
- exp
660
- else
661
- @calls << [:should, args]
662
- end
663
- end
664
-
665
- def should_not(*args)
666
- if args.empty?
667
- exp = Expectation.new(self, :should_not)
668
- @expectations << exp
669
- exp
670
- else
671
- @calls << [:should_not, args]
672
- end
673
- end
674
-
675
- class Expectation
676
- def initialize(mock, call)
677
- @mock = mock
678
- @call = call
679
- @calls = []
680
- end
681
-
682
- undef_method *(instance_methods.map { |x| x.to_sym } - [:__id__, :__send__])
683
-
684
- def invoke
685
- @calls.each do |sym, args|
686
- (@mock.obj.send @call).send sym, *args
687
- end
688
- end
689
-
690
- def method_missing(sym, *args)
691
- @calls << [sym, args]
692
- end
693
- end
694
- end
695
-
696
- def method_missing(sym, *args)
697
- @parent.send sym, *args
698
- end
699
- end
700
- end