hub_test.rb 12.0 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 9 10 11 12 13 14 15 16
  COMMANDS = []

  Hub::Commands.class_eval do
    remove_method :command?
    define_method :command? do |name|
      COMMANDS.include?(name)
    end
  end

17
  def setup
18 19
    COMMANDS.replace %w[open groff]

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    @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
37 38
  end

C
Chris Wanstrath 已提交
39
  def test_private_clone
40 41
    input   = "clone -p rtomayko/ronn"
    command = "git clone git@github.com:rtomayko/ronn.git"
C
Chris Wanstrath 已提交
42
    assert_command input, command
C
Chris Wanstrath 已提交
43 44 45
  end

  def test_public_clone
46 47
    input   = "clone rtomayko/ronn"
    command = "git clone git://github.com/rtomayko/ronn.git"
C
Chris Wanstrath 已提交
48
    assert_command input, command
C
Chris Wanstrath 已提交
49 50
  end

51 52 53 54 55 56 57 58 59 60 61 62
  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

63 64 65 66 67 68
  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

69 70
  def test_your_private_clone_fails_without_config
    out = hub("clone -p mustache") do
71
      stub_github_user(nil)
72 73 74 75 76 77 78
    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
79
      stub_github_user(nil)
80 81 82 83 84
    end

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

85
  def test_private_clone_left_alone
86 87
    input   = "clone git@github.com:rtomayko/ronn.git"
    command = "git clone git@github.com:rtomayko/ronn.git"
88 89 90 91
    assert_command input, command
  end

  def test_public_clone_left_alone
92 93
    input   = "clone git://github.com/rtomayko/ronn.git"
    command = "git clone git://github.com/rtomayko/ronn.git"
94 95
    assert_command input, command
  end
C
Chris Wanstrath 已提交
96 97

  def test_normal_public_clone_with_path
98 99
    input   = "clone git://github.com/rtomayko/ronn.git ronn-dev"
    command = "git clone git://github.com/rtomayko/ronn.git ronn-dev"
C
Chris Wanstrath 已提交
100 101
    assert_command input, command
  end
102 103 104 105 106 107

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

109 110 111 112 113 114 115 116 117 118 119 120
  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

121 122 123 124 125 126 127
  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 已提交
128 129 130 131 132
    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 已提交
133 134
  def test_public_submodule
    input   = "submodule add wycats/bundler vendor/bundler"
J
Justin Ridgewell 已提交
135 136
    command = "git submodule add git://github.com/wycats/bundler.git vendor/bundler"
    assert_command input, command
S
Stephen Celis 已提交
137 138 139 140
  end

  def test_private_submodule
    input   = "submodule add -p grit vendor/grit"
J
Justin Ridgewell 已提交
141 142 143 144 145 146 147 148
    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 已提交
149 150 151 152 153
  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 已提交
154
    assert_command input, command
S
Stephen Celis 已提交
155 156
  end

C
Chris Wanstrath 已提交
157
  def test_private_remote
158
    input   = "remote add -p rtomayko"
C
Chris Wanstrath 已提交
159 160
    command = "git remote add rtomayko git@github.com:rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
161 162 163
  end

  def test_public_remote
164
    input   = "remote add rtomayko"
C
Chris Wanstrath 已提交
165 166
    command = "git remote add rtomayko git://github.com/rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
167 168
  end

C
Chris Wanstrath 已提交
169 170 171 172 173 174
  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

175 176 177 178 179 180
  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

181 182 183 184 185 186
  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 已提交
187
  def test_private_remote_with_repo
188 189
    input   = "remote add -p jashkenas/coffee-script"
    command = "git remote add jashkenas git@github.com:jashkenas/coffee-script.git"
C
Chris Wanstrath 已提交
190 191 192 193
    assert_command input, command
  end

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

  def test_public_remote_f_with_repo
200 201
    input   = "remote add -f jashkenas/coffee-script"
    command = "git remote add -f jashkenas git://github.com/jashkenas/coffee-script.git"
C
Chris Wanstrath 已提交
202 203 204
    assert_command input, command
  end

205
  def test_named_private_remote_with_repo
206 207
    input   = "remote add -p origin jashkenas/coffee-script"
    command = "git remote add origin git@github.com:jashkenas/coffee-script.git"
208 209 210
    assert_command input, command
  end

C
Chris Wanstrath 已提交
211
  def test_init
C
Chris Wanstrath 已提交
212 213
    h = Hub("init -g")
    assert_equal "git init", h.command
214 215 216 217 218
    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
219
      stub_github_user(nil)
220 221 222
    end

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

225 226 227 228 229 230
  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 已提交
231 232 233 234 235
  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 已提交
236

237
  def test_fork
