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

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

20 21
  COMMANDS = []

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

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

35 36
    Hub::Context::GIT_CONFIG.executable = 'git'

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

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

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

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

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

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

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

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

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

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

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

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

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

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

M
Mislav Marohnić 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  def test_cherry_pick_github_user_notation
338
    assert_commands "git fetch mislav", "git cherry-pick 368af20", "cherry-pick mislav@368af20"
339 340 341 342
  end

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

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

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

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

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

381 382 383 384 385 386 387 388 389 390
  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

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
  def test_apply_untouched
    assert_forwarded "apply some.patch"
  end

  def test_apply_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 apply /tmp/55.patch -p2",
                      "apply https://github.com/defunkt/hub/pull/55 -p2"

      cmd = Hub("apply https://github.com/defunkt/hub/pull/55/files").command
      assert_includes '/pull/55.patch', cmd
    end
  end

  def test_apply_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 apply /tmp/fdb9921.patch -p2",
                      "apply #{url} -p2"
    end
  end

  def test_apply_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 apply /tmp/gist-8da7fb575debd88c54cf.txt -p2",
                      "apply #{url} -p2"
    end
  end

C
Chris Wanstrath 已提交
426
  def test_init
427
    assert_commands "git init", "git remote add origin git@github.com:tpw/hub.git", "init -g"
428 429 430 431
  end

  def test_init_no_login
    out = hub("init -g") do
432
      stub_github_user(nil)
433 434
    end

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

438 439 440 441
  def test_push_untouched
    assert_forwarded "push"
  end

442
  def test_push_two
443 444
    assert_commands "git push origin cool-feature", "git push staging cool-feature",
                    "push origin,staging cool-feature"
445 446
  end

447 448 449 450 451 452
  def test_push_current_branch
    stub_branch('refs/heads/cool-feature')
    assert_commands "git push origin cool-feature", "git push staging cool-feature",
                    "push origin,staging"
  end

C
Chris Wanstrath 已提交
453
  def test_push_more
454 455 456 457
    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 已提交
458
  end
C
Chris Wanstrath 已提交
459

460
  def test_create
461
    stub_no_remotes
462
    stub_nonexisting_fork('tpw')
463 464
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/repos/create").
      with(:body => { 'name' => 'hub' })
465

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

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
  def test_create_custom_name
    stub_no_remotes
    stub_nonexisting_fork('tpw', 'hubbub')
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/repos/create").
      with(:body => { 'name' => 'hubbub' })

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

  def test_create_in_organization
    stub_no_remotes
    stub_nonexisting_fork('acme', 'hubbub')
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/repos/create").
      with(:body => { 'name' => 'acme/hubbub' })

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

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
  def test_create_no_openssl
    stub_no_remotes
    stub_nonexisting_fork('tpw')
    stub_request(:post, "http://#{auth}github.com/api/v2/yaml/repos/create").
      with(:body => { 'name' => 'hub' })

    expected = "remote add -f origin git@github.com:tpw/hub.git\n"
    expected << "created repository: tpw/hub\n"

    assert_equal expected, hub("create") {
      ENV['GIT'] = 'echo'
      require 'net/https'
      Object.send :remove_const, :OpenSSL
    }
  end

509 510 511
  def test_create_failed
    stub_no_remotes
    stub_nonexisting_fork('tpw')
512
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/repos/create").
513 514
      to_return(:status => [401, "Your token is fail"])

515 516
    expected = "Error creating repository: Your token is fail (HTTP 401)\n"
    expected << "Check your token configuration (`git config github.token`)\n"
517 518 519
    assert_equal expected, hub("create") { ENV['GIT'] = 'echo' }
  end

520 521 522 523 524 525 526 527 528
  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'

529 530
    stub_request(:post, "https://#{auth('mojombo', '123abc')}github.com/api/v2/yaml/repos/create").
      with(:body => { 'name' => 'hub' })
531 532 533 534 535 536 537 538 539 540

    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

541
  def test_create_private_repository
542
    stub_no_remotes
543
    stub_nonexisting_fork('tpw')
544 545
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/repos/create").
      with(:body => { 'name' => 'hub', 'public' => '0' })
546

547
    expected = "remote add -f origin git@github.com:tpw/hub.git\n"
548
    expected << "created repository: tpw/hub\n"
549 550 551
    assert_equal expected, hub("create -p") { ENV['GIT'] = 'echo' }
  end

552
  def test_create_with_description_and_homepage
553
    stub_no_remotes
554
    stub_nonexisting_fork('tpw')
