hub_test.rb 22.9 KB
Newer Older
1 2
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'helper'
3
require 'webmock/test_unit'
C
Chris Wanstrath 已提交
4

5 6 7 8 9 10
WebMock::BodyPattern.class_eval do
  undef normalize_hash
  # override normalizing hash since it otherwise requires JSON
  def normalize_hash(hash) hash end
end

C
Chris Wanstrath 已提交
11
class HubTest < Test::Unit::TestCase
12 13 14 15 16
  if defined? WebMock::API
    include WebMock::API
  else
    include WebMock
  end
C
Chris Wanstrath 已提交
17

18 19 20 21 22 23 24 25 26
  COMMANDS = []

  Hub::Commands.class_eval do
    remove_method :command?
    define_method :command? do |name|
      COMMANDS.include?(name)
    end
  end

27
  def setup
28
    COMMANDS.replace %w[open groff]
29
    Hub::Context::DIRNAME.replace 'hub'
30
    Hub::Context::REMOTES.clear
31

32 33 34
    @git = Hub::Context::GIT_CONFIG.replace(Hash.new { |h, k|
      raise ArgumentError, "`git #{k}` not stubbed"
    }).update(
35
      'remote' => "mislav\norigin",
36 37 38
      'symbolic-ref -q HEAD' => 'refs/heads/master',
      'config github.user'   => 'tpw',
      'config github.token'  => 'abc123',
39 40
      'config --get-all remote.origin.url' => 'git://github.com/defunkt/hub.git',
      'config --get-all remote.mislav.url' => 'git://github.com/mislav/hub.git',
41 42 43 44
      'config branch.master.remote'  => 'origin',
      'config branch.master.merge'   => 'refs/heads/master',
      'config branch.feature.remote' => 'mislav',
      'config branch.feature.merge'  => 'refs/heads/experimental',
45 46
      'config --bool hub.http-clone' => 'false',
      'config core.repositoryformatversion' => '0'
47
    )
48
    super
49 50
  end

C
Chris Wanstrath 已提交
51
  def test_private_clone
52 53
    input   = "clone -p rtomayko/ronn"
    command = "git clone git@github.com:rtomayko/ronn.git"
C
Chris Wanstrath 已提交
54
    assert_command input, command
C
Chris Wanstrath 已提交
55 56 57
  end

  def test_public_clone
58 59
    input   = "clone rtomayko/ronn"
    command = "git clone git://github.com/rtomayko/ronn.git"
C
Chris Wanstrath 已提交
60
    assert_command input, command
C
Chris Wanstrath 已提交
61 62
  end

63 64 65 66 67 68 69 70 71 72 73 74
  def test_your_private_clone
    input   = "clone -p resque"
    command = "git clone git@github.com:tpw/resque.git"
    assert_command input, command
  end

  def test_your_public_clone
    input   = "clone resque"
    command = "git clone git://github.com/tpw/resque.git"
    assert_command input, command
  end

75 76 77
  def test_clone_with_arguments
    input   = "clone --bare -o master resque"
    command = "git clone --bare -o master git://github.com/tpw/resque.git"
78 79 80
    assert_command input, command
  end

81 82 83 84
  def test_clone_with_arguments_and_destination
    assert_forwarded "clone --template=one/two git://github.com/tpw/resque.git --origin master resquetastic"
  end

85 86
  def test_your_private_clone_fails_without_config
    out = hub("clone -p mustache") do
87
      stub_github_user(nil)
88 89 90 91 92 93 94
    end

    assert_equal "** No GitHub user set. See http://github.com/guides/local-github-config\n", out
  end

  def test_your_public_clone_fails_without_config
    out = hub("clone mustache") do
95
      stub_github_user(nil)
96 97 98 99 100
    end

    assert_equal "** No GitHub user set. See http://github.com/guides/local-github-config\n", out
  end

101
  def test_private_clone_left_alone
102
    assert_forwarded "clone git@github.com:rtomayko/ronn.git"
103 104 105
  end

  def test_public_clone_left_alone
106
    assert_forwarded "clone git://github.com/rtomayko/ronn.git"
107
  end
C
Chris Wanstrath 已提交
108 109

  def test_normal_public_clone_with_path
110
    assert_forwarded "clone git://github.com/rtomayko/ronn.git ronn-dev"
C
Chris Wanstrath 已提交
111
  end
