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

class HubTest < Test::Unit::TestCase
6 7
  include WebMock
  
8
  def setup
9
    Hub::Commands::REPO.replace("hub")
10
    Hub::Commands::USER.replace("tpw")
11
    Hub::Commands::TOKEN.replace("abc123")
12
    Hub::Commands::OWNER.replace("defunkt")
13 14
  end

C
Chris Wanstrath 已提交
15
  def test_private_clone
C
Chris Wanstrath 已提交
16 17 18
    input   = "clone -p rtomayko/ron"
    command = "git clone git@github.com:rtomayko/ron.git"
    assert_command input, command
C
Chris Wanstrath 已提交
19 20 21
  end

  def test_public_clone
C
Chris Wanstrath 已提交
22 23 24
    input   = "clone rtomayko/ron"
    command = "git clone git://github.com/rtomayko/ron.git"
    assert_command input, command
C
Chris Wanstrath 已提交
25 26
  end

27 28 29 30 31 32 33 34 35 36 37 38
  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

39 40 41 42 43 44
  def test_clone_with_arguments_and_path
    input   = "clone --bare -o master -- resque"
    command = "git clone --bare -o master -- git://github.com/tpw/resque.git"
    assert_command input, command
  end

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  def test_your_private_clone_fails_without_config
    out = hub("clone -p mustache") do
      Hub::Commands::USER.replace("")
    end

    assert_equal "** No GitHub user set. See http://github.com/guides/local-github-config\n", out
  end

  def test_your_public_clone_fails_without_config
    out = hub("clone mustache") do
      Hub::Commands::USER.replace("")
    end

    assert_equal "** No GitHub user set. See http://github.com/guides/local-github-config\n", out
  end

61 62 63 64 65 66 67 68 69 70 71
  def test_private_clone_left_alone
    input   = "clone git@github.com:rtomayko/ron.git"
    command = "git clone git@github.com:rtomayko/ron.git"
    assert_command input, command
  end

  def test_public_clone_left_alone
    input   = "clone git://github.com/rtomayko/ron.git"
    command = "git clone git://github.com/rtomayko/ron.git"
    assert_command input, command
  end
C
Chris Wanstrath 已提交
72 73 74 75 76 77

  def test_normal_public_clone_with_path
    input   = "clone git://github.com/rtomayko/ron.git ron-dev"
    command = "git clone git://github.com/rtomayko/ron.git ron-dev"
    assert_command input, command
  end
78 79 80 81 82 83

  def test_normal_clone_from_path
    input   = "clone ./test"
    command = "git clone ./test"
    assert_command input, command
  end
84

85 86 87 88 89 90 91 92 93 94 95 96
  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

97 98 99 100 101 102 103
  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

  def test_private_remote_origin_as_normal
C
Chris Wanstrath 已提交
104 105 106 107 108
    input   = "remote add origin git@github.com:defunkt/resque.git"
    command = "git remote add origin git@github.com:defunkt/resque.git"
    assert_command input, command
  end

S
Stephen Celis 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  def test_public_submodule
    input   = "submodule add wycats/bundler vendor/bundler"
    command = "git submodule add git://github.com/wycats.bundler.git"
  end

  def test_private_submodule
    input   = "submodule add -p grit vendor/grit"
    command = "git submodule add git@github.com:tpw/grit.git"
  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"
  end

C
Chris Wanstrath 已提交
124
  def test_private_remote
125
    input   = "remote add -p rtomayko"
C
Chris Wanstrath 已提交
126 127
    command = "git remote add rtomayko git@github.com:rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
128 129 130
  end

  def test_public_remote
131
    input   = "remote add rtomayko"
C
Chris Wanstrath 已提交
132 133
    command = "git remote add rtomayko git://github.com/rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
134 135
  end

C
Chris Wanstrath 已提交
136 137 138 139 140 141
  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

142 143 144 145 146 147
  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

148 149 150 151 152 153
  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 已提交
154
  def test_private_remote_with_repo
155 156
    input   = "remote add -p jashkenas/coffee-script"
    command = "git remote add jashkenas git@github.com:jashkenas/coffee-script.git"
C
Chris Wanstrath 已提交
157 158 159 160
    assert_command input, command
  end

  def test_public_remote_with_repo
161 162
    input   = "remote add jashkenas/coffee-script"
    command = "git remote add jashkenas git://github.com/jashkenas/coffee-script.git"
163 164 165 166
    assert_command input, command
  end

  def test_public_remote_f_with_repo
167 168
    input   = "remote add -f jashkenas/coffee-script"
    command = "git remote add -f jashkenas git://github.com/jashkenas/coffee-script.git"
C
Chris Wanstrath 已提交
169 170 171
    assert_command input, command
  end

172
  def test_named_private_remote_with_repo
173 174
    input   = "remote add -p origin jashkenas/coffee-script"
    command = "git remote add origin git@github.com:jashkenas/coffee-script.git"
175 176 177
    assert_command input, command
  end

