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

class HubTest < Test::Unit::TestCase
6
  include WebMock
C
Chris Wanstrath 已提交
7

8 9 10 11 12 13 14 15 16
  COMMANDS = []

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

17
  def setup
18
    COMMANDS.replace %w[open groff]
19
    Hub::Context::DIRNAME.replace 'hub'
20

21 22 23
    @git = Hub::Context::GIT_CONFIG.replace(Hash.new { |h, k|
      raise ArgumentError, "`git #{k}` not stubbed"
    }).update(
24
      'remote' => "mislav\norigin",
25 26 27 28 29 30 31 32 33
      'symbolic-ref -q HEAD' => 'refs/heads/master',
      'config github.user'   => 'tpw',
      'config github.token'  => 'abc123',
      'config remote.origin.url'     => 'git://github.com/defunkt/hub.git',
      'config remote.mislav.url'     => 'git://github.com/mislav/hub.git',
      'config branch.master.remote'  => 'origin',
      'config branch.master.merge'   => 'refs/heads/master',
      'config branch.feature.remote' => 'mislav',
      'config branch.feature.merge'  => 'refs/heads/experimental',
34 35
      'config --bool hub.http-clone' => 'false',
      'config core.repositoryformatversion' => '0'
36
    )
37
    super
38 39
  end

C
Chris Wanstrath 已提交
40
  def test_private_clone
41 42
    input   = "clone -p rtomayko/ronn"
    command = "git clone git@github.com:rtomayko/ronn.git"
C
Chris Wanstrath 已提交
43
    assert_command input, command
C
Chris Wanstrath 已提交
44 45 46
  end

  def test_public_clone
47 48
    input   = "clone rtomayko/ronn"
    command = "git clone git://github.com/rtomayko/ronn.git"
C
Chris Wanstrath 已提交
49
    assert_command input, command
C
Chris Wanstrath 已提交
50 51
  end

52 53 54 55 56 57 58 59 60 61 62 63
  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

64 65 66 67 68 69
  def test_clone_with_arguments_and_path
    input   = "clone --bare -o master -- resque"
    command = "git clone --bare -o master -- git://github.com/tpw/resque.git"
    assert_command input, command
  end

70 71
  def test_your_private_clone_fails_without_config
    out = hub("clone -p mustache") do
72
      stub_github_user(nil)
73 74 75 76 77 78 79
    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
80
      stub_github_user(nil)
81 82 83 84 85
    end

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

86
  def test_private_clone_left_alone
87 88
    input   = "clone git@github.com:rtomayko/ronn.git"
    command = "git clone git@github.com:rtomayko/ronn.git"
89 90 91 92
    assert_command input, command
  end

  def test_public_clone_left_alone
93 94
    input   = "clone git://github.com/rtomayko/ronn.git"
    command = "git clone git://github.com/rtomayko/ronn.git"
95 96
    assert_command input, command
  end
C
Chris Wanstrath 已提交
97 98

  def test_normal_public_clone_with_path
99 100
    input   = "clone git://github.com/rtomayko/ronn.git ronn-dev"
    command = "git clone git://github.com/rtomayko/ronn.git ronn-dev"
C
Chris Wanstrath 已提交
101 102
    assert_command input, command
  end
103 104 105 106 107 108

  def test_normal_clone_from_path
    input   = "clone ./test"
    command = "git clone ./test"
    assert_command input, command
  end
109

110 111 112 113 114 115 116 117 118 119 120 121
  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

122 123 124 125 126 127
  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

128 129 130 131 132 133 134 135 136 137 138 139
  def test_remote_from_rel_path
    input = "remote add origin ./path"
    command = "git remote add origin ./path"
    assert_command input, command
  end

  def test_remote_from_abs_path
    input = "remote add origin /path"
    command = "git remote add origin /path"
    assert_command input, command
  end

140
  def test_private_remote_origin_as_normal
C
Chris Wanstrath 已提交
141 142 143 144 145
    input   = "remote add origin git@github.com:defunkt/resque.git"
    command = "git remote add origin git@github.com:defunkt/resque.git"
    assert_command input, command
  end

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

  def test_private_submodule
    input   = "submodule add -p grit vendor/grit"
