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

6 7 8 9 10 11
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 已提交
12
class HubTest < Test::Unit::TestCase
13 14 15 16 17
  if defined? WebMock::API
    include WebMock::API
  else
    include WebMock
  end
C
Chris Wanstrath 已提交
18

19 20
  COMMANDS = []

21 22 23 24
  Hub::Context.class_eval do
    remove_method :which
    define_method :which do |name|
      COMMANDS.include?(name) ? "/usr/bin/#{name}" : nil
25 26 27
    end
  end

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

33 34
    Hub::Context::GIT_CONFIG.executable = 'git'

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

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

  def test_public_clone
63 64
    input   = "clone rtomayko/ronn"
    command = "git clone git://github.com/rtomayko/ronn.git"
C
Chris Wanstrath 已提交
65
    assert_command input, command
C
Chris Wanstrath 已提交
66 67
  end

68 69 70 71 72 73 74 75 76 77 78 79
  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

80 81 82
  def test_clone_with_arguments
    input   = "clone --bare -o master resque"
    command = "git clone --bare -o master git://github.com/tpw/resque.git"
83 84 85
    assert_command input, command
  end

86 87 88 89
  def test_clone_with_arguments_and_destination
    assert_forwarded "clone --template=one/two git://github.com/tpw/resque.git --origin master resquetastic"
  end

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

95
    assert_equal "** No GitHub user set. See http://help.github.com/git-email-settings/\n", out
96 97 98 99
  end

  def test_your_public_clone_fails_without_config
    out = hub("clone mustache") do
100
      stub_github_user(nil)
101 102
    end

103
    assert_equal "** No GitHub user set. See http://help.github.com/git-email-settings/\n", out
104 105
  end

106
  def test_private_clone_left_alone
107
    assert_forwarded "clone git@github.com:rtomayko/ronn.git"
108 109 110
  end

  def test_public_clone_left_alone
111
    assert_forwarded "clone git://github.com/rtomayko/ronn.git"
112
  end
C
Chris Wanstrath 已提交
113 114

  def test_normal_public_clone_with_path
115
    assert_forwarded "clone git://github.com/rtomayko/ronn.git ronn-dev"
C
Chris Wanstrath 已提交
116
  end
117 118

  def test_normal_clone_from_path
119
    assert_forwarded "clone ./test"
120
  end
121

M
Mislav Marohnić 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  def test_alias_expand
    stub_alias 'c', 'clone --bare'
    input   = "c rtomayko/ronn"
    command = "git clone --bare git://github.com/rtomayko/ronn.git"
    assert_command input, command
  end

  def test_alias_expand_advanced
    stub_alias 'c', 'clone --template="white space"'
    input   = "c rtomayko/ronn"
    command = "git clone '--template=white space' git://github.com/rtomayko/ronn.git"
    assert_command input, command
  end

  def test_alias_doesnt_expand_for_unknown_commands
    stub_alias 'c', 'compute --fast'
    assert_forwarded "c rtomayko/ronn"
  end

141 142 143 144 145 146 147 148 149 150 151 152
  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

153 154 155 156 157 158
  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

159
  def test_remote_from_rel_path
160
    assert_forwarded "remote add origin ./path"
161 162 163
  end

  def test_remote_from_abs_path
164
    assert_forwarded "remote add origin /path"
165 166
  end

167
  def test_private_remote_origin_as_normal
168
    assert_forwarded "remote add origin git@github.com:defunkt/resque.git"
C
Chris Wanstrath 已提交
169 170
  end

S
Stephen Celis 已提交
171 172
  def test_public_submodule
    input   = "submodule add wycats/bundler vendor/bundler"
J
Justin Ridgewell 已提交
173 174
    command = "git submodule add git://github.com/wycats/bundler.git vendor/bundler"
    assert_command input, command
S
Stephen Celis 已提交
175 176 177 178
  end

  def test_private_submodule
    input   = "submodule add -p grit vendor/grit"
J
Justin Ridgewell 已提交
179 180 181 182 183 184 185 186
    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 已提交
187 188 189 190 191
  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 已提交
192
    assert_command input, command
S
Stephen Celis 已提交
193 194
  end

C
Chris Wanstrath 已提交
195
  def test_private_remote
196
    input   = "remote add -p rtomayko"
C
Chris Wanstrath 已提交
197 198
    command = "git remote add rtomayko git@github.com:rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
199 200 201
  end

  def test_public_remote
202
    input   = "remote add rtomayko"
C
Chris Wanstrath 已提交
203 204
    command = "git remote add rtomayko git://github.com/rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
205 206
  end

