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

     1  require 'aruba/cucumber'
     2  require 'fileutils'
     3  require 'json'
     4  require 'forwardable'
     5  
     6  system_git = `which git 2>/dev/null`.chomp
     7  lib_dir = File.expand_path('../../../lib', __FILE__)
     8  bin_dir = File.expand_path('../fakebin', __FILE__)
     9  gh_dir = File.expand_path('../../../', __FILE__)
    10  
    11  raise 'gh build failed' unless system('godep go build -o hub')
    12  at_exit do
    13    FileUtils.rm_f('hub')
    14  end
    15  
    16  Before do
    17    # don't want hub to run in bundle
    18    unset_bundler_env_vars
    19    # have bin/hub load code from the current project
    20    set_env 'RUBYLIB', lib_dir
    21    # speed up load time by skipping RubyGems
    22    set_env 'RUBYOPT', '--disable-gems' if RUBY_VERSION > '1.9'
    23    # put fakebin on the PATH
    24    set_env 'PATH', "#{gh_dir}:#{bin_dir}:#{ENV['PATH']}"
    25    # clear out GIT if it happens to be set
    26    set_env 'GIT', nil
    27    # exclude this project's git directory from use in testing
    28    set_env 'GIT_CEILING_DIRECTORIES', File.dirname(lib_dir)
    29    # sabotage git commands that might try to access a remote host
    30    set_env 'GIT_PROXY_COMMAND', 'echo'
    31    # avoids reading from current user's "~/.gitconfig"
    32    set_env 'HOME', File.expand_path(File.join(current_dir, 'home'))
    33    # used in fakebin/git
    34    set_env 'HUB_SYSTEM_GIT', system_git
    35    # ensure that api.github.com is actually never hit in tests
    36    set_env 'HUB_TEST_HOST', '127.0.0.1:0'
    37    set_env 'GH_API_HOST', '127.0.0.1:0'
    38    # ensure we use fakebin `open` to test browsing
    39    set_env 'BROWSER', 'open'
    40    # sabotage opening a commit message editor interactively
    41    set_env 'GIT_EDITOR', 'false'
    42  
    43    author_name  = "Hub"
    44    author_email = "hub@test.local"
    45    set_env 'GIT_AUTHOR_NAME',     author_name
    46    set_env 'GIT_COMMITTER_NAME',  author_name
    47    set_env 'GIT_AUTHOR_EMAIL',    author_email
    48    set_env 'GIT_COMMITTER_EMAIL', author_email
    49  
    50    set_env 'GH_VERSION', 'dev'
    51  
    52    FileUtils.mkdir_p ENV['HOME']
    53  
    54    # increase process exit timeout from the default of 3 seconds
    55    @aruba_timeout_seconds = 5
    56  
    57    if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby'
    58      @aruba_io_wait_seconds = 0.1
    59    else
    60      @aruba_io_wait_seconds = 0.02
    61    end
    62  end
    63  
    64  After do
    65    @server.stop if defined? @server and @server
    66    FileUtils.rm_f("#{bin_dir}/vim")
    67  end
    68  
    69  RSpec::Matchers.define :be_successful_command do
    70    match do |cmd|
    71      cmd.success?
    72    end
    73  
    74    failure_message_for_should do |cmd|
    75      %(command "#{cmd}" exited with status #{cmd.status}:) <<
    76      cmd.output.gsub(/^/, ' ' * 2)
    77    end
    78  end
    79  
    80  class SimpleCommand
    81    attr_reader :output
    82    extend Forwardable
    83  
    84    def_delegator :@status, :exitstatus, :status
    85    def_delegators :@status, :success?
    86  
    87    def initialize cmd
    88      @cmd = cmd
    89    end
    90  
    91    def to_s
    92      @cmd
    93    end
    94  
    95    def self.run cmd
    96      command = new(cmd)
    97      command.run
    98      command
    99    end
   100  
   101    def run
   102      @output = `#{@cmd} 2>&1`.chomp
   103      @status = $?
   104        $?.success?
   105    end
   106  end
   107  
   108  World Module.new {
   109    # FIXEM: hack to make all exact match a partial match
   110    def assert_exact_output(*args)
   111      assert_partial_output(*args)
   112    end
   113  
   114    def history
   115      histfile = File.join(ENV['HOME'], '.history')
   116      if File.exist? histfile
   117        File.readlines histfile
   118      else
   119        []
   120      end
   121    end
   122  
   123    def assert_command_run cmd
   124      cmd += "\n" unless cmd[-1..-1] == "\n"
   125      history.should include(cmd)
   126    end
   127  
   128    def edit_hub_config
   129      config = File.join(ENV['HOME'], '.config/hub')
   130      FileUtils.mkdir_p File.dirname(config)
   131      if File.exist? config
   132        data = YAML.load File.read(config)
   133      else
   134        data = {}
   135      end
   136  
   137      gh_config = File.join(ENV['HOME'], '.config/gh')
   138      FileUtils.mkdir_p File.dirname(gh_config)
   139      if File.exist? config
   140        data = JSON.parse File.read(gh_config)
   141      else
   142        data = []
   143      end
   144  
   145      yield data
   146      File.open(config, 'w') { |cfg| cfg << YAML.dump(data) }
   147      File.open(gh_config, 'w') { |cfg| cfg << JSON.generate(data) }
   148    end
   149  
   150    define_method(:text_editor_script) do |bash_code|
   151      File.open("#{bin_dir}/vim", 'w', 0755) { |exe|
   152        exe.puts "#!/bin/bash"
   153        exe.puts "set -e"
   154        exe.puts bash_code
   155      }
   156    end
   157  
   158    def run_silent cmd
   159      in_current_dir do
   160        command = SimpleCommand.run(cmd)
   161        command.should be_successful_command
   162        command.output
   163      end
   164    end
   165  
   166    def empty_commit
   167      run_silent "git commit --quiet -m empty --allow-empty"
   168    end
   169  
   170    # Aruba unnecessarily creates new Announcer instance on each invocation
   171    def announcer
   172      @announcer ||= super
   173    end
   174  }