555 556
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/repos/create").with(:body => {
      'name' => 'hub', 'description' => 'toyproject', 'homepage' => 'http://example.com'
557 558
    })

559
    expected = "remote add -f origin git@github.com:tpw/hub.git\n"
560
    expected << "created repository: tpw/hub\n"
561
    assert_equal expected, hub("create -d toyproject -h http://example.com") { ENV['GIT'] = 'echo' }
562 563
  end

564 565 566 567 568
  def test_create_with_invalid_arguments
    assert_equal "invalid argument: -a\n",   hub("create -a blah")   { ENV['GIT'] = 'echo' }
    assert_equal "invalid argument: bleh\n", hub("create blah bleh") { ENV['GIT'] = 'echo' }
  end

569
  def test_create_with_existing_repository
570
    stub_no_remotes
571 572 573 574
    stub_existing_fork('tpw')

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

579
  def test_create_no_user
580
    stub_no_remotes
581 582 583
    out = hub("create") do
      stub_github_token(nil)
    end
584
    assert_equal "** No GitHub token set. See http://help.github.com/git-email-settings/\n", out
585 586
  end

587
  def test_create_outside_git_repo
588
    stub_no_git_repo
589 590
    assert_equal "'create' must be run from inside a git repository\n", hub("create")
  end
591 592

  def test_create_origin_already_exists
C
Chris Wanstrath 已提交
593
    stub_nonexisting_fork('tpw')
594 595
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/repos/create").
      with(:body => { 'name' => 'hub' })
C
Chris Wanstrath 已提交
596 597

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

601
  def test_fork
C
Chris Wanstrath 已提交
602
    stub_nonexisting_fork('tpw')
603
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/repos/fork/defunkt/hub")
C
Chris Wanstrath 已提交
604

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

610 611
  def test_fork_failed
    stub_nonexisting_fork('tpw')
612
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/repos/fork/defunkt/hub").
613 614
      to_return(:status => [500, "Your fork is fail"])

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

619
  def test_fork_no_remote
620
    stub_nonexisting_fork('tpw')
621
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/repos/fork/defunkt/hub")
C
Chris Wanstrath 已提交
622

623 624 625 626
    assert_equal "", hub("fork --no-remote") { ENV['GIT'] = 'echo' }
  end

  def test_fork_already_exists
627
    stub_existing_fork('tpw')
C
Chris Wanstrath 已提交
628

629
    expected = "tpw/hub already exists on GitHub\n"
C
Chris Wanstrath 已提交
630
    expected << "remote add -f tpw git@github.com:tpw/hub.git\n"
631 632 633
    expected << "new remote: tpw\n"
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end
C
Chris Wanstrath 已提交
634

635 636 637 638 639 640
  def test_pullrequest
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/pulls/defunkt/hub").
      with(:body => { 'pull' => {'base' => "master", 'head' => "tpw:master", 'title' => "hereyougo"} }).
      to_return(:body => mock_pullreq_response(1))

    expected = "https://github.com/defunkt/hub/pull/1\n"
641 642 643 644 645 646 647 648
    assert_output expected, "pull-request hereyougo -f"
  end

  def test_pullrequest_with_checks
    @git["rev-list --cherry origin/master..."] = "+abcd1234\n+bcde2345"

    expected = "Aborted: 2 commits are not yet pushed to origin/master\n" <<
      "(use `-f` to force submit a pull request anyway)\n"
649 650 651 652 653 654 655 656 657 658 659
    assert_output expected, "pull-request hereyougo"
  end

  def test_pullrequest_from_branch
    stub_branch('refs/heads/feature')
    stub_tracking_nothing('feature')
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/pulls/defunkt/hub").
      with(:body => { 'pull' => {'base' => "master", 'head' => "tpw:feature", 'title' => "hereyougo"} }).
      to_return(:body => mock_pullreq_response(1))

    expected = "https://github.com/defunkt/hub/pull/1\n"
660
    assert_output expected, "pull-request hereyougo -f"
661 662 663 664 665 666 667 668 669 670
  end

  def test_pullrequest_from_tracking_branch
    stub_branch('refs/heads/feature')
    stub_tracking('feature', 'tpw', 'yay-feature')
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/pulls/defunkt/hub").
      with(:body => { 'pull' => {'base' => "master", 'head' => "tpw:yay-feature", 'title' => "hereyougo"} }).
      to_return(:body => mock_pullreq_response(1))

    expected = "https://github.com/defunkt/hub/pull/1\n"
671
    assert_output expected, "pull-request hereyougo -f"
