github.com/scorpionis/hub@v2.2.1+incompatible/features/steps.rb (about)

     1  require 'fileutils'
     2  
     3  Given(/^HTTPS is preferred$/) do
     4    run_silent %(git config --global hub.protocol https)
     5  end
     6  
     7  Given(/^there are no remotes$/) do
     8    result = run_silent('git remote')
     9    expect(result).to be_empty
    10  end
    11  
    12  Given(/^"([^"]*)" is a whitelisted Enterprise host$/) do |host|
    13    run_silent %(git config --global --add hub.host "#{host}")
    14  end
    15  
    16  Given(/^git "(.+?)" is set to "(.+?)"$/) do |key, value|
    17    run_silent %(git config #{key} "#{value}")
    18  end
    19  
    20  Given(/^the "([^"]*)" remote has url "([^"]*)"$/) do |remote_name, url|
    21    remotes = run_silent('git remote').split("\n")
    22    unless remotes.include? remote_name
    23      run_silent %(git remote add #{remote_name} "#{url}")
    24    else
    25      run_silent %(git remote set-url #{remote_name} "#{url}")
    26    end
    27  end
    28  
    29  Given(/^I am "([^"]*)" on ([\S]+)(?: with OAuth token "([^"]*)")?$/) do |name, host, token|
    30    edit_hub_config do |cfg|
    31      entry = {'user' => name}
    32      host = host.sub(%r{^([\w-]+)://}, '')
    33      entry['oauth_token'] = token if token
    34      entry['protocol'] = $1 if $1
    35      cfg[host.downcase] = [entry]
    36    end
    37  end
    38  
    39  Given(/^\$(\w+) is "([^"]*)"$/) do |name, value|
    40    set_env name, value
    41  end
    42  
    43  Given(/^I am in "([^"]*)" git repo$/) do |dir_name|
    44    if dir_name.include?(':')
    45      origin_url = dir_name
    46      dir_name = File.basename origin_url, '.git'
    47    end
    48    step %(a git repo in "#{dir_name}")
    49    step %(I cd to "#{dir_name}")
    50    step %(the "origin" remote has url "#{origin_url}") if origin_url
    51  end
    52  
    53  Given(/^a git repo in "([^"]*)"$/) do |dir_name|
    54    step %(a directory named "#{dir_name}")
    55    dirs << dir_name
    56    step %(I successfully run `git init --quiet`)
    57    dirs.pop
    58  end
    59  
    60  Given(/^there is a commit named "([^"]+)"$/) do |name|
    61    empty_commit
    62    empty_commit
    63    run_silent %(git tag #{name})
    64    run_silent %(git reset --quiet --hard HEAD^)
    65  end
    66  
    67  When(/^I make (a|\d+) commits?(?: with message "([^"]+)")?$/) do |num, msg|
    68    num = num == 'a' ? 1 : num.to_i
    69    num.times { empty_commit(msg) }
    70  end
    71  
    72  Then(/^the latest commit message should be "([^"]+)"$/) do |subject|
    73    step %(I successfully run `git log -1 --format=%s`)
    74    step %(the output should contain exactly "#{subject}\\n")
    75  end
    76  
    77  Given(/^the "([^"]+)" branch is pushed to "([^"]+)"$/) do |name, upstream|
    78    full_upstream = ".git/refs/remotes/#{upstream}"
    79    in_current_dir do
    80      FileUtils.mkdir_p File.dirname(full_upstream)
    81      FileUtils.cp ".git/refs/heads/#{name}", full_upstream
    82    end
    83  end
    84  
    85  Given(/^I am on the "([^"]+)" branch(?: (pushed to|with upstream) "([^"]+)")?$/) do |name, type, upstream|
    86    empty_commit
    87    if upstream
    88      if upstream =~ /^refs\//
    89        full_upstream = ".git/#{upstream}"
    90      else
    91        full_upstream = ".git/refs/remotes/#{upstream}"
    92      end
    93      in_current_dir do
    94        FileUtils.mkdir_p File.dirname(full_upstream)
    95        FileUtils.cp '.git/refs/heads/master', full_upstream
    96      end unless upstream == 'refs/heads/master'
    97    end
    98    track = type == 'pushed to' ? '--no-track' : '--track'
    99    run_silent %(git checkout --quiet -B #{shell_escape name} #{track} #{shell_escape upstream})
   100  end
   101  
   102  Given(/^the default branch for "([^"]+)" is "([^"]+)"$/) do |remote, branch|
   103    empty_commit
   104    ref_file = ".git/refs/remotes/#{remote}/#{branch}"
   105    in_current_dir do
   106      FileUtils.mkdir_p File.dirname(ref_file)
   107      FileUtils.cp '.git/refs/heads/master', ref_file
   108    end
   109    run_silent %(git remote set-head #{remote} #{branch})
   110  end
   111  
   112  Given(/^I am in detached HEAD$/) do
   113    empty_commit
   114    empty_commit
   115    run_silent %(git checkout HEAD^)
   116  end
   117  
   118  Given(/^the current dir is not a repo$/) do
   119    in_current_dir do
   120      FileUtils.rm_rf '.git'
   121    end
   122  end
   123  
   124  Given(/^the GitHub API server:$/) do |endpoints_str|
   125    @server = Hub::LocalServer.start_sinatra do
   126      eval endpoints_str, binding
   127    end
   128    # hit our Sinatra server instead of github.com
   129    set_env 'HUB_TEST_HOST', "http://127.0.0.1:#{@server.port}"
   130  end
   131  
   132  Given(/^I use a debugging proxy(?: at "(.+?)")?$/) do |address|
   133    address ||= 'localhost:8888'
   134    set_env 'HTTP_PROXY', address
   135    set_env 'HTTPS_PROXY', address
   136  end
   137  
   138  Then(/^shell$/) do
   139    in_current_dir do
   140      system '/bin/bash -i'
   141    end
   142  end
   143  
   144  Then(/^"([^"]*)" should be run$/) do |cmd|
   145    assert_command_run cmd
   146  end
   147  
   148  Then(/^it should clone "([^"]*)"$/) do |repo|
   149    step %("git clone #{repo}" should be run)
   150  end
   151  
   152  Then(/^it should not clone anything$/) do
   153    history.each { |h| expect(h).to_not match(/^git clone/) }
   154  end
   155  
   156  Then(/^"([^"]+)" should not be run$/) do |pattern|
   157    history.each { |h| expect(h).to_not include(pattern) }
   158  end
   159  
   160  Then(/^there should be no output$/) do
   161    assert_exact_output('', all_output)
   162  end
   163  
   164  Then(/^the git command should be unchanged$/) do
   165    expect(@commands).to_not be_empty
   166    assert_command_run @commands.last.sub(/^hub\b/, 'git')
   167  end
   168  
   169  Then(/^the url for "([^"]*)" should be "([^"]*)"$/) do |name, url|
   170    found = run_silent %(git config --get-all remote.#{name}.url)
   171    expect(found).to eql(url)
   172  end
   173  
   174  Then(/^the "([^"]*)" submodule url should be "([^"]*)"$/) do |name, url|
   175    found = run_silent %(git config --get-all submodule."#{name}".url)
   176    expect(found).to eql(url)
   177  end
   178  
   179  Then(/^there should be no "([^"]*)" remote$/) do |remote_name|
   180    remotes = run_silent('git remote').split("\n")
   181    expect(remotes).to_not include(remote_name)
   182  end
   183  
   184  Then(/^the file "([^"]*)" should have mode "([^"]*)"$/) do |file, expected_mode|
   185    prep_for_fs_check do
   186      mode = File.stat(file).mode
   187      expect(mode.to_s(8)).to match(/#{expected_mode}$/)
   188    end
   189  end
   190  
   191  Given(/^the file named "(.+?)" is older than hub source$/) do |file|
   192    prep_for_fs_check do
   193      time = File.mtime(File.expand_path('../../lib/hub/commands.rb', __FILE__)) - 60
   194      File.utime(time, time, file)
   195    end
   196  end
   197  
   198  Given(/^the remote commit states of "(.*?)" "(.*?)" are:$/) do |proj, ref, json_value|
   199    if ref == 'HEAD'
   200      empty_commit
   201    end
   202    rev = run_silent %(git rev-parse #{ref})
   203  
   204    status_endpoint = <<-EOS
   205      get('/repos/#{proj}/statuses/#{rev}') {
   206        json #{json_value}
   207      }
   208      EOS
   209    step %{the GitHub API server:}, status_endpoint
   210  end
   211  
   212  Given(/^the remote commit state of "(.*?)" "(.*?)" is "(.*?)"$/) do |proj, ref, status|
   213    step %{the remote commit states of "#{proj}" "#{ref}" are:}, <<-EOS
   214      [ { :state => \"#{status}\",
   215          :target_url => 'https://travis-ci.org/#{proj}/builds/1234567' } ]
   216      EOS
   217  end
   218  
   219  Given(/^the remote commit state of "(.*?)" "(.*?)" is nil$/) do |proj, ref|
   220    step %{the remote commit states of "#{proj}" "#{ref}" are:}, "[ ]"
   221  end
   222  
   223  Given(/^the text editor exits with error status$/) do
   224    text_editor_script "exit 1"
   225  end
   226  
   227  Given(/^the text editor adds:$/) do |text|
   228    text_editor_script <<-BASH
   229      file="$3"
   230      contents="$(cat "$file" 2>/dev/null || true)"
   231      { echo "#{text}"
   232        echo
   233        echo "$contents"
   234      } > "$file"
   235    BASH
   236  end
   237  
   238  When(/^I pass in:$/) do |input|
   239    type(input)
   240    @interactive.stdin.close
   241  end
   242  
   243  Given(/^the git commit editor is "([^"]+)"$/) do |cmd|
   244    set_env('GIT_EDITOR', cmd)
   245  end
   246  
   247  Given(/^the SSH config:$/) do |config_lines|
   248    ssh_config = "#{ENV['HOME']}/.ssh/config"
   249    FileUtils.mkdir_p(File.dirname(ssh_config))
   250    File.open(ssh_config, 'w') {|f| f << config_lines }
   251  end
   252  
   253  Given(/^the SHAs and timestamps are normalized in "([^"]+)"$/) do |file|
   254    in_current_dir do
   255      contents = File.read(file)
   256      contents.gsub!(/[0-9a-f]{7} \(Hub, \d seconds? ago\)/, "SHA1SHA (Hub, 0 seconds ago)")
   257      File.open(file, "w") { |f| f.write(contents) }
   258    end
   259  end