class HubTest

Constants

COMMANDS

Attributes

git_reader[R]

Public Instance Methods

setup() click to toggle source
Calls superclass method
# File test/hub_test.rb, line 44
def setup
  super
  COMMANDS.replace %w[open groff]
  Hub::Context::PWD.replace '/path/to/hub'
  Hub::SshConfig::CONFIG_FILES.replace []

  @prompt_stubs = prompt_stubs = []
  @password_prompt_stubs = password_prompt_stubs = []
  @repo_file_read = repo_file_read = {}

  Hub::GitHubAPI::Configuration.class_eval do
    undef prompt
    undef prompt_password

    define_method :prompt do |what|
      prompt_stubs.shift.call(what)
    end
    define_method :prompt_password do |host, user|
      password_prompt_stubs.shift.call(host, user)
    end
  end

  Hub::Context::LocalRepo.class_eval do
    undef file_read
    undef file_exist?

    define_method(:file_read) do |*args|
      name = File.join(*args)
      if value = repo_file_read[name]
        value.dup
      else
        raise Errno::ENOENT
      end
    end

    define_method(:file_exist?) do |*args|
      name = File.join(*args)
      !!repo_file_read[name]
    end
  end

  @git_reader = Hub::Context::GitReader.new 'git' do |cache, cmd|
    unless cmd.index('config --get alias.') == 0
      raise ArgumentError, "`git #{cmd}` not stubbed"
    end
  end

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

  FileUtils.rm_rf ENV['HUB_CONFIG']

  edit_hub_config do |data|
    data['github.com'] = [{'user' => 'tpw', 'oauth_token' => 'OTOKEN'}]
  end

  @git_reader.stub!        'remote -v' => "origin\tgit://github.com/defunkt/hub.git (fetch)\nmislav\tgit://github.com/mislav/hub.git (fetch)",
    'rev-parse --symbolic-full-name master@{upstream}' => 'refs/remotes/origin/master',
    'config --get --bool hub.http-clone' => 'false',
    'config --get hub.protocol' => nil,
    'config --get-all hub.host' => nil,
    'config --get push.default' => nil,
    'rev-parse -q --git-dir' => '.git'

  stub_branch('refs/heads/master')
  stub_remote_branch('origin/master')
end
teardown() click to toggle source
Calls superclass method
# File test/hub_test.rb, line 114
def teardown
  super
  WebMock.reset!
end
test_am_commit_url() click to toggle source
# File test/hub_test.rb, line 192
def test_am_commit_url
  stub_request(:get, "https://api.github.com/repos/davidbalbert/hub/commits/fdb9921").
    with(:headers => {'Accept'=>'application/vnd.github.v3.patch', 'Authorization'=>'token OTOKEN'}).
    to_return(:status => 200)

  with_tmpdir('/tmp/') do
    url = 'https://github.com/davidbalbert/hub/commit/fdb9921'
    assert_commands %r{git am --signoff /tmp/fdb9921\.patch[\w-]+ -p2},
                    "am --signoff #{url} -p2"
  end
end
test_am_gist() click to toggle source
# File test/hub_test.rb, line 204
def test_am_gist
  stub_request(:get, "https://api.github.com/gists/8da7fb575debd88c54cf").
    with(:headers => {'Authorization'=>'token OTOKEN'}).
    to_return(:body => Hub::JSON.generate(:files => {
      'file.diff' => {
        :raw_url => "https://gist.github.com/raw/8da7fb575debd88c54cf/SHA/file.diff"
      }
    }))

  stub_request(:get, "https://gist.github.com/raw/8da7fb575debd88c54cf/SHA/file.diff").
    with(:headers => {'Accept'=>'text/plain'}).
    to_return(:status => 200)

  with_tmpdir('/tmp/') do
    url = 'https://gist.github.com/8da7fb575debd88c54cf'

    assert_commands %r{git am --signoff /tmp/gist-8da7fb575debd88c54cf\.txt[\w-]+ -p2},
                    "am --signoff #{url} -p2"
  end
end
test_am_no_tmpdir() click to toggle source
# File test/hub_test.rb, line 182
def test_am_no_tmpdir
  stub_request(:get, "https://api.github.com/repos/defunkt/hub/pulls/55").
    to_return(:status => 200)

  with_tmpdir(nil) do
    cmd = Hub("am https://github.com/defunkt/hub/pull/55").command
    assert_includes '/tmp/55.patch', cmd
  end