672 673 674 675 676 677 678 679
  end

  def test_pullrequest_explicit_head
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/pulls/defunkt/hub").
      with(:body => { 'pull' => {'base' => "master", 'head' => "tpw:yay-feature", 'title' => "hereyougo"} }).
      to_return(:body => mock_pullreq_response(1))

    expected = "https://github.com/defunkt/hub/pull/1\n"
680
    assert_output expected, "pull-request hereyougo -h yay-feature -f"
681 682 683 684 685 686 687 688
  end

  def test_pullrequest_explicit_head_with_owner
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/pulls/defunkt/hub").
      with(:body => { 'pull' => {'base' => "master", 'head' => "mojombo:feature", 'title' => "hereyougo"} }).
      to_return(:body => mock_pullreq_response(1))

    expected = "https://github.com/defunkt/hub/pull/1\n"
689
    assert_output expected, "pull-request hereyougo -h mojombo:feature -f"
690 691 692 693 694 695 696 697
  end

  def test_pullrequest_explicit_base
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/pulls/defunkt/hub").
      with(:body => { 'pull' => {'base' => "feature", 'head' => "tpw:master", 'title' => "hereyougo"} }).
      to_return(:body => mock_pullreq_response(1))

    expected = "https://github.com/defunkt/hub/pull/1\n"
698
    assert_output expected, "pull-request hereyougo -b feature -f"
699 700 701 702 703 704 705 706
  end

  def test_pullrequest_explicit_base_with_owner
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/pulls/mojombo/hub").
      with(:body => { 'pull' => {'base' => "feature", 'head' => "tpw:master", 'title' => "hereyougo"} }).
      to_return(:body => mock_pullreq_response(1))

    expected = "https://github.com/defunkt/hub/pull/1\n"
707
    assert_output expected, "pull-request hereyougo -b mojombo:feature -f"
708 709
  end

710 711 712 713 714 715
  def test_pullrequest_explicit_base_with_repo
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/pulls/mojombo/hubbub").
      with(:body => { 'pull' => {'base' => "feature", 'head' => "tpw:master", 'title' => "hereyougo"} }).
      to_return(:body => mock_pullreq_response(1))

    expected = "https://github.com/defunkt/hub/pull/1\n"
716
    assert_output expected, "pull-request hereyougo -b mojombo/hubbub:feature -f"
717 718 719
  end

  def test_pullrequest_existing_issue
720 721 722 723 724
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/pulls/defunkt/hub").
      with(:body => { 'pull' => {'base' => "master", 'head' => "tpw:master", 'issue' => '92'} }).
      to_return(:body => mock_pullreq_response(92))

    expected = "https://github.com/defunkt/hub/pull/92\n"
725
    assert_output expected, "pull-request #92 -f"
726 727
  end

728 729 730 731 732 733
  def test_pullrequest_existing_issue_url
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/pulls/mojombo/hub").
      with(:body => { 'pull' => {'base' => "master", 'head' => "tpw:master", 'issue' => '92'} }).
      to_return(:body => mock_pullreq_response(92, 'mojombo/hub'))

    expected = "https://github.com/mojombo/hub/pull/92\n"
734
    assert_output expected, "pull-request https://github.com/mojombo/hub/issues/92#comment_4 -f"
735 736
  end

C
Chris Wanstrath 已提交
737
  def test_version
738
    out = hub('--version')
739
    assert_includes "git version 1.7.0.4", out
C
Chris Wanstrath 已提交
740
    assert_includes "hub version #{Hub::Version}", out
C
Chris Wanstrath 已提交
741
  end
C
Chris Wanstrath 已提交
742

743 744 745 746 747 748 749
  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')
750
    assert_equal Hub::Commands.improved_help_text, out
751 752 753 754 755 756 757
  end

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

C
Chris Wanstrath 已提交
758 759 760 761 762 763 764
  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 已提交
765

S
Stephen Celis 已提交
766 767 768 769
  def test_help_with_pager
    assert_equal Hub::Commands.improved_help_text, hub("-p")
  end

C
Chris Wanstrath 已提交
770 771 772 773
  def test_help_hub
    help_manpage = hub("help hub")
    assert_includes "git + hub = github", help_manpage
    assert_includes <<-config, help_manpage
C
Chris Wanstrath 已提交
774
Use git-config(1) to display the currently configured GitHub username:
C
Chris Wanstrath 已提交
775 776 777 778
config
  end

  def test_help_hub_no_groff
779 780
    stub_available_commands()
    assert_equal "** Can't find groff(1)\n", hub("help hub")
C
Chris Wanstrath 已提交
781
  end
782

