1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
sinatra/test/integration_helper.rb

129 lines
2.7 KiB
Ruby

require 'sinatra/base'
require 'rbconfig'
require 'open-uri'
require 'sinatra/runner'
module IntegrationHelper
class BaseServer < Sinatra::Runner
extend Enumerable
attr_accessor :server, :port
alias name server
def self.all
@all ||= []
end
def self.each(&block)
all.each(&block)
end
def self.run(server, port)
new(server, port).run
end
def app_file
File.expand_path('../integration/app.rb', __FILE__)
end
def environment
"development"
end
def initialize(server, port)
@installed, @pipe, @server, @port = nil, nil, server, port
Server.all << self
end
def run
return unless installed?
kill
@log = ""
super
at_exit { kill }
end
def installed?
return @installed unless @installed.nil?
s = server == 'HTTP' ? 'net/http/server' : server
require s
@installed = true
rescue LoadError
warn "#{server} is not installed, skipping integration tests"
@installed = false
end
def command
@command ||= begin
cmd = ["APP_ENV=#{environment}", "exec"]
if RbConfig.respond_to? :ruby
cmd << RbConfig.ruby.inspect
else
file, dir = RbConfig::CONFIG.values_at('ruby_install_name', 'bindir')
cmd << File.expand_path(file, dir).inspect
end
cmd << "-w" unless net_http_server?
cmd << "-I" << File.expand_path('../../lib', __FILE__).inspect
cmd << app_file.inspect << '-s' << server << '-o' << '127.0.0.1' << '-p' << port
cmd << "-e" << environment.to_s << '2>&1'
cmd.join " "
end
end
def webrick?
name.to_s == "webrick"
end
def rainbows?
name.to_s == "rainbows"
end
def puma?
name.to_s == "puma"
end
def reel?
name.to_s == "reel"
end
def trinidad?
name.to_s == "trinidad"
end
def net_http_server?
name.to_s == 'HTTP'
end
def warnings
log.scan(%r[(?:\(eval|lib/sinatra).*warning:.*$])
end
def run_test(target, &block)
retries ||= 3
target.server = self
run unless alive?
target.instance_eval(&block)
rescue Exception => error
retries -= 1
kill
retries < 0 ? retry : raise(error)
end
end
Server = BaseServer
def it(message, &block)
Server.each do |server|
next unless server.installed?
super("with #{server.name}: #{message}") { server.run_test(self, &block) }
end
end
def self.extend_object(obj)
super
base_port = 5000 + Process.pid % 100
Sinatra::Base.server.each_with_index do |server, index|
Server.run(server, base_port+index)
end
end
end