hub_test.rb 47.1 KB
Newer Older
1
require 'helper'
2
require 'webmock/test_unit'
3
require 'rbconfig'
4
require 'yaml'
5
require 'forwardable'
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
  extend Forwardable

16 17 18 19 20
  if defined? WebMock::API
    include WebMock::API
  else
    include WebMock
  end
C
Chris Wanstrath 已提交
21

22 23
  COMMANDS = []

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

31 32 33 34
  attr_reader :git_reader
  include Hub::Context::GitReaderMethods
  def_delegators :git_reader, :stub_config_value, :stub_command_output

35
  def setup
36
    super
37
    COMMANDS.replace %w[open groff]
38
    Hub::Context::PWD.replace '/path/to/hub'
39
    Hub::SshConfig::CONFIG_FILES.replace []
40

41 42 43
    @git_reader = Hub::Context::GitReader.new 'git' do |cache, cmd|
      unless cmd.index('config --get alias.') == 0
        raise ArgumentError, "`git #{cmd}` not stubbed"
M
Mislav Marohnić 已提交
44
      end
45 46 47 48 49 50
    end

    Hub::Commands.instance_variable_set :@git_reader, @git_reader
    Hub::Commands.instance_variable_set :@local_repo, nil

    @git_reader.stub! \
51
      'remote' => "mislav\norigin",
52
      'symbolic-ref -q HEAD' => 'refs/heads/master',
53 54
      'config --get github.user'   => 'tpw',
      'config --get github.token'  => 'abc123',
55 56
      'config --get-all remote.origin.url' => 'git://github.com/defunkt/hub.git',
      'config --get-all remote.mislav.url' => 'git://github.com/mislav/hub.git',
57
      'rev-parse --symbolic-full-name master@{upstream}' => 'refs/remotes/origin/master',
58 59
      'config --get --bool hub.http-clone' => 'false',
      'config --get hub.protocol' => nil,
60
      'config --get-all hub.host' => nil,
61
      'rev-parse -q --git-dir' => '.git'
62 63
  end

C
Chris Wanstrath 已提交
64
  def test_private_clone
65
    stub_no_git_repo
66 67
    input   = "clone -p rtomayko/ronn"
    command = "git clone git@github.com:rtomayko/ronn.git"
C
Chris Wanstrath 已提交
68
    assert_command input, command
C
Chris Wanstrath 已提交
69 70
  end

71
  def test_private_clone_noop
72
    stub_no_git_repo
73 74 75 76 77
    input   = "--noop clone -p rtomayko/ronn"
    command = "git clone git@github.com:rtomayko/ronn.git\n"
    assert_output command, hub(input)
  end

78
  def test_https_clone
79
    stub_no_git_repo
80 81 82 83 84 85
    stub_https_is_preferred
    input   = "clone rtomayko/ronn"
    command = "git clone https://github.com/rtomayko/ronn.git"
    assert_command input, command
  end

C
Chris Wanstrath 已提交
86
  def test_public_clone
87
    stub_no_git_repo
88 89
    input   = "clone rtomayko/ronn"
    command = "git clone git://github.com/rtomayko/ronn.git"
C
Chris Wanstrath 已提交
90
    assert_command input, command
C
Chris Wanstrath 已提交
91 92
  end

93
  def test_your_private_clone
94
    stub_no_git_repo
95 96 97 98 99
    input   = "clone -p resque"
    command = "git clone git@github.com:tpw/resque.git"
    assert_command input, command
  end

100
  def test_your_clone_is_always_private
101
    stub_no_git_repo
102
    input   = "clone resque"
103
    command = "git clone git@github.com:tpw/resque.git"
104 105 106
    assert_command input, command
  end

107
  def test_clone_repo_with_period
108
    stub_no_git_repo
109 110 111 112 113
    input   = "clone hookio/hook.js"
    command = "git clone git://github.com/hookio/hook.js.git"
    assert_command input, command
  end

114
  def test_clone_with_arguments
115
    stub_no_git_repo
116
    input   = "clone --bare -o master resque"
117
    command = "git clone --bare -o master git@github.com:tpw/resque.git"
118 119 120
    assert_command input, command
  end

121
  def test_clone_with_arguments_and_destination
122
    stub_no_git_repo
123 124 125
    assert_forwarded "clone --template=one/two git://github.com/tpw/resque.git --origin master resquetastic"
  end

126
  def test_your_private_clone_fails_without_config
127
    stub_no_git_repo
128
    out = hub("clone -p mustache") do
129
      stub_github_user(nil)
130 131
    end

132
    assert_equal "** No GitHub user set. See http://help.github.com/set-your-user-name-email-and-github-token/\n", out
133 134 135
  end

  def test_your_public_clone_fails_without_config
136
    stub_no_git_repo
137
    out = hub("clone mustache") do
138
      stub_github_user(nil)
139 140
    end

141
    assert_equal "** No GitHub user set. See http://help.github.com/set-your-user-name-email-and-github-token/\n", out
142 143
  end

144
  def test_private_clone_left_alone
145
    stub_no_git_repo
146
    assert_forwarded "clone git@github.com:rtomayko/ronn.git"
147 148 149
  end

  def test_public_clone_left_alone
150
    stub_no_git_repo
151
    assert_forwarded "clone git://github.com/rtomayko/ronn.git"
152
  end
C
Chris Wanstrath 已提交
153 154

  def test_normal_public_clone_with_path
155
    stub_no_git_repo
156
    assert_forwarded "clone git://github.com/rtomayko/ronn.git ronn-dev"
C
Chris Wanstrath 已提交
157
  end
158 159

  def test_normal_clone_from_path
160
    stub_no_git_repo
161
    assert_forwarded "clone ./test"
162
  end
163

164 165 166 167 168 169 170 171 172 173
  def test_unchanged_clone_from_existing_directory
    stub_no_git_repo
    assert_forwarded "clone test"
  end

  def test_local_clone_with_destination
    stub_no_git_repo
    assert_forwarded "clone -l . ../copy"
  end