783 784 785 786 787
  def test_hub_standalone
    help_standalone = hub("hub standalone")
    assert_equal Hub::Standalone.build, help_standalone
  end

C
Chris Wanstrath 已提交
788 789
  def test_hub_compare
    assert_command "compare refactor",
790
      "open https://github.com/defunkt/hub/compare/refactor"
791
  end
C
Chris Wanstrath 已提交
792

793 794 795 796
  def test_hub_compare_nothing
    expected = "Usage: hub compare [USER] [<START>...]<END>\n"
    assert_equal expected, hub("compare")
  end
C
Chris Wanstrath 已提交
797

798 799 800 801 802 803 804 805
  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')
806
    stub_tracking('feature', 'mislav', 'experimental')
807 808

    assert_command "compare",
809
      "open https://github.com/mislav/hub/compare/experimental"
810 811
  end

812
  def test_hub_compare_range
C
Chris Wanstrath 已提交
813
    assert_command "compare 1.0...fix",
814
      "open https://github.com/defunkt/hub/compare/1.0...fix"
815
  end
C
Chris Wanstrath 已提交
816

817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
  def test_hub_compare_range_fixes_two_dots_for_tags
    assert_command "compare 1.0..fix",
      "open https://github.com/defunkt/hub/compare/1.0...fix"
  end

  def test_hub_compare_range_fixes_two_dots_for_shas
    assert_command "compare 1234abc..3456cde",
      "open https://github.com/defunkt/hub/compare/1234abc...3456cde"
  end

  def test_hub_compare_range_ignores_two_dots_for_complex_ranges
    assert_command "compare @{a..b}..@{c..d}",
      "open https://github.com/defunkt/hub/compare/@{a..b}..@{c..d}"
  end

832 833 834 835 836 837
  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

838
  def test_hub_compare_fork
C
Chris Wanstrath 已提交
839
    assert_command "compare myfork feature",
840
      "open https://github.com/myfork/hub/compare/feature"
841 842 843 844
  end

  def test_hub_compare_url
    assert_command "compare -u 1.0...1.1",
845
      "echo https://github.com/defunkt/hub/compare/1.0...1.1"
846 847 848
  end

  def test_hub_browse
849
    assert_command "browse mojombo/bert", "open https://github.com/mojombo/bert"
850 851
  end

852 853 854 855
  def test_hub_browse_commit
    assert_command "browse mojombo/bert commit/5d5582", "open https://github.com/mojombo/bert/commit/5d5582"
  end

856 857
  def test_hub_browse_tracking_nothing
    stub_tracking_nothing
858
    assert_command "browse mojombo/bert", "open https://github.com/mojombo/bert"
859 860
  end

861
  def test_hub_browse_url
862
    assert_command "browse -u mojombo/bert", "echo https://github.com/mojombo/bert"
863 864
  end

865
  def test_hub_browse_self
866
    assert_command "browse resque", "open https://github.com/tpw/resque"
867 868
  end

869 870
  def test_hub_browse_subpage
    assert_command "browse resque commits",
871
      "open https://github.com/tpw/resque/commits/master"
872
    assert_command "browse resque issues",
873
      "open https://github.com/tpw/resque/issues"
874
    assert_command "browse resque wiki",
875
      "open https://github.com/tpw/resque/wiki"
876 877 878 879
  end

  def test_hub_browse_on_branch
    stub_branch('refs/heads/feature')
880
    stub_tracking('feature', 'mislav', 'experimental')
881

882
    assert_command "browse resque", "open https://github.com/tpw/resque"
883
    assert_command "browse resque commits",
884
      "open https://github.com/tpw/resque/commits/master"
885 886

    assert_command "browse",
887
      "open https://github.com/mislav/hub/tree/experimental"
888
    assert_command "browse -- tree",
889
      "open https://github.com/mislav/hub/tree/experimental"
890
    assert_command "browse -- commits",
891
      "open https://github.com/mislav/hub/commits/experimental"
892
  end
893

894
  def test_hub_browse_current
895 896
    assert_command "browse", "open https://github.com/defunkt/hub"
    assert_command "browse --", "open https://github.com/defunkt/hub"
897 898
  end

899 900 901 902
  def test_hub_browse_commit_from_current
    assert_command "browse -- commit/6616e4", "open https://github.com/defunkt/hub/commit/6616e4"
  end

903 904 905 906 907 908 909
  def test_hub_browse_no_tracking
    stub_tracking_nothing
    assert_command "browse", "open https://github.com/defunkt/hub"
  end

  def test_hub_browse_no_tracking_on_branch
    stub_branch('refs/heads/feature')