112 113

  def test_normal_clone_from_path
114
    assert_forwarded "clone ./test"
115
  end
116

117 118 119 120 121 122 123 124 125 126 127 128
  def test_remote_origin
    input   = "remote add origin"
    command = "git remote add origin git://github.com/tpw/hub.git"
    assert_command input, command
  end

  def test_private_remote_origin
    input   = "remote add -p origin"
    command = "git remote add origin git@github.com:tpw/hub.git"
    assert_command input, command
  end

129 130 131 132 133 134
  def test_public_remote_origin_as_normal
    input   = "remote add origin http://github.com/defunkt/resque.git"
    command = "git remote add origin http://github.com/defunkt/resque.git"
    assert_command input, command
  end

135
  def test_remote_from_rel_path
136
    assert_forwarded "remote add origin ./path"
137 138 139
  end

  def test_remote_from_abs_path
140
    assert_forwarded "remote add origin /path"
141 142
  end

143
  def test_private_remote_origin_as_normal
144
    assert_forwarded "remote add origin git@github.com:defunkt/resque.git"
C
Chris Wanstrath 已提交
145 146
  end

S
Stephen Celis 已提交
147 148
  def test_public_submodule
    input   = "submodule add wycats/bundler vendor/bundler"
J
Justin Ridgewell 已提交
149 150
    command = "git submodule add git://github.com/wycats/bundler.git vendor/bundler"
    assert_command input, command
S
Stephen Celis 已提交
151 152 153 154
  end

  def test_private_submodule
    input   = "submodule add -p grit vendor/grit"
J
Justin Ridgewell 已提交
155 156 157 158 159 160 161 162
    command = "git submodule add git@github.com:tpw/grit.git vendor/grit"
    assert_command input, command
  end

  def test_submodule_branch
    input   = "submodule add -b ryppl ryppl/pip vendor/pip"
    command = "git submodule add -b ryppl git://github.com/ryppl/pip.git vendor/pip"
    assert_command input, command
S
Stephen Celis 已提交
163 164 165 166 167
  end

  def test_submodule_with_args
    input   = "submodule -q add --bare -- grit grit"
    command = "git submodule -q add --bare -- git://github.com/tpw/grit.git grit"
J
Justin Ridgewell 已提交
168
    assert_command input, command
S
Stephen Celis 已提交
169 170
  end

C
Chris Wanstrath 已提交
171
  def test_private_remote
172
    input   = "remote add -p rtomayko"
C
Chris Wanstrath 已提交
173 174
    command = "git remote add rtomayko git@github.com:rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
175 176 177
  end

  def test_public_remote
178
    input   = "remote add rtomayko"
C
Chris Wanstrath 已提交
179 180
    command = "git remote add rtomayko git://github.com/rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
181 182
  end

C
Chris Wanstrath 已提交
183 184 185 186 187 188
  def test_public_remote_f
    input   = "remote add -f rtomayko"
    command = "git remote add -f rtomayko git://github.com/rtomayko/hub.git"
    assert_command input, command
  end

189 190 191 192 193 194
  def test_named_public_remote
    input   = "remote add origin rtomayko"
    command = "git remote add origin git://github.com/rtomayko/hub.git"
    assert_command input, command
  end

195 196 197 198 199 200
  def test_named_public_remote_f
    input   = "remote add -f origin rtomayko"
    command = "git remote add -f origin git://github.com/rtomayko/hub.git"
    assert_command input, command
  end

C
Chris Wanstrath 已提交
201
  def test_private_remote_with_repo
202 203
    input   = "remote add -p jashkenas/coffee-script"
    command = "git remote add jashkenas git@github.com:jashkenas/coffee-script.git"
C
Chris Wanstrath 已提交
204 205 206 207
    assert_command input, command
  end

  def test_public_remote_with_repo
208 209
    input   = "remote add jashkenas/coffee-script"
    command = "git remote add jashkenas git://github.com/jashkenas/coffee-script.git"
210 211 212 213
    assert_command input, command
  end

  def test_public_remote_f_with_repo
214 215
    input   = "remote add -f jashkenas/coffee-script"
    command = "git remote add -f jashkenas git://github.com/jashkenas/coffee-script.git"
C
Chris Wanstrath 已提交
216 217 218
    assert_command input, command
  end

219
  def test_named_private_remote_with_repo