C
Chris Wanstrath 已提交
178
  def test_init
C
Chris Wanstrath 已提交
179 180
    h = Hub("init -g")
    assert_equal "git init", h.command
181 182 183 184 185 186 187 188 189
    assert_equal "git remote add origin git@github.com:tpw/hub.git", h.after
  end

  def test_init_no_login
    out = hub("init -g") do
      Hub::Commands::USER.replace("")
    end

    assert_equal "** No GitHub user set. See http://github.com/guides/local-github-config\n", out
C
Chris Wanstrath 已提交
190 191
  end

192 193 194 195 196 197
  def test_push_two
    h = Hub("push origin,staging cool-feature")
    assert_equal "git push origin cool-feature", h.command
    assert_equal "git push staging cool-feature", h.after
  end

C
Chris Wanstrath 已提交
198 199 200 201 202
  def test_push_more
    h = Hub("push origin,staging,qa cool-feature")
    assert_equal "git push origin cool-feature", h.command
    assert_equal "git push staging cool-feature; git push qa cool-feature", h.after
  end
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
  
  def test_fork
    stub_request(:get, "github.com/api/v2/yaml/repos/show/tpw/hub").to_return(:status => 404)
    stub_request(:post, "github.com/api/v2/yaml/repos/fork/defunkt/hub").with { |req|
      params = Hash[*req.body.split(/[&=]/)]
      params == { 'login'=>'tpw', 'token'=>'abc123' }
    }
    
    expected = "remote add tpw git@github.com:tpw/hub.git\n"
    expected << "new remote: tpw\n"
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end

  def test_fork_no_remote
    stub_request(:get, "github.com/api/v2/yaml/repos/show/tpw/hub").to_return(:status => 404)
    stub_request(:post, "github.com/api/v2/yaml/repos/fork/defunkt/hub")
    
    assert_equal "", hub("fork --no-remote") { ENV['GIT'] = 'echo' }
  end

  def test_fork_already_exists
    stub_request(:get, "github.com/api/v2/yaml/repos/show/tpw/hub").to_return(:status => 200)
    
    expected = "tpw/hub already exists on GitHub\n"
    expected << "remote add tpw git@github.com:tpw/hub.git\n"
    expected << "new remote: tpw\n"
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end
C
Chris Wanstrath 已提交
231

C
Chris Wanstrath 已提交
232
  def test_version
233
    out = hub('--version')
S
Stephen Celis 已提交
234
    assert_includes "git version", out
C
Chris Wanstrath 已提交
235
    assert_includes "hub version #{Hub::Version}", out
C
Chris Wanstrath 已提交
236
  end
C
Chris Wanstrath 已提交
237 238 239 240 241 242 243 244

  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 已提交
245

S
Stephen Celis 已提交
246 247 248 249
  def test_help_with_pager
    assert_equal Hub::Commands.improved_help_text, hub("-p")
  end

C
Chris Wanstrath 已提交
250 251 252 253 254
  def test_help_hub
    help_manpage = hub("help hub")
    assert_includes "git + hub = github", help_manpage
    assert_includes "Chris Wanstrath :: chris@ozmm.org", help_manpage
    assert_includes <<-config, help_manpage
C
Chris Wanstrath 已提交
255
Use git-config(1) to display the currently configured GitHub username:
C
Chris Wanstrath 已提交
256 257 258 259 260 261
config
  end

  def test_help_hub_no_groff
    help_manpage = hub("help hub") do
      Hub::Commands.class_eval do
262
        remove_method :groff?
C
Chris Wanstrath 已提交
263 264 265 266 267
        def groff?; false end
      end
    end
    assert_equal "** Can't find groff(1)\n", help_manpage
  end
268

269 270 271 272 273
  def test_hub_standalone
    help_standalone = hub("hub standalone")
    assert_equal Hub::Standalone.build, help_standalone
  end

274
  def test_hub_open
275
    assert_command "browse mojombo/bert", "open http://github.com/mojombo/bert"
276 277 278
  end

  def test_hub_open_private
279
    assert_command "browse -p bmizerany/sinatra", "open https://github.com/bmizerany/sinatra"
280 281 282
  end

  def test_hub_open_self
283
    assert_command "browse resque", "open http://github.com/tpw/resque"
284 285 286
  end

  def test_hub_open_self_private
287
    assert_command "browse -p github", "open https://github.com/tpw/github"
288
  end
289 290 291 292 293 294 295 296 297 298 299 300 301 302

  def test_hub_open_current
    assert_command "browse", "open http://github.com/defunkt/hub"
  end

  def test_hub_open_current_private
    assert_command "browse -p", "open https://github.com/defunkt/hub"
  end

  def test_hub_open_no_repo
    Hub::Commands::OWNER.replace("")
    input = "browse"
    assert_equal "Usage: hub browse [<USER>/]<REPOSITORY>\n", hub(input)
  end
C
Chris Wanstrath 已提交
303
end