J
Justin Ridgewell 已提交
154 155 156 157 158 159 160 161
    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 已提交
162 163 164 165 166
  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 已提交
167
    assert_command input, command
S
Stephen Celis 已提交
168 169
  end

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

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

C
Chris Wanstrath 已提交
182 183 184 185 186 187
  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

188 189 190 191 192 193
  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

194 195 196 197 198 199
  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 已提交
200
  def test_private_remote_with_repo
201 202
    input   = "remote add -p jashkenas/coffee-script"
    command = "git remote add jashkenas git@github.com:jashkenas/coffee-script.git"
C
Chris Wanstrath 已提交
203 204 205 206
    assert_command input, command
  end

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

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

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

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
  def test_fetch_existing_remote
    assert_command "fetch mislav", "git fetch mislav"
  end

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

    h = Hub("fetch xoebus")
    assert_equal "git remote add xoebus git://github.com/xoebus/hub.git", h.command
    assert_equal "git fetch xoebus", h.after
  end

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

    h = Hub("fetch --depth=1 --prune xoebus")
    assert_equal "git remote add xoebus git://github.com/xoebus/hub.git", h.command
    assert_equal "git fetch --depth=1 --prune xoebus", h.after
  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')

    h = Hub("fetch --multiple xoebus rtomayko")

    assert_equal "git remote add xoebus git://github.com/xoebus/hub.git", h.command
    expected = ["git remote add rtomayko git://github.com/rtomayko/hub.git"] <<
                "git fetch --multiple xoebus rtomayko"
    assert_equal expected.join('; '), h.after
  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')

    h = Hub("fetch xoebus,rtomayko")

    assert_equal "git remote add xoebus git://github.com/xoebus/hub.git", h.command
    expected = ["git remote add rtomayko git://github.com/rtomayko/hub.git"] <<
                "git fetch --multiple xoebus rtomayko"
    assert_equal expected.join('; '), h.after
  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
    h = Hub("fetch --multiple mislav xoebus mygrp git://example.com typo")

    assert_equal "git remote add xoebus git://github.com/xoebus/hub.git", h.command
    expected = "git fetch --multiple mislav xoebus mygrp git://example.com typo"
    assert_equal expected, h.after
  end

293 294 295 296 297 298 299
  def test_cherry_pick
    h = Hub("cherry-pick a319d88")
    assert_equal "git cherry-pick a319d88", h.command
    assert !h.args.after?
  end

  def test_cherry_pick_url
300
    url = 'http://github.com/mislav/hub/commit/a319d88'
301 302 303 304 305
    h = Hub("cherry-pick #{url}")
    assert_equal "git fetch mislav", h.command
    assert_equal "git cherry-pick a319d88", h.after
  end

306 307 308 309 310 311 312
  def test_cherry_pick_url_with_fragment
    url = 'http://github.com/mislav/hub/commit/abcdef0123456789#comments'
    h = Hub("cherry-pick #{url}")
    assert_equal "git fetch mislav", h.command
    assert_equal "git cherry-pick abcdef0123456789", h.after
  end

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
  def test_cherry_pick_url_with_remote_add
    url = 'http://github.com/xoebus/hub/commit/a319d88'
    h = Hub("cherry-pick #{url}")
    assert_equal "git remote add -f xoebus git://github.com/xoebus/hub.git", h.command
    assert_equal "git cherry-pick a319d88", h.after
  end

  def test_cherry_pick_private_url_with_remote_add
    url = 'https://github.com/xoebus/hub/commit/a319d88'
    h = Hub("cherry-pick #{url}")
    assert_equal "git remote add -f xoebus git@github.com:xoebus/hub.git", h.command
    assert_equal "git cherry-pick a319d88", h.after
  end

  def test_cherry_pick_origin_url
    url = 'https://github.com/defunkt/hub/commit/a319d88'
    h = Hub("cherry-pick #{url}")
    assert_equal "git fetch origin", h.command
    assert_equal "git cherry-pick a319d88", h.after
  end

  def test_cherry_pick_github_user_notation
    h = Hub("cherry-pick mislav@a319d88")
    assert_equal "git fetch mislav", h.command
    assert_equal "git cherry-pick a319d88", h.after
  end

  def test_cherry_pick_github_user_repo_notation
    # not supported
    h = Hub("cherry-pick mislav/hubbub@a319d88")
    assert_equal "git cherry-pick mislav/hubbub@a319d88", h.command
    assert !h.args.after?
  end

  def test_cherry_pick_github_notation_too_short
    h = Hub("cherry-pick mislav@a319")
    assert_equal "git cherry-pick mislav@a319", h.command
    assert !h.args.after?
  end

  def test_cherry_pick_github_notation_with_remote_add
    h = Hub("cherry-pick xoebus@a319d88")
    assert_equal "git remote add -f xoebus git://github.com/xoebus/hub.git", h.command
    assert_equal "git cherry-pick a319d88", h.after
  end