220 221
    input   = "remote add -p origin jashkenas/coffee-script"
    command = "git remote add origin git@github.com:jashkenas/coffee-script.git"
222 223 224
    assert_command input, command
  end

225
  def test_fetch_existing_remote
226
    assert_forwarded "fetch mislav"
227 228 229 230 231 232
  end

  def test_fetch_new_remote
    stub_remotes_group('xoebus', nil)
    stub_existing_fork('xoebus')

233 234 235
    assert_commands "git remote add xoebus git://github.com/xoebus/hub.git",
                    "git fetch xoebus",
                    "fetch xoebus"
236 237 238 239 240 241
  end

  def test_fetch_new_remote_with_options
    stub_remotes_group('xoebus', nil)
    stub_existing_fork('xoebus')

242 243 244
    assert_commands "git remote add xoebus git://github.com/xoebus/hub.git",
                    "git fetch --depth=1 --prune xoebus",
                    "fetch --depth=1 --prune xoebus"
245 246 247 248 249 250 251 252
  end

  def test_fetch_multiple_new_remotes
    stub_remotes_group('xoebus', nil)
    stub_remotes_group('rtomayko', nil)
    stub_existing_fork('xoebus')
    stub_existing_fork('rtomayko')

253 254 255 256
    assert_commands "git remote add xoebus git://github.com/xoebus/hub.git",
                    "git remote add rtomayko git://github.com/rtomayko/hub.git",
                    "git fetch --multiple xoebus rtomayko",
                    "fetch --multiple xoebus rtomayko"
257 258 259 260 261 262 263 264
  end

  def test_fetch_multiple_comma_separated_remotes
    stub_remotes_group('xoebus', nil)
    stub_remotes_group('rtomayko', nil)
    stub_existing_fork('xoebus')
    stub_existing_fork('rtomayko')

265 266 267 268
    assert_commands "git remote add xoebus git://github.com/xoebus/hub.git",
                    "git remote add rtomayko git://github.com/rtomayko/hub.git",
                    "git fetch --multiple xoebus rtomayko",
                    "fetch xoebus,rtomayko"
269 270 271 272 273 274 275 276 277 278 279 280 281 282
  end

  def test_fetch_multiple_new_remotes_with_filtering
    stub_remotes_group('xoebus', nil)
    stub_remotes_group('mygrp', 'one two')
    stub_remotes_group('typo', nil)
    stub_existing_fork('xoebus')
    stub_nonexisting_fork('typo')

    # mislav: existing remote; skipped
    # xoebus: new remote, fork exists; added
    # mygrp:  a remotes group; skipped
    # URL:    can't be a username; skipped
    # typo:   fork doesn't exist; skipped
283 284 285
    assert_commands "git remote add xoebus git://github.com/xoebus/hub.git",
                    "git fetch --multiple mislav xoebus mygrp git://example.com typo",
                    "fetch --multiple mislav xoebus mygrp git://example.com typo"
286 287
  end

288
  def test_cherry_pick
289
    assert_forwarded "cherry-pick a319d88"
290 291 292
  end

  def test_cherry_pick_url
293
    url = 'http://github.com/mislav/hub/commit/a319d88'
294
    assert_commands "git fetch mislav", "git cherry-pick a319d88", "cherry-pick #{url}"
295 296
  end

297 298
  def test_cherry_pick_url_with_fragment
    url = 'http://github.com/mislav/hub/commit/abcdef0123456789#comments'
299
    assert_commands "git fetch mislav", "git cherry-pick abcdef0123456789", "cherry-pick #{url}"
300 301
  end

302 303
  def test_cherry_pick_url_with_remote_add
    url = 'https://github.com/xoebus/hub/commit/a319d88'
304
    assert_commands "git remote add -f xoebus git://github.com/xoebus/hub.git",
305 306
                    "git cherry-pick a319d88",
                    "cherry-pick #{url}"
307 308 309 310
  end

  def test_cherry_pick_origin_url
    url = 'https://github.com/defunkt/hub/commit/a319d88'
311
    assert_commands "git fetch origin", "git cherry-pick a319d88", "cherry-pick #{url}"
312 313 314
  end

  def test_cherry_pick_github_user_notation
315
    assert_commands "git fetch mislav", "git cherry-pick a319d88", "cherry-pick mislav@a319d88"
316 317 318 319
  end

  def test_cherry_pick_github_user_repo_notation
    # not supported