end
test_am_pull_request() click to toggle source
# File test/hub_test.rb, line 168
def test_am_pull_request
  stub_request(:get, "https://api.github.com/repos/defunkt/hub/pulls/55").
    with(:headers => {'Accept'=>'application/vnd.github.v3.patch', 'Authorization'=>'token OTOKEN'}).
    to_return(:status => 200)

  with_tmpdir('/tmp/') do
    assert_commands %r{git am --signoff /tmp/55\.patch[\w-]+ -p2},
                    "am --signoff https://github.com/defunkt/hub/pull/55#comment_123 -p2"

    cmd = Hub("am https://github.com/defunkt/hub/pull/55/files").command
    assert_includes '/tmp/55.patch', cmd
  end
end
test_am_untouched() click to toggle source
# File test/hub_test.rb, line 164
def test_am_untouched
  assert_forwarded "am some.patch"
end
test_apply_commit_url() click to toggle source
# File test/hub_test.rb, line 242
def test_apply_commit_url
  stub_request(:get, "https://api.github.com/repos/davidbalbert/hub/commits/fdb9921").
    to_return(:status => 200)

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

    assert_commands %r{git apply /tmp/fdb9921\.patch[\w-]+ -p2},
                    "apply #{url} -p2"
  end
end
test_apply_gist() click to toggle source
# File test/hub_test.rb, line 254
def test_apply_gist
  stub_request(:get, "https://api.github.com/gists/8da7fb575debd88c54cf").
    with(:headers => {'Authorization'=>'token OTOKEN'}).
    to_return(:body => Hub::JSON.generate(:files => {
      'file.diff' => {
        :raw_url => "https://gist.github.com/raw/8da7fb575debd88c54cf/SHA/file.diff"
      }
    }))

  stub_request(:get, "https://gist.github.com/raw/8da7fb575debd88c54cf/SHA/file.diff").
    to_return(:status => 200)

  with_tmpdir('/tmp/') do
    url = 'https://gist.github.com/mislav/8da7fb575debd88c54cf'

    assert_commands %r{git apply /tmp/gist-8da7fb575debd88c54cf\.txt[\w-]+ -p2},
                    "apply #{url} -p2"
  end
end
test_apply_pull_request() click to toggle source
# File test/hub_test.rb, line 229
def test_apply_pull_request
  stub_request(:get, "https://api.github.com/repos/defunkt/hub/pulls/55").
    to_return(:status => 200)

  with_tmpdir('/tmp/') do
    assert_commands %r{git apply /tmp/55\.patch[\w-]+ -p2},
                    "apply https://github.com/defunkt/hub/pull/55 -p2"

    cmd = Hub("apply https://github.com/defunkt/hub/pull/55/files").command
    assert_includes '/tmp/55.patch', cmd
  end
end
test_apply_untouched() click to toggle source
# File test/hub_test.rb, line 225
def test_apply_untouched
  assert_forwarded "apply some.patch"
end
test_cherry_pick() click to toggle source
# File test/hub_test.rb, line 119
def test_cherry_pick
  assert_forwarded "cherry-pick a319d88"
end
test_cherry_pick_github_notation_too_short() click to toggle source
# File test/hub_test.rb, line 154
def test_cherry_pick_github_notation_too_short
  assert_forwarded "cherry-pick mislav@a319"
end
test_cherry_pick_github_notation_with_remote_add() click to toggle source
# File test/hub_test.rb, line 158
def test_cherry_pick_github_notation_with_remote_add
  assert_commands "git remote add -f xoebus git://github.com/xoebus/hub.git",
                  "git cherry-pick a319d88",
                  "cherry-pick xoebus@a319d88"
end
test_cherry_pick_github_user_notation() click to toggle source
# File test/hub_test.rb, line 145
def test_cherry_pick_github_user_notation
  assert_commands "git fetch mislav", "git cherry-pick 368af20", "cherry-pick mislav@368af20"
end
test_cherry_pick_github_user_repo_notation() click to toggle source
# File test/hub_test.rb, line 149
def test_cherry_pick_github_user_repo_notation
  # not supported
  assert_forwarded "cherry-pick mislav/hubbub@a319d88"
end
test_cherry_pick_origin_url() click to toggle source
# File test/hub_test.rb, line 140
def test_cherry_pick_origin_url
  url = 'https://github.com/defunkt/hub/commit/a319d88'
  assert_commands "git fetch origin", "git cherry-pick a319d88", "cherry-pick #{url}"
end
test_cherry_pick_url() click to toggle source
# File test/hub_test.rb, line 123
def test_cherry_pick_url
  url = 'http://github.com/mislav/hub/commit/a319d88'
  assert_commands "git fetch mislav", "git cherry-pick a319d88", "cherry-pick #{url}"
end
test_cherry_pick_url_with_fragment() click to toggle source
# File test/hub_test.rb, line 128
def test_cherry_pick_url_with_fragment
  url = 'http://github.com/mislav/hub/commit/abcdef0123456789#comments'
  assert_commands "git fetch mislav", "git cherry-pick abcdef0123456789", "cherry-pick #{url}"