C
Chris Wanstrath 已提交
359
  def test_init
C
Chris Wanstrath 已提交
360 361
    h = Hub("init -g")
    assert_equal "git init", h.command
362
    assert_equal "git remote add origin git@github.com:tpw/hub.git", h.after
363 364 365 366
  end

  def test_init_no_login
    out = hub("init -g") do
367
      stub_github_user(nil)
368 369 370
    end

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

373 374 375 376 377 378
  def test_push_two
    h = Hub("push origin,staging cool-feature")
    assert_equal "git push origin cool-feature", h.command
    assert_equal "git push staging cool-feature", h.after
  end

C
Chris Wanstrath 已提交
379 380 381 382 383
  def test_push_more
    h = Hub("push origin,staging,qa cool-feature")
    assert_equal "git push origin cool-feature", h.command
    assert_equal "git push staging cool-feature; git push qa cool-feature", h.after
  end
C
Chris Wanstrath 已提交
384

385
  def test_create
386
    stub_no_remotes
387 388 389 390 391 392
    stub_nonexisting_fork('tpw')
    stub_request(:post, "github.com/api/v2/yaml/repos/create").with { |req|
      params = Hash[*req.body.split(/[&=]/)]
      params == { 'login'=>'tpw', 'token'=>'abc123', 'name' => 'hub' }
    }
    expected = "remote add -f origin git@github.com:tpw/hub.git\n"
393
    expected << "created repository: tpw/hub\n"
394 395 396
    assert_equal expected, hub("create") { ENV['GIT'] = 'echo' }
  end

397
  def test_create_private_repository
398
    stub_no_remotes
399
    stub_nonexisting_fork('tpw')
400 401 402 403 404
    stub_request(:post, "github.com/api/v2/yaml/repos/create").with { |req|
      params = Hash[*req.body.split(/[&=]/)]
      params == { 'login'=>'tpw', 'token'=>'abc123', 'name' => 'hub', 'public' => '0' }
    }
    expected = "remote add -f origin git@github.com:tpw/hub.git\n"
405
    expected << "created repository: tpw/hub\n"
406 407 408
    assert_equal expected, hub("create -p") { ENV['GIT'] = 'echo' }
  end

409
  def test_create_with_description_and_homepage
410
    stub_no_remotes
411 412 413 414 415 416
    stub_nonexisting_fork('tpw')
    stub_request(:post, "github.com/api/v2/yaml/repos/create").with { |req|
      params = Hash[*req.body.split(/[&=]/)]
      params == { 'login'=>'tpw', 'token'=>'abc123', 'name' => 'hub', 'description' => 'description', 'homepage' => 'http%3a%2f%2fgithub.com%2ftpw%2fhub.git' }
    }
    expected = "remote add -f origin git@github.com:tpw/hub.git\n"
417
    expected << "created repository: tpw/hub\n"
418 419 420 421
    assert_equal expected, hub("create -d description -h http://github.com/tpw/hub.git") { ENV['GIT'] = 'echo' }
  end

  def test_create_with_existing_repository
422
    stub_no_remotes
423 424 425 426
    stub_existing_fork('tpw')

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

431
  def test_create_no_user