320
    assert_forwarded "cherry-pick mislav/hubbub@a319d88"
321 322 323
  end

  def test_cherry_pick_github_notation_too_short
324
    assert_forwarded "cherry-pick mislav@a319"
325 326 327
  end

  def test_cherry_pick_github_notation_with_remote_add
328 329 330
    assert_commands "git remote add -f xoebus git://github.com/xoebus/hub.git",
                    "git cherry-pick a319d88",
                    "cherry-pick xoebus@a319d88"
331 332
  end

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
  def test_am_untouched
    assert_forwarded "am some.patch"
  end

  def test_am_pull_request
    with_tmpdir('/tmp/') do
      assert_commands "curl -#LA 'hub #{Hub::Version}' https://github.com/defunkt/hub/pull/55.patch -o /tmp/55.patch",
                      "git am --signoff /tmp/55.patch -p2",
                      "am --signoff https://github.com/defunkt/hub/pull/55 -p2"
    end
  end

  def test_am_commit_url
    with_tmpdir('/tmp/') do
      url = 'https://github.com/davidbalbert/hub/commit/fdb9921'

      assert_commands "curl -#LA 'hub #{Hub::Version}' #{url}.patch -o /tmp/fdb9921.patch",
                      "git am --signoff /tmp/fdb9921.patch -p2",
                      "am --signoff #{url} -p2"
    end
  end

C
Chris Wanstrath 已提交
355
  def test_init
356
    assert_commands "git init", "git remote add origin git@github.com:tpw/hub.git", "init -g"
357 358 359 360
  end

  def test_init_no_login
    out = hub("init -g") do
361
      stub_github_user(nil)
362 363 364
    end

    assert_equal "** No GitHub user set. See http://github.com/guides/local-github-config\n", out
C
Chris Wanstrath 已提交
365 366
  end

367
  def test_push_two
368 369
    assert_commands "git push origin cool-feature", "git push staging cool-feature",
                    "push origin,staging cool-feature"
370 371
  end

C
Chris Wanstrath 已提交
372
  def test_push_more
373 374 375 376
    assert_commands "git push origin cool-feature",
                    "git push staging cool-feature",
                    "git push qa cool-feature",
                    "push origin,staging,qa cool-feature"
C
Chris Wanstrath 已提交
377
  end
C
Chris Wanstrath 已提交
378

379
  def test_create
380
    stub_no_remotes
381
    stub_nonexisting_fork('tpw')
382 383 384
    stub_request(:post, "github.com/api/v2/yaml/repos/create").
      with(:body => { 'login'=>'tpw', 'token'=>'abc123', 'name' => 'hub' })

385
    expected = "remote add -f origin git@github.com:tpw/hub.git\n"
386
    expected << "created repository: tpw/hub\n"
387 388 389
    assert_equal expected, hub("create") { ENV['GIT'] = 'echo' }
  end

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
  def test_create_with_env_authentication
    stub_no_remotes
    stub_nonexisting_fork('mojombo')

    old_user  = ENV['GITHUB_USER']
    old_token = ENV['GITHUB_TOKEN']
    ENV['GITHUB_USER']  = 'mojombo'
    ENV['GITHUB_TOKEN'] = '123abc'

    stub_request(:post, "github.com/api/v2/yaml/repos/create").
      with(:body => { 'login'=>'mojombo', 'token'=>'123abc', 'name' => 'hub' })

    expected = "remote add -f origin git@github.com:mojombo/hub.git\n"
    expected << "created repository: mojombo/hub\n"
    assert_equal expected, hub("create") { ENV['GIT'] = 'echo' }

  ensure
    ENV['GITHUB_USER']  = old_user
    ENV['GITHUB_TOKEN'] = old_token
  end

411
  def test_create_private_repository
412
    stub_no_remotes
413
    stub_nonexisting_fork('tpw')
414 415 416
    stub_request(:post, "github.com/api/v2/yaml/repos/create").
      with(:body => { 'login'=>'tpw', 'token'=>'abc123', 'name' => 'hub', 'public' => '0' })

417
    expected = "remote add -f origin git@github.com:tpw/hub.git\n"
418
    expected << "created repository: tpw/hub\n"
419 420 421
    assert_equal expected, hub("create -p") { ENV['GIT'] = 'echo' }
  end

422
  def test_create_with_description_and_homepage
