1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
require "spring/env"
module Spring
module Test
class Application
DEFAULT_TIMEOUT = ENV['CI'] ? 300 : 10
attr_reader :root, :spring_env
def initialize(root)
@root = Pathname.new(root)
@spring_env = Spring::Env.new(root: root)
@times = nil
end
def exists?
root.exist?
end
def stdout
@stdout ||= IO.pipe
end
def stderr
@stderr ||= IO.pipe
end
def log_file
@log_file ||= path("tmp/spring.log").open("w+")
end
def env
@env ||= {
"GEM_HOME" => gem_home.to_s,
"GEM_PATH" => gem_home.to_s,
"HOME" => user_home.to_s,
"RAILS_ENV" => nil,
"RACK_ENV" => nil,
"SPRING_LOG" => log_file.path
}
end
def path(addition)
root.join addition
end
def gemfile
path "Gemfile"
end
def gemfile_lock
path "Gemfile.lock"
end
def gems_rb
path "gems.rb"
end
def gems_locked
path "gems.locked"
end
def gem_home
path "../gems/#{RUBY_VERSION}"
end
def user_home
path "user_home"
end
def spring
gem_home.join "bin/spring"
end
def rails_version
@rails_version ||= RailsVersion.new(gemfile.read.match(/gem 'rails', '(.*)'/)[1])
end
def spring_test_command
"bin/rake test #{test}"
end
def stop_spring
run "#{spring} stop"
rescue Errno::ENOENT
end
def test
path "test/controllers/posts_controller_test.rb"
end
def controller
path "app/controllers/posts_controller.rb"
end
def application_config
path "config/application.rb"
end
def spring_config
path "config/spring.rb"
end
def spring_client_config
path "config/spring_client.rb"
end
def run(command, opts = {})
start_time = Time.now
Bundler.with_clean_env do
Process.spawn(
env,
command.to_s,
out: stdout.last,
err: stderr.last,
in: :close,
chdir: root.to_s,
)
end
_, status = Timeout.timeout(opts.fetch(:timeout, DEFAULT_TIMEOUT)) { Process.wait2 }
if pid = spring_env.pid
@server_pid = pid
lines = `ps -A -o ppid= -o pid= | egrep '^\\s*#{@server_pid}'`.lines
@application_pids = lines.map { |l| l.split.last.to_i }
end
output = read_streams
puts dump_streams(command, output) if ENV["SPRING_DEBUG"]
@times << (Time.now - start_time) if @times
output.merge(status: status, command: command)
rescue Timeout::Error
raise Timeout::Error, "While running command:\n\n#{dump_streams(command, read_streams)}"
end
def with_timing
@times = []
yield
ensure
@times = nil
end
def last_time
@times.last
end
def first_time
@times.first
end
def timing_ratio
last_time / first_time
end
def read_streams
{
stdout: read_stream(stdout.first),
stderr: read_stream(stderr.first),
log: read_stream(log_file)
}
end
def read_stream(stream)
output = ""
while IO.select([stream], [], [], 0.5) && !stream.eof?
output << stream.readpartial(10240)
end
output
end
def dump_streams(command, streams)
output = "$ #{command}\n"
streams.each do |name, stream|
unless stream.chomp.empty?
output << "--- #{name} ---\n"
output << "#{stream.chomp}\n"
end
end
output << "\n"
output
end
def debug(artifacts)
artifacts = artifacts.dup
artifacts.delete :status
dump_streams(artifacts.delete(:command), artifacts)
end
def await_reload
raise "no pid" if @application_pids.nil? || @application_pids.empty?
Timeout.timeout(DEFAULT_TIMEOUT) do
sleep 0.1 while @application_pids.any? { |p| process_alive?(p) }
end
end
def run!(command, options = {})
attempts = (options.delete(:retry) || 0) + 1
artifacts = nil
until attempts == 0 || artifacts && artifacts[:status].success?
artifacts = run(command, options)
attempts -= 1
end
if artifacts[:status].success?
artifacts
else
raise "command failed\n\n#{debug(artifacts)}"
end
end
def bundle
# Version restriction is a workaround for https://github.com/bundler/bundler/pull/4981
# The problem breaks our tests on Ruby 2.2
# We can remove once it's fixed
run! "(gem list bundler | grep bundler) || gem install bundler --version '~> 1.12.0'", timeout: nil, retry: 2
run! "bundle check || bundle update --retry=2", timeout: nil
end
def insert_into_test(code)
File.write(test, test.read.sub(/^\s*get .+$/, code))
end
private
def process_alive?(pid)
Process.kill 0, pid
true
rescue Errno::ESRCH
false
end
end
end
end
|