C
Chris Wanstrath 已提交
207 208 209 210 211 212
  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

213 214 215 216 217 218
  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

219 220 221 222 223 224
  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 已提交
225
  def test_private_remote_with_repo
226 227
    input   = "remote add -p jashkenas/coffee-script"
    command = "git remote add jashkenas git@github.com:jashkenas/coffee-script.git"
C
Chris Wanstrath 已提交
228 229 230 231
    assert_command input, command
  end

  def test_public_remote_with_repo
232 233
    input   = "remote add jashkenas/coffee-script"
    command = "git remote add jashkenas git://github.com/jashkenas/coffee-script.git"
234 235 236 237
    assert_command input, command
  end

  def test_public_remote_f_with_repo
238 239
    input   = "remote add -f jashkenas/coffee-script"
    command = "git remote add -f jashkenas git://github.com/jashkenas/coffee-script.git"
C
Chris Wanstrath 已提交
240 241 242
    assert_command input, command
  end

243
  def test_named_private_remote_with_repo
244 245
    input   = "remote add -p origin jashkenas/coffee-script"
    command = "git remote add origin git@github.com:jashkenas/coffee-script.git"
246 247 248
    assert_command input, command
  end

249
  def test_fetch_existing_remote
250
    assert_forwarded "fetch mislav"
251 252 253 254 255 256
  end

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

257 258 259
    assert_commands "git remote add xoebus git://github.com/xoebus/hub.git",
                    "git fetch xoebus",
                    "fetch xoebus"
260 261 262 263 264 265
  end

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

266 267 268
    assert_commands "git remote add xoebus git://github.com/xoebus/hub.git",
                    "git fetch --depth=1 --prune xoebus",
                    "fetch --depth=1 --prune xoebus"
269 270 271 272 273 274 275 276
  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')

277 278 279 280
    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"
281 282 283 284 285 286 287 288
  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')

289 290 291 292
    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"
293 294 295 296 297 298 299 300 301 302 303 304 305 306
  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
307 308 309
    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"
310 311
  end

312
  def test_cherry_pick
313
    assert_forwarded "cherry-pick a319d88"
314 315 316
  end

  def test_cherry_pick_url
317
    url = 'http://github.com/mislav/hub/commit/a319d88'
318
    assert_commands "git fetch mislav", "git cherry-pick a319d88", "cherry-pick #{url}"
319 320
  end

321 322
  def test_cherry_pick_url_with_fragment
    url = 'http://github.com/mislav/hub/commit/abcdef0123456789#comments'
323
    assert_commands "git fetch mislav", "git cherry-pick abcdef0123456789", "cherry-pick #{url}"
324 325
  end

326 327
  def test_cherry_pick_url_with_remote_add
    url = 'https://github.com/xoebus/hub/commit/a319d88'
328
    assert_commands "git remote add -f xoebus git://github.com/xoebus/hub.git",
329 330
                    "git cherry-pick a319d88",
                    "cherry-pick #{url}"
331 332 333 334
  end

  def test_cherry_pick_origin_url
    url = 'https://github.com/defunkt/hub/commit/a319d88'
335
    assert_commands "git fetch origin", "git cherry-pick a319d88", "cherry-pick #{url}"
336 337 338
  end

  def test_cherry_pick_github_user_notation
339
    assert_commands "git fetch mislav", "git cherry-pick a319d88", "cherry-pick mislav@a319d88"
340 341 342 343
  end

  def test_cherry_pick_github_user_repo_notation
    # not supported
344
    assert_forwarded "cherry-pick mislav/hubbub@a319d88"
345 346 347
  end

  def test_cherry_pick_github_notation_too_short
348
    assert_forwarded "cherry-pick mislav@a319"
349 350 351
  end

  def test_cherry_pick_github_notation_with_remote_add
352 353 354
    assert_commands "git remote add -f xoebus git://github.com/xoebus/hub.git",
                    "git cherry-pick a319d88",
                    "cherry-pick xoebus@a319d88"
355 356
  end

357 358 359 360 361 362 363 364 365
  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"
366 367 368

      cmd = Hub("am https://github.com/defunkt/hub/pull/55/files").command
      assert_includes '/pull/55.patch', cmd
369 370 371 372 373 374 375 376 377 378 379 380 381
    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

382 383 384 385 386 387 388 389 390 391
  def test_am_gist
    with_tmpdir('/tmp/') do
      url = 'https://gist.github.com/8da7fb575debd88c54cf'

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

C
Chris Wanstrath 已提交
392
  def test_init
393
    assert_commands "git init", "git remote add origin git@github.com:tpw/hub.git", "init -g"
