hub_test.rb 10.5 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
  include WebMock
C
Chris Wanstrath 已提交
7

8
  def setup
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    @git = Hub::Context::GIT_CONFIG.replace(Hash.new { |h, k|
      raise ArgumentError, "`git #{k}` not stubbed"
    }).update(
      'config github.user' => 'tpw',
      'config github.token' => 'abc123',
      'config remote.origin.url' => 'git://github.com/defunkt/hub.git',
      'config --bool hub.http-clone' => 'false'
    )
  end

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

  def stub_repo_url(value)
    @git['config remote.origin.url'] = value
    Hub::Context::REMOTES.clear
26 27
  end

C
Chris Wanstrath 已提交
28
  def test_private_clone
29 30
    input   = "clone -p rtomayko/ronn"
    command = "git clone git@github.com:rtomayko/ronn.git"
C
Chris Wanstrath 已提交
31
    assert_command input, command
C
Chris Wanstrath 已提交
32 33 34
  end

  def test_public_clone
35 36
    input   = "clone rtomayko/ronn"
    command = "git clone git://github.com/rtomayko/ronn.git"
C
Chris Wanstrath 已提交
37
    assert_command input, command
C
Chris Wanstrath 已提交
38 39
  end

40 41 42 43 44 45 46 47 48 49 50 51
  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

52 53 54 55 56 57
  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

58 59
  def test_your_private_clone_fails_without_config
    out = hub("clone -p mustache") do
60
      stub_github_user(nil)
61 62 63 64 65 66 67
    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
68
      stub_github_user(nil)
69 70 71 72 73
    end

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

74
  def test_private_clone_left_alone
75 76
    input   = "clone git@github.com:rtomayko/ronn.git"
    command = "git clone git@github.com:rtomayko/ronn.git"
77 78 79 80
    assert_command input, command
  end

  def test_public_clone_left_alone
81 82
    input   = "clone git://github.com/rtomayko/ronn.git"
    command = "git clone git://github.com/rtomayko/ronn.git"
83 84
    assert_command input, command
  end
C
Chris Wanstrath 已提交
85 86

  def test_normal_public_clone_with_path
87 88
    input   = "clone git://github.com/rtomayko/ronn.git ronn-dev"
    command = "git clone git://github.com/rtomayko/ronn.git ronn-dev"
C
Chris Wanstrath 已提交
89 90
    assert_command input, command
  end
91 92 93 94 95 96

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

98 99 100 101 102 103 104 105 106 107 108 109
  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

110 111 112 113 114 115 116
  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 已提交
117 118 119 120 121
    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 已提交
122 123
  def test_public_submodule
    input   = "submodule add wycats/bundler vendor/bundler"
J
Justin Ridgewell 已提交
124 125
    command = "git submodule add git://github.com/wycats/bundler.git vendor/bundler"
    assert_command input, command
S
Stephen Celis 已提交
126 127 128 129
  end

  def test_private_submodule
    input   = "submodule add -p grit vendor/grit"
J
Justin Ridgewell 已提交
130 131 132 133 134 135 136 137
    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 已提交
138 139 140 141 142
  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 已提交
143
    assert_command input, command
S
Stephen Celis 已提交
144 145
  end

C
Chris Wanstrath 已提交
146
  def test_private_remote
147
    input   = "remote add -p rtomayko"
C
Chris Wanstrath 已提交
148 149
    command = "git remote add rtomayko git@github.com:rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
150 151 152
  end

  def test_public_remote
153
    input   = "remote add rtomayko"
C
Chris Wanstrath 已提交
154 155
    command = "git remote add rtomayko git://github.com/rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
156 157
  end

C
Chris Wanstrath 已提交
158 159 160 161 162 163
  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

164 165 166 167 168 169
  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

170 171 172 173 174 175
  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 已提交
176
  def test_private_remote_with_repo
177 178
    input   = "remote add -p jashkenas/coffee-script"
    command = "git remote add jashkenas git@github.com:jashkenas/coffee-script.git"
C
Chris Wanstrath 已提交
179 180 181 182
    assert_command input, command
  end

  def test_public_remote_with_repo
183 184
    input   = "remote add jashkenas/coffee-script"
    command = "git remote add jashkenas git://github.com/jashkenas/coffee-script.git"
185 186 187 188
    assert_command input, command
  end

  def test_public_remote_f_with_repo
189 190
    input   = "remote add -f jashkenas/coffee-script"
    command = "git remote add -f jashkenas git://github.com/jashkenas/coffee-script.git"
C
Chris Wanstrath 已提交
191 192 193
    assert_command input, command
  end

194
  def test_named_private_remote_with_repo
195 196
    input   = "remote add -p origin jashkenas/coffee-script"
    command = "git remote add origin git@github.com:jashkenas/coffee-script.git"
197 198 199
    assert_command input, command
  end

C
Chris Wanstrath 已提交
200
  def test_init
C
Chris Wanstrath 已提交
201 202
    h = Hub("init -g")
    assert_equal "git init", h.command
203 204 205 206 207
    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
208
      stub_github_user(nil)
209 210 211
    end

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

214 215 216 217 218 219
  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 已提交
