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