174
  def test_clone_with_host_alias
175
    stub_no_git_repo
176 177 178
    assert_forwarded "clone server:git/repo.git"
  end

179
  def test_enterprise_clone
180
    stub_no_git_repo
181 182 183 184 185 186 187
    stub_github_user('myfiname', 'git.my.org')
    with_host_env('git.my.org') do
      assert_command "clone myrepo", "git clone git@git.my.org:myfiname/myrepo.git"
      assert_command "clone another/repo", "git clone git@git.my.org:another/repo.git"
    end
  end

M
Mislav Marohnić 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  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

207 208 209 210 211 212
  def test_remote_origin
    input   = "remote add origin"
    command = "git remote add origin git://github.com/tpw/hub.git"
    assert_command input, command
  end

213 214 215 216 217 218
  def test_remote_add_with_name
    input   = "remote add another hookio/hub.js"
    command = "git remote add another git://github.com/hookio/hub.js.git"
    assert_command input, command
  end

219 220 221 222 223 224
  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

225 226 227 228 229 230
  def test_public_remote_url_untouched
    assert_forwarded "remote add origin http://github.com/defunkt/resque.git"
  end

  def test_private_remote_url_untouched
    assert_forwarded "remote add origin git@github.com:defunkt/resque.git"
231 232
  end

233
  def test_remote_from_rel_path
234
    assert_forwarded "remote add origin ./path"
235 236 237
  end

  def test_remote_from_abs_path
238
    assert_forwarded "remote add origin /path"
239 240
  end

241
  def test_remote_with_host_alias
242 243 244
    assert_forwarded "remote add origin server:/git/repo.git"
  end

245 246 247 248
  def test_remote_add_enterprise
    stub_hub_host('git.my.org')
    stub_repo_url('git@git.my.org:defunkt/hub.git')
    assert_command "remote add another", "git remote add another git@git.my.org:another/hub.git"
C
Chris Wanstrath 已提交
249 250
  end

S
Stephen Celis 已提交
251 252
  def test_public_submodule
    input   = "submodule add wycats/bundler vendor/bundler"
J
Justin Ridgewell 已提交
253 254
    command = "git submodule add git://github.com/wycats/bundler.git vendor/bundler"
    assert_command input, command
S
Stephen Celis 已提交
255 256 257 258
  end

  def test_private_submodule
    input   = "submodule add -p grit vendor/grit"
J
Justin Ridgewell 已提交
259 260 261 262 263 264 265 266
    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 已提交
267 268 269 270 271
  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 已提交
272
    assert_command input, command
S
Stephen Celis 已提交
273 274
  end

C
Chris Wanstrath 已提交
275
  def test_private_remote
276
    input   = "remote add -p rtomayko"
C
Chris Wanstrath 已提交
277 278
    command = "git remote add rtomayko git@github.com:rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
279 280
  end

281 282 283 284 285 286 287
  def test_https_protocol_remote
    stub_https_is_preferred
    input   = "remote add rtomayko"
    command = "git remote add rtomayko https://github.com/rtomayko/hub.git"
    assert_command input, command
  end

C
Chris Wanstrath 已提交
288
  def test_public_remote
289
    input   = "remote add rtomayko"
C
Chris Wanstrath 已提交
290 291
    command = "git remote add rtomayko git://github.com/rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
292 293
  end

C
Chris Wanstrath 已提交
294 295 296 297 298 299
  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

300 301 302 303 304 305
  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

306 307 308 309 310 311
  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 已提交
312
  def test_private_remote_with_repo
313 314
    input   = "remote add -p jashkenas/coffee-script"
    command = "git remote add jashkenas git@github.com:jashkenas/coffee-script.git"
C
Chris Wanstrath 已提交
315 316 317 318
    assert_command input, command
  end

  def test_public_remote_with_repo
319 320
    input   = "remote add jashkenas/coffee-script"
    command = "git remote add jashkenas git://github.com/jashkenas/coffee-script.git"
321 322 323 324
    assert_command input, command
  end

  def test_public_remote_f_with_repo
325 326
    input   = "remote add -f jashkenas/coffee-script"
    command = "git remote add -f jashkenas git://github.com/jashkenas/coffee-script.git"
C
Chris Wanstrath 已提交
327 328 329
    assert_command input, command
  end

330
  def test_named_private_remote_with_repo
331 332
    input   = "remote add -p origin jashkenas/coffee-script"
    command = "git remote add origin git@github.com:jashkenas/coffee-script.git"
333 334 335
    assert_command input, command
  end

336
  def test_fetch_existing_remote
337
    assert_forwarded "fetch mislav"
338 339 340 341 342 343
  end

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

344 345 346
    assert_commands "git remote add xoebus git://github.com/xoebus/hub.git",
                    "git fetch xoebus",
                    "fetch xoebus"
347 348
  end

349 350 351 352 353 354 355 356 357 358
  def test_fetch_new_remote_https_protocol
    stub_remotes_group('xoebus', nil)
    stub_existing_fork('xoebus')
    stub_https_is_preferred

    assert_commands "git remote add xoebus https://github.com/xoebus/hub.git",
                    "git fetch xoebus",
                    "fetch xoebus"
  end

359 360 361 362 363 364 365 366 367 368 369 370 371
  def test_fetch_no_auth
    stub_github_user nil
    stub_github_token nil
    stub_remotes_group('xoebus', nil)
    # stub_existing_fork('xoebus')
    stub_request(:get, "https://github.com/api/v2/yaml/repos/show/xoebus/hub").
      to_return(:status => 200)

    assert_commands "git remote add xoebus git://github.com/xoebus/hub.git",
                    "git fetch xoebus",
                    "fetch xoebus"
  end

372 373 374 375
  def test_fetch_new_remote_with_options
    stub_remotes_group('xoebus', nil)
    stub_existing_fork('xoebus')

376 377 378
    assert_commands "git remote add xoebus git://github.com/xoebus/hub.git",
                    "git fetch --depth=1 --prune xoebus",
                    "fetch --depth=1 --prune xoebus"
