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