432
    stub_no_remotes
433 434 435 436 437 438
    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

439
  def test_create_outside_git_repo
440
    stub_no_git_repo
441 442
    assert_equal "'create' must be run from inside a git repository\n", hub("create")
  end
443 444

  def test_create_origin_already_exists
C
Chris Wanstrath 已提交
445 446 447 448 449 450 451
    stub_nonexisting_fork('tpw')
    stub_request(:post, "github.com/api/v2/yaml/repos/create").with { |req|
      params = Hash[*req.body.split(/[&=]/)]
      params == { 'login'=>'tpw', 'token'=>'abc123', 'name' => 'hub' }
    }

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

455
  def test_fork
C
Chris Wanstrath 已提交
456
    stub_nonexisting_fork('tpw')
457 458 459 460
    stub_request(:post, "github.com/api/v2/yaml/repos/fork/defunkt/hub").with { |req|
      params = Hash[*req.body.split(/[&=]/)]
      params == { 'login'=>'tpw', 'token'=>'abc123' }
    }
C
Chris Wanstrath 已提交
461

C
Chris Wanstrath 已提交
462
    expected = "remote add -f tpw git@github.com:tpw/hub.git\n"
463 464 465 466 467
    expected << "new remote: tpw\n"
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end

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

471 472 473 474
    assert_equal "", hub("fork --no-remote") { ENV['GIT'] = 'echo' }
  end

  def test_fork_already_exists
475
    stub_existing_fork('tpw')
C
Chris Wanstrath 已提交
476

477
    expected = "tpw/hub already exists on GitHub\n"
C
Chris Wanstrath 已提交
478
    expected << "remote add -f tpw git@github.com:tpw/hub.git\n"
479 480 481
    expected << "new remote: tpw\n"
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end
C
Chris Wanstrath 已提交
482

C
Chris Wanstrath 已提交
483
  def test_version
484
    out = hub('--version')
485
    assert_includes "git version 1.7.0.4", out
C
Chris Wanstrath 已提交
486
    assert_includes "hub version #{Hub::Version}", out
C
Chris Wanstrath 已提交
487
  end
C
Chris Wanstrath 已提交
488 489 490 491 492 493 494 495

  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 已提交
496

S
Stephen Celis 已提交
497 498 499 500
  def test_help_with_pager
    assert_equal Hub::Commands.improved_help_text, hub("-p")
  end

C
Chris Wanstrath 已提交
501 502 503 504 505
  def test_help_hub
    help_manpage = hub("help hub")
    assert_includes "git + hub = github", help_manpage
    assert_includes "Chris Wanstrath :: chris@ozmm.org", help_manpage
    assert_includes <<-config, help_manpage
C
Chris Wanstrath 已提交
506
Use git-config(1) to display the currently configured GitHub username:
C
Chris Wanstrath 已提交
507 508 509 510
config
  end

  def test_help_hub_no_groff
511 512
    stub_available_commands()
    assert_equal "** Can't find groff(1)\n", hub("help hub")
C
Chris Wanstrath 已提交
513
  end
514

515 516 517 518 519
  def test_hub_standalone
    help_standalone = hub("hub standalone")
    assert_equal Hub::Standalone.build, help_standalone
  end

C
Chris Wanstrath 已提交
520 521 522
  def test_hub_compare
    assert_command "compare refactor",
      "open http://github.com/defunkt/hub/compare/refactor"
523
  end
C
Chris Wanstrath 已提交
524

525 526 527 528
  def test_hub_compare_nothing
    expected = "Usage: hub compare [USER] [<START>...]<END>\n"
    assert_equal expected, hub("compare")
  end
C
Chris Wanstrath 已提交
529

530 531 532 533 534 535 536 537 538 539 540 541 542
  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",
      "open http://github.com/mislav/hub/compare/experimental"
  end

543
  def test_hub_compare_range
C
Chris Wanstrath 已提交
544 545
    assert_command "compare 1.0...fix",
      "open http://github.com/defunkt/hub/compare/1.0...fix"
546
  end
C
Chris Wanstrath 已提交
547