379 380 381 382 383 384 385 386
  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')

387 388 389 390
    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"
391 392 393 394 395 396 397 398
  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')

399 400 401 402
    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"
403 404 405 406 407 408 409 410 411 412 413 414 415 416
  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
417 418 419
    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"
420 421
  end

422
  def test_cherry_pick
423
    assert_forwarded "cherry-pick a319d88"
424 425 426
  end

  def test_cherry_pick_url
427
    url = 'http://github.com/mislav/hub/commit/a319d88'
428
    assert_commands "git fetch mislav", "git cherry-pick a319d88", "cherry-pick #{url}"
429 430
  end

431 432
  def test_cherry_pick_url_with_fragment
    url = 'http://github.com/mislav/hub/commit/abcdef0123456789#comments'
433
    assert_commands "git fetch mislav", "git cherry-pick abcdef0123456789", "cherry-pick #{url}"
434 435
  end

436 437
  def test_cherry_pick_url_with_remote_add
    url = 'https://github.com/xoebus/hub/commit/a319d88'
438
    assert_commands "git remote add -f xoebus git://github.com/xoebus/hub.git",
439 440
                    "git cherry-pick a319d88",
                    "cherry-pick #{url}"
441 442 443 444
  end

  def test_cherry_pick_origin_url
    url = 'https://github.com/defunkt/hub/commit/a319d88'
445
    assert_commands "git fetch origin", "git cherry-pick a319d88", "cherry-pick #{url}"
446 447 448
  end

  def test_cherry_pick_github_user_notation
449
    assert_commands "git fetch mislav", "git cherry-pick 368af20", "cherry-pick mislav@368af20"
450 451 452 453
  end

  def test_cherry_pick_github_user_repo_notation
    # not supported
454
    assert_forwarded "cherry-pick mislav/hubbub@a319d88"
455 456 457
  end

  def test_cherry_pick_github_notation_too_short
458
    assert_forwarded "cherry-pick mislav@a319"
459 460 461
  end

  def test_cherry_pick_github_notation_with_remote_add
462 463 464
    assert_commands "git remote add -f xoebus git://github.com/xoebus/hub.git",
                    "git cherry-pick a319d88",
                    "cherry-pick xoebus@a319d88"
465 466
  end

467 468 469 470 471 472 473 474
  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",
475
                      "am --signoff https://github.com/defunkt/hub/pull/55#comment_123 -p2"
476 477 478

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

482 483 484 485 486 487 488
  def test_am_no_tmpdir
    with_tmpdir(nil) do
      cmd = Hub("am https://github.com/defunkt/hub/pull/55").command
      assert_includes '/tmp/55.patch', cmd
    end
  end

489 490 491 492 493 494 495 496 497 498
  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

499 500 501 502 503 504 505 506 507 508
  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

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
  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 已提交
544
  def test_init
M
Mislav Marohnić 已提交
545 546
    stub_no_remotes
    stub_no_git_repo
547
    assert_commands "git init", "git remote add origin git@github.com:tpw/hub.git", "init -g"
548 549
  end

550 551 552 553 554 555 556 557 558
  def test_init_enterprise
    stub_no_remotes
    stub_no_git_repo
    stub_github_user('myfiname', 'git.my.org')
    with_host_env('git.my.org') do
      assert_commands "git init", "git remote add origin git@git.my.org:myfiname/hub.git", "init -g"
    end
  end

559 560
  def test_init_no_login
    out = hub("init -g") do
561
      stub_github_user(nil)
562 563
    end

564
    assert_equal "** No GitHub user set. See http://help.github.com/set-your-user-name-email-and-github-token/\n", out
C
Chris Wanstrath 已提交
565 566
  end

567 568 569 570
  def test_push_untouched
    assert_forwarded "push"
  end

571
  def test_push_two
572 573
    assert_commands "git push origin cool-feature", "git push staging cool-feature",
                    "push origin,staging cool-feature"
574 575
  end

576 577 578 579 580 581
  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 已提交
582
  def test_push_more
583 584 585 586
    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 已提交
587
  end
C
Chris Wanstrath 已提交
588

589
  def test_create
590
    stub_no_remotes
591
    stub_nonexisting_fork('tpw')
592
    stub_request(:post, "https://#{auth}github.com/api/v2/json/repos/create").
593
      with(:body => { 'name' => 'hub' })
594

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

600 601 602
  def test_create_custom_name
    stub_no_remotes
    stub_nonexisting_fork('tpw', 'hubbub')
603
    stub_request(:post, "https://#{auth}github.com/api/v2/json/repos/create").
604 605 606 607 608 609 610 611 612 613
      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')
614
    stub_request(:post, "https://#{auth}github.com/api/v2/json/repos/create").
615 616 617 618 619 620 621
      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

622 623
  def test_create_no_openssl
    stub_no_remotes
624 625 626 627
    # stub_nonexisting_fork('tpw')
    stub_request(:get, "http://#{auth}github.com/api/v2/yaml/repos/show/tpw/hub").
      to_return(:status => 404)

628
    stub_request(:post, "http://#{auth}github.com/api/v2/json/repos/create").
629 630 631 632 633 634 635 636 637 638 639 640
      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

641 642 643
  def test_create_failed
    stub_no_remotes
    stub_nonexisting_fork('tpw')
644
    stub_request(:post, "https://#{auth}github.com/api/v2/json/repos/create").
645 646
      to_return(:status => [401, "Your token is fail"])

647 648
    expected = "Error creating repository: Your token is fail (HTTP 401)\n"
    expected << "Check your token configuration (`git config github.token`)\n"
649 650 651
    assert_equal expected, hub("create") { ENV['GIT'] = 'echo' }
  end

652 653 654 655 656 657 658
  def test_create_with_env_authentication
    stub_no_remotes
    old_user  = ENV['GITHUB_USER']
    old_token = ENV['GITHUB_TOKEN']
    ENV['GITHUB_USER']  = 'mojombo'
    ENV['GITHUB_TOKEN'] = '123abc'

