github.com/jingweno/gh@v2.1.1-0.20221007190738-04a7985fa9a1+incompatible/features/steps.rb (about)

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