548
  def test_hub_compare_fork
C
Chris Wanstrath 已提交
549 550 551 552
    assert_command "compare myfork feature",
      "open http://github.com/myfork/hub/compare/feature"
  end

553 554 555 556 557 558 559 560 561 562 563
  def test_hub_compare_private
    assert_command "compare -p myfork topsecret",
      "open https://github.com/myfork/hub/compare/topsecret"
  end

  def test_hub_compare_url
    assert_command "compare -u 1.0...1.1",
      "echo http://github.com/defunkt/hub/compare/1.0...1.1"
  end

  def test_hub_browse
564
    assert_command "browse mojombo/bert", "open http://github.com/mojombo/bert"
565 566
  end

567 568
  def test_hub_browse_tracking_nothing
    stub_tracking_nothing
569
    assert_command "browse mojombo/bert", "open http://github.com/mojombo/bert"
570 571
  end

572 573 574 575 576
  def test_hub_browse_url
    assert_command "browse -u mojombo/bert", "echo http://github.com/mojombo/bert"
  end

  def test_hub_browse_private
C
Chris Wanstrath 已提交
577 578
    assert_command "browse -p bmizerany/sinatra",
      "open https://github.com/bmizerany/sinatra"
579 580
  end

581
  def test_hub_browse_self
582
    assert_command "browse resque", "open http://github.com/tpw/resque"
583 584
  end

585 586 587 588 589 590 591 592 593 594 595 596
  def test_hub_browse_subpage
    assert_command "browse resque commits",
      "open http://github.com/tpw/resque/commits/master"
    assert_command "browse resque issues",
      "open http://github.com/tpw/resque/issues"
    assert_command "browse resque wiki",
      "open http://wiki.github.com/tpw/resque/"
  end

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

597
    assert_command "browse resque", "open http://github.com/tpw/resque"
598 599 600 601 602 603 604 605 606
    assert_command "browse resque commits",
      "open http://github.com/tpw/resque/commits/master"

    assert_command "browse",
      "open http://github.com/mislav/hub/tree/experimental"
    assert_command "browse -- tree",
      "open http://github.com/mislav/hub/tree/experimental"
    assert_command "browse -- commits",
      "open http://github.com/mislav/hub/commits/experimental"
607 608
  end

609
  def test_hub_browse_self_private
610
    assert_command "browse -p github", "open https://github.com/tpw/github"
611
  end
612

613
  def test_hub_browse_current
614
    assert_command "browse", "open http://github.com/defunkt/hub"
615 616 617 618 619 620 621 622
    assert_command "browse --", "open http://github.com/defunkt/hub"
  end

  def test_hub_browse_current_subpage
    assert_command "browse -- network",
      "open http://github.com/defunkt/hub/network"
    assert_command "browse -- anything/everything",
      "open http://github.com/defunkt/hub/anything/everything"
623 624
  end

625
  def test_hub_browse_current_private
626 627 628
    assert_command "browse -p", "open https://github.com/defunkt/hub"
  end

629 630 631
  def test_hub_browse_no_repo
    stub_repo_url(nil)
    assert_equal "Usage: hub browse [<USER>/]<REPOSITORY>\n", hub("browse")
632
  end
633 634 635 636 637 638 639 640 641 642 643 644 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

  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

  protected

670 671 672 673
    def stub_github_user(name)
      @git['config github.user'] = name
    end

674 675 676 677
    def stub_github_token(token)
      @git['config github.token'] = token
    end

678 679 680 681 682 683 684 685 686 687 688 689 690 691
    def stub_repo_url(value)
      @git['config remote.origin.url'] = value
      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

692 693 694 695
    def stub_remotes_group(name, value)
      @git["config remotes.#{name}"] = value
    end

696 697 698 699 700 701 702 703
    def stub_no_remotes
      @git['remote'] = ''
    end

    def stub_no_git_repo
      @git.replace({})
    end

704 705 706 707 708 709 710 711 712 713 714 715 716
    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

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
    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

    def assert_browser(browser)
      assert_command "browse", "#{browser} http://github.com/defunkt/hub"
    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 已提交
742
end