659 660 661 662
    # stub_nonexisting_fork('mojombo')
    stub_request(:get, "https://#{auth('mojombo', '123abc')}github.com/api/v2/yaml/repos/show/mojombo/hub").
      to_return(:status => 404)

663
    stub_request(:post, "https://#{auth('mojombo', '123abc')}github.com/api/v2/json/repos/create").
664
      with(:body => { 'name' => 'hub' })
665 666 667 668 669 670 671 672 673 674

    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

675
  def test_create_private_repository
676
    stub_no_remotes
677
    stub_nonexisting_fork('tpw')
678
    stub_request(:post, "https://#{auth}github.com/api/v2/json/repos/create").
679
      with(:body => { 'name' => 'hub', 'public' => '0' })
680

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

686 687 688 689 690 691 692 693 694 695 696 697 698
  def test_create_private_repository_fails
    stub_no_remotes
    stub_nonexisting_fork('tpw')
    stub_request(:post, "https://#{auth}github.com/api/v2/json/repos/create").
      to_return(:status => [422, "Unprocessable Entity"],
                :headers => {"Content-type" => "application/json"},
                :body => %({"error":"repository creation failed: You are over your quota."}))

    expected = "Error creating repository: Unprocessable Entity (HTTP 422)\n"
    expected << "repository creation failed: You are over your quota.\n"
    assert_equal expected, hub("create -p") { ENV['GIT'] = 'echo' }
  end

699
  def test_create_with_description_and_homepage
700
    stub_no_remotes
701
    stub_nonexisting_fork('tpw')
702
    stub_request(:post, "https://#{auth}github.com/api/v2/json/repos/create").with(:body => {
703
      'name' => 'hub', 'description' => 'toyproject', 'homepage' => 'http://example.com'
704 705
    })

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

711 712 713 714 715
  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

716
  def test_create_with_existing_repository
717
    stub_no_remotes
718 719
    stub_existing_fork('tpw')

720
    expected = "tpw/hub already exists on github.com\n"
721
    expected << "remote add -f origin git@github.com:tpw/hub.git\n"
722
    expected << "set remote origin: tpw/hub\n"
723 724 725
    assert_equal expected, hub("create") { ENV['GIT'] = 'echo' }
  end

726 727 728 729 730
  def test_create_https_protocol
    stub_no_remotes
    stub_existing_fork('tpw')
    stub_https_is_preferred

731
    expected = "tpw/hub already exists on github.com\n"
732 733 734 735 736
    expected << "remote add -f origin https://github.com/tpw/hub.git\n"
    expected << "set remote origin: tpw/hub\n"
    assert_equal expected, hub("create") { ENV['GIT'] = 'echo' }
  end

737
  def test_create_no_user
738
    stub_no_remotes
739 740 741
    out = hub("create") do
      stub_github_token(nil)
    end
742
    assert_equal "** No GitHub token set. See http://help.github.com/set-your-user-name-email-and-github-token/\n", out
743 744
  end

745
  def test_create_outside_git_repo
746
    stub_no_git_repo
747 748
    assert_equal "'create' must be run from inside a git repository\n", hub("create")
  end
749 750

  def test_create_origin_already_exists
C
Chris Wanstrath 已提交
751
    stub_nonexisting_fork('tpw')
752
    stub_request(:post, "https://#{auth}github.com/api/v2/json/repos/create").
753
      with(:body => { 'name' => 'hub' })
C
Chris Wanstrath 已提交
754 755

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

759
  def test_fork
C
Chris Wanstrath 已提交
760
    stub_nonexisting_fork('tpw')
761 762
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/repos/fork/defunkt/hub").
      with { |req| req.headers['Content-Length'] == 0 }
C
Chris Wanstrath 已提交
763

C
Chris Wanstrath 已提交
764
    expected = "remote add -f tpw git@github.com:tpw/hub.git\n"
765
    expected << "new remote: tpw\n"
766
    assert_output expected, "fork"
767 768
  end

769 770 771 772 773 774 775 776 777 778
  def test_fork_https_protocol
    stub_https_is_preferred
    stub_nonexisting_fork('tpw')
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/repos/fork/defunkt/hub")

    expected = "remote add -f tpw https://github.com/tpw/hub.git\n"
    expected << "new remote: tpw\n"
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end

779 780 781 782 783 784
  def test_fork_not_in_repo
    stub_no_git_repo
    expected = "fatal: Not a git repository\n"
    assert_output expected, "fork"
  end

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
  def test_fork_enterprise
    stub_hub_host('git.my.org')
    stub_repo_url('git@git.my.org:defunkt/hub.git')
    stub_github_user('myfiname', 'git.my.org')
    stub_github_token('789xyz', 'git.my.org')

    stub_request(:get, "https://#{auth('myfiname', '789xyz')}git.my.org/api/v2/yaml/repos/show/myfiname/hub").
      to_return(:status => 404)
    stub_request(:post, "https://#{auth('myfiname', '789xyz')}git.my.org/api/v2/yaml/repos/fork/defunkt/hub")

    expected = "remote add -f myfiname git@git.my.org:myfiname/hub.git\n"
    expected << "new remote: myfiname\n"
    assert_output expected, "fork"
  end

800 801
  def test_fork_failed
    stub_nonexisting_fork('tpw')
802
    stub_request(:post, "https://#{auth}github.com/api/v2/yaml/repos/fork/defunkt/hub").
803 804
      to_return(:status => [500, "Your fork is fail"])

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

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

813 814 815 816
    assert_equal "", hub("fork --no-remote") { ENV['GIT'] = 'echo' }
  end

  def test_fork_already_exists
817
    stub_existing_fork('tpw')
C
Chris Wanstrath 已提交
818

819
    expected = "Error creating fork: tpw/hub already exists on github.com\n"
820 821 822
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end

823
  def test_pullrequest
M
Mislav Marohnić 已提交
824 825 826
    expected = "Aborted: head branch is the same as base (\"master\")\n" <<
      "(use `-h <branch>` to specify an explicit pull request head)\n"
    assert_output expected, "pull-request hereyougo"