910
    stub_tracking_nothing('feature')
911 912 913
    assert_command "browse", "open https://github.com/defunkt/hub"
  end

914 915 916 917 918 919 920 921 922
  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

923 924
  def test_hub_browse_current_subpage
    assert_command "browse -- network",
925
      "open https://github.com/defunkt/hub/network"
926
    assert_command "browse -- anything/everything",
927
      "open https://github.com/defunkt/hub/anything/everything"
928 929
  end

930 931 932 933
  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
934 935
  end

936 937 938
  def test_hub_browse_no_repo
    stub_repo_url(nil)
    assert_equal "Usage: hub browse [<USER>/]<REPOSITORY>\n", hub("browse")
939
  end
940 941 942 943 944 945 946 947 948 949

  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
950
      with_host_os("i686-linux") do
951 952 953 954 955 956 957 958
        assert_browser("xdg-open")
      end
    end
  end

  def test_cygwin_browser
    stub_available_commands "open", "cygstart"
    with_browser_env(nil) do
959
      with_host_os("i686-linux") do
960 961 962 963 964 965 966 967 968
        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
969
      with_host_os("i686-linux") do
970 971 972 973 974
        assert_equal expected, hub("browse")
      end
    end
  end

975
  def test_context_method_doesnt_hijack_git_command
976
    assert_forwarded 'remotes'
977 978
  end

979 980 981 982 983
  def test_not_choking_on_ruby_methods
    assert_forwarded 'id'
    assert_forwarded 'name'
  end

984 985
  def test_multiple_remote_urls
    stub_repo_url("git://example.com/other.git\ngit://github.com/my/repo.git")
986
    assert_command "browse", "open https://github.com/my/repo"
987 988
  end

989 990 991 992 993 994
  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

995 996
  protected

997 998 999 1000
    def stub_github_user(name)
      @git['config github.user'] = name
    end

1001 1002 1003 1004
    def stub_github_token(token)
      @git['config github.token'] = token
    end

1005
    def stub_repo_url(value)
1006
      @git['config --get-all remote.origin.url'] = value
1007 1008 1009 1010 1011 1012 1013
      Hub::Context::REMOTES.clear
    end

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

1014
    def stub_tracking(from, remote_name, remote_branch)
1015
      value = remote_branch ? "remotes/#{remote_name}/#{remote_branch}" : nil
1016
      @git["name-rev #{from}@{upstream} --name-only --refs='refs/remotes/*' --no-undefined"] = value
1017 1018
    end

1019 1020
    def stub_tracking_nothing(from = 'master')
      stub_tracking(from, nil, nil)
1021 1022
    end

1023 1024 1025 1026
    def stub_remotes_group(name, value)
      @git["config remotes.#{name}"] = value
    end

1027 1028 1029 1030 1031 1032 1033 1034
    def stub_no_remotes
      @git['remote'] = ''
    end

    def stub_no_git_repo
      @git.replace({})
    end

M
Mislav Marohnić 已提交
1035 1036 1037 1038
    def stub_alias(name, value)
      @git["config alias.#{name}"] = value
    end

1039 1040
    def stub_existing_fork(user, repo = 'hub')
      stub_fork(user, repo, 200)
1041 1042
    end

1043 1044
    def stub_nonexisting_fork(user, repo = 'hub')
      stub_fork(user, repo, 404)
1045 1046
    end

1047 1048
    def stub_fork(user, repo, status)
      stub_request(:get, "github.com/api/v2/yaml/repos/show/#{user}/#{repo}").
1049 1050 1051
        to_return(:status => status)
    end

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
    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

1063 1064 1065 1066 1067 1068 1069
    def with_tmpdir(value)
      dir, ENV['TMPDIR'] = ENV['TMPDIR'], value
      yield
    ensure
      ENV['TMPDIR'] = dir
    end

1070
    def assert_browser(browser)
1071
      assert_command "browse", "#{browser} https://github.com/defunkt/hub"
1072 1073
    end

1074 1075 1076 1077 1078 1079 1080 1081
    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
1082 1083
    end

1084 1085 1086 1087
    def auth(user = @git['config github.user'], password = @git['config github.token'])
      "#{user}%2Ftoken:#{password}@"
    end

1088 1089 1090 1091 1092 1093
    def mock_pullreq_response(id, name_with_owner = 'defunkt/hub')
      YAML.dump('pull' => {
        'html_url' => "https://github.com/#{name_with_owner}/pull/#{id}"
      })
    end

C
Chris Wanstrath 已提交
1094
end