hub_test.rb 9.8 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
    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
16 17
    input   = "clone -p rtomayko/ronn"
    command = "git clone git@github.com:rtomayko/ronn.git"
C
Chris Wanstrath 已提交
18
    assert_command input, command
C
Chris Wanstrath 已提交
19 20 21
  end

  def test_public_clone
22 23
    input   = "clone rtomayko/ronn"
    command = "git clone git://github.com/rtomayko/ronn.git"
C
Chris Wanstrath 已提交
24
    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
  def test_private_clone_left_alone
62 63
    input   = "clone git@github.com:rtomayko/ronn.git"
    command = "git clone git@github.com:rtomayko/ronn.git"
64 65 66 67
    assert_command input, command
  end

  def test_public_clone_left_alone
68 69
    input   = "clone git://github.com/rtomayko/ronn.git"
    command = "git clone git://github.com/rtomayko/ronn.git"
70 71
    assert_command input, command
  end
C
Chris Wanstrath 已提交
72 73

  def test_normal_public_clone_with_path
74 75
    input   = "clone git://github.com/rtomayko/ronn.git ronn-dev"
    command = "git clone git://github.com/rtomayko/ronn.git ronn-dev"
C
Chris Wanstrath 已提交
76 77
    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
  def test_public_submodule
    input   = "submodule add wycats/bundler vendor/bundler"
J
Justin Ridgewell 已提交
111 112
    command = "git submodule add git://github.com/wycats/bundler.git vendor/bundler"
    assert_command input, command
S
Stephen Celis 已提交
113 114 115 116
  end

  def test_private_submodule
    input   = "submodule add -p grit vendor/grit"
J
Justin Ridgewell 已提交
117 118 119 120 121 122 123 124
    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 已提交
125 126 127 128 129
  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 已提交
130
    assert_command input, command
S
Stephen Celis 已提交
131 132
  end

C
Chris Wanstrath 已提交
133
  def test_private_remote
134
    input   = "remote add -p rtomayko"
C
Chris Wanstrath 已提交
135 136
    command = "git remote add rtomayko git@github.com:rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
137 138 139
  end

  def test_public_remote
140
    input   = "remote add rtomayko"
C
Chris Wanstrath 已提交
141 142
    command = "git remote add rtomayko git://github.com/rtomayko/hub.git"
    assert_command input, command
C
Chris Wanstrath 已提交
143 144
  end

C
Chris Wanstrath 已提交
145 146 147 148 149 150
  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

151 152 153 154 155 156
  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

157 158 159 160 161 162
  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 已提交
163
  def test_private_remote_with_repo
164 165
    input   = "remote add -p jashkenas/coffee-script"
    command = "git remote add jashkenas git@github.com:jashkenas/coffee-script.git"
C
Chris Wanstrath 已提交
166 167 168 169
    assert_command input, command
  end

  def test_public_remote_with_repo
170 171
    input   = "remote add jashkenas/coffee-script"
    command = "git remote add jashkenas git://github.com/jashkenas/coffee-script.git"
172 173 174 175
    assert_command input, command
  end

  def test_public_remote_f_with_repo
176 177
    input   = "remote add -f jashkenas/coffee-script"
    command = "git remote add -f jashkenas git://github.com/jashkenas/coffee-script.git"
C
Chris Wanstrath 已提交
178 179 180
    assert_command input, command
  end

181
  def test_named_private_remote_with_repo
182 183
    input   = "remote add -p origin jashkenas/coffee-script"
    command = "git remote add origin git@github.com:jashkenas/coffee-script.git"
184 185 186
    assert_command input, command
  end

C
Chris Wanstrath 已提交
187
  def test_init
C
Chris Wanstrath 已提交
188 189
    h = Hub("init -g")
    assert_equal "git init", h.command
190 191 192 193 194 195 196 197 198
    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 已提交
199 200
  end

201 202 203 204 205 206
  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 已提交
207 208 209 210 211
  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 已提交
212

213
  def test_fork
C
Chris Wanstrath 已提交
214 215
    stub_request(:get, "github.com/api/v2/yaml/repos/show/tpw/hub").
      to_return(:status => 404)
216 217 218 219
    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 已提交
220 221

    expected = "remote add -f tpw git@github.com:tpw/hub.git\n"
222 223 224 225 226
    expected << "new remote: tpw\n"
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end

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

231 232 233 234
    assert_equal "", hub("fork --no-remote") { ENV['GIT'] = 'echo' }
  end

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

238
    expected = "tpw/hub already exists on GitHub\n"
C
Chris Wanstrath 已提交
239
    expected << "remote add -f tpw git@github.com:tpw/hub.git\n"
240 241 242
    expected << "new remote: tpw\n"
    assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
  end
C
Chris Wanstrath 已提交
243

C
Chris Wanstrath 已提交
244
  def test_version
245
    out = hub('--version')
S
Stephen Celis 已提交
246
    assert_includes "git version", out
C
Chris Wanstrath 已提交
247
    assert_includes "hub version #{Hub::Version}", out
C
Chris Wanstrath 已提交
248
  end
C
Chris Wanstrath 已提交
249 250 251 252 253 254 255 256

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

S
Stephen Celis 已提交
258 259 260 261
  def test_help_with_pager
    assert_equal Hub::Commands.improved_help_text, hub("-p")
  end

C
Chris Wanstrath 已提交
262 263 264 265 266
  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 已提交
267
Use git-config(1) to display the currently configured GitHub username:
C
Chris Wanstrath 已提交
268 269 270 271 272 273
config
  end

  def test_help_hub_no_groff
    help_manpage = hub("help hub") do
      Hub::Commands.class_eval do
274
        remove_method :groff?
C
Chris Wanstrath 已提交
275 276 277 278 279
        def groff?; false end
      end
    end
    assert_equal "** Can't find groff(1)\n", help_manpage
  end
280

281 282 283 284 285
  def test_hub_standalone
    help_standalone = hub("hub standalone")
    assert_equal Hub::Standalone.build, help_standalone
  end

C
Chris Wanstrath 已提交
286 287 288 289 290 291 292 293 294 295 296
  def test_hub_compare
    assert_command "compare refactor",
      "open http://github.com/defunkt/hub/compare/refactor"

    assert_command "compare 1.0...fix",
      "open http://github.com/defunkt/hub/compare/1.0...fix"

    assert_command "compare myfork feature",
      "open http://github.com/myfork/hub/compare/feature"
  end

297
  def test_hub_open
298
    assert_command "browse mojombo/bert", "open http://github.com/mojombo/bert"
299 300 301
  end

  def test_hub_open_private
C
Chris Wanstrath 已提交
302 303
    assert_command "browse -p bmizerany/sinatra",
      "open https://github.com/bmizerany/sinatra"
304 305 306
  end

  def test_hub_open_self
307
    assert_command "browse resque", "open http://github.com/tpw/resque"
308 309 310
  end

  def test_hub_open_self_private
311
    assert_command "browse -p github", "open https://github.com/tpw/github"
312
  end
313 314 315 316 317 318 319 320 321 322 323 324 325 326

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