423
    stub_no_remotes
424
    stub_nonexisting_fork('tpw')
425 426 427 428 429
    stub_request(:post, "github.com/api/v2/yaml/repos/create").with(:body => {
      'login'=>'tpw', 'token'=>'abc123', 'name' => 'hub',
      'description' => 'toyproject', 'homepage' => 'http://example.com'
    })

430
    expected = "remote add -f origin git@github.com:tpw/hub.git\n"
431
    expected << "created repository: tpw/hub\n"
432
    assert_equal expected, hub("create -d toyproject -h http://example.com") { ENV['GIT'] = 'echo' }
433 434 435
  end

  def test_create_with_existing_repository
436
    stub_no_remotes
437 438 439 440
    stub_existing_fork('tpw')

    expected = "tpw/hub already exists on GitHub\n"
    expected << "remote add -f origin git@github.com:tpw/hub.git\n"
441
    expected << "set remote origin: tpw/hub\n"
442 443 444
    assert_equal expected, hub("create") { ENV['GIT'] = 'echo' }
  end

445
  def test_create_no_user
446
    stub_no_remotes
447 448 449 450 451 452
    out = hub("create") do
      stub_github_token(nil)
    end
    assert_equal "** No GitHub token set. See http://github.com/guides/local-github-config\n", out
  end

453
  def test_create_outside_git_repo
454
    stub_no_git_repo
455 456
    assert_equal "'create' must be run from inside a git repository\n", hub("create")
  end
457 458

  def test_create_origin_already_exists
C
Chris Wanstrath 已提交
459
    stub_nonexisting_fork('tpw')
460 461
    stub_request(:post, "github.com/api/v2/yaml/repos/create").
      with(:body => { 'login'=>'tpw', 'token'=>'abc123', 'name' => 'hub' })
C
Chris Wanstrath 已提交
462 463

    expected = "remote -v\ncreated repository: tpw/hub\n"
464 465
    assert_equal expected, hub("create") { ENV['GIT'] = 'echo' }
  end
C
Chris Wanstrath 已提交
466

467
  def test_fork
C
Chris Wanstrath 已提交
468
    stub_nonexisting_fork('tpw')
469 470
    stub_request(:post, "github.com/api/v2/yaml/repos/fork/defunkt/hub").
      with(:body => { 'login'=>'tpw', 'token'=>'abc123' })
C
Chris Wanstrath 已提交
471

C
Chris Wanstrath 已提交
472
    expected = "remote add -f tpw git@github.com:tpw/hub.git\n"
473 474 475 476 477
    expected << "new remote: tpw\n"
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end

  def test_fork_no_remote
478
    stub_nonexisting_fork('tpw')
479
    stub_request(:post, "github.com/api/v2/yaml/repos/fork/defunkt/hub")
C
Chris Wanstrath 已提交
480

481 482 483 484
    assert_equal "", hub("fork --no-remote") { ENV['GIT'] = 'echo' }
  end

  def test_fork_already_exists
485
    stub_existing_fork('tpw')
C
Chris Wanstrath 已提交
486

487
    expected = "tpw/hub already exists on GitHub\n"
C
Chris Wanstrath 已提交
488
    expected << "remote add -f tpw git@github.com:tpw/hub.git\n"
489 490 491
    expected << "new remote: tpw\n"
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end
C
Chris Wanstrath 已提交
492

C
Chris Wanstrath 已提交
493
  def test_version
494
    out = hub('--version')
495
    assert_includes "git version 1.7.0.4", out
C
Chris Wanstrath 已提交
496
    assert_includes "hub version #{Hub::Version}", out
C
Chris Wanstrath 已提交
497
  end
C
Chris Wanstrath 已提交
498

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
  def test_exec_path
    out = hub('--exec-path')
    assert_equal "/usr/lib/git-core\n", out
  end

  def test_exec_path_arg
    out = hub('--exec-path=/home/wombat/share/my-l33t-git-core')
    assert_equal Hub::Commands.improved_help_text, hub("")
  end

  def test_html_path
    out = hub('--html-path')
    assert_equal "/usr/share/doc/git-doc\n", out
  end

C
Chris Wanstrath 已提交
514 515 516 517 518 519 520
  def test_help
    assert_equal Hub::Commands.improved_help_text, hub("help")
  end

  def test_help_by_default
    assert_equal Hub::Commands.improved_help_text, hub("")
  end