end
test_cherry_pick_url_with_remote_add() click to toggle source
# File test/hub_test.rb, line 133
def test_cherry_pick_url_with_remote_add
  url = 'https://github.com/xoebus/hub/commit/a319d88'
  assert_commands "git remote add -f xoebus git://github.com/xoebus/hub.git",
                  "git cherry-pick a319d88",
                  "cherry-pick #{url}"
end
test_context_method_doesnt_hijack_git_command() click to toggle source
# File test/hub_test.rb, line 479
def test_context_method_doesnt_hijack_git_command
  assert_forwarded 'remotes'
end
test_custom_browser() click to toggle source
# File test/hub_test.rb, line 445
def test_custom_browser
  with_browser_env("custom") do
    assert_browser("custom")
  end
end
test_cygwin_browser() click to toggle source
# File test/hub_test.rb, line 460
def test_cygwin_browser
  stub_available_commands "open", "cygstart"
  with_browser_env(nil) do
    with_host_os("i686-linux") do
      assert_browser("cygstart")
    end
  end
end
test_exec_path() click to toggle source
# File test/hub_test.rb, line 363
def test_exec_path
  out = hub('--exec-path')
  assert_equal "/usr/lib/git-core\n", out
end
test_exec_path_arg() click to toggle source
# File test/hub_test.rb, line 368
def test_exec_path_arg
  out = hub('--exec-path=/home/wombat/share/my-l33t-git-core')
  assert_equal improved_help_text, out
end
test_global_flags_preserved() click to toggle source
# File test/hub_test.rb, line 488
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_reader.executable
end
test_help() click to toggle source
# File test/hub_test.rb, line 378
def test_help
  assert_equal improved_help_text, hub("help")
end
test_help_by_default() click to toggle source
# File test/hub_test.rb, line 382
def test_help_by_default
  assert_equal improved_help_text, hub("")
end
test_help_custom_command() click to toggle source
# File test/hub_test.rb, line 402
def test_help_custom_command
  help_manpage = strip_man_escapes hub("help fork")
  assert_includes "git fork [--no-remote]", help_manpage
end
test_help_flag_on_command() click to toggle source
# File test/hub_test.rb, line 396
def test_help_flag_on_command
  help_manpage = strip_man_escapes hub("browse --help")
  assert_includes "git + hub = github", help_manpage
  assert_includes "git browse", help_manpage
end
test_help_hub() click to toggle source
# File test/hub_test.rb, line 390
def test_help_hub
  help_manpage = strip_man_escapes hub("help hub")
  assert_includes "git + hub = github", help_manpage
  assert_includes "Hub will prompt for GitHub username & password", help_manpage.gsub(/ {2,}/, ' ')
end
test_help_hub_no_groff() click to toggle source
# File test/hub_test.rb, line 417
def test_help_hub_no_groff
  stub_available_commands()
  assert_equal "** Can't find groff(1)\n", hub("help hub")
end
test_help_short_flag_on_command() click to toggle source
# File test/hub_test.rb, line 407
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 [-o|--browse] [-f] [-m MESSAGE|-F FILE|-i ISSUE|ISSUE-URL] [-b BASE] [-h HEAD]\n"
  assert_equal expected, usage_help
end
test_help_with_pager() click to toggle source
# File test/hub_test.rb, line 386
def test_help_with_pager
  assert_equal improved_help_text, hub("-p")
end
test_html_path() click to toggle source
# File test/hub_test.rb, line 373
def test_html_path
  out = hub('--html-path')
  assert_equal "/usr/share/doc/git-doc\n", out
end
test_hub_browse_no_repo() click to toggle source
# File test/hub_test.rb, line 426
def test_hub_browse_no_repo
  stub_repo_url(nil)
  assert_equal "Usage: hub browse [<USER>/]<REPOSITORY>\n", hub("browse")
end
test_hub_browse_ssh_alias() click to toggle source
# File test/hub_test.rb, line 431
def test_hub_browse_ssh_alias
  with_ssh_config "Host gh\n User git\n HostName github.com" do
    stub_repo_url "gh:singingwolfboy/sekrit.git"
    assert_command "browse", "open https://github.com/singingwolfboy/sekrit"
  end
end
test_hub_browse_ssh_github_alias() click to toggle source
# File test/hub_test.rb, line 438
def test_hub_browse_ssh_github_alias
  with_ssh_config "Host github.com\n HostName ssh.github.com" do
    stub_repo_url "git@github.com:suan/git-sanity.git"
    assert_command "browse", "open https://github.com/suan/git-sanity"
  end
