hub_test.rb 2.3 KB
Newer Older
C
Chris Wanstrath 已提交
1
require 'test/unit'
C
Chris Wanstrath 已提交
2 3
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
require 'hub'
C
Chris Wanstrath 已提交
4 5

class HubTest < Test::Unit::TestCase
C
Chris Wanstrath 已提交
6 7
  # Shortcut for creating a `Hub` instance. Pass it what you would
  # normally pass `hub` on the command line, e.g.
C
Chris Wanstrath 已提交
8
  #
C
Chris Wanstrath 已提交
9 10
  # shell: hub clone rtomayko/tilt
  #  test: Hub("clone rtomayko/tilt")
C
Chris Wanstrath 已提交
11
  def Hub(args)
C
Chris Wanstrath 已提交
12
    Hub::Runner.new(*args.split(' '))
C
Chris Wanstrath 已提交
13 14
  end

C
Chris Wanstrath 已提交
15 16 17 18 19 20
  # Shortcut for running the `hub` command in a subprocess. Returns
  # STDOUT as a string. Pass it what you would normall pass `hub` on
  # the command line, e.g.
  #
  # shell: hub clone rtomayko/tilt
  #  test: hub("clone rtomayko/tilt")
C
Chris Wanstrath 已提交
21
  def hub(args)
C
Chris Wanstrath 已提交
22 23 24 25 26 27 28 29 30
    parent_read, child_write = IO.pipe

    fork do
      $stdout.reopen(child_write)
      Hub(args).execute
    end

    child_write.close
    parent_read.read
C
Chris Wanstrath 已提交
31 32
  end

C
Chris Wanstrath 已提交
33 34 35 36 37 38 39 40 41 42
  # Asserts that `hub` will run a specific git command based on
  # certain input.
  #
  # e.g.
  #  assert_command "clone git/hub", "git clone git://github.com/git/hub.git"
  #
  # Here we are saying that this:
  #   $ hub clone git/hub
  # Should in turn execute this:
  #   $ git clone git://github.com/git/hub.git
C
Chris Wanstrath 已提交
43 44 45 46 47 48 49 50
  def assert_command(input, expected)
    assert_equal expected, Hub(input).command
  end

  #
  # Assertions
  #

C
Chris Wanstrath 已提交
51
  def test_private_clone
C
Chris Wanstrath 已提交
52 53 54
    input   = "clone -p rtomayko/ron"
    command = "git clone git@github.com:rtomayko/ron.git"
    assert_command input, command
C
Chris Wanstrath 已提交
55 56 57
  end

  def test_public_clone
C
Chris Wanstrath 已提交
58 59 60
    input   = "clone rtomayko/ron"
    command = "git clone git://github.com/rtomayko/ron.git"
    assert_command input, command
C
Chris Wanstrath 已提交
61 62 63
  end

  def test_private_remote
64
    input   = "remote add -p rtomayko"
C
Chris Wanstrath 已提交
65 66
    command = "git remote add rtomayko git@github.com:rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
67 68 69
  end

  def test_public_remote
70
    input   = "remote add rtomayko"
C
Chris Wanstrath 已提交
71 72
    command = "git remote add rtomayko git://github.com/rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
73 74 75
  end

  def test_init
C
Chris Wanstrath 已提交
76 77 78 79 80 81 82 83
    h = Hub("init -g")
    assert_equal "git init", h.command
    assert_equal "git remote add origin git@github.com:defunkt/hub.git", h.after
  end

  def test_version
    h = hub("--version")
    assert_equal "git version 1.6.4.2\nhub version 0.1.0\n", h
C
Chris Wanstrath 已提交
84
  end
C
Chris Wanstrath 已提交
85 86 87 88 89 90 91 92

  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 已提交
93
end