C
Chris Wanstrath 已提交
521

S
Stephen Celis 已提交
522 523 524 525
  def test_help_with_pager
    assert_equal Hub::Commands.improved_help_text, hub("-p")
  end

C
Chris Wanstrath 已提交
526 527 528 529
  def test_help_hub
    help_manpage = hub("help hub")
    assert_includes "git + hub = github", help_manpage
    assert_includes <<-config, help_manpage
C
Chris Wanstrath 已提交
530
Use git-config(1) to display the currently configured GitHub username:
C
Chris Wanstrath 已提交
531 532 533 534
config
  end

  def test_help_hub_no_groff
535 536
    stub_available_commands()
    assert_equal "** Can't find groff(1)\n", hub("help hub")
C
Chris Wanstrath 已提交
537
  end
538

539 540 541 542 543
  def test_hub_standalone
    help_standalone = hub("hub standalone")
    assert_equal Hub::Standalone.build, help_standalone
  end

C
Chris Wanstrath 已提交
544 545
  def test_hub_compare
    assert_command "compare refactor",
546
      "open https://github.com/defunkt/hub/compare/refactor"
547
  end
C
Chris Wanstrath 已提交
548

549 550 551 552
  def test_hub_compare_nothing
    expected = "Usage: hub compare [USER] [<START>...]<END>\n"
    assert_equal expected, hub("compare")
  end
C
Chris Wanstrath 已提交
553

554 555 556 557 558 559 560 561 562 563
  def test_hub_compare_tracking_nothing
    stub_tracking_nothing
    expected = "Usage: hub compare [USER] [<START>...]<END>\n"
    assert_equal expected, hub("compare")
  end

  def test_hub_compare_tracking_branch
    stub_branch('refs/heads/feature')

    assert_command "compare",
564
      "open https://github.com/mislav/hub/compare/experimental"
565 566
  end

567
  def test_hub_compare_range
C
Chris Wanstrath 已提交
568
    assert_command "compare 1.0...fix",
569
      "open https://github.com/defunkt/hub/compare/1.0...fix"
570
  end
C
Chris Wanstrath 已提交
571

572
  def test_hub_compare_fork
C
Chris Wanstrath 已提交
573
    assert_command "compare myfork feature",
574
      "open https://github.com/myfork/hub/compare/feature"
575 576 577 578
  end

  def test_hub_compare_url
    assert_command "compare -u 1.0...1.1",
579
      "echo https://github.com/defunkt/hub/compare/1.0...1.1"
580 581 582
  end

  def test_hub_browse
583
    assert_command "browse mojombo/bert", "open https://github.com/mojombo/bert"
584 585
  end

586 587
  def test_hub_browse_tracking_nothing
    stub_tracking_nothing
588
    assert_command "browse mojombo/bert", "open https://github.com/mojombo/bert"
589 590
  end

591
  def test_hub_browse_url
592
    assert_command "browse -u mojombo/bert", "echo https://github.com/mojombo/bert"
593 594
  end

595
  def test_hub_browse_self
596
    assert_command "browse resque", "open https://github.com/tpw/resque"
597 598
  end

599 600
  def test_hub_browse_subpage
    assert_command "browse resque commits",
601
      "open https://github.com/tpw/resque/commits/master"
602
    assert_command "browse resque issues",
603
      "open https://github.com/tpw/resque/issues"
604
    assert_command "browse resque wiki",
605
      "open https://github.com/tpw/resque/wiki"
606 607 608 609 610
  end

  def test_hub_browse_on_branch
    stub_branch('refs/heads/feature')

611
    assert_command "browse resque", "open https://github.com/tpw/resque"
612
    assert_command "browse resque commits",
613
      "open https://github.com/tpw/resque/commits/master"
614 615

    assert_command "browse",
616
      "open https://github.com/mislav/hub/tree/experimental"
617
    assert_command "browse -- tree",
618
      "open https://github.com/mislav/hub/tree/experimental"
619
    assert_command "browse -- commits",
620
      "open https://github.com/mislav/hub/commits/experimental"
621
  end
622

623
  def test_hub_browse_current
624 625
    assert_command "browse", "open https://github.com/defunkt/hub"
    assert_command "browse --", "open https://github.com/defunkt/hub"
626 627 628 629
  end

  def test_hub_browse_current_subpage
    assert_command "browse -- network",