394 395 396 397
  end

  def test_init_no_login
    out = hub("init -g") do
398
      stub_github_user(nil)
399 400
    end

401
    assert_equal "** No GitHub user set. See http://help.github.com/git-email-settings/\n", out
C
Chris Wanstrath 已提交
402 403
  end

404
  def test_push_two
405 406
    assert_commands "git push origin cool-feature", "git push staging cool-feature",
                    "push origin,staging cool-feature"
407 408
  end

C
Chris Wanstrath 已提交
409
  def test_push_more
410 411 412 413
    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 已提交
414
  end
C
Chris Wanstrath 已提交
415

416
  def test_create
417
    stub_no_remotes
418
    stub_nonexisting_fork('tpw')
419 420 421
    stub_request(:post, "github.com/api/v2/yaml/repos/create").
      with(:body => { 'login'=>'tpw', 'token'=>'abc123', 'name' => 'hub' })

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

427 428 429 430 431 432 433 434 435 436
  def test_create_failed
    stub_no_remotes
    stub_nonexisting_fork('tpw')
    stub_request(:post, "github.com/api/v2/yaml/repos/create").
      to_return(:status => [401, "Your token is fail"])

    expected = "error creating repository: Your token is fail (HTTP 401)\n"
    assert_equal expected, hub("create") { ENV['GIT'] = 'echo' }
  end

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
  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

458
  def test_create_private_repository
459
    stub_no_remotes
460
    stub_nonexisting_fork('tpw')
461 462 463
    stub_request(:post, "github.com/api/v2/yaml/repos/create").
      with(:body => { 'login'=>'tpw', 'token'=>'abc123', 'name' => 'hub', 'public' => '0' })

464
    expected = "remote add -f origin git@github.com:tpw/hub.git\n"
465
    expected << "created repository: tpw/hub\n"
466 467 468
    assert_equal expected, hub("create -p") { ENV['GIT'] = 'echo' }
  end

469
  def test_create_with_description_and_homepage
470
    stub_no_remotes
471
    stub_nonexisting_fork('tpw')
472 473 474 475 476
    stub_request(:post, "github.com/api/v2/yaml/repos/create").with(:body => {
      'login'=>'tpw', 'token'=>'abc123', 'name' => 'hub',
      'description' => 'toyproject', 'homepage' => 'http://example.com'
    })

477
    expected = "remote add -f origin git@github.com:tpw/hub.git\n"
478
    expected << "created repository: tpw/hub\n"
479
    assert_equal expected, hub("create -d toyproject -h http://example.com") { ENV['GIT'] = 'echo' }
480 481 482
  end

  def test_create_with_existing_repository
483
    stub_no_remotes
484 485 486 487
    stub_existing_fork('tpw')

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

492
  def test_create_no_user
493
    stub_no_remotes
494 495 496
    out = hub("create") do
      stub_github_token(nil)
    end
497
    assert_equal "** No GitHub token set. See http://help.github.com/git-email-settings/\n", out
498 499
  end

500
  def test_create_outside_git_repo
501
    stub_no_git_repo
502 503
    assert_equal "'create' must be run from inside a git repository\n", hub("create")
  end
504 505

  def test_create_origin_already_exists
C
Chris Wanstrath 已提交
506
    stub_nonexisting_fork('tpw')
507 508
    stub_request(:post, "github.com/api/v2/yaml/repos/create").
      with(:body => { 'login'=>'tpw', 'token'=>'abc123', 'name' => 'hub' })
C
Chris Wanstrath 已提交
509 510

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

514
  def test_fork
C
Chris Wanstrath 已提交
515
    stub_nonexisting_fork('tpw')
516 517
    stub_request(:post, "github.com/api/v2/yaml/repos/fork/defunkt/hub").
      with(:body => { 'login'=>'tpw', 'token'=>'abc123' })
C
Chris Wanstrath 已提交
518

C
Chris Wanstrath 已提交
519
    expected = "remote add -f tpw git@github.com:tpw/hub.git\n"
520 521 522 523
    expected << "new remote: tpw\n"
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end

524 525 526 527 528 529 530 531 532
  def test_fork_failed
    stub_nonexisting_fork('tpw')
    stub_request(:post, "github.com/api/v2/yaml/repos/fork/defunkt/hub").
      to_return(:status => [500, "Your fork is fail"])

    expected = "error creating fork: Your fork is fail (HTTP 500)\n"
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end

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

537 538 539 540
    assert_equal "", hub("fork --no-remote") { ENV['GIT'] = 'echo' }
  end

  def test_fork_already_exists