end
test_hub_standalone() click to toggle source
# File test/hub_test.rb, line 422
def test_hub_standalone
  assert_includes 'This file is generated code', hub("hub standalone")
end
test_init() click to toggle source
# File test/hub_test.rb, line 274
def test_init
  stub_no_remotes
  stub_no_git_repo
  assert_commands "git init", "git remote add origin git@github.com:tpw/hub.git", "init -g"
end
test_init_enterprise() click to toggle source
# File test/hub_test.rb, line 280
def test_init_enterprise
  stub_no_remotes
  stub_no_git_repo
  edit_hub_config do |data|
    data['git.my.org'] = [{'user'=>'myfiname'}]
  end

  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
test_linux_browser() click to toggle source
# File test/hub_test.rb, line 451
def test_linux_browser
  stub_available_commands "open", "xdg-open", "cygstart"
  with_browser_env(nil) do
    with_host_os("i686-linux") do
      assert_browser("xdg-open")
    end
  end
end
test_no_browser() click to toggle source
# File test/hub_test.rb, line 469
def test_no_browser
  stub_available_commands()
  expected = "Please set $BROWSER to a web launcher to use this command.\n"
  with_browser_env(nil) do
    with_host_os("i686-linux") do
      assert_equal expected, hub("browse")
    end
  end
end
test_not_choking_on_ruby_methods() click to toggle source
# File test/hub_test.rb, line 483
def test_not_choking_on_ruby_methods
  assert_forwarded 'id'
  assert_forwarded 'name'
end
test_pullrequest_alias() click to toggle source
# File test/hub_test.rb, line 352
def test_pullrequest_alias
  out = hub('e-note')
  assert_equal hub('pull-request'), out
end
test_pullrequest_enterprise_no_tracking() click to toggle source
# File test/hub_test.rb, line 333
def test_pullrequest_enterprise_no_tracking
  stub_hub_host('git.my.org')
  stub_repo_url('git@git.my.org:defunkt/hub.git')
  stub_branch('refs/heads/feature')
  stub_remote_branch('origin/feature')
  stub_tracking_nothing('feature')
  stub_command_output "rev-list --cherry-pick --right-only --no-merges origin/feature...", nil
  edit_hub_config do |data|
    data['git.my.org'] = [{'user'=>'myfiname', 'oauth_token' => 'FITOKEN'}]
  end

  stub_request(:post, "https://git.my.org/api/v3/repos/defunkt/hub/pulls").
    with(:body => {'base' => "master", 'head' => "defunkt:feature", 'title' => "hereyougo" }).
    to_return(:body => mock_pullreq_response(1, 'api/v3/defunkt/hub', 'git.my.org'))

  expected = "https://git.my.org/api/v3/defunkt/hub/pull/1\n"
  assert_output expected, "pull-request -m hereyougo -f"
end
test_pullrequest_from_branch_tracking_local() click to toggle source
# File test/hub_test.rb, line 320
def test_pullrequest_from_branch_tracking_local
  stub_config_value 'push.default', 'upstream'
  stub_branch('refs/heads/feature')
  stub_tracking('feature', 'refs/heads/master')

  stub_request(:post, "https://api.github.com/repos/defunkt/hub/pulls").
    with(:body => {'base' => "master", 'head' => "defunkt:feature", 'title' => "hereyougo" }).
    to_return(:body => mock_pullreq_response(1))

  expected = "https://github.com/defunkt/hub/pull/1\n"
  assert_output expected, "pull-request -m hereyougo -f"
end
test_push_current_branch() click to toggle source
# File test/hub_test.rb, line 301
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
test_push_more() click to toggle source
# File test/hub_test.rb, line 307
def test_push_more
  assert_commands "git push origin cool-feature",
                  "git push staging cool-feature",
                  "git push qa cool-feature",
                  "push origin,staging,qa cool-feature"
end
test_push_multiple_refs() click to toggle source
# File test/hub_test.rb, line 314
def test_push_multiple_refs
  assert_commands "git push origin master new-feature",
                  "git push staging master new-feature",
                  "push origin,staging master new-feature"
end
test_push_two() click to toggle source
# File test/hub_test.rb, line 296
def test_push_two
  assert_commands "git push origin cool-feature", "git push staging cool-feature",
                  "push origin,staging cool-feature"
end
test_push_untouched() click to toggle source
# File test/hub_test.rb, line 292
def test_push_untouched
  assert_forwarded "push"
end
test_version() click to toggle source
# File test/hub_test.rb, line 357
def test_version
  out = hub('--version')
  assert_includes "git version 1.7.0.4", out
  assert_includes "hub version #{Hub::Version}", out
end