827 828
  end

M
Mislav Marohnić 已提交
829 830
  def test_pullrequest_with_unpushed_commits
    stub_tracking('master', 'mislav', 'master')
831
    stub_command_output "rev-list --cherry-pick --right-only --no-merges mislav/master...", "+abcd1234\n+bcde2345"
832

M
Mislav Marohnić 已提交
833
    expected = "Aborted: 2 commits are not yet pushed to mislav/master\n" <<
834
      "(use `-f` to force submit a pull request anyway)\n"
835 836 837 838 839 840
    assert_output expected, "pull-request hereyougo"
  end

  def test_pullrequest_from_branch
    stub_branch('refs/heads/feature')
    stub_tracking_nothing('feature')
M
Mislav Marohnić 已提交
841

842
    stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/defunkt/hub").
843 844 845
      with(:body => { 'pull' => {'base' => "master", 'head' => "tpw:feature", 'title' => "hereyougo"} }) { |req|
        req.headers['Content-Length'] == 76
      }.to_return(:body => mock_pullreq_response(1))
846 847

    expected = "https://github.com/defunkt/hub/pull/1\n"
848
    assert_output expected, "pull-request hereyougo -f"
849 850 851 852
  end

  def test_pullrequest_from_tracking_branch
    stub_branch('refs/heads/feature')
M
Mislav Marohnić 已提交
853
    stub_tracking('feature', 'mislav', 'yay-feature')
854

855
    stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/defunkt/hub").
M
Mislav Marohnić 已提交
856
      with(:body => { 'pull' => {'base' => "master", 'head' => "mislav:yay-feature", 'title' => "hereyougo"} }).
857 858 859
      to_return(:body => mock_pullreq_response(1))

    expected = "https://github.com/defunkt/hub/pull/1\n"
860
    assert_output expected, "pull-request hereyougo -f"
861 862
  end