C
Chris Wanstrath 已提交
238 239
    stub_request(:get, "github.com/api/v2/yaml/repos/show/tpw/hub").
      to_return(:status => 404)
240 241 242 243
    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 已提交
244 245

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

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

255 256 257 258
    assert_equal "", hub("fork --no-remote") { ENV['GIT'] = 'echo' }
  end

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

262
    expected = "tpw/hub already exists on GitHub\n"
C
Chris Wanstrath 已提交
263
    expected << "remote add -f tpw git@github.com:tpw/hub.git\n"
264 265 266
    expected << "new remote: tpw\n"
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end
C
Chris Wanstrath 已提交
267

C
Chris Wanstrath 已提交
268
  def test_version
269
    out = hub('--version')
270
    assert_includes "git version 1.7.0.4", out
C
Chris Wanstrath 已提交
271
    assert_includes "hub version #{Hub::Version}", out
C
Chris Wanstrath 已提交
272
  end
C
Chris Wanstrath 已提交
273 274 275 276 277 278 279 280

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

S
Stephen Celis 已提交
282 283 284 285
  def test_help_with_pager
    assert_equal Hub::Commands.improved_help_text, hub("-p")
  end

C
Chris Wanstrath 已提交
286 287 288 289 290
  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 已提交
291
Use git-config(1) to display the currently configured GitHub username:
C
Chris Wanstrath 已提交
292 293 294 295
config
  end

  def test_help_hub_no_groff
296 297
    stub_available_commands()
    assert_equal "** Can't find groff(1)\n", hub("help hub")
C
Chris Wanstrath 已提交
298
  end
299

300 301 302 303 304
  def test_hub_standalone
    help_standalone = hub("hub standalone")
    assert_equal Hub::Standalone.build, help_standalone
  end

C
Chris Wanstrath 已提交
305 306 307
  def test_hub_compare
    assert_command "compare refactor",
      "open http://github.com/defunkt/hub/compare/refactor"
308
  end
C
Chris Wanstrath 已提交
309

310
  def test_hub_compare_range
C
Chris Wanstrath 已提交
311 312
    assert_command "compare 1.0...fix",
      "open http://github.com/defunkt/hub/compare/1.0...fix"
313
  end
C
Chris Wanstrath 已提交
314

315
  def test_hub_compare_fork
C
Chris Wanstrath 已提交
316 317 318 319
    assert_command "compare myfork feature",
      "open http://github.com/myfork/hub/compare/feature"
  end

320 321 322 323 324 325 326 327 328 329 330
  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
331
    assert_command "browse mojombo/bert", "open http://github.com/mojombo/bert"
332 333
  end

334 335 336 337 338
  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 已提交
339 340
    assert_command "browse -p bmizerany/sinatra",
      "open https://github.com/bmizerany/sinatra"
341 342
  end

343
  def test_hub_browse_self
344
    assert_command "browse resque", "open http://github.com/tpw/resque"
345 346
  end

347
  def test_hub_browse_self_private
348
    assert_command "browse -p github", "open https://github.com/tpw/github"
349
  end
350

351
  def test_hub_browse_current
352 353 354
    assert_command "browse", "open http://github.com/defunkt/hub"
  end

355
  def test_hub_browse_current_private
356 357 358
    assert_command "browse -p", "open https://github.com/defunkt/hub"
  end

359 360 361
  def test_hub_browse_no_repo
    stub_repo_url(nil)
    assert_equal "Usage: hub browse [<USER>/]<REPOSITORY>\n", hub("browse")
362
  end
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

  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
      with_ruby_platform("i686-linux") do
        assert_browser("xdg-open")
      end
    end
  end

  def test_cygwin_browser
    stub_available_commands "open", "cygstart"
    with_browser_env(nil) do
      with_ruby_platform("i686-linux") do
        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
      with_ruby_platform("i686-linux") do
        assert_equal expected, hub("browse")
      end
    end
  end

  protected

    def stub_available_commands(*names)
      COMMANDS.replace names
    end

    def with_browser_env(value)
      browser, ENV['BROWSER'] = ENV['BROWSER'], value
      yield
    ensure
      ENV['BROWSER'] = browser
    end

    def assert_browser(browser)
      assert_command "browse", "#{browser} http://github.com/defunkt/hub"
    end

    def with_ruby_platform(value)
      platform = RUBY_PLATFORM
      Object.send(:remove_const, :RUBY_PLATFORM)
      Object.const_set(:RUBY_PLATFORM, value)
      yield
    ensure
      Object.send(:remove_const, :RUBY_PLATFORM)
      Object.const_set(:RUBY_PLATFORM, platform)
    end

C
Chris Wanstrath 已提交
425
end