541
    stub_existing_fork('tpw')
C
Chris Wanstrath 已提交
542

543
    expected = "tpw/hub already exists on GitHub\n"
C
Chris Wanstrath 已提交
544
    expected << "remote add -f tpw git@github.com:tpw/hub.git\n"
545 546 547
    expected << "new remote: tpw\n"
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end
C
Chris Wanstrath 已提交
548

C
Chris Wanstrath 已提交
549
  def test_version
550
    out = hub('--version')
551
    assert_includes "git version 1.7.0.4", out
C
Chris Wanstrath 已提交
552
    assert_includes "hub version #{Hub::Version}", out
C
Chris Wanstrath 已提交
553
  end
C
Chris Wanstrath 已提交
554

555 556 557 558 559 560 561
  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')
562
    assert_equal Hub::Commands.improved_help_text, out
563 564 565 566 567 568 569
  end

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

C
Chris Wanstrath 已提交
570 571 572 573 574 575 576
  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 已提交
577

S
Stephen Celis 已提交
578 579 580 581
  def test_help_with_pager
    assert_equal Hub::Commands.improved_help_text, hub("-p")
  end

C
Chris Wanstrath 已提交
582 583 584 585
  def test_help_hub
    help_manpage = hub("help hub")
    assert_includes "git + hub = github", help_manpage
    assert_includes <<-config, help_manpage
C
Chris Wanstrath 已提交
586
Use git-config(1) to display the currently configured GitHub username:
C
Chris Wanstrath 已提交
587 588 589 590
config
  end

  def test_help_hub_no_groff
591 592
    stub_available_commands()
    assert_equal "** Can't find groff(1)\n", hub("help hub")
C
Chris Wanstrath 已提交
593
  end
594

595 596 597 598 599
  def test_hub_standalone
    help_standalone = hub("hub standalone")
    assert_equal Hub::Standalone.build, help_standalone
  end

C
Chris Wanstrath 已提交
600 601
  def test_hub_compare
    assert_command "compare refactor",
602
      "open https://github.com/defunkt/hub/compare/refactor"
603
  end
C
Chris Wanstrath 已提交
604

605 606 607 608
  def test_hub_compare_nothing
    expected = "Usage: hub compare [USER] [<START>...]<END>\n"
    assert_equal expected, hub("compare")
  end
C
Chris Wanstrath 已提交
609

610 611 612 613 614 615 616 617 618 619
  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",
620
      "open https://github.com/mislav/hub/compare/experimental"
621 622
  end

623
  def test_hub_compare_range
C
Chris Wanstrath 已提交
624
    assert_command "compare 1.0...fix",
625
      "open https://github.com/defunkt/hub/compare/1.0...fix"
626
  end
C
Chris Wanstrath 已提交
627

628 629 630 631 632 633
  def test_hub_compare_on_wiki
    stub_repo_url 'git://github.com/defunkt/hub.wiki.git'
    assert_command "compare 1.0...fix",
      "open https://github.com/defunkt/hub/wiki/_compare/1.0...fix"
  end

634
  def test_hub_compare_fork
C
Chris Wanstrath 已提交
635
    assert_command "compare myfork feature",
636
      "open https://github.com/myfork/hub/compare/feature"
637 638 639 640
  end

  def test_hub_compare_url
    assert_command "compare -u 1.0...1.1",
641
      "echo https://github.com/defunkt/hub/compare/1.0...1.1"
642 643 644
  end

  def test_hub_browse
645
    assert_command "browse mojombo/bert", "open https://github.com/mojombo/bert"
646 647
  end

648 649
  def test_hub_browse_tracking_nothing
    stub_tracking_nothing
650
    assert_command "browse mojombo/bert", "open https://github.com/mojombo/bert"
651 652
  end

653
  def test_hub_browse_url
654
    assert_command "browse -u mojombo/bert", "echo https://github.com/mojombo/bert"
655 656
  end

657
  def test_hub_browse_self
658
    assert_command "browse resque", "open https://github.com/tpw/resque"
659 660
  end

661 662
  def test_hub_browse_subpage
    assert_command "browse resque commits",
663
      "open https://github.com/tpw/resque/commits/master"
664
    assert_command "browse resque issues",
665
      "open https://github.com/tpw/resque/issues"
666
    assert_command "browse resque wiki",
667
      "open https://github.com/tpw/resque/wiki"
668 669 670 671 672
  end

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

673
    assert_command "browse resque", "open https://github.com/tpw/resque"
674
    assert_command "browse resque commits",
675
      "open https://github.com/tpw/resque/commits/master"