863 864 865 866 867 868 869 870 871 872 873 874
  def test_pullrequest_from_branch_tracking_local
    stub_branch('refs/heads/feature')
    stub_tracking('feature', 'refs/heads/master')

    stub_request(:post, "https://#{auth}github.com/api/v2/json/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"
    assert_output expected, "pull-request hereyougo -f"
  end

875 876 877 878 879 880 881 882 883
  def test_pullrequest_invalid_remote
    stub_repo_url('gh:singingwolfboy/sekrit.git')
    stub_branch('refs/heads/feature')
    stub_tracking('feature', 'origin', 'feature')

    expected = "Aborted: the origin remote doesn't point to a GitHub repository.\n"
    assert_output expected, "pull-request hereyougo"
  end

884 885 886 887 888 889 890
  def test_pullrequest_enterprise_no_tracking
    stub_hub_host('git.my.org')
    stub_repo_url('git@git.my.org:defunkt/hub.git')
    stub_github_user('myfiname', 'git.my.org')
    stub_github_token('789xyz', 'git.my.org')
    stub_branch('refs/heads/feature')
    stub_tracking_nothing('feature')
891
    stub_command_output "rev-list --cherry-pick --right-only --no-merges origin/feature...", nil
892 893 894 895 896 897 898 899 900

    stub_request(:post, "https://#{auth('myfiname', '789xyz')}git.my.org/api/v2/json/pulls/defunkt/hub").
      with(:body => { 'pull' => {'base' => "master", 'head' => "myfiname:feature", 'title' => "hereyougo"} }).
      to_return(:body => mock_pullreq_response(1, 'defunkt/hub', 'git.my.org'))

    expected = "https://git.my.org/defunkt/hub/pull/1\n"
    assert_output expected, "pull-request hereyougo -f"
  end

901
  def test_pullrequest_explicit_head
902
    stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/defunkt/hub").
903 904 905 906
      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"
907
    assert_output expected, "pull-request hereyougo -h yay-feature -f"
908 909 910
  end

  def test_pullrequest_explicit_head_with_owner
911
    stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/defunkt/hub").
912 913 914 915
      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"
916
    assert_output expected, "pull-request hereyougo -h mojombo:feature -f"
917 918 919
  end

  def test_pullrequest_explicit_base
920
    stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/defunkt/hub").
M
Mislav Marohnić 已提交
921
      with(:body => { 'pull' => {'base' => "feature", 'head' => "defunkt:master", 'title' => "hereyougo"} }).
922 923 924
      to_return(:body => mock_pullreq_response(1))

    expected = "https://github.com/defunkt/hub/pull/1\n"
925
    assert_output expected, "pull-request hereyougo -b feature -f"
926 927 928
  end

  def test_pullrequest_explicit_base_with_owner
929
    stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/mojombo/hub").
M
Mislav Marohnić 已提交
930
      with(:body => { 'pull' => {'base' => "feature", 'head' => "defunkt:master", 'title' => "hereyougo"} }).
931 932 933
      to_return(:body => mock_pullreq_response(1))

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

937
  def test_pullrequest_explicit_base_with_repo
938
    stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/mojombo/hubbub").
M
Mislav Marohnić 已提交
939
      with(:body => { 'pull' => {'base' => "feature", 'head' => "defunkt:master", 'title' => "hereyougo"} }).
940 941 942
      to_return(:body => mock_pullreq_response(1))

    expected = "https://github.com/defunkt/hub/pull/1\n"
943
    assert_output expected, "pull-request hereyougo -b mojombo/hubbub:feature -f"
944 945 946
  end

  def test_pullrequest_existing_issue
M
Mislav Marohnić 已提交
947 948
    stub_branch('refs/heads/myfix')
    stub_tracking('myfix', 'mislav', 'awesomefix')
949
    stub_command_output "rev-list --cherry-pick --right-only --no-merges mislav/awesomefix...", nil
M
Mislav Marohnić 已提交
950

951
    stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/defunkt/hub").
M
Mislav Marohnić 已提交
952
      with(:body => { 'pull' => {'base' => "master", 'head' => "mislav:awesomefix", 'issue' => '92'} }).
953 954 955
      to_return(:body => mock_pullreq_response(92))

    expected = "https://github.com/defunkt/hub/pull/92\n"
M
Mislav Marohnić 已提交
956
    assert_output expected, "pull-request -i 92"
957 958
  end

959
  def test_pullrequest_existing_issue_url
M
Mislav Marohnić 已提交
960 961
    stub_branch('refs/heads/myfix')
    stub_tracking('myfix', 'mislav', 'awesomefix')
962
    stub_command_output "rev-list --cherry-pick --right-only --no-merges mislav/awesomefix...", nil
M
Mislav Marohnić 已提交
963

964
    stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/mojombo/hub").
M
Mislav Marohnić 已提交
965
      with(:body => { 'pull' => {'base' => "master", 'head' => "mislav:awesomefix", 'issue' => '92'} }).
966 967 968
      to_return(:body => mock_pullreq_response(92, 'mojombo/hub'))

    expected = "https://github.com/mojombo/hub/pull/92\n"
M
Mislav Marohnić 已提交
969
    assert_output expected, "pull-request https://github.com/mojombo/hub/issues/92#comment_4"
970 971
  end

972 973 974 975 976 977 978 979 980 981 982
  def test_pullrequest_fails
    stub_request(:post, "https://#{auth}github.com/api/v2/json/pulls/defunkt/hub").
      to_return(:status => [422, "Unprocessable Entity"],
                :headers => {"Content-type" => "application/json"},
                :body => %({"error":["oh no!", "it failed."]}))

    expected = "Error creating pull request: Unprocessable Entity (HTTP 422)\n"
    expected << "oh no!\nit failed.\n"
    assert_output expected, "pull-request hereyougo -b feature -f"
  end

983 984 985 986 987
  def test_checkout_no_changes
    assert_forwarded "checkout master"
  end

  def test_checkout_pullrequest
988
    stub_request(:get, "https://#{auth}github.com/api/v2/json/pulls/defunkt/hub/73").
989 990 991
      to_return(:body => mock_pull_response('blueyed:feature'))

    assert_commands 'git remote add -f -t feature blueyed git://github.com/blueyed/hub.git',
M
Mislav Marohnić 已提交
992 993
      'git checkout -f --track -B blueyed-feature blueyed/feature -q',
      "checkout -f https://github.com/defunkt/hub/pull/73/files -q"
994 995
  end

996 997 998 999 1000
  def test_checkout_private_pullrequest
    stub_request(:get, "https://#{auth}github.com/api/v2/json/pulls/defunkt/hub/73").
      to_return(:body => mock_pull_response('blueyed:feature', :private))

    assert_commands 'git remote add -f -t feature blueyed git@github.com:blueyed/hub.git',
M
Mislav Marohnić 已提交
1001
      'git checkout --track -B blueyed-feature blueyed/feature',
1002 1003 1004
      "checkout https://github.com/defunkt/hub/pull/73/files"
  end

1005
  def test_checkout_pullrequest_custom_branch
1006
    stub_request(:get, "https://#{auth}github.com/api/v2/json/pulls/defunkt/hub/73").
1007 1008 1009
      to_return(:body => mock_pull_response('blueyed:feature'))

    assert_commands 'git remote add -f -t feature blueyed git://github.com/blueyed/hub.git',
M
Mislav Marohnić 已提交
1010
      'git checkout --track -B review blueyed/feature',
1011 1012 1013 1014 1015 1016
      "checkout https://github.com/defunkt/hub/pull/73/files review"
  end

  def test_checkout_pullrequest_existing_remote
    stub_command_output 'remote', "origin\nblueyed"

1017
    stub_request(:get, "https://#{auth}github.com/api/v2/json/pulls/defunkt/hub/73").
1018 1019 1020 1021
      to_return(:body => mock_pull_response('blueyed:feature'))

    assert_commands 'git remote set-branches --add blueyed feature',
      'git fetch blueyed +refs/heads/feature:refs/remotes/blueyed/feature',
M
Mislav Marohnić 已提交
1022
      'git checkout --track -B blueyed-feature blueyed/feature',
1023 1024 1025
      "checkout https://github.com/defunkt/hub/pull/73/files"
  end

C
Chris Wanstrath 已提交
1026
  def test_version
1027
    out = hub('--version')
1028
    assert_includes "git version 1.7.0.4", out
C
Chris Wanstrath 已提交
1029
    assert_includes "hub version #{Hub::Version}", out
C
Chris Wanstrath 已提交
1030
  end
C
Chris Wanstrath 已提交
1031

1032 1033 1034 1035 1036 1037 1038
  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')
1039
    assert_equal improved_help_text, out
1040 1041 1042 1043 1044 1045 1046
  end

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

C
Chris Wanstrath 已提交
1047
  def test_help
1048
    assert_equal improved_help_text, hub("help")
C
Chris Wanstrath 已提交
1049 1050 1051
  end

  def test_help_by_default
1052
    assert_equal improved_help_text, hub("")
C
Chris Wanstrath 已提交
1053
  end
C
Chris Wanstrath 已提交
1054

S
Stephen Celis 已提交
1055
  def test_help_with_pager
1056
    assert_equal improved_help_text, hub("-p")
S
Stephen Celis 已提交
1057 1058
  end

C
Chris Wanstrath 已提交
1059 1060 1061 1062
  def test_help_hub
    help_manpage = hub("help hub")
    assert_includes "git + hub = github", help_manpage
    assert_includes <<-config, help_manpage
C
Chris Wanstrath 已提交
1063
Use git-config(1) to display the currently configured GitHub username:
C
Chris Wanstrath 已提交
1064 1065 1066
config
  end

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
  def test_help_flag_on_command
    help_manpage = hub("browse --help")
    assert_includes "git + hub = github", help_manpage
    assert_includes "git browse", help_manpage
  end

  def test_help_short_flag_on_command
    usage_help = hub("create -h")
    expected = "Usage: git create [NAME] [-p] [-d DESCRIPTION] [-h HOMEPAGE]\n"
    assert_equal expected, usage_help

    usage_help = hub("pull-request -h")
    expected = "Usage: git pull-request [-f] [TITLE|-i ISSUE] [-b BASE] [-h HEAD]\n"
    assert_equal expected, usage_help
  end

C
Chris Wanstrath 已提交
1083
  def test_help_hub_no_groff
1084 1085
    stub_available_commands()
    assert_equal "** Can't find groff(1)\n", hub("help hub")
C
Chris Wanstrath 已提交
1086
  end
1087

1088
  def test_hub_standalone
1089
    assert_includes 'This file is generated code', hub("hub standalone")
1090 1091
  end

C
Chris Wanstrath 已提交
1092 1093
  def test_hub_compare
    assert_command "compare refactor",
1094
      "open https://github.com/defunkt/hub/compare/refactor"
1095
  end
C
Chris Wanstrath 已提交
1096

1097 1098 1099 1100
  def test_hub_compare_nothing
    expected = "Usage: hub compare [USER] [<START>...]<END>\n"
    assert_equal expected, hub("compare")
  end
C
Chris Wanstrath 已提交
1101

1102 1103 1104 1105 1106 1107 1108 1109
  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')
1110
    stub_tracking('feature', 'mislav', 'experimental')
1111 1112

    assert_command "compare",
1113
      "open https://github.com/mislav/hub/compare/experimental"
1114 1115
  end

1116
  def test_hub_compare_range
C
Chris Wanstrath 已提交
1117
    assert_command "compare 1.0...fix",
1118
      "open https://github.com/defunkt/hub/compare/1.0...fix"
1119
  end
C
Chris Wanstrath 已提交
1120

1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
  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

1136 1137 1138 1139 1140 1141
  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

1142
  def test_hub_compare_fork
C
Chris Wanstrath 已提交
1143
    assert_command "compare myfork feature",
1144
      "open https://github.com/myfork/hub/compare/feature"
1145 1146 1147 1148
  end

  def test_hub_compare_url
    assert_command "compare -u 1.0...1.1",
1149
      "echo https://github.com/defunkt/hub/compare/1.0...1.1"
1150 1151 1152
  end

  def test_hub_browse
1153
    assert_command "browse mojombo/bert", "open https://github.com/mojombo/bert"
1154 1155
  end

1156 1157 1158 1159
  def test_hub_browse_commit
    assert_command "browse mojombo/bert commit/5d5582", "open https://github.com/mojombo/bert/commit/5d5582"
  end

1160 1161
  def test_hub_browse_tracking_nothing
    stub_tracking_nothing
1162
    assert_command "browse mojombo/bert", "open https://github.com/mojombo/bert"
1163 1164
  end

1165
  def test_hub_browse_url
1166
    assert_command "browse -u mojombo/bert", "echo https://github.com/mojombo/bert"
1167 1168
  end

1169
  def test_hub_browse_self
1170
    assert_command "browse resque", "open https://github.com/tpw/resque"
1171 1172
  end

1173 1174
  def test_hub_browse_subpage
    assert_command "browse resque commits",
1175
      "open https://github.com/tpw/resque/commits/master"
1176
    assert_command "browse resque issues",
1177
      "open https://github.com/tpw/resque/issues"
1178
    assert_command "browse resque wiki",
1179
      "open https://github.com/tpw/resque/wiki"
1180 1181 1182 1183
  end

  def test_hub_browse_on_branch
    stub_branch('refs/heads/feature')
1184
    stub_tracking('feature', 'mislav', 'experimental')
1185

1186
    assert_command "browse resque", "open https://github.com/tpw/resque"
1187
    assert_command "browse resque commits",
1188
      "open https://github.com/tpw/resque/commits/master"
1189 1190

    assert_command "browse",
1191
      "open https://github.com/mislav/hub/tree/experimental"
1192
    assert_command "browse -- tree",
1193
      "open https://github.com/mislav/hub/tree/experimental"
1194
    assert_command "browse -- commits",
1195
      "open https://github.com/mislav/hub/commits/experimental"
1196
  end
1197

1198 1199 1200 1201 1202 1203 1204 1205
  def test_hub_browse_on_complex_branch
    stub_branch('refs/heads/feature/foo')
    stub_tracking('feature/foo', 'mislav', 'feature/bar')

    assert_command 'browse',
      'open https://github.com/mislav/hub/tree/feature/bar'
  end

1206 1207 1208 1209 1210
  def test_hub_browse_no_branch
    stub_branch(nil)
    assert_command 'browse', 'open https://github.com/defunkt/hub'
  end

1211
  def test_hub_browse_current
1212 1213
    assert_command "browse", "open https://github.com/defunkt/hub"
    assert_command "browse --", "open https://github.com/defunkt/hub"
1214 1215
  end

1216 1217 1218 1219 1220
  def test_hub_browse_current_https_uri
    stub_repo_url "https://github.com/defunkt/hub"
    assert_command "browse", "open https://github.com/defunkt/hub"
  end

1221 1222 1223 1224
  def test_hub_browse_commit_from_current
    assert_command "browse -- commit/6616e4", "open https://github.com/defunkt/hub/commit/6616e4"
  end

1225 1226 1227 1228 1229 1230 1231
  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')
1232
    stub_tracking_nothing('feature')
1233 1234 1235
    assert_command "browse", "open https://github.com/defunkt/hub"
  end

1236 1237 1238 1239 1240 1241 1242 1243 1244
  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

1245 1246
  def test_hub_browse_current_subpage
    assert_command "browse -- network",
1247
      "open https://github.com/defunkt/hub/network"
1248
    assert_command "browse -- anything/everything",
1249
      "open https://github.com/defunkt/hub/anything/everything"
1250 1251
  end

1252 1253 1254 1255
  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
1256 1257
  end

1258 1259 1260
  def test_hub_browse_no_repo
    stub_repo_url(nil)
    assert_equal "Usage: hub browse [<USER>/]<REPOSITORY>\n", hub("browse")
1261
  end
1262

1263 1264 1265 1266 1267 1268 1269
  def test_hub_browse_ssh_alias
    with_ssh_config do
      stub_repo_url "gh:singingwolfboy/sekrit.git"
      assert_command "browse", "open https://github.com/singingwolfboy/sekrit"
    end
  end

1270 1271 1272 1273 1274 1275 1276 1277 1278
  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
1279
      with_host_os("i686-linux") do
1280 1281 1282 1283 1284 1285 1286 1287
        assert_browser("xdg-open")
      end
    end
  end

  def test_cygwin_browser
    stub_available_commands "open", "cygstart"
    with_browser_env(nil) do
1288
      with_host_os("i686-linux") do
1289 1290 1291 1292 1293 1294 1295 1296 1297
        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
1298
      with_host_os("i686-linux") do
1299 1300 1301 1302 1303
        assert_equal expected, hub("browse")
      end
    end
  end

1304
  def test_context_method_doesnt_hijack_git_command
1305
    assert_forwarded 'remotes'
1306 1307
  end

1308 1309 1310 1311 1312
  def test_not_choking_on_ruby_methods
    assert_forwarded 'id'
    assert_forwarded 'name'
  end

1313 1314
  def test_multiple_remote_urls
    stub_repo_url("git://example.com/other.git\ngit://github.com/my/repo.git")
1315
    assert_command "browse", "open https://github.com/my/repo"
1316 1317
  end

1318 1319 1320
  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'
1321
    assert_equal %w[git --bare -c core.awesome=true -c name=value --git-dir=/srv/www], git_reader.executable
1322 1323
  end

1324
  private
1325

1326 1327 1328
    def stub_github_user(name, host = '')
      host = %(."#{host}") unless host.empty?
      stub_config_value "github#{host}.user", name
1329 1330
    end

1331 1332 1333
    def stub_github_token(token, host = '')
      host = %(."#{host}") unless host.empty?
      stub_config_value "github#{host}.token", token
1334 1335
    end

M
Mislav Marohnić 已提交
1336 1337
    def stub_repo_url(value, remote_name = 'origin')
      stub_config_value "remote.#{remote_name}.url", value, '--get-all'
1338 1339 1340
    end

    def stub_branch(value)
1341
      stub_command_output 'symbolic-ref -q HEAD', value
1342 1343
    end

1344
    def stub_tracking(from, upstream, remote_branch = nil)
1345
      stub_command_output "rev-parse --symbolic-full-name #{from}@{upstream}",
1346
        remote_branch ? "refs/remotes/#{upstream}/#{remote_branch}" : upstream
1347 1348
    end

1349
    def stub_tracking_nothing(from = 'master')
1350
      stub_tracking(from, nil)
1351 1352
    end

1353
    def stub_remotes_group(name, value)
1354
      stub_config_value "remotes.#{name}", value
1355 1356
    end

1357
    def stub_no_remotes
1358
      stub_command_output 'remote', nil
1359 1360 1361
    end

    def stub_no_git_repo
1362
      stub_command_output 'rev-parse -q --git-dir', nil
1363 1364
    end

M
Mislav Marohnić 已提交
1365
    def stub_alias(name, value)
1366
      stub_config_value "alias.#{name}", value
M
Mislav Marohnić 已提交
1367 1368
    end

1369 1370
    def stub_existing_fork(user, repo = 'hub')
      stub_fork(user, repo, 200)
1371 1372
    end

1373 1374
    def stub_nonexisting_fork(user, repo = 'hub')
      stub_fork(user, repo, 404)
1375 1376
    end

1377
    def stub_fork(user, repo, status)
1378
      stub_request(:get, "https://#{auth}github.com/api/v2/yaml/repos/show/#{user}/#{repo}").
1379 1380 1381
        to_return(:status => status)
    end

1382 1383 1384 1385
    def stub_available_commands(*names)
      COMMANDS.replace names
    end

1386
    def stub_https_is_preferred
1387
      stub_config_value 'hub.protocol', 'https'
1388 1389
    end

1390 1391 1392 1393
    def stub_hub_host(names)
      stub_config_value "hub.host", Array(names).join("\n"), '--get-all'
    end

1394 1395 1396 1397 1398 1399 1400
    def with_browser_env(value)
      browser, ENV['BROWSER'] = ENV['BROWSER'], value
      yield
    ensure
      ENV['BROWSER'] = browser
    end

1401 1402 1403 1404 1405 1406 1407
    def with_tmpdir(value)
      dir, ENV['TMPDIR'] = ENV['TMPDIR'], value
      yield
    ensure
      ENV['TMPDIR'] = dir
    end

1408 1409 1410 1411 1412 1413 1414
    def with_host_env(value)
      host, ENV['GITHUB_HOST'] = ENV['GITHUB_HOST'], value
      yield
    ensure
      ENV['GITHUB_HOST'] = host
    end

1415
    def assert_browser(browser)
1416
      assert_command "browse", "#{browser} https://github.com/defunkt/hub"
1417 1418
    end

1419 1420 1421 1422 1423 1424 1425 1426
    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
1427 1428
    end

1429
    def auth(user = git_config('github.user'), password = git_config('github.token'))
1430 1431 1432
      "#{user}%2Ftoken:#{password}@"
    end

1433 1434
    def mock_pullreq_response(id, name_with_owner = 'defunkt/hub', host = 'github.com')
      %({"pull": { "html_url": "https://#{host}/#{name_with_owner}/pull/#{id}" }})
1435 1436
    end

1437 1438
    def mock_pull_response(label, priv = false)
      %({"pull": { "head": { "label": "#{label}", "repository": {"private":#{!!priv}} }}})
1439 1440
    end

1441 1442 1443 1444
    def improved_help_text
      Hub::Commands.send :improved_help_text
    end

1445 1446
    def with_ssh_config
      config_file = File.expand_path '../ssh_config', __FILE__
1447
      Hub::SshConfig::CONFIG_FILES.replace [config_file]
1448 1449 1450
      yield
    end

C
Chris Wanstrath 已提交
1451
end