| 1 |
#!/usr/bin/env ruby |
| 2 |
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../rspec/lib' # For svn |
| 3 |
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../vendor/plugins/rspec/lib' # For rspec installed as plugin |
| 4 |
require 'rubygems' |
| 5 |
require 'drb/drb' |
| 6 |
require 'rbconfig' |
| 7 |
require 'spec' |
| 8 |
require 'optparse' |
| 9 |
|
| 10 |
# This is based on Florian Weber's TDDMate |
| 11 |
module Spec |
| 12 |
module Runner |
| 13 |
class RailsSpecServer |
| 14 |
def run(argv, stderr, stdout) |
| 15 |
$stdout = stdout |
| 16 |
$stderr = stderr |
| 17 |
|
| 18 |
base = ActiveRecord::Base |
| 19 |
def base.clear_reloadable_connections! |
| 20 |
active_connections.each do |name, conn| |
| 21 |
if conn.requires_reloading? |
| 22 |
conn.disconnect! |
| 23 |
active_connections.delete(name) |
| 24 |
end |
| 25 |
end |
| 26 |
end |
| 27 |
|
| 28 |
if ActionController.const_defined?(:Dispatcher) |
| 29 |
dispatcher = ::ActionController::Dispatcher.new($stdout) |
| 30 |
dispatcher.cleanup_application(true) |
| 31 |
elsif ::Dispatcher.respond_to?(:reset_application!) |
| 32 |
::Dispatcher.reset_application! |
| 33 |
else |
| 34 |
raise "Application reloading failed" |
| 35 |
end |
| 36 |
::Dependencies.mechanism = :load |
| 37 |
require_dependency('application.rb') unless Object.const_defined?(:ApplicationController) |
| 38 |
load File.dirname(__FILE__) + '/../spec/spec_helper.rb' |
| 39 |
|
| 40 |
::Spec::Runner::CommandLine.run( |
| 41 |
::Spec::Runner::OptionParser.parse( |
| 42 |
argv, |
| 43 |
$stderr, |
| 44 |
$stdout |
| 45 |
) |
| 46 |
) |
| 47 |
end |
| 48 |
end |
| 49 |
end |
| 50 |
end |
| 51 |
puts "Loading Rails environment" |
| 52 |
|
| 53 |
ENV["RAILS_ENV"] = "test" |
| 54 |
require File.expand_path(File.dirname(__FILE__) + "/../config/environment") |
| 55 |
require 'dispatcher' |
| 56 |
|
| 57 |
def restart_test_server |
| 58 |
puts "restarting" |
| 59 |
config = ::Config::CONFIG |
| 60 |
ruby = File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT'] |
| 61 |
command_line = [ruby, $0, ARGV].flatten.join(' ') |
| 62 |
exec(command_line) |
| 63 |
end |
| 64 |
|
| 65 |
def daemonize(pid_file = nil) |
| 66 |
return yield if $DEBUG |
| 67 |
pid = Process.fork{ |
| 68 |
Process.setsid |
| 69 |
Dir.chdir(RAILS_ROOT) |
| 70 |
trap("SIGINT"){ exit! 0 } |
| 71 |
trap("SIGTERM"){ exit! 0 } |
| 72 |
trap("SIGHUP"){ restart_test_server } |
| 73 |
File.open("/dev/null"){|f| |
| 74 |
STDERR.reopen f |
| 75 |
STDIN.reopen f |
| 76 |
STDOUT.reopen f |
| 77 |
} |
| 78 |
yield |
| 79 |
} |
| 80 |
puts "spec_server launched. (PID: %d)" % pid |
| 81 |
File.open(pid_file,"w"){|f| f.puts pid } if pid_file |
| 82 |
exit! 0 |
| 83 |
end |
| 84 |
|
| 85 |
options = Hash.new |
| 86 |
opts = OptionParser.new |
| 87 |
opts.on("-d", "--daemon"){|v| options[:daemon] = true } |
| 88 |
opts.on("-p", "--pid PIDFILE"){|v| options[:pid] = v } |
| 89 |
opts.parse!(ARGV) |
| 90 |
|
| 91 |
puts "Ready" |
| 92 |
exec_server = lambda { |
| 93 |
trap("USR2") { restart_test_server } if Signal.list.has_key?("USR2") |
| 94 |
DRb.start_service("druby://localhost:8989", Spec::Runner::RailsSpecServer.new) |
| 95 |
DRb.thread.join |
| 96 |
} |
| 97 |
|
| 98 |
if options[:daemon] |
| 99 |
daemonize(options[:pid], &exec_server) |
| 100 |
else |
| 101 |
exec_server.call |
| 102 |
end |