630
      "open https://github.com/defunkt/hub/network"
631
    assert_command "browse -- anything/everything",
632
      "open https://github.com/defunkt/hub/anything/everything"
633 634
  end

635 636 637 638
  def test_hub_browse_deprecated_private
    with_browser_env('echo') do
      assert_includes "Warning: the `-p` flag has no effect anymore\n", hub("browse -p defunkt/hub")
    end
639 640
  end

641 642 643
  def test_hub_browse_no_repo
    stub_repo_url(nil)
    assert_equal "Usage: hub browse [<USER>/]<REPOSITORY>\n", hub("browse")
644
  end
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679

  def test_custom_browser
    with_browser_env("custom") do
      assert_browser("custom")
    end
  end

  def test_linux_browser
    stub_available_commands "open", "xdg-open", "cygstart"
    with_browser_env(nil) do
      with_ruby_platform("i686-linux") do
        assert_browser("xdg-open")
      end
    end
  end

  def test_cygwin_browser
    stub_available_commands "open", "cygstart"
    with_browser_env(nil) do
      with_ruby_platform("i686-linux") do
        assert_browser("cygstart")
      end
    end
  end

  def test_no_browser
    stub_available_commands()
    expected = "Please set $BROWSER to a web launcher to use this command.\n"
    with_browser_env(nil) do
      with_ruby_platform("i686-linux") do
        assert_equal expected, hub("browse")
      end
    end
  end

680
  def test_context_method_doesnt_hijack_git_command
681
    assert_forwarded 'remotes'
682 683
  end

684 685 686 687 688
  def test_not_choking_on_ruby_methods
    assert_forwarded 'id'
    assert_forwarded 'name'
  end

689 690
  def test_multiple_remote_urls
    stub_repo_url("git://example.com/other.git\ngit://github.com/my/repo.git")
691
    assert_command "browse", "open https://github.com/my/repo"
692 693
  end

694 695
  protected

696 697 698 699
    def stub_github_user(name)
      @git['config github.user'] = name
    end

700 701 702 703
    def stub_github_token(token)
      @git['config github.token'] = token
    end

704
    def stub_repo_url(value)
705
      @git['config --get-all remote.origin.url'] = value
706 707 708 709 710 711 712 713 714 715 716 717
      Hub::Context::REMOTES.clear
    end

    def stub_branch(value)
      @git['symbolic-ref -q HEAD'] = value
    end

    def stub_tracking_nothing
      @git['config branch.master.remote'] = nil
      @git['config branch.master.merge'] = nil
    end

718 719 720 721
    def stub_remotes_group(name, value)
      @git["config remotes.#{name}"] = value
    end

722 723 724 725 726 727 728 729
    def stub_no_remotes
      @git['remote'] = ''
    end

    def stub_no_git_repo
      @git.replace({})
    end

730 731 732 733 734 735 736 737 738 739 740 741 742
    def stub_existing_fork(user)
      stub_fork(user, 200)
    end

    def stub_nonexisting_fork(user)
      stub_fork(user, 404)
    end

    def stub_fork(user, status)
      stub_request(:get, "github.com/api/v2/yaml/repos/show/#{user}/hub").
        to_return(:status => status)
    end

743 744 745 746 747 748 749 750 751 752 753
    def stub_available_commands(*names)
      COMMANDS.replace names
    end

    def with_browser_env(value)
      browser, ENV['BROWSER'] = ENV['BROWSER'], value
      yield
    ensure
      ENV['BROWSER'] = browser
    end

754 755 756 757 758 759 760
    def with_tmpdir(value)
      dir, ENV['TMPDIR'] = ENV['TMPDIR'], value
      yield
    ensure
      ENV['TMPDIR'] = dir
    end

761
    def assert_browser(browser)
762
      assert_command "browse", "#{browser} https://github.com/defunkt/hub"
763 764 765 766 767 768 769 770 771 772 773 774
    end

    def with_ruby_platform(value)
      platform = RUBY_PLATFORM
      Object.send(:remove_const, :RUBY_PLATFORM)
      Object.const_set(:RUBY_PLATFORM, value)
      yield
    ensure
      Object.send(:remove_const, :RUBY_PLATFORM)
      Object.const_set(:RUBY_PLATFORM, platform)
    end

C
Chris Wanstrath 已提交
775
end