220 221 222 223 224
  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
C
Chris Wanstrath 已提交
225

226
  def test_fork
C
Chris Wanstrath 已提交
227 228
    stub_request(:get, "github.com/api/v2/yaml/repos/show/tpw/hub").
      to_return(:status => 404)
229 230 231 232
    stub_request(:post, "github.com/api/v2/yaml/repos/fork/defunkt/hub").with { |req|
      params = Hash[*req.body.split(/[&=]/)]
      params == { 'login'=>'tpw', 'token'=>'abc123' }
    }
C
Chris Wanstrath 已提交
233 234

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

  def test_fork_no_remote
C
Chris Wanstrath 已提交
240 241
    stub_request(:get, "github.com/api/v2/yaml/repos/show/tpw/hub").
      to_return(:status => 404)
242
    stub_request(:post, "github.com/api/v2/yaml/repos/fork/defunkt/hub")
C
Chris Wanstrath 已提交
243

244 245 246 247
    assert_equal "", hub("fork --no-remote") { ENV['GIT'] = 'echo' }
  end

  def test_fork_already_exists
C
Chris Wanstrath 已提交
248 249 250
    stub_request(:get, "github.com/api/v2/yaml/repos/show/tpw/hub").
      to_return(:status => 200)

251
    expected = "tpw/hub already exists on GitHub\n"
C
Chris Wanstrath 已提交
252
    expected << "remote add -f tpw git@github.com:tpw/hub.git\n"
253 254 255
    expected << "new remote: tpw\n"
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end
C
Chris Wanstrath 已提交
256

C
Chris Wanstrath 已提交
257
  def test_version
258
    out = hub('--version')
259
    assert_includes "git version 1.7.0.4", out
C
Chris Wanstrath 已提交
260
    assert_includes "hub version #{Hub::Version}", out
C
Chris Wanstrath 已提交
261
  end
C
Chris Wanstrath 已提交
262 263 264 265 266 267 268 269

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

S
Stephen Celis 已提交
271 272 273 274
  def test_help_with_pager
    assert_equal Hub::Commands.improved_help_text, hub("-p")
  end

C
Chris Wanstrath 已提交
275 276 277 278 279
  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 已提交
280
Use git-config(1) to display the currently configured GitHub username:
C
Chris Wanstrath 已提交
281 282 283 284 285 286
config
  end

  def test_help_hub_no_groff
    help_manpage = hub("help hub") do
      Hub::Commands.class_eval do
287
        remove_method :groff?
C
Chris Wanstrath 已提交
288 289 290 291 292
        def groff?; false end
      end
    end
    assert_equal "** Can't find groff(1)\n", help_manpage
  end
293

294 295 296 297 298
  def test_hub_standalone
    help_standalone = hub("hub standalone")
    assert_equal Hub::Standalone.build, help_standalone
  end

C
Chris Wanstrath 已提交
299 300 301
  def test_hub_compare
    assert_command "compare refactor",
      "open http://github.com/defunkt/hub/compare/refactor"
302
  end
C
Chris Wanstrath 已提交
303

304
  def test_hub_compare_range
C
Chris Wanstrath 已提交
305 306
    assert_command "compare 1.0...fix",
      "open http://github.com/defunkt/hub/compare/1.0...fix"
307
  end
C
Chris Wanstrath 已提交
308

309
  def test_hub_compare_fork
C
Chris Wanstrath 已提交
310 311 312 313
    assert_command "compare myfork feature",
      "open http://github.com/myfork/hub/compare/feature"
  end

314 315 316 317 318 319 320 321 322 323 324
  def test_hub_compare_private
    assert_command "compare -p myfork topsecret",
      "open https://github.com/myfork/hub/compare/topsecret"
  end

  def test_hub_compare_url
    assert_command "compare -u 1.0...1.1",
      "echo http://github.com/defunkt/hub/compare/1.0...1.1"
  end

  def test_hub_browse
325
    assert_command "browse mojombo/bert", "open http://github.com/mojombo/bert"
326 327
  end

328 329 330 331 332
  def test_hub_browse_url
    assert_command "browse -u mojombo/bert", "echo http://github.com/mojombo/bert"
  end

  def test_hub_browse_private
C
Chris Wanstrath 已提交
333 334
    assert_command "browse -p bmizerany/sinatra",
      "open https://github.com/bmizerany/sinatra"
335 336
  end

337
  def test_hub_browse_self
338
    assert_command "browse resque", "open http://github.com/tpw/resque"
339 340
  end

341
  def test_hub_browse_self_private
342
    assert_command "browse -p github", "open https://github.com/tpw/github"
343
  end
344

345
  def test_hub_browse_current
346 347 348
    assert_command "browse", "open http://github.com/defunkt/hub"
  end

349
  def test_hub_browse_current_private
350 351 352
    assert_command "browse -p", "open https://github.com/defunkt/hub"
  end

353 354 355
  def test_hub_browse_no_repo
    stub_repo_url(nil)
    assert_equal "Usage: hub browse [<USER>/]<REPOSITORY>\n", hub("browse")
356
  end
C
Chris Wanstrath 已提交
357
end