676 677

    assert_command "browse",
678
      "open https://github.com/mislav/hub/tree/experimental"
679
    assert_command "browse -- tree",
680
      "open https://github.com/mislav/hub/tree/experimental"
681
    assert_command "browse -- commits",
682
      "open https://github.com/mislav/hub/commits/experimental"
683
  end
684

685
  def test_hub_browse_current
686 687
    assert_command "browse", "open https://github.com/defunkt/hub"
    assert_command "browse --", "open https://github.com/defunkt/hub"
688 689
  end

690 691 692 693 694 695 696 697 698
  def test_hub_browse_current_wiki
    stub_repo_url 'git://github.com/defunkt/hub.wiki.git'

    assert_command "browse", "open https://github.com/defunkt/hub/wiki"
    assert_command "browse -- wiki", "open https://github.com/defunkt/hub/wiki"
    assert_command "browse -- commits", "open https://github.com/defunkt/hub/wiki/_history"
    assert_command "browse -- pages", "open https://github.com/defunkt/hub/wiki/_pages"
  end

699 700
  def test_hub_browse_current_subpage
    assert_command "browse -- network",
701
      "open https://github.com/defunkt/hub/network"
702
    assert_command "browse -- anything/everything",
703
      "open https://github.com/defunkt/hub/anything/everything"
704 705
  end

706 707 708 709
  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
710 711
  end

712 713 714
  def test_hub_browse_no_repo
    stub_repo_url(nil)
    assert_equal "Usage: hub browse [<USER>/]<REPOSITORY>\n", hub("browse")
715
  end
716 717 718 719 720 721 722 723 724 725

  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
726
      with_host_os("i686-linux") do
727 728 729 730 731 732 733 734
        assert_browser("xdg-open")
      end
    end
  end

  def test_cygwin_browser
    stub_available_commands "open", "cygstart"
    with_browser_env(nil) do
735
      with_host_os("i686-linux") do
736 737 738 739 740 741 742 743 744
        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
745
      with_host_os("i686-linux") do
746 747 748 749 750
        assert_equal expected, hub("browse")
      end
    end
  end

751
  def test_context_method_doesnt_hijack_git_command
752
    assert_forwarded 'remotes'
753 754
  end

755 756 757 758 759
  def test_not_choking_on_ruby_methods
    assert_forwarded 'id'
    assert_forwarded 'name'
  end

760 761
  def test_multiple_remote_urls
    stub_repo_url("git://example.com/other.git\ngit://github.com/my/repo.git")
762
    assert_command "browse", "open https://github.com/my/repo"
763 764
  end

765 766 767 768 769 770
  def test_global_flags_preserved
    cmd = '--no-pager --bare -c core.awesome=true -c name=value --git-dir=/srv/www perform'
    assert_command cmd, 'git --bare -c core.awesome=true -c name=value --git-dir=/srv/www --no-pager perform'
    assert_equal %w[git --bare -c core.awesome=true -c name=value --git-dir=/srv/www], @git.executable
  end

771 772
  protected

773 774 775 776
    def stub_github_user(name)
      @git['config github.user'] = name
    end

777 778 779 780
    def stub_github_token(token)
      @git['config github.token'] = token
    end

781
    def stub_repo_url(value)
782
      @git['config --get-all remote.origin.url'] = value
783 784 785 786 787 788 789 790 791 792 793 794
      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

795 796 797 798
    def stub_remotes_group(name, value)
      @git["config remotes.#{name}"] = value
    end

799 800 801 802 803 804 805 806
    def stub_no_remotes
      @git['remote'] = ''
    end

    def stub_no_git_repo
      @git.replace({})
    end

M
Mislav Marohnić 已提交
807 808 809 810
    def stub_alias(name, value)
      @git["config alias.#{name}"] = value
    end

811 812 813 814 815 816 817 818 819 820 821 822 823
    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

824 825 826 827 828 829 830 831 832 833 834
    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

835 836 837 838 839 840 841
    def with_tmpdir(value)
      dir, ENV['TMPDIR'] = ENV['TMPDIR'], value
      yield
    ensure
      ENV['TMPDIR'] = dir
    end

842
    def assert_browser(browser)
843
      assert_command "browse", "#{browser} https://github.com/defunkt/hub"
844 845
    end

846 847 848 849 850 851 852 853
    def with_host_os(value)
      host_os = RbConfig::CONFIG['host_os']
      RbConfig::CONFIG['host_os'] = value
      begin
        yield
      ensure
        RbConfig::CONFIG['host_os'] = host_os
      end
854 855
    end

C
Chris